Add GROUP BY alias rendering to ClickHouse SQLAlchemy compiler#655
Merged
Conversation
villebro
approved these changes
Feb 21, 2026
villebro
left a comment
There was a problem hiding this comment.
LGTM, the Superset community will definitely ❤️ this improvement!
Closed
9 tasks
Contributor
Author
|
Tested end to end by:
The chart renders correctly in the UI and the rendered SQL is as follows: SELECT
dateTrunc('DAY', toDateTime(dateTrunc('DAY', dt))) AS "startOfDay",
sum(number) AS "sum(number)"
FROM "default"."test_dates"
GROUP BY
"startOfDay"
ORDER BY
"sum(number)" DESC
LIMIT 10000 |
Contributor
Author
|
Also worth noting that this change is backwards compatible with superset mutation logic. This |
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
ClickHouse resolves
SELECTaliases insideGROUP BYexpressions which causes circular reference errors when an alias shadows a source column name. For examplefails because
timein the expression resolves to the alias, not the column.This PR adds
group_by_clauseandvisit_labeloverrides toChStatementCompilerso that labeled expressions inGROUP BYrender as their alias name instead of the full expression. Note thatSELECTandORDER BYrendering is unaffected.This fixes the root cause and underlying issue behind apache/superset#33551
Consequently, this should eliminate the need for Superset's alias-mangling workaround that appends hash suffixes to all ClickHouse column aliases to avoid these
GROUP BYcollisions.To fully close the loop here I'll need to close apache/superset#34091 and then open another PR that no-ops or removes the current
_mutate_labelbehavior and then require a version of ciikchouse-connect in superset greater than or equal to whatever version we cut this release in.Checklist
Delete items not relevant to your PR: