feat(exasol): auto-alias CTE projections and transpile JSON_OBJECT#7539
Closed
mkcorneli wants to merge 1 commit intotobymao:mainfrom
Closed
feat(exasol): auto-alias CTE projections and transpile JSON_OBJECT#7539mkcorneli wants to merge 1 commit intotobymao:mainfrom
mkcorneli wants to merge 1 commit intotobymao:mainfrom
Conversation
Three orthogonal Exasol output-correctness fixes:
* Auto-inject synthetic column aliases (_col_0, _col_1, ...) for
unaliased non-column projections inside CTE SELECT lists. Exasol
rejects unaliased expressions in CTEs with
"must name expression in query <cte> with a column alias".
Bare column references and stars (including t.*) are left alone so
the rewrite does not produce invalid SQL like `SELECT * AS "_col_0"`.
* Add `jsonobject_sql` to ExasolGenerator so `JSON_OBJECT(k, v, ...)`
is transpiled to a CONCAT expression with per-value-type NULL
handling. String-typed columns use
`CASE WHEN v IS NULL THEN 'null' ELSE CONCAT('"', v, '"') END`;
numeric/date/other types use
`COALESCE(CAST(v AS VARCHAR(100)), 'null')`. Empty arg list emits
the literal `'{}'`. Previously the base generator emitted
`JSON_OBJECT('k': v)` with colon syntax, which Exasol rejects.
* Add test coverage for the existing `_add_local_prefix_for_aliases`
preprocessor when transpiling GROUP BY / HAVING alias references
from other dialects (e.g. `GROUP BY cnt` from MySQL becomes
`GROUP BY LOCAL.cnt` on Exasol).
Collaborator
|
Hey @mkcorneli thanks for the PRs. Could you please split these across three different PRs, given that they're orthogonal? Reviewing will be easier in that way. |
Collaborator
|
Closing this one to review the changes separately. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Three orthogonal Exasol output-correctness fixes bundled together (all touch the Exasol generator):
_col_0,_col_1, ... aliases for them.JSON_OBJECT→ CONCAT with NULL handling — Exasol has no nativeJSON_OBJECT; the base generator was emitting invalid colon syntax. This PR produces a CONCAT expression with per-type NULL handling._add_local_prefix_for_aliasespreprocessor already handles MySQL → Exasol alias references correctly; this PR adds regression test coverage so the behavior is pinned.Before / After
CTE unaliased literals
JSON_OBJECT
GROUP BY alias
Implementation notes
CTE preprocessor skips stars.
_add_cte_column_aliasesleavesexp.Alias,exp.Column,exp.Star, and any projection containing aStar(e.g.t.*) untouched. Wrapping a star in an alias would produce invalid SQL likeSELECT * AS \"_col_0\". Regression tests coverSELECT *,SELECT t.*, and nested CTEs.jsonobject_sqluses expression builders. No f-string SQL construction. Keys get their\"characters escaped before being emitted as string literals. Value branches:CONCAT('\"', value, '\"')is_type(*exp.DataType.TEXT_TYPES), annotate-on-demand) →CASE WHEN v IS NULL THEN 'null' ELSE CONCAT('\"', v, '\"') ENDCOALESCE(CAST(v AS VARCHAR(100)), 'null')'{}'exp.Concat→||chain. Exasol hasCONCAT_COALESCE = True, so the builder renders||chains rather thanCONCAT(...). Functionally equivalent.TRANSFORMS override needed for
exp.JSONObject. The baseGenerator.TRANSFORMSalready mapsexp.JSONObject→_jsonobject_sql, which shadows the auto-discoveredjsonobject_sqlmethod. A one-line TRANSFORMS entry routes through the new method.Test plan
test_cte_literal_auto_aliascovers 8 scenarios (literals, mixed, existing alias, function calls/arithmetic, bare columns, bare*, qualifiedt.*, nested CTE, non-CTE subquery)test_json_objectcovers empty args, string-typed column (CASE WHEN), numeric-typed column (COALESCE/CAST), multi-pair commastest_group_by_alias_localcovers bare alias, expression alias, non-alias column unchanged, HAVING aliastests/dialects/suite runs cleanly (no new failures vs. main)ruff check+ruff format --checkpass