def to_ruby(self, schema): if not self.terms: return NULL.to_ruby(schema) v = self.terms[-1] acc = FirstOp("first", v).partial_eval().to_ruby(schema) for v in reversed(self.terms[:-1]): m = v.missing().partial_eval() e = NotOp("not", m).partial_eval().to_ruby(schema) r = FirstOp("first", v).partial_eval().to_ruby(schema) if r.miss is TRUE: continue elif r.miss is FALSE: acc = r continue elif acc.type == r.type: new_type = r.type elif acc.type == NUMBER and r.type == INTEGER: new_type = NUMBER elif acc.type == INTEGER and r.type == NUMBER: new_type = NUMBER else: new_type = OBJECT acc = Ruby(miss=AndOp("and", [acc.miss, m]).partial_eval(), type=new_type, expr="(" + e.expr + ") ? (" + r.expr + ") : (" + acc.expr + ")", frum=self) return acc
def to_ruby(self, schema): if self.var == ".": return "_source" else: if self.var == "_id": return Ruby( type=STRING, expr= 'doc["_uid"].value.substring(doc["_uid"].value.indexOf(\'#\')+1)', frum=self) columns = schema.values(self.var) acc = [] for c in columns: varname = c.es_column frum = Variable(c.es_column) q = quote(varname) acc.append( Ruby(miss=frum.missing(), type=c.type, expr="doc[" + q + "].values", frum=frum, many=True)) if len(acc) == 0: return NULL.to_ruby(schema) elif len(acc) == 1: return acc[0] else: return CoalesceOp("coalesce", acc).to_ruby(schema)
def _convert(v): if v is None: return NULL.to_ruby(schema) if v is True: return Ruby( type=BOOLEAN, expr="true", frum=self ) if v is False: return Ruby( type=BOOLEAN, expr="false", frum=self ) if isinstance(v, text_type): return Ruby( type=STRING, expr=quote(v), frum=self ) if isinstance(v, int): return Ruby( type=INTEGER, expr=text_type(v), frum=self ) if isinstance(v, float): return Ruby( type=NUMBER, expr=text_type(v), frum=self ) if isinstance(v, dict): return Ruby( type=OBJECT, expr="[" + ", ".join(quote(k) + ": " + _convert(vv) for k, vv in v.items()) + "]", frum=self ) if isinstance(v, (list, tuple)): return Ruby( type=OBJECT, expr="[" + ", ".join(_convert(vv).expr for vv in v) + "]", frum=self )