Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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")
if alias_node and alias_node.name:
seen_aliases[alias_node.name] = bool(alias_node.args.get("quoted"))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Afaict this can be simplified to:

Suggested change
alias_node = sel.args.get("alias")
if alias_node and alias_node.name:
seen_aliases[alias_node.name] = bool(alias_node.args.get("quoted"))
seen_aliases[sel.alias] = bool(alias_node.args.get("quoted"))

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,14 @@ 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',
'SELECT a_year AS a_year FROM "LOCAL" GROUP BY "LOCAL".a_year',
)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The output SQL is identical to input, we don't need to redefine it

Suggested change
self.validate_identity(
'SELECT a_year AS a_year FROM "LOCAL" GROUP BY "LOCAL".a_year',
'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'
)

self.validate_identity(
"SELECT YEAR(current_date) AS current_year, current_year + 1 AS next_year",
"SELECT YEAR(CURRENT_DATE) AS current_year, LOCAL.current_year + 1 AS next_year",
)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the input query work on Exasol? If not, we shouldn't be adding validate_identity that the input SQL does not work on that engine, those should be done through validate_all as you have done for DBX


test_cases = [
(
Expand All @@ -705,6 +712,11 @@ 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",
),
]
for title, exasol_sql, dbx_sql in test_cases:
with self.subTest(clause=title):
Expand Down