Repository navigation
fix(sql): allow constant and grouped-column Custom SQL metrics under GROUP BY - #46648
MikolajBalcerek wants to merge 6 commits into
Conversation
…GROUP BY The non-aggregate Custom SQL metric check from apache#42199 rejected metrics that are valid SQL under a GROUP BY: constants ('WIP', NULL, 1) and expressions whose columns are all grouped. Only raise when the expression references a column that is not itself one of the GROUP BY expressions; calculated columns and time-grain buckets don't count as grouped. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
| dialect = SQLGLOT_DIALECTS.get(engine) | ||
| try: | ||
| parsed = sqlglot.parse_one(f"SELECT {expression}", dialect=dialect) | ||
| grouped = [sqlglot.parse_one(str(sql), dialect=dialect) for sql in group_by] |
There was a problem hiding this comment.
Suggestion: On MySQL, SQLAlchemy may render a reserved grouped column with double quotes, which this dialect reads as a string literal, causing a valid metric to be rejected.
Assessment: 🟠 Major · 🔁 Occurrence: Rarely · 🏷️ Api mismatch
Prompt for AI Agent 🤖
This is a comment left during a code review.
**Path:** superset/sql/parse.py
**Line:** 301:301
**Comment:**
*Api Mismatch: On MySQL, SQLAlchemy may render a reserved grouped column with double quotes, which this dialect reads as a string literal, causing a valid metric to be rejected.
Validate the correctness of the flagged issue. If correct, How can I resolve this? If you propose a fix, implement it and please make it concise.
Once fix is implemented, also check other comments on the same PR, and ask user if the user wants to fix the rest of the comments as well. if said yes, then fetch all the comments validate the correctness and implement a minimal fixThere was a problem hiding this comment.
I am flagging this as Known Issue in PR description.
You could render the GROUP BY columns in the database's own SQL style instead of with a plain str(), i.e. col.compile(dialect=self.database.get_dialect()) to get this-DB-style-comparison, more complex
So getting at least partial fix in
I could try it if we want
To fix all the subcases perhaps just reverting #42199 and trying a different way to fix that issue would be preferable
| return True | ||
| # case-insensitive, erring toward letting the database decide | ||
| names = {col.name.lower() for col in grouped if isinstance(col, exp.Column)} | ||
| return all(col.name.lower() in names for col in parsed.find_all(exp.Column)) |
There was a problem hiding this comment.
Suggestion: This also finds columns inside scalar subqueries, so an independent subquery metric is mistaken for an ungrouped outer reference and rejected.
Assessment: 🟠 Major · 🔁 Occurrence: Rarely · 🏷️ Logic error
Prompt for AI Agent 🤖
This is a comment left during a code review.
**Path:** superset/sql/parse.py
**Line:** 306:306
**Comment:**
*Logic Error: This also finds columns inside scalar subqueries, so an independent subquery metric is mistaken for an ungrouped outer reference and rejected.
Validate the correctness of the flagged issue. If correct, How can I resolve this? If you propose a fix, implement it and please make it concise.
Once fix is implemented, also check other comments on the same PR, and ask user if the user wants to fix the rest of the comments as well. if said yes, then fetch all the comments validate the correctness and implement a minimal fixThere was a problem hiding this comment.
Yea, true.
Telling the two kinds of subquery apart would require sqlglot's scope analysis.
Making the fix more thorough but more complex.
To fix all the subcases perhaps just reverting #42199 and trying a different way to fix that issue would be preferable
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #46648 +/- ##
===========================================
- Coverage 85.98% 65.73% -20.26%
===========================================
Files 3129 3143 +14
Lines 217840 219768 +1928
Branches 51648 51962 +314
===========================================
- Hits 187320 144467 -42853
- Misses 27096 71869 +44773
- Partials 3424 3432 +8
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
Closing in favour of just revert #46650 to avoid unsolved cases of blocked charts when not needed |
SUMMARY
#42199 rejects a Custom SQL metric using a GROUP BY when
has_aggregate()is False.Issue: #46641 with description
That is stricter than SQL and 6.1.0, and breaks charts that worked before, using legitimate SQL.
Example of bug behaviour
This blocks legit SQLs from being charted that worked on 6.1.0, like:
using constants as temporary values
or expressions over grouped columns
Charts like these worked in 6.x and break after upgrading.
There's no setting to turn the check off, and there's no automatic migration for the issue
Screenshots/recordings
Working on Superset 6.1.0
Broken on Superset 7.0 rc3
example query used
Chart types:
The fix
The check now raises only when the expression references a column that isn't itself one of the GROUP BY expressions.
Compares the metric's columns with the GROUP BY expressions that are bare column references.
Plain dataset columns and adhoc SQL columns like strings
WIPcount as grouped.TESTING INSTRUCTIONS
pytest tests/unit_tests/sql/parse_tests.py tests/unit_tests/models/helpers_test.pyADDITIONAL INFORMATION
Follow-up to fix(sql): validate Custom SQL metric has an aggregate under GROUP BY #42199 / deck.gl Screen Grid - column must appear in the GROUP BY clause or be used in an aggregate function #38913
Known gap: on MySQL, BigQuery, Hive and Databricks a grouped column SQLAlchemy quotes (mixed case, reserved word) renders as
"Col", which those sqlglot dialects read as a string, so it would still be rejected thereFixes [Superset 7.0 rc] SQL metric check blocks previously valid queries using groupby #46641