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):
        lhs = FirstOp(self.lhs).partial_eval()
        rhs = FirstOp(self.rhs).partial_eval()

        if rhs == ONE:
            script = "(int)Math.floor(" + lhs.to_es_script(schema).expr + ")"
        else:
            rhs = rhs.to_es_script(schema)
            script = ("Math.floor((" + lhs.to_es_script(schema).expr +
                      ") / (" + rhs.expr + "))*(" + rhs.expr + ")")

        output = WhenOp(
            OrOp([lhs.missing(),
                  rhs.missing(),
                  EqOp([self.rhs, ZERO])]), **{
                      "then":
                      self.default,
                      "else":
                      EsScript(type=NUMBER,
                               expr=script,
                               frum=self,
                               miss=FALSE,
                               schema=schema),
                  }).partial_eval().to_es_script(schema)
        return output
Exemple #3
0
    def to_es_script(self, schema, not_null=False, boolean=False, many=True):
        term = FirstOp(self.term).partial_eval()

        value = term.to_es_script(schema)

        if is_op(value.frum, CoalesceOp_):
            return CoalesceOp([
                NumberOp(t).partial_eval().to_es_script(schema)
                for t in value.frum.terms
            ]).to_es_script(schema)

        if value is null_script:
            return Literal(0).to_es_script(schema)
        if value is false_script:
            return Literal(0).to_es_script(schema)
        if value is true_script:
            return Literal(1).to_es_script(schema)
        elif value.type == BOOLEAN:
            return EsScript(
                miss=term.missing().partial_eval(),
                type=NUMBER,
                expr="(" + value.expr + ") ? 1 : 0",
                frum=self,
                schema=schema,
            )
        elif value.type == INTEGER:
            return EsScript(
                miss=term.missing().partial_eval(),
                type=NUMBER,
                expr=value.expr,
                frum=self,
                schema=schema,
            )
        elif value.type == NUMBER:
            return EsScript(
                miss=term.missing().partial_eval(),
                type=NUMBER,
                expr=value.expr,
                frum=self,
                schema=schema,
            )
        elif value.type == STRING:
            return EsScript(
                miss=term.missing().partial_eval(),
                type=NUMBER,
                expr="Double.parseDouble(" + value.expr + ")",
                frum=self,
                schema=schema,
            )
        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,
                schema=schema,
            )
    def to_es_script(self, schema, not_null=False, boolean=False, many=True):
        term = FirstOp(self.term).partial_eval()
        value = term.to_es_script(schema)

        if is_op(value.frum, CoalesceOp_):
            return CoalesceOp([
                StringOp(t).partial_eval() for t in value.frum.terms
            ]).to_es_script(schema)

        if value.miss is TRUE:
            return empty_string_script
        elif value.type == BOOLEAN:
            return EsScript(
                miss=self.term.missing().partial_eval(),
                type=STRING,
                expr=value.expr + ' ? "T" : "F"',
                frum=self,
                schema=schema,
            )
        elif value.type == INTEGER:
            return EsScript(
                miss=self.term.missing().partial_eval(),
                type=STRING,
                expr="String.valueOf(" + value.expr + ")",
                frum=self,
                schema=schema,
            )
        elif value.type == NUMBER:
            return EsScript(
                miss=self.term.missing().partial_eval(),
                type=STRING,
                expr=expand_template(NUMBER_TO_STRING, {"expr": value.expr}),
                frum=self,
                schema=schema,
            )
        elif value.type == STRING:
            return value
        else:
            return EsScript(
                miss=self.term.missing().partial_eval(),
                type=STRING,
                expr=expand_template(NUMBER_TO_STRING, {"expr": value.expr}),
                frum=self,
                schema=schema,
            )
 def to_es_script(self, schema, not_null=False, boolean=False, many=True):
     expr = ("new Object[]{" + ",".join(
         text(FirstOp(t).partial_eval().to_es_script(schema))
         for t in self.terms) + "}")
     return EsScript(type=OBJECT,
                     expr=expr,
                     many=FALSE,
                     frum=self,
                     schema=schema)
Exemple #6
0
    def to_es_script(self, schema, not_null=False, boolean=False, many=True):
        expr = Painless[FirstOp(
            self.value)].partial_eval().to_es_script(schema)
        if expr is empty_string_script:
            return false_script

        prefix = Painless[self.prefix].to_es_script(schema).partial_eval()
        return EsScript(
            miss=FALSE,
            type=BOOLEAN,
            expr="(" + expr.expr + ").startsWith(" + prefix.expr + ")",
            frum=self,
            schema=schema,
        )