Ejemplo n.º 1
0
def to_ruby(self, schema):
    if len(self.terms) == 0:
        return self.default.to_ruby(schema)

    acc = []
    separator = StringOp("string", self.separator).partial_eval()
    sep = separator.to_ruby(schema).expr
    for t in self.terms:
        val = WhenOp(
            "when",
            t.missing(),
            **{
                "then":
                Literal("literal", ""),
                "else":
                Ruby(type=STRING,
                     expr=sep + "+" +
                     StringOp(None, t).partial_eval().to_ruby(schema).expr,
                     frum=t)
                # "else": ConcatOp("concat", [sep, t])
            })
        acc.append("(" + val.partial_eval().to_ruby(schema).expr + ")")
    expr_ = "(" + "+".join(acc) + ").substring(" + LengthOp(
        "length", separator).to_ruby(schema).expr + ")"

    if isinstance(self.default, NullOp):
        return Ruby(miss=self.missing(), type=STRING, expr=expr_, frum=self)
    else:
        return Ruby(miss=self.missing(),
                    type=STRING,
                    expr="((" + expr_ + ").length==0) ? (" +
                    self.default.to_ruby(schema).expr + ") : (" + expr_ + ")",
                    frum=self)
Ejemplo n.º 2
0
def to_sql(self, schema, not_null=False, boolean=False):
    if not self.expr:
        return wrap([{"name": ".", "sql": {"b": SQL_FALSE}}])
    elif is_literal(self.suffix) and not self.suffix.value:
        return wrap([{"name": ".", "sql": {"b": SQL_TRUE}}])
    else:
        return EqOp([RightOp([self.expr, LengthOp(self.suffix)]),
                     self.suffix]).partial_eval().to_sql(schema)
Ejemplo n.º 3
0
def to_sql(self, schema, not_null=False, boolean=False):
    if not self.term:
        return wrap([{"name": ".", "sql": {"b": SQL_FALSE}}])
    elif isinstance(self.suffix, Literal) and not self.suffix.value:
        return wrap([{"name": ".", "sql": {"b": SQL_TRUE}}])
    else:
        return EqOp("eq", [
            RightOp("right",
                    [self.term, LengthOp("length", self.suffix)]), self.suffix
        ]).partial_eval().to_sql(schema)
Ejemplo n.º 4
0
def partial_eval(self):
    value = self.value.partial_eval()
    length = self.length.partial_eval()
    max_length = LengthOp("length", value)

    return BasicSubstringOp("substring", [
        value,
        MaxOp("max", [ZERO, MinOp("min", [max_length, BinaryOp("sub", [max_length, length])])]),
        max_length
    ])
Ejemplo n.º 5
0
def partial_eval(self):
    value = self.value.partial_eval()
    length = self.length.partial_eval()
    max_length = LengthOp(value)

    return BasicSubstringOp([
        value,
        MaxOp([ZERO,
               MinOp([max_length,
                      BaseBinaryOp([max_length, length])])]), max_length
    ])
Ejemplo n.º 6
0
def to_sql(self, schema, not_null=False, boolean=False):
    defult = self.default.to_sql(schema)
    if len(self.terms) == 0:
        return defult
    defult = coalesce(defult[0].sql, SQL_NULL)
    sep = self.separator.to_sql(schema)[0].sql.s

    acc = []
    for t in self.terms:
        missing = t.missing().partial_eval()

        term = t.to_sql(schema, not_null=True)[0].sql
        if term.s:
            term_sql = term.s
        elif term.n:
            term_sql = "cast(" + term.n + " as text)"
        else:
            term_sql = SQL_CASE + SQL_WHEN + term.b + SQL_THEN + quote_value(
                "true") + SQL_ELSE + quote_value("false") + SQL_END

        if is_op(missing, Variable):
            acc.append(SQL_EMPTY_STRING)
        elif missing:
            acc.append(SQL_CASE + SQL_WHEN +
                       sql_iso(missing.to_sql(schema, boolean=True)[0].sql.b) +
                       SQL_THEN + SQL_EMPTY_STRING + SQL_ELSE +
                       sql_iso(sql_concat([sep, term_sql])) + SQL_END)
        else:
            acc.append(sql_concat([sep, term_sql]))

    expr_ = "substr(" + sql_concat(acc) + ", " + LengthOp(
        self.separator).to_sql(schema)[0].sql.n + "+1)"

    missing = self.missing()
    if not missing:
        return wrap([{"name": ".", "sql": {"s": expr_}}])
    else:
        return wrap([{
            "name": ".",
            "sql": {
                "s":
                SQL_CASE + SQL_WHEN + "(" +
                missing.to_sql(schema, boolean=True)[0].sql.b + ")" +
                SQL_THEN + "(" + defult + ")" + SQL_ELSE + "(" + expr_ + ")" +
                SQL_END
            }
        }])
Ejemplo n.º 7
0
def to_sql(self, schema, not_null=False, boolean=False):
    defult = self.default.to_sql(schema)
    if len(self.terms) == 0:
        return defult
    defult = coalesce(defult[0].sql, "NULL")
    sep = self.separator.to_sql(schema)[0].sql.s

    acc = []
    for t in self.terms:
        missing = t.missing()

        term = t.to_sql(schema, not_null=True)[0].sql
        term_sql = coalesce(
            term.s, "cast(" + term.n + " as text)",
            "CASE WHEN " + term.b + " THEN `true` ELSE `false` END")

        if isinstance(missing, TrueOp):
            acc.append("''")
        elif missing:
            acc.append("CASE WHEN (" +
                       missing.to_sql(schema, boolean=True)[0].sql.b +
                       ") THEN '' ELSE  ((" + sep + ") || (" + term_sql +
                       ")) END")
        else:
            acc.append("(" + sep + ") || (" + term_sql + ")")

    expr_ = "substr(" + " || ".join(acc) + ", " + LengthOp(
        None, self.separator).to_sql(schema)[0].sql.n + "+1)"

    missing = self.missing()
    if not missing:
        return wrap([{"name": ".", "sql": {"s": expr_}}])
    else:
        return wrap([{
            "name": ".",
            "sql": {
                "s":
                "CASE WHEN (" + missing.to_sql(schema, boolean=True)[0].sql.b +
                ") THEN (" + defult + ") ELSE (" + expr_ + ") END"
            }
        }])