1
0
Fork 0

Merging upstream version 17.2.0.

Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
Daniel Baumann 2025-02-13 20:43:05 +01:00
parent 06c5965633
commit ff2afd7448
Signed by: daniel
GPG key ID: FBB4F0E80A80222F
91 changed files with 42856 additions and 42624 deletions

View file

@ -105,6 +105,7 @@ class TypeAnnotator(metaclass=_TypeAnnotator):
exp.CurrentDate,
exp.Date,
exp.DateAdd,
exp.DateFromParts,
exp.DateStrToDate,
exp.DateSub,
exp.DateTrunc,

View file

@ -220,25 +220,6 @@ def _expand_group_by(scope: Scope):
group.set("expressions", _expand_positional_references(scope, group.expressions))
expression.set("group", group)
# group by expressions cannot be simplified, for example
# select x + 1 + 1 FROM y GROUP BY x + 1 + 1
# the projection must exactly match the group by key
groups = set(group.expressions)
group.meta["final"] = True
for e in expression.selects:
for node, *_ in e.walk():
if node in groups:
e.meta["final"] = True
break
having = expression.args.get("having")
if having:
for node, *_ in having.walk():
if node in groups:
having.meta["final"] = True
break
def _expand_order_by(scope: Scope, resolver: Resolver):
order = scope.expression.args.get("order")

View file

@ -8,6 +8,9 @@ from sqlglot import exp
from sqlglot.generator import cached_generator
from sqlglot.helper import first, while_changing
# Final means that an expression should not be simplified
FINAL = "final"
def simplify(expression):
"""
@ -27,8 +30,29 @@ def simplify(expression):
generate = cached_generator()
# group by expressions cannot be simplified, for example
# select x + 1 + 1 FROM y GROUP BY x + 1 + 1
# the projection must exactly match the group by key
for group in expression.find_all(exp.Group):
select = group.parent
groups = set(group.expressions)
group.meta[FINAL] = True
for e in select.selects:
for node, *_ in e.walk():
if node in groups:
e.meta[FINAL] = True
break
having = select.args.get("having")
if having:
for node, *_ in having.walk():
if node in groups:
having.meta[FINAL] = True
break
def _simplify(expression, root=True):
if expression.meta.get("final"):
if expression.meta.get(FINAL):
return expression
node = expression
node = rewrite_between(node)