Skip to content

Commit

Permalink
updates from other projects
Browse files Browse the repository at this point in the history
  • Loading branch information
klahnakoski committed Mar 10, 2024
1 parent 660b2d0 commit ea71503
Show file tree
Hide file tree
Showing 12 changed files with 73 additions and 17 deletions.
2 changes: 2 additions & 0 deletions jx_base/expressions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
from jx_base.expressions.sql_alias_op import SqlAliasOp
from jx_base.expressions.sql_and_op import SqlAndOp
from jx_base.expressions.sql_cast_op import SqlCastOp
from jx_base.expressions.sql_concat_op import SqlConcatOp
from jx_base.expressions.sql_eq_op import SqlEqOp
from jx_base.expressions.sql_group_by_op import SqlGroupByOp
from jx_base.expressions.sql_gt_op import SqlGtOp
Expand Down Expand Up @@ -229,6 +230,7 @@
"split": SplitOp,
"sql.and": SqlAndOp,
"sql.alias": SqlAliasOp,
"sql.concat": SqlConcatOp,
"sql.eq": SqlEqOp,
"sql.group_by": SqlGroupByOp,
"sql.inner_join": SqlInnerJoinOp,
Expand Down
1 change: 1 addition & 0 deletions jx_base/expressions/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ def merge_types(jx_types):
"min": lambda *v: min(*v),
"most": lambda *v: max(*v),
"least": lambda *v: min(*v),
"sql.concat": lambda *v: "".join(*v)
}

operators = {}
Expand Down
2 changes: 1 addition & 1 deletion jx_base/expressions/base_multi_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def partial_eval(self, lang):
literal_acc = None
terms = []
for t in self.terms:
simple = ToNumberOp(t).partial_eval(lang)
simple = t.partial_eval(lang)
if simple is NULL:
if self.decisive:
pass
Expand Down
2 changes: 1 addition & 1 deletion jx_base/expressions/case_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def __init__(self, *terms):

for w in self._whens:
if not is_op(w, WhenOp) or w.els_ is not NULL:
Log.error("case expression does not allow `else` clause in `when` sub-clause")
Log.error("case expression does not allow `else` clause in `when` sub-clause {case}", case=self.__data__())

@property
def whens(self):
Expand Down
5 changes: 2 additions & 3 deletions jx_base/expressions/literal.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
from mo_imports import expect, export
from mo_json import value2json, value_to_jx_type

(DateOp,
FALSE, TRUE, NULL) = expect("DateOp", "FALSE", "TRUE", "NULL")
(DateOp, FALSE, TRUE, NULL) = expect("DateOp", "FALSE", "TRUE", "NULL")


class Literal(Expression):
"""
A literal JSON document
"""
simplified = True

def __new__(cls, term):
if term == None:
Expand All @@ -44,7 +44,6 @@ def __new__(cls, term):

def __init__(self, value):
Expression.__init__(self)
self.simplified = True
self._value = value

@classmethod
Expand Down
13 changes: 6 additions & 7 deletions jx_base/expressions/sql_and_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,17 @@
#


from jx_base.expressions.expression import Expression
from jx_base.language import is_op
from jx_base.expressions.base_multi_op import BaseMultiOp
from jx_base.expressions.or_op import OrOp
from jx_base.language import is_op
from mo_json.types import JX_BOOLEAN


class SqlAndOp(Expression):
_data_type = JX_BOOLEAN
class SqlAndOp(BaseMultiOp):
_jx_type = JX_BOOLEAN

def __init__(self, *terms):
Expression.__init__(self, *terms)
self.terms = terms
def __init__(self, *args):
super().__init__(*args)

def __data__(self):
return {"sql.and": [t.__data__() for t in self.terms]}
Expand Down
48 changes: 48 additions & 0 deletions jx_base/expressions/sql_concat_op.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# encoding: utf-8
#
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http:# mozilla.org/MPL/2.0/.
#
# Contact: Kyle Lahnakoski (kyle@lahnakoski.com)
#


from jx_base.expressions.expression import Expression
from jx_base.expressions.or_op import OrOp
from jx_base.language import is_op
from mo_json.types import JX_TEXT


class SqlConcatOp(Expression):
"""
conservative string concatenation
"""
op = "sql.concat"
_jx_type = JX_TEXT

def __init__(self, *terms):
Expression.__init__(self, *terms)
self.terms = terms

def __data__(self):
return {"sql.concat": [t.__data__() for t in self.terms]}

def missing(self, lang):
return OrOp(*(t.missing(lang) for t in self.terms))

def __eq__(self, other):
if not is_op(other, SqlConcatOp):
return False
if len(self.terms) != len(other.terms):
return False
return all(t==o for t, o in zip(self.terms, other.terms))

def partial_eval(self, lang):
if not self.terms:
return NULL
elif len(self.terms) == 1:
return self.terms[0].partial_eval(lang)
else:
return lang.SqlConcatOp(*(t.partial_eval(lang) for t in self.terms))
6 changes: 6 additions & 0 deletions jx_base/expressions/sql_eq_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
from jx_base.expressions.expression import Expression
from jx_base.expressions.false_op import FALSE
from jx_base.expressions.literal import ZERO, ONE, is_literal
from jx_base.expressions.null_op import NULL
from jx_base.expressions.sql_is_null_op import SqlIsNullOp
from jx_base.language import is_op, value_compare
from mo_json.types import JX_BOOLEAN

Expand All @@ -38,5 +40,9 @@ def partial_eval(self, lang):

if is_literal(lhs) and is_literal(rhs):
return ZERO if value_compare(lhs.value, rhs.value) else ONE
elif lhs is NULL:
return SqlIsNullOp(self.rhs)
elif rhs is NULL:
return SqlIsNullOp(self.lhs)
else:
return lang.SqlEqOp(lhs, rhs)
2 changes: 1 addition & 1 deletion jx_base/expressions/sql_is_null_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@


class SqlIsNullOp(Expression):
_data_type = JX_BOOLEAN
_jx_type = JX_BOOLEAN

def __init__(self, term):
Expression.__init__(self, term)
Expand Down
2 changes: 1 addition & 1 deletion jx_base/expressions/sql_or_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class SqlOrOp(Expression):
"""
A CONSERVATIVE OR OPERATION
"""
_data_type = JX_BOOLEAN
_jx_type = JX_BOOLEAN

def __init__(self, *terms):
Expression.__init__(self, *terms)
Expand Down
5 changes: 2 additions & 3 deletions jx_base/expressions/sql_substr_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,15 @@
# Contact: Kyle Lahnakoski (kyle@lahnakoski.com)
#


from jx_base.expressions import NULL, FALSE
from jx_base.expressions.expression import Expression
from jx_base.expressions.false_op import FALSE
from mo_json import JX_INTEGER


class SqlSubstrOp(Expression):
_jx_type = JX_INTEGER

def __init__(self, value, start, length):
def __init__(self, value, start, length=NULL):
Expression.__init__(self, value, start, length)
self.value, self.start, self.length = value, start, length

Expand Down
2 changes: 2 additions & 0 deletions jx_base/expressions/to_text_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ def partial_eval(self, lang):
return term.term.partial_eval(lang)
elif is_op(term, CoalesceOp):
return lang.CoalesceOp(*(ToTextOp(t).partial_eval(lang) for t in term.terms))
elif term.jx_type == JX_TEXT:
return term
elif is_literal(term):
if term.jx_type == JX_TEXT:
return term
Expand Down

0 comments on commit ea71503

Please sign in to comment.