Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions core/query_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,17 @@ def format_expression(node: Node):

elif node.type == NodeType.OPERATOR:
# format: {'operator': [left, right]} or {'operator': operand} for unary ops
def _flatten_logical(n: Node, op_upper: str) -> list:
if (
isinstance(n, Node)
and n.type == NodeType.OPERATOR
and getattr(n, "name", "").upper() == op_upper
and len(list(n.children)) == 2
):
ch = list(n.children)
return _flatten_logical(ch[0], op_upper) + _flatten_logical(ch[1], op_upper)
return [n]

Comment on lines +330 to +340
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_flatten_logical is defined in both query_rewriter_v2.py and query_formatter.py. Could consider moving it to a new core/ast/utils.py file and importing it in both, but it may be more overhead than needed if this small function would the only thing in there

op_map = {
'>': 'gt',
'<': 'lt',
Expand Down Expand Up @@ -368,6 +379,12 @@ def format_expression(node: Node):

if op_name == 'sub' and len(children) == 2 and children[0].type == NodeType.LITERAL and children[0].value == 0:
return {'neg': format_expression(children[1])}

# Flatten left-associative AND/OR trees into the list form mo_sql_parsing expects.
if op_name in ("and", "or"):
flat = _flatten_logical(node, node.name.upper())
rendered = [format_expression(c) for c in flat]
return {op_name: rendered}

left = format_expression(children[0])

Expand Down
3 changes: 3 additions & 0 deletions core/query_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,9 @@ def parse_expression(self, expr, aliases: dict = None) -> Node:

# Special cases first
if 'all_columns' in expr:
qualifier = expr['all_columns']
if qualifier and qualifier != '*':
return ColumnNode('*', _parent_alias=qualifier)
return ColumnNode('*')
if 'literal' in expr:
# mo_sql_parsing uses {'literal': [..]} for IN literal lists and
Expand Down
Loading
Loading