summaryrefslogtreecommitdiffstats
path: root/sqlglot/optimizer/pushdown_predicates.py
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2023-06-02 23:59:11 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2023-06-02 23:59:11 +0000
commitcaea5267cb8e1fea3702adbdf6f68fd37d13b3b7 (patch)
treef06f1da1ab3b6906beca1c3c7222d28ff00766ac /sqlglot/optimizer/pushdown_predicates.py
parentAdding upstream version 12.2.0. (diff)
downloadsqlglot-0c9fd0a27262a4b82d2347fe92db95748c7421d4.tar.xz
sqlglot-0c9fd0a27262a4b82d2347fe92db95748c7421d4.zip
Adding upstream version 15.0.0.upstream/15.0.0
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'sqlglot/optimizer/pushdown_predicates.py')
-rw-r--r--sqlglot/optimizer/pushdown_predicates.py42
1 files changed, 22 insertions, 20 deletions
diff --git a/sqlglot/optimizer/pushdown_predicates.py b/sqlglot/optimizer/pushdown_predicates.py
index ba5c8b5..96dda33 100644
--- a/sqlglot/optimizer/pushdown_predicates.py
+++ b/sqlglot/optimizer/pushdown_predicates.py
@@ -21,26 +21,28 @@ def pushdown_predicates(expression):
sqlglot.Expression: optimized expression
"""
root = build_scope(expression)
- scope_ref_count = root.ref_count()
-
- for scope in reversed(list(root.traverse())):
- select = scope.expression
- where = select.args.get("where")
- if where:
- selected_sources = scope.selected_sources
- # a right join can only push down to itself and not the source FROM table
- 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
- pushdown(where.this, selected_sources, scope_ref_count)
-
- # joins should only pushdown into itself, not to other joins
- # so we limit the selected sources to only itself
- for join in select.args.get("joins") or []:
- name = join.this.alias_or_name
- pushdown(join.args.get("on"), {name: scope.selected_sources[name]}, scope_ref_count)
+
+ if root:
+ scope_ref_count = root.ref_count()
+
+ for scope in reversed(list(root.traverse())):
+ select = scope.expression
+ where = select.args.get("where")
+ if where:
+ selected_sources = scope.selected_sources
+ # a right join can only push down to itself and not the source FROM table
+ 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
+ pushdown(where.this, selected_sources, scope_ref_count)
+
+ # joins should only pushdown into itself, not to other joins
+ # so we limit the selected sources to only itself
+ for join in select.args.get("joins") or []:
+ name = join.this.alias_or_name
+ pushdown(join.args.get("on"), {name: scope.selected_sources[name]}, scope_ref_count)
return expression