Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
23 changes: 19 additions & 4 deletions sqlglot/dialects/exasol.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,18 +92,33 @@ def _add_local_prefix_for_aliases(expression: exp.Expression) -> exp.Expression:
):
table_ident.replace(exp.to_identifier(table_ident.name.upper(), quoted=True))

def prefix_local(node):
def prefix_local(node, visible_aliases: dict[str, bool]) -> exp.Expression:
if isinstance(node, exp.Column) and not node.table:
if node.name in aliases:
if node.name in visible_aliases:
return exp.Column(
this=exp.to_identifier(node.name, quoted=aliases[node.name]),
this=exp.to_identifier(node.name, quoted=visible_aliases[node.name]),
table=exp.to_identifier("LOCAL", quoted=False),
)
return node

for key in ("where", "group", "having"):
if arg := expression.args.get(key):
expression.set(key, arg.transform(prefix_local))
expression.set(key, arg.transform(lambda node: prefix_local(node, aliases)))

seen_aliases: dict[str, bool] = {}
new_selects: list[exp.Expression] = []
for sel in expression.selects:
if isinstance(sel, exp.Alias):
inner = sel.this.transform(lambda node: prefix_local(node, seen_aliases))
sel.set("this", inner)

alias_node = sel.args.get("alias")

seen_aliases[sel.alias] = bool(alias_node and getattr(alias_node, "quoted", False))
new_selects.append(sel)
else:
new_selects.append(sel.transform(lambda node: prefix_local(node, seen_aliases)))
expression.set("expressions", new_selects)

return expression

Expand Down
14 changes: 13 additions & 1 deletion tests/dialects/test_exasol.py
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,9 @@ def test_local_prefix_for_alias(self):
self.validate_identity(
'SELECT YEAR(a_date) AS "a_year" FROM MY_SUMMARY_TABLE GROUP BY LOCAL."a_year"',
)
self.validate_identity('SELECT a_year AS a_year FROM "LOCAL" GROUP BY "LOCAL".a_year')
self.validate_identity(
'SELECT a_year AS a_year FROM "LOCAL" GROUP BY "LOCAL".a_year',
)

test_cases = [
(
Expand All @@ -705,6 +707,16 @@ def test_local_prefix_for_alias(self):
"SELECT YEAR(a_date) AS a_year, MONTH(a_date) AS a_month FROM my_table WHERE LOCAL.a_year > 2020 AND LOCAL.a_month < 6",
"SELECT YEAR(a_date) AS a_year, MONTH(a_date) AS a_month FROM my_table WHERE a_year > 2020 AND a_month < 6",
),
(
"Select list aliases",
"SELECT YR AS THE_YEAR, ID AS YR, LOCAL.THE_YEAR + 1 AS NEXT_YEAR FROM my_table",
"SELECT YR AS THE_YEAR, ID AS YR, THE_YEAR + 1 AS NEXT_YEAR FROM my_table",
),
(
"Select list aliases without Local keyword",
"SELECT YEAR(CURRENT_DATE) AS current_year, LOCAL.current_year + 1 AS next_year",
"SELECT YEAR(CURRENT_DATE) AS current_year, current_year + 1 AS next_year",
),
]
for title, exasol_sql, dbx_sql in test_cases:
with self.subTest(clause=title):
Expand Down