1
0
Fork 0

Merging upstream version 26.17.1.

Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
Daniel Baumann 2025-05-12 06:51:01 +02:00
parent 85ae79ba39
commit 7a05a01838
Signed by: daniel
GPG key ID: FBB4F0E80A80222F
63 changed files with 16004 additions and 15816 deletions

View file

@ -2,6 +2,7 @@ from sqlglot import exp
from sqlglot.optimizer.normalize import normalized
from sqlglot.optimizer.scope import build_scope, find_in_scope
from sqlglot.optimizer.simplify import simplify
from sqlglot import Dialect
def pushdown_predicates(expression, dialect=None):
@ -20,8 +21,13 @@ def pushdown_predicates(expression, dialect=None):
Returns:
sqlglot.Expression: optimized expression
"""
from sqlglot.dialects.presto import Presto
root = build_scope(expression)
dialect = Dialect.get_or_raise(dialect)
unnest_requires_cross_join = isinstance(dialect, Presto)
if root:
scope_ref_count = root.ref_count()
@ -35,13 +41,20 @@ def pushdown_predicates(expression, dialect=None):
}
# a right join can only push down to itself and not the source FROM table
# presto, trino and athena don't support inner joins where the RHS is an UNNEST expression
pushdown_allowed = True
for k, (node, source) in selected_sources.items():
parent = node.find_ancestor(exp.Join, exp.From)
if isinstance(parent, exp.Join) and parent.side == "RIGHT":
selected_sources = {k: (node, source)}
break
if isinstance(parent, exp.Join):
if parent.side == "RIGHT":
selected_sources = {k: (node, source)}
break
if isinstance(node, exp.Unnest) and unnest_requires_cross_join:
pushdown_allowed = False
break
pushdown(where.this, selected_sources, scope_ref_count, dialect, join_index)
if pushdown_allowed:
pushdown(where.this, selected_sources, scope_ref_count, dialect, join_index)
# joins should only pushdown into itself, not to other joins
# so we limit the selected sources to only itself