1
0
Fork 0

Merging upstream version 11.3.0.

Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
Daniel Baumann 2025-02-13 15:42:13 +01:00
parent f223c02081
commit 1c10961499
Signed by: daniel
GPG key ID: FBB4F0E80A80222F
62 changed files with 26499 additions and 24781 deletions

View file

@ -314,13 +314,27 @@ def _merge_where(outer_scope, inner_scope, from_or_join):
if not where or not where.this:
return
expression = outer_scope.expression
if isinstance(from_or_join, exp.Join):
# Merge predicates from an outer join to the ON clause
from_or_join.on(where.this, copy=False)
from_or_join.set("on", simplify(from_or_join.args.get("on")))
else:
outer_scope.expression.where(where.this, copy=False)
outer_scope.expression.set("where", simplify(outer_scope.expression.args.get("where")))
# if it only has columns that are already joined
from_ = expression.args.get("from")
sources = {table.alias_or_name for table in from_.expressions} if from_ else {}
for join in expression.args["joins"]:
source = join.alias_or_name
sources.add(source)
if source == from_or_join.alias_or_name:
break
if set(exp.column_table_names(where.this)) <= sources:
from_or_join.on(where.this, copy=False)
from_or_join.set("on", simplify(from_or_join.args.get("on")))
return
expression.where(where.this, copy=False)
expression.set("where", simplify(expression.args.get("where")))
def _merge_order(outer_scope, inner_scope):

View file

@ -13,7 +13,7 @@ SELECT_ALL = object()
DEFAULT_SELECTION = lambda: alias("1", "_")
def pushdown_projections(expression, schema=None):
def pushdown_projections(expression, schema=None, remove_unused_selections=True):
"""
Rewrite sqlglot AST to remove unused columns projections.
@ -26,6 +26,7 @@ def pushdown_projections(expression, schema=None):
Args:
expression (sqlglot.Expression): expression to optimize
remove_unused_selections (bool): remove selects that are unused
Returns:
sqlglot.Expression: optimized expression
"""
@ -57,7 +58,8 @@ def pushdown_projections(expression, schema=None):
]
if isinstance(scope.expression, exp.Select):
_remove_unused_selections(scope, parent_selections, schema)
if remove_unused_selections:
_remove_unused_selections(scope, parent_selections, schema)
# Group columns by source name
selects = defaultdict(set)