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 to_es_script(self, schema): if not self.terms: return NULL.to_es_script(schema) v = self.terms[-1] acc = FirstOp("first", v).partial_eval().to_es_script(schema) for v in reversed(self.terms[:-1]): m = v.missing().partial_eval() e = NotOp("not", m).partial_eval().to_es_script(schema) r = FirstOp("first", v).partial_eval().to_es_script(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 = EsScript( miss=AndOp("and", [acc.miss, m]).partial_eval(), type=new_type, expr="(" + e.expr + ") ? (" + r.expr + ") : (" + acc.expr + ")", frum=self ) return acc
def to_es_script(self, schema, not_null=False, boolean=False, many=True): if not self.terms: return NULL.to_es_script(schema) # acc.miss WILL SAY IF THIS COALESCE RETURNS NULL, # acc.expr WILL ASSUMED TO BE A VALUE, SO THE LAST TERM IS ASSUMED NOT NULL v = self.terms[-1] acc = FirstOp(v).partial_eval().to_es_script(schema) for v in reversed(self.terms[:-1]): m = v.missing().partial_eval() e = NotOp(m).partial_eval().to_es_script(schema) r = FirstOp(v).partial_eval().to_es_script(schema) if r.miss is TRUE: continue elif r.miss is FALSE: acc = r continue elif acc.type == r.type or acc.type == IS_NULL: 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 = EsScript( miss=AndOp([acc.miss, m]).partial_eval(), type=new_type, expr="(" + e.expr + ") ? (" + r.expr + ") : (" + acc.expr + ")", frum=self, schema=schema, ) return acc
def to_es_script(self, schema, not_null=False, boolean=False, many=True): if not self.terms: return NULL.to_es_script(schema) # acc.miss WILL SAY IF THIS COALESCE RETURNS NULL, # acc.expr WILL ASSUMED TO BE A VALUE, SO THE LAST TERM IS ASSUMED NOT NULL v = self.terms[-1] acc = FirstOp(v).partial_eval().to_es_script(schema) for v in reversed(self.terms[:-1]): m = v.missing().partial_eval() e = NotOp(m).partial_eval().to_es_script(schema) r = FirstOp(v).partial_eval().to_es_script(schema) if r.miss is TRUE: continue elif r.miss is FALSE: acc = r continue elif acc.type == r.type or acc.type == IS_NULL: 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 = EsScript( miss=AndOp([acc.miss, m]).partial_eval(), type=new_type, expr="(" + e.expr + ") ? (" + r.expr + ") : (" + acc.expr + ")", frum=self, schema=schema, ) return acc
def _convert(v): if v is None: return NULL.to_es_script(schema) if v is True: return EsScript( type=BOOLEAN, expr="true", frum=self ) if v is False: return EsScript( type=BOOLEAN, expr="false", frum=self ) if isinstance(v, text_type): return EsScript( type=STRING, expr=quote(v), frum=self ) if isinstance(v, int): return EsScript( type=INTEGER, expr=text_type(v), frum=self ) if isinstance(v, float): return EsScript( type=NUMBER, expr=text_type(v), frum=self ) if isinstance(v, dict): return EsScript( type=OBJECT, expr="[" + ", ".join(quote(k) + ": " + _convert(vv) for k, vv in v.items()) + "]", frum=self ) if isinstance(v, (list, tuple)): return EsScript( type=OBJECT, expr="[" + ", ".join(_convert(vv).expr for vv in v) + "]", frum=self ) if isinstance(v, Date): return EsScript( type=NUMBER, expr=text_type(v.unix), frum=self )
def to_es_script(self, schema, not_null=False, boolean=False, many=True): if self.var == ".": return EsScript(type=OBJECT, expr="_source", frum=self) else: if self.var == "_id": return EsScript( type=STRING, expr='doc["_uid"].value.substring(doc["_uid"].value.indexOf(\'#\')+1)', frum=self, schema=schema, ) columns = schema.values(self.var) acc = [] for c in columns: varname = c.es_column frum = Variable(c.es_column) q = quote(varname) if many: acc.append( EsScript( miss=frum.missing(), type=c.jx_type, expr="doc[" + q + "].values" if c.jx_type != BOOLEAN else "doc[" + q + "].value", frum=frum, schema=schema, many=c.jx_type != BOOLEAN, ) ) else: acc.append( EsScript( miss=frum.missing(), type=c.jx_type, expr="doc[" + q + "].value" if c.jx_type != BOOLEAN else "doc[" + q + "].value", frum=frum, schema=schema, many=True, ) ) if len(acc) == 0: return NULL.to_es_script(schema) elif len(acc) == 1: return acc[0] else: return CoalesceOp(acc).to_es_script(schema)
def to_es_script(self, schema, not_null=False, boolean=False, many=True): if self.var == ".": return EsScript(type=OBJECT, expr="_source", frum=self) else: if self.var == "_id": return EsScript( type=STRING, expr='doc["_uid"].value.substring(doc["_uid"].value.indexOf(\'#\')+1)', frum=self, schema=schema, ) columns = schema.values(self.var) acc = [] for c in columns: varname = c.es_column frum = Variable(c.es_column) q = quote(varname) if many: acc.append( EsScript( miss=frum.missing(), type=c.jx_type, expr="doc[" + q + "].values" if c.jx_type != BOOLEAN else "doc[" + q + "].value", frum=frum, schema=schema, many=c.jx_type != BOOLEAN, ) ) else: acc.append( EsScript( miss=frum.missing(), type=c.jx_type, expr="doc[" + q + "].value" if c.jx_type != BOOLEAN else "doc[" + q + "].value", frum=frum, schema=schema, many=True, ) ) if len(acc) == 0: return NULL.to_es_script(schema) elif len(acc) == 1: return acc[0] else: return CoalesceOp(acc).to_es_script(schema)
def to_bq(self, schema, not_null=False, boolean=False): sql = [] if not self.terms: return NULL.to_bq(schema) for term in self.terms: sql.append(BQLang[term].to_bq(schema)) return BQLScript(data_type=merge_json_type(*(t.type for t in sql)), expr=sql_call("COALESCE", *sql), frum=self, miss=AndOp([MissingOp(t) for t in self.terms]).partial_eval(), many=False, schema=schema)