Repository navigation
Fix NOT_IMPLEMENTED for Merge over Distributed with a JOIN and a GROUP BY modifier - #118658
PedroTadim merged 3 commits into
Conversation
Reading a Merge table that matches a Distributed table with a JOIN and a GROUP BY ... WITH TOTALS/ROLLUP/CUBE/GROUPING SETS failed outright: Code: 48. DB::Exception: WITH TOTALS, ROLLUP, CUBE or GROUPING SETS are not supported without aggregation: Child table: default.d. (NOT_IMPLEMENTED) replaceTableExpressionAndRemoveJoin builds a separate query for each matched child table. It clears the GROUP BY, ORDER BY and LIMIT BY node lists, because they may reference the JOIN it removes, and it replaces the projection with plain column identifiers, which drops the aggregate functions. It did not reset the modifier flags belonging to those clauses, so the child query was left with no GROUP BY keys and no aggregates while still carrying WITH TOTALS. When the child is a Distributed table that tree is re-analyzed and validateAggregates rejects it. The child query the rewrite produced is visible in the server log as: SELECT `__table1`.`a` AS `a` FROM `t_99508` AS `__table1` WITH TOTALS The old-analyzer twin of this rewrite, removeJoin in getHeaderForProcessingStage.cpp, already resets all seven of these flags, which is why enable_analyzer = 0 was unaffected. This mirrors it, in the same spirit as the is_limit_with_ties reset the function already performs for the same reason: a clause modifier flag must not outlive the clause it modifies. WITH TOTALS, WITH ROLLUP, WITH CUBE, and GROUPING SETS with more than one grouping set are the independently observable cases, reachable through JOIN, ARRAY JOIN, GLOBAL JOIN and the merge() table function. A single-set GROUPING SETS list is collapsed during resolution by GroupingFunctionsResolvePass, which clears the flag with it, so only a multi-set list survives to reach this rewrite. is_group_by_all, is_order_by_all and is_limit_by_all are cleared during resolution, so they cannot be witnessed on their own today; they are reset as well so that the two rewrites agree, and because a surviving order_by_all with no ORDER BY list later dereferences a null orderBy() in ASTSelectQuery. The AST side of that pair was fixed in ClickHouse#109139 and ClickHouse#113759. Reported by Nihal Z. Miaji on ClickHouse#99508 as a pre-existing master bug. Co-Authored-By: Claude Opus 5 <[email protected]>
Internal second-model review (3 rounds): 7 agreed and fixed, 3 declined, 1 notedAn independent model reviewed this change against the resulting code, then a second pass re-reviewed it ❌ Blockers: none.
💡 Declined, with reasons:
💡 Noted, not fixed: the test's header comment says the rejection happens on the initiator while Nothing was deferred. |
|
Workflow [PR], commit [6b3c797] Summary: ✅
AI ReviewSummaryThe PR clears stale clause modifiers when Missing context / blind spots
Final VerdictStatus: ✅ Approve. No code changes requested; current-head CI remains pending. |
|
cc @vdimir @azat, could you review this? |
CI finish ledger - 3cf3e46Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task whose fixing-PR link will be posted here when it opens. Only
Session id: cron:our-pr-ci-monitor:20260907-233055 |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units7 translation units recompiled, 23 s compile time in total, 7 of them have a recent master baseline. |
|
cc @nihalzp |
CI finish ledger - abf782aEvery failure below has an owner: a fixing PR (mine or external), or a full-effort fix task whose
Everything else at this head is green: 153 pass, 18 skipped, 0 other red. Not PR-caused, measured rather than assumed: this diff is Read from the per-attempt GitHub Actions job log rather than the CIDB Session id: cron:our-pr-ci-monitor:20260908-090153 |
|
@groeneai is this PR still needed with all the switches it toggles? |
|
@groeneai was it ever supported in any version? If not it's ok too keep it not implemented |
|
@PedroTadim @vdimir It did work before the analyzer, and it is still broken on master. Same query (
So it is a regression of the analyzer switch, and since 26.9 there is no workaround. On the switches: the bug needs only the four GROUP BY modifier resets ( |
|
@vdimir this seems to be a long time regression |
Related: #99508
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed a
NOT_IMPLEMENTEDerror ("WITH TOTALS, ROLLUP, CUBE or GROUPING SETS are not supported without aggregation") when a query with a JOIN or ARRAY JOIN and aGROUP BY ... WITH TOTALS/ROLLUP/CUBE/GROUPING SETSread aMergetable that matches aDistributedtable. The rewritten child query kept the GROUP BY modifier after itsGROUP BYclause was removed.Description
Requested by @ nihalzp in #99508 (comment) as a pre-existing master bug, out of scope of that PR.
A valid query is rejected. With
mdaMergetable matching aDistributedtable:Root cause:
replaceTableExpressionAndRemoveJoinbuilds a per-child query and clears theGROUP BYandORDER BYnode lists (they may reference the removed JOIN), but never resets the modifier flags, and it also replaces the projection, dropping the aggregate functions. The child query fordis therefore aggregation-free while still carryingWITH TOTALS, and aDistributedchild re-analyses that tree, wherevalidateAggregatesrejects it.The old-analyzer twin,
removeJoiningetHeaderForProcessingStage.cpp, already resets these flags, soenable_analyzer = 0is unaffected. This mirrors it, alongside theis_limit_with_tiesreset the same function already performs.Verified witnesses:
WITH TOTALS,WITH ROLLUP,WITH CUBE, andGROUPING SETSwith more than one grouping set, reached through JOIN,ARRAY JOIN,GLOBAL JOINand themerge()table function. A single-setGROUPING SETSlist is collapsed during resolution, which clears its flag too.is_group_by_all,is_order_by_allandis_limit_by_allare also cleared during resolution, so they are not independently observable today; I reset them so the two rewrites agree, matching the AST-side fixes in #109139 and #113759.Present in 26.3, 26.6 and 26.8.
New test
05136_merge_over_distributed_join_group_by_modifiers: five witnesses plus three controls (aMergeover the plain table as oracle,enable_analyzer = 0, and no JOIN). It fails on master and passes with this change.Workflow [PR]
Sync PR [sync-upstream/pr/118658]
Version info
26.11.1.219-master(included in26.11and later)