diff options
Diffstat (limited to 'sqlglot/optimizer/scope.py')
-rw-r--r-- | sqlglot/optimizer/scope.py | 20 |
1 files changed, 19 insertions, 1 deletions
diff --git a/sqlglot/optimizer/scope.py b/sqlglot/optimizer/scope.py index badbb87..8565c64 100644 --- a/sqlglot/optimizer/scope.py +++ b/sqlglot/optimizer/scope.py @@ -237,6 +237,8 @@ class Scope: ancestor = column.find_ancestor(exp.Qualify, exp.Order, exp.Having, exp.Hint) if ( not ancestor + # Window functions can have an ORDER BY clause + or not isinstance(ancestor.parent, exp.Select) or column.table or (column.name not in named_selects and not isinstance(ancestor, exp.Hint)) ): @@ -479,7 +481,7 @@ def _traverse_scope(scope): elif isinstance(scope.expression, exp.Union): yield from _traverse_union(scope) elif isinstance(scope.expression, exp.UDTF): - pass + _set_udtf_scope(scope) elif isinstance(scope.expression, exp.Subquery): yield from _traverse_subqueries(scope) else: @@ -509,6 +511,22 @@ def _traverse_union(scope): scope.union_scopes = [left, right] +def _set_udtf_scope(scope): + parent = scope.expression.parent + from_ = parent.args.get("from") + + if not from_: + return + + for table in from_.expressions: + if isinstance(table, exp.Table): + scope.tables.append(table) + elif isinstance(table, exp.Subquery): + scope.subqueries.append(table) + _add_table_sources(scope) + _traverse_subqueries(scope) + + def _traverse_derived_tables(derived_tables, scope, scope_type): sources = {} is_cte = scope_type == ScopeType.CTE |