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): term = FirstOp("first", self.term).partial_eval() value = term.to_ruby(schema) if isinstance(value.frum, CoalesceOp): return CoalesceOp( "coalesce", [StringOp("string", t).partial_eval() for t in value.frum.terms]).to_ruby(schema) if value.type == BOOLEAN: return Ruby(miss=self.term.missing().partial_eval(), type=STRING, expr=value.expr + ' ? "T" : "F"', frum=self) elif value.type == INTEGER: return Ruby(miss=self.term.missing().partial_eval(), type=STRING, expr="String.valueOf(" + value.expr + ")", frum=self) elif value.type == NUMBER: return Ruby(miss=self.term.missing().partial_eval(), type=STRING, expr=expand_template(TO_STRING, {"expr": value.expr}), frum=self) elif value.type == STRING: return value else: return Ruby(miss=self.term.missing().partial_eval(), type=STRING, expr=expand_template(TO_STRING, {"expr": value.expr}), frum=self)
def to_es_script(self, schema): terms = [ FirstOp("first", t).partial_eval().to_es_script(schema) for t in self.terms ] expr = 'new Object[]{' + ','.join(t.expr for t in terms) + '}' return EsScript(type=OBJECT, expr=expr, miss=FALSE, many=FALSE, frum=self)
def to_es_script(self, schema): term = FirstOp("first", self.term).partial_eval() value = term.to_es_script(schema) if isinstance(value.frum, CoalesceOp): return CoalesceOp("coalesce", [StringOp("string", t).partial_eval() for t in value.frum.terms]).to_es_script(schema) if value.type == BOOLEAN: return EsScript( miss=self.term.missing().partial_eval(), type=STRING, expr=value.expr + ' ? "T" : "F"', frum=self ) elif value.type == INTEGER: return EsScript( miss=self.term.missing().partial_eval(), type=STRING, expr="String.valueOf(" + value.expr + ")", frum=self ) elif value.type == NUMBER: return EsScript( miss=self.term.missing().partial_eval(), type=STRING, expr=expand_template(TO_STRING, {"expr":value.expr}), frum=self ) elif value.type == STRING: return value else: return EsScript( miss=self.term.missing().partial_eval(), type=STRING, expr=expand_template(TO_STRING, {"expr":value.expr}), frum=self )
def to_painless(self, schema): term = self.term.to_painless(schema) if isinstance(term.frum, CoalesceOp): return CoalesceOp("coalesce", [FirstOp("first", t.partial_eval().to_painless(schema)) for t in term.frum.terms]).to_painless(schema) if term.many: return Painless( miss=term.miss, type=term.type, expr="(" + term.expr + ")[0]", frum=term.frum ).to_painless(schema) else: return term
def to_ruby(self, schema): value = self.term.to_ruby(schema) if isinstance(self.term, Variable): if value.many: expr = "!" + value.expr + ".isEmpty() && " + value.expr + "[0]==\"T\"" else: expr = value.expr + "==\"T\"" return Ruby(miss=FALSE, type=BOOLEAN, expr=expr, frum=self) if value.type == BOOLEAN: return AndOp( "and", [ExistsOp("exists", self.term), FirstOp("first", self.term)]).partial_eval().to_ruby() else: return ExistsOp("exists", self.term).partial_eval().to_ruby()
def to_ruby(self, schema): term = FirstOp("first", self.term).partial_eval() value = term.to_ruby(schema) if isinstance(value.frum, CoalesceOp): return CoalesceOp("coalesce", [NumberOp("number", t).partial_eval().to_ruby(schema) for t in value.frum.terms]).to_ruby(schema) if value.type == BOOLEAN: return Ruby( miss=term.missing().partial_eval(), type=NUMBER, expr=value.expr + " ? 1 : 0", frum=self ) elif value.type == INTEGER: return Ruby( miss=term.missing().partial_eval(), type=NUMBER, expr=value.expr, frum=self ) elif value.type == NUMBER: return Ruby( miss=term.missing().partial_eval(), type=NUMBER, expr=value.expr, frum=self ) elif value.type == STRING: return Ruby( miss=term.missing().partial_eval(), type=NUMBER, expr="Double.parseDouble(" + value.expr + ")", frum=self ) elif value.type == OBJECT: return Ruby( miss=term.missing().partial_eval(), type=NUMBER, expr="((" + value.expr + ") instanceof String) ? Double.parseDouble(" + value.expr + ") : (" + value.expr + ")", frum=self )
def to_es_script(self, schema): if isinstance(self.term, Variable): columns = schema.values(self.term.var) if len(columns) == 1: return self.term.to_es_script(schema, many=False) term = self.term.to_es_script(schema) if isinstance(term.frum, CoalesceOp): return CoalesceOp("coalesce", [FirstOp("first", t.partial_eval().to_es_script(schema)) for t in term.frum.terms]).to_es_script(schema) if term.many: return EsScript( miss=term.miss, type=term.type, expr="(" + term.expr + ")[0]", frum=term.frum ).to_es_script(schema) else: return term
def to_es_script(self, schema): term = FirstOp("first", self.term).partial_eval() value = term.to_es_script(schema) if isinstance(value.frum, CoalesceOp): return CoalesceOp("coalesce", [NumberOp("number", t).partial_eval().to_es_script(schema) for t in value.frum.terms]).to_es_script(schema) if value.type == BOOLEAN: return EsScript( miss=term.missing().partial_eval(), type=NUMBER, expr=value.expr + " ? 1 : 0", frum=self ) elif value.type == INTEGER: return EsScript( miss=term.missing().partial_eval(), type=NUMBER, expr=value.expr, frum=self ) elif value.type == NUMBER: return EsScript( miss=term.missing().partial_eval(), type=NUMBER, expr=value.expr, frum=self ) elif value.type == STRING: return EsScript( miss=term.missing().partial_eval(), type=NUMBER, expr="Double.parseDouble(" + value.expr + ")", frum=self ) elif value.type == OBJECT: return EsScript( miss=term.missing().partial_eval(), type=NUMBER, expr="((" + value.expr + ") instanceof String) ? Double.parseDouble(" + value.expr + ") : (" + value.expr + ")", frum=self )