Skip to content

Commit

Permalink
Fix: expand laterals first if no schema is present
Browse files Browse the repository at this point in the history
  • Loading branch information
tobymao authored and adrianisk committed Jun 21, 2023
1 parent ca5bf34 commit 5f06fb0
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 5 deletions.
4 changes: 2 additions & 2 deletions sqlglot/optimizer/expand_laterals.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ def expand_laterals(expression: exp.Expression) -> exp.Expression:
for column in projection.find_all(exp.Column):
if not column.table and column.name in alias_to_expression:
column.replace(alias_to_expression[column.name].copy())
if isinstance(projection, exp.Alias):
alias_to_expression[projection.alias] = projection.this
if isinstance(projection, exp.Alias):
alias_to_expression[projection.alias] = projection.this
return expression
2 changes: 0 additions & 2 deletions sqlglot/optimizer/optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from sqlglot.optimizer.eliminate_ctes import eliminate_ctes
from sqlglot.optimizer.eliminate_joins import eliminate_joins
from sqlglot.optimizer.eliminate_subqueries import eliminate_subqueries
from sqlglot.optimizer.expand_laterals import expand_laterals
from sqlglot.optimizer.expand_multi_table_selects import expand_multi_table_selects
from sqlglot.optimizer.isolate_table_selects import isolate_table_selects
from sqlglot.optimizer.lower_identities import lower_identities
Expand All @@ -30,7 +29,6 @@
qualify_tables,
isolate_table_selects,
qualify_columns,
expand_laterals,
pushdown_projections,
validate_qualify_columns,
normalize,
Expand Down
9 changes: 8 additions & 1 deletion sqlglot/optimizer/qualify_columns.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@

from sqlglot import alias, exp
from sqlglot.errors import OptimizeError
from sqlglot.optimizer.expand_laterals import expand_laterals as _expand_laterals
from sqlglot.optimizer.scope import Scope, traverse_scope
from sqlglot.schema import ensure_schema


def qualify_columns(expression, schema):
def qualify_columns(expression, schema, expand_laterals=True):
"""
Rewrite sqlglot AST to have fully qualified columns.
Expand All @@ -26,6 +27,9 @@ def qualify_columns(expression, schema):
"""
schema = ensure_schema(schema)

if not schema.mapping and expand_laterals:
expression = _expand_laterals(expression)

for scope in traverse_scope(expression):
resolver = Resolver(scope, schema)
_pop_table_column_aliases(scope.ctes)
Expand All @@ -39,6 +43,9 @@ def qualify_columns(expression, schema):
_expand_group_by(scope, resolver)
_expand_order_by(scope)

if schema.mapping and expand_laterals:
expression = _expand_laterals(expression)

return expression


Expand Down
6 changes: 6 additions & 0 deletions tests/test_optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,12 @@ def test_pushdown_predicates(self):
self.check_file("pushdown_predicates", optimizer.pushdown_predicates.pushdown_predicates)

def test_expand_laterals(self):
# check order of lateral expansion with no schema
self.assertEqual(
optimizer.optimize("SELECT a + 1 AS d, d + 1 AS e FROM x " "").sql(),
'SELECT "x"."a" + 1 AS "d", "x"."a" + 2 AS "e" FROM "x" AS "x"',
)

self.check_file(
"expand_laterals",
optimizer.expand_laterals.expand_laterals,
Expand Down

0 comments on commit 5f06fb0

Please sign in to comment.