Skip to content

Conversation

@nnamdi16
Copy link
Contributor

@nnamdi16 nnamdi16 commented Dec 2, 2025

What motivated this PR?

Exasol requires the LOCAL qualifier when referencing column aliases in GROUP BY, HAVING, WHERE, and the SELECT list.
The previous implementation only handled GROUP BY, WHERE, and HAVING, but did not apply LOCAL to alias references inside the SELECT list.
This PR adds the missing behavior in accordance with Exasol’s documented rules:
https://docs.exasol.com/db/latest/sql/select.htm


What was incorrect in the existing logic?

Given a query such as:

SELECT YEAR(current_date) AS current_year,
       current_year + 1 AS next_year

The transpiler did not rewrite current_year to LOCAL.current_year inside the SELECT list.
Since Exasol requires LOCAL.alias for any alias referenced within the same SELECT clause, the generated SQL was invalid.


How does this PR resolve the issue?

The PR extends the existing alias-handling transformation so that:

  • Alias references inside the SELECT list are detected, and
  • They are rewritten as LOCAL.alias, consistent with Exasol requirements.

This makes alias resolution behavior complete and aligned across SELECT, GROUP BY, HAVING, and WHERE.


Documentation & Semantic Notes

Exasol’s rules for alias qualification are documented here:
https://docs.exasol.com/db/latest/sql/select.htm

The semantics are fully preserved — the PR does not change query meaning.
It only adds the required LOCAL prefix in the SELECT list so that transpiled queries execute correctly on Exasol.


@nnamdi16 nnamdi16 closed this Dec 2, 2025
@nnamdi16 nnamdi16 reopened this Dec 4, 2025
Comment on lines 685 to 688
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'
)

Comment on lines 689 to 692
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

Comment on lines 115 to 117
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"))

@georgesittas georgesittas changed the title chore(exasol): qualified select list with "LOCAL" fix(exasol)!: qualified select list with "LOCAL" Dec 8, 2025
@georgesittas georgesittas merged commit b19dbd4 into tobymao:main Dec 8, 2025
8 checks passed
georgesittas pushed a commit that referenced this pull request Dec 8, 2025
* 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants