Esempio n. 1
0
    def partial_eval(self):
        term = self.lang[FirstOp(self.term)].partial_eval()

        if is_literal(term):
            if term is NULL:
                return NULL
            elif term is FALSE:
                return ZERO
            elif term is TRUE:
                return ONE

            v = term.value
            if isinstance(v, (text, Date)):
                return self.lang[Literal(float(v))]
            elif isinstance(v, (int, float)):
                return term
            else:
                Log.error("can not convert {{value|json}} to number",
                          value=term.value)
        elif is_op(term, CaseOp):  # REWRITING
            return self.lang[CaseOp([
                WhenOp(t.when, **{"then": NumberOp(t.then)})
                for t in term.whens[:-1]
            ] + [NumberOp(term.whens[-1])])].partial_eval()
        elif is_op(term, WhenOp):  # REWRITING
            return self.lang[WhenOp(
                term.when, **{
                    "then": NumberOp(term.then),
                    "else": NumberOp(term.els_)
                })].partial_eval()
        elif is_op(term, CoalesceOp):
            return self.lang[CoalesceOp([NumberOp(t) for t in term.terms])]
        return self.lang[NumberOp(term)]
Esempio n. 2
0
 def partial_eval(self):
     term = self.lang[FirstOp(self.term)].partial_eval()
     if is_op(term, CoalesceOp):
         return self.lang[CoalesceOp([IntegerOp(t) for t in term.terms])]
     if term.type == INTEGER:
         return term
     return self.lang[IntegerOp(term)]
Esempio n. 3
0
 def partial_eval(self, lang):
     term = FirstOp(self.term).partial_eval(lang)
     if is_op(term, CoalesceOp):
         return (CoalesceOp([IntegerOp(t) for t in term.terms]))
     if term.type == INTEGER:
         return term
     return (IntegerOp(term))
Esempio n. 4
0
    def partial_eval(self):
        acc = None
        terms = []
        for t in self.terms:
            simple = t.partial_eval()
            if simple is NULL:
                pass
            elif is_literal(simple):
                if acc is None:
                    acc = simple.value
                else:
                    acc = builtin_ops[self.op](acc, simple.value)
            else:
                terms.append(simple)

        lang = self.lang
        if len(terms) == 0:
            if acc == None:
                return self.default.partial_eval()
            else:
                return lang[Literal(acc)]
        elif self.nulls:
            # DECISIVE
            if acc is not None:
                terms.append(Literal(acc))

            output = lang[
                WhenOp(
                    AndOp([t.missing() for t in terms]),
                    **{
                        "then": self.default,
                        "else": operators["basic." + self.op](
                            [CoalesceOp([t, _jx_identity[self.op]]) for t in terms]
                        ),
                    }
                )
            ].partial_eval()
        else:
            # CONSERVATIVE
            if acc is not None:
                terms.append(lang[Literal(acc)])

            output = lang[
                WhenOp(
                    lang[OrOp([t.missing() for t in terms])],
                    **{
                        "then": self.default,
                        "else": operators["basic." + self.op](terms),
                    }
                )
            ].partial_eval()

        return output
Esempio n. 5
0
 def partial_eval(self):
     term = self.term
     if term.type is IS_NULL:
         return NULL
     term = self.lang[FirstOp(term)].partial_eval()
     if is_op(term, StringOp):
         return term.term.partial_eval()
     elif is_op(term, CoalesceOp):
         return self.lang[CoalesceOp(
             [self.lang[StringOp(t)].partial_eval() for t in term.terms])]
     elif is_literal(term):
         if term.type == STRING:
             return term
         else:
             return self.lang[Literal(mo_json.value2json(term.value))]
     return self
Esempio n. 6
0
 def partial_eval(self, lang):
     term = self.term
     if term.type is IS_NULL:
         return NULL
     term = (FirstOp(term)).partial_eval(lang)
     if is_op(term, StringOp):
         return term.term.partial_eval(lang)
     elif is_op(term, CoalesceOp):
         return CoalesceOp([
             (StringOp(t)).partial_eval(lang) for t in term.terms
         ])
     elif is_literal(term):
         if term.type == STRING:
             return term
         else:
             return (Literal(mo_json.value2json(term.value)))
     return self