Skip to content

Commit b19dbd4

Browse files
authored
fix(exasol)!: qualified select list with "LOCAL" (#6450)
* chore(exasol): qualified select list with "LOCAL" * chore(exasol): implementing alias referencing using local keyword for select list * chore(exasol): refactored test and simplifying implementation * chore(exasol): refactored the test and added safe checking when reading alias.node
1 parent 1b6076b commit b19dbd4

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

sqlglot/dialects/exasol.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,18 +92,33 @@ def _add_local_prefix_for_aliases(expression: exp.Expression) -> exp.Expression:
9292
):
9393
table_ident.replace(exp.to_identifier(table_ident.name.upper(), quoted=True))
9494

95-
def prefix_local(node):
95+
def prefix_local(node, visible_aliases: dict[str, bool]) -> exp.Expression:
9696
if isinstance(node, exp.Column) and not node.table:
97-
if node.name in aliases:
97+
if node.name in visible_aliases:
9898
return exp.Column(
99-
this=exp.to_identifier(node.name, quoted=aliases[node.name]),
99+
this=exp.to_identifier(node.name, quoted=visible_aliases[node.name]),
100100
table=exp.to_identifier("LOCAL", quoted=False),
101101
)
102102
return node
103103

104104
for key in ("where", "group", "having"):
105105
if arg := expression.args.get(key):
106-
expression.set(key, arg.transform(prefix_local))
106+
expression.set(key, arg.transform(lambda node: prefix_local(node, aliases)))
107+
108+
seen_aliases: dict[str, bool] = {}
109+
new_selects: list[exp.Expression] = []
110+
for sel in expression.selects:
111+
if isinstance(sel, exp.Alias):
112+
inner = sel.this.transform(lambda node: prefix_local(node, seen_aliases))
113+
sel.set("this", inner)
114+
115+
alias_node = sel.args.get("alias")
116+
117+
seen_aliases[sel.alias] = bool(alias_node and getattr(alias_node, "quoted", False))
118+
new_selects.append(sel)
119+
else:
120+
new_selects.append(sel.transform(lambda node: prefix_local(node, seen_aliases)))
121+
expression.set("expressions", new_selects)
107122

108123
return expression
109124

tests/dialects/test_exasol.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -682,7 +682,9 @@ def test_local_prefix_for_alias(self):
682682
self.validate_identity(
683683
'SELECT YEAR(a_date) AS "a_year" FROM MY_SUMMARY_TABLE GROUP BY LOCAL."a_year"',
684684
)
685-
self.validate_identity('SELECT a_year AS a_year FROM "LOCAL" GROUP BY "LOCAL".a_year')
685+
self.validate_identity(
686+
'SELECT a_year AS a_year FROM "LOCAL" GROUP BY "LOCAL".a_year',
687+
)
686688

687689
test_cases = [
688690
(
@@ -705,6 +707,16 @@ def test_local_prefix_for_alias(self):
705707
"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",
706708
"SELECT YEAR(a_date) AS a_year, MONTH(a_date) AS a_month FROM my_table WHERE a_year > 2020 AND a_month < 6",
707709
),
710+
(
711+
"Select list aliases",
712+
"SELECT YR AS THE_YEAR, ID AS YR, LOCAL.THE_YEAR + 1 AS NEXT_YEAR FROM my_table",
713+
"SELECT YR AS THE_YEAR, ID AS YR, THE_YEAR + 1 AS NEXT_YEAR FROM my_table",
714+
),
715+
(
716+
"Select list aliases without Local keyword",
717+
"SELECT YEAR(CURRENT_DATE) AS current_year, LOCAL.current_year + 1 AS next_year",
718+
"SELECT YEAR(CURRENT_DATE) AS current_year, current_year + 1 AS next_year",
719+
),
708720
]
709721
for title, exasol_sql, dbx_sql in test_cases:
710722
with self.subTest(clause=title):

0 commit comments

Comments
 (0)