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.miss is TRUE: new_type = r.type elif acc.type in NUMBER_TYPES and r.type in NUMBER_TYPES: 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 is_op(self.expr, Variable_): if self.expr.var == "_id": return EsScript(type=BOOLEAN, expr="false", frum=self, schema=schema) else: columns = schema.leaves(self.expr.var) return ( AndOp( [ EsScript( type=BOOLEAN, expr="doc[" + quote(c.es_column) + "].empty", frum=self, schema=schema, ) for c in columns ] ) .partial_eval() .to_es_script(schema) ) elif is_literal(self.expr): return self.expr.missing().to_es_script(schema) else: return self.expr.missing().partial_eval().to_es_script(schema)
def missing(self): output = AndOp([ self.default.missing(), OrOp([ self.value.missing(), self.find.missing(), EqOp([ BasicIndexOfOp([self.value, self.find, self.start]), Literal(-1), ]), ]), ]).partial_eval() return output
def to_es_script(self, schema, not_null=False, boolean=False, many=True): acc = NumberOp(self.terms[-1]).partial_eval().to_es_script(schema).expr for t in reversed(self.terms[0:-1]): acc = ("Math.max(" + NumberOp(t).partial_eval().to_es_script(schema).expr + " , " + acc + ")") return EsScript( miss=AndOp([t.missing() for t in self.terms]), type=NUMBER, expr=acc, frum=self, schema=schema, )
def to_es_script(self, schema, not_null=False, boolean=False, many=True): simple_rhs = Painless[self.rhs].partial_eval() lhs = Painless[self.lhs].partial_eval().to_es_script(schema) rhs = simple_rhs.to_es_script(schema) if lhs.many: if rhs.many: return AndOp([ EsScript( type=BOOLEAN, expr="(" + lhs.expr + ").size()==(" + rhs.expr + ").size()", frum=self, schema=schema, ), EsScript( type=BOOLEAN, expr="(" + rhs.expr + ").containsAll(" + lhs.expr + ")", frum=self, schema=schema, ), ]).to_es_script(schema) else: if lhs.type == BOOLEAN: if is_literal(simple_rhs) and simple_rhs.value in ("F", False): return EsScript(type=BOOLEAN, expr="!" + lhs.expr, frum=self, schema=schema) elif is_literal(simple_rhs) and simple_rhs.value in ("T", True): return EsScript(type=BOOLEAN, expr=lhs.expr, frum=self, schema=schema) else: return EsScript( type=BOOLEAN, expr="(" + lhs.expr + ")==(" + rhs.expr + ")", frum=self, schema=schema, ) elif lhs.type == rhs.type: return EsScript( type=BOOLEAN, expr="(" + lhs.expr + ").contains(" + rhs.expr + ")", frum=self, schema=schema, ) elif lhs.type == NUMBER and rhs.type == INTEGER: return EsScript( type=BOOLEAN, expr="(" + lhs.expr + ").contains((double)" + rhs.expr + ")", frum=self, schema=schema, ) else: Log.error( "type mismatch not expected while converting to painless" ) elif rhs.many: return EsScript( type=BOOLEAN, expr="(" + rhs.expr + ").contains(" + lhs.expr + ")", frum=self, schema=schema, ) else: if lhs is null_script: if rhs is null_script: return TRUE.to_es_script(schema) return FALSE.to_es_script(schema) elif lhs.type == BOOLEAN: if is_literal(simple_rhs) and simple_rhs.value in ("F", False): return EsScript(type=BOOLEAN, expr="!" + lhs.expr, frum=self, schema=schema) elif is_literal(simple_rhs) and simple_rhs.value in ("T", True): return EsScript(type=BOOLEAN, expr=lhs.expr, frum=self, schema=schema) else: return EsScript( type=BOOLEAN, expr="(" + lhs.expr + ")==(" + rhs.expr + ")", frum=self, schema=schema, ) else: return EsScript( type=BOOLEAN, expr="(" + lhs.expr + ")==(" + rhs.expr + ")", frum=self, schema=schema, )