Skip to content

Commit

Permalink
Refactor(scope): rename _is_subquery_scope to _is_derived_table for c…
Browse files Browse the repository at this point in the history
…larity (#1912)
  • Loading branch information
georgesittas authored Jul 12, 2023
1 parent cbcb113 commit 726306e
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions sqlglot/optimizer/scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def _collect(self):
elif (
isinstance(node, exp.Subquery)
and isinstance(parent, (exp.From, exp.Join))
and _is_subquery_scope(node)
and _is_derived_table(node)
):
self._derived_tables.append(node)
elif isinstance(node, exp.Subqueryable):
Expand Down Expand Up @@ -610,13 +610,13 @@ def _traverse_ctes(scope):
scope.sources.update(sources)


def _is_subquery_scope(expression: exp.Subquery) -> bool:
def _is_derived_table(expression: exp.Subquery) -> bool:
"""
We represent (tbl1 JOIN tbl2) as a Subquery, but it's not really a new scope.
If an alias is present, it shadows all names under the Subquery, so that's an
exception to this rule.
We represent (tbl1 JOIN tbl2) as a Subquery, but it's not really a "derived table",
as it doesn't introduce a new scope. If an alias is present, it shadows all names
under the Subquery, so that's one exception to this rule.
"""
return bool(not isinstance(expression.unnest(), exp.Table) or expression.alias)
return bool(expression.alias or not isinstance(expression.unnest(), exp.Table))


def _traverse_tables(scope):
Expand Down Expand Up @@ -664,7 +664,7 @@ def _traverse_tables(scope):
lateral_sources = sources
scope_type = ScopeType.UDTF
scopes = scope.udtf_scopes
elif _is_subquery_scope(expression):
elif _is_derived_table(expression):
lateral_sources = None
scope_type = ScopeType.DERIVED_TABLE
scopes = scope.derived_table_scopes
Expand Down Expand Up @@ -736,7 +736,7 @@ def walk_in_scope(expression, bfs=True):
or (
isinstance(node, exp.Subquery)
and isinstance(parent, (exp.From, exp.Join))
and _is_subquery_scope(node)
and _is_derived_table(node)
)
or isinstance(node, exp.UDTF)
or isinstance(node, exp.Subqueryable)
Expand Down

0 comments on commit 726306e

Please sign in to comment.