Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix(optimizer): traverse UNNEST scope #1960

Merged
merged 4 commits into from
Jul 26, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion sqlglot/optimizer/scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ def _traverse_scope(scope):
elif isinstance(scope.expression, exp.Table):
yield from _traverse_tables(scope)
elif isinstance(scope.expression, exp.UDTF):
pass
yield from _traverse_udtfs(scope)
else:
logger.warning(
"Cannot traverse scope %s with type '%s'", scope.expression, type(scope.expression)
Expand Down Expand Up @@ -711,6 +711,31 @@ def _traverse_subqueries(scope):
scope.subquery_scopes.append(top)


def _traverse_udtfs(scope):
sources = {}

if isinstance(scope.expression, exp.Unnest):
georgesittas marked this conversation as resolved.
Show resolved Hide resolved
unnested_source = scope.expression.expressions[0]
georgesittas marked this conversation as resolved.
Show resolved Hide resolved
if isinstance(unnested_source, exp.Subquery) and _is_derived_table(unnested_source):
top = None
for child_scope in _traverse_scope(
scope.branch(
unnested_source,
scope_type=ScopeType.DERIVED_TABLE,
outer_column_list=unnested_source.alias_column_names,
)
):
yield child_scope
top = child_scope

alias = unnested_source.alias
sources[alias] = child_scope

scope.derived_table_scopes.append(top)

scope.sources.update(sources)


def walk_in_scope(expression, bfs=True):
"""
Returns a generator object which visits all nodes in the syntrax tree, stopping at
Expand Down