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 #2
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,
            )