Repository navigation
Fix logical error on GROUP BY GROUPING SETS with force_aggregation_in_order - #109862
Conversation
…_order The old-analyzer force_aggregation_in_order branch of InterpreterSelectQuery::executeAggregation called getSortDescriptionFromGroupBy, which invokes getColumnName() on every GROUP BY child. With GROUPING SETS those children are ExpressionList nodes, not columns, so this aborted the server with "Trying to get name of not a column: ExpressionList". The sibling optimize_aggregation_in_order branch already guarded this with !useGroupingSetKey(); add the same guard to the force branch. Closes ClickHouse#97988 Co-Authored-By: Claude Opus 4.8 <[email protected]>
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-worker-slot-2:20260709-041400 |
|
cc @KochetovNicolai @nickitat — could you review this? It adds the missing |
|
Workflow [PR], commit [abd94f4] Summary: ✅
AI ReviewSummaryThis PR fixes two Final Verdict✅ No new findings. |
CI finish ledger — 42e5493Every failure below has an owner: a fixing PR (ours or external), or a full-effort fix task
#109114's merge commit Session id: cron:our-pr-ci-monitor:20260727-170000 |
Picks up ClickHouse#109114 (merge 5e9c1f3, 2026-07-17), which owns the Stress test (amd_debug) 'Block structure mismatch in JoinStep' failure recorded on the previous head's CI ledger, and gets a current-base run.
The GROUPING SETS guard was applied only where the sort descriptions are built, while `AggregatingStep` was still constructed with the ungated `force_aggregation_in_order` expression. For GROUPING SETS that left the step claiming `explicit_sorting_required_for_aggregation_in_order` with both sort descriptions empty, which makes `isSerializable` return false, so `calculateHashTableCacheKeys` omits the step parameters and layouts with identical flattened keys can share preallocation statistics. Derive the eligibility once and use it in both places. The result rows of the existing queries are the same whether or not in-order aggregation runs, so an over-broad guard that also disabled ROLLUP, CUBE and plain GROUP BY passed the test unchanged. Add `EXPLAIN PIPELINE` assertions that require `AggregatingInOrderTransform` for the sibling forms and reject it for GROUPING SETS. Co-Authored-By: Claude Fable 5 <[email protected]>
|
Merged master to pick up #109114, whose fix owns the While re-reviewing I found and fixed two defects in my own change:
Validation on the merged base, debug build
|
…UPING SETS Planner::addAggregationStep passed force_aggregation_in_order to AggregatingStep unguarded, so a GROUPING SETS query kept in-order sort metadata even though AggregatingStep::transformPipeline returns from the grouping-sets branch before any AggregatingInOrderTransform is built. In a distributed query with enable_memory_bound_merging_of_aggregation_results, enableMemoryBoundMerging then treated the local step as in-order-capable and called MergingAggregatedStep::applyOrder, and the initiator raised "Memory bound merging of aggregated results is not supported for grouping sets." from MergingAggregatedStep::transformPipeline. The guard keys on the grouping-sets parameter list alone. Only GROUPING SETS cannot support memory bound merging; ROLLUP and CUBE use it today, so reusing the wider aggregation_with_rollup_or_cube_or_grouping_sets predicate from the sibling distributed_aggregation_memory_efficient argument would remove a working optimization. The extended test pins both directions with EXPLAIN PIPELINE. Reproduced on master before this change and verified after it against a materialized oracle, with the previously measured old-analyzer, ROLLUP, CUBE, WITH TOTALS, plain GROUP BY and distributed_aggregation_memory_efficient routes kept as controls.
…s of 03892 The CI `distributed plan` job installs tests/config/users.d/distributed_plan.xml, which sets serialize_query_plan = 1 for every query. The four distributed control rows of this test use plain GROUP BY / ROLLUP / CUBE / WITH TOTALS, so with force_aggregation_in_order = 1 they legitimately keep an in-order sort description, and AggregatingStep::serialize refuses to serialize such a step: Code: 48. Serialization of AggregatingStep optimized for in-order is not supported. (NOT_IMPLEMENTED) That refusal is pre-existing engine behaviour on master, unrelated to grouping sets; this test is simply the first one driving force_aggregation_in_order = 1 across remote() while the plan is serialized. Measured on a pristine master debug build: the same four statements fail identically there, and serialize_query_plan = 0 makes exactly those four pass. The pin is per statement rather than a file-level SET, because the two GROUPING SETS rows must keep running under the ambient CI settings: they are what proves the fix. Verified they still abort on pristine master with the pin in place elsewhere, so the pin hides no coverage. The EXPLAIN PIPELINE mechanism rows never serialize a plan and need no pin. serialize_query_plan only affects plan serialization across the network and cannot reach the grouping-sets branch this PR fixes, so it is orthogonal to the fix. Co-Authored-By: Claude Fable 5 <[email protected]>
The changelog entry says the old-analyzer half of the fix also covers a scalar or mutation subquery containing GROUPING SETS, but the test only exercised top-level SELECTs, so that half of the contract was asserted and not pinned. Review of the test raised this. The reported query in the linked issue was `ALTER ... UPDATE c0 = (SELECT ... GROUP BY GROUPING SETS ...)` with `mutations_execute_subqueries_on_initiator = 1`. That carrier reaches the failing code through `replaceNonDeterministicToScalars` -> `ExecuteScalarSubqueriesVisitor` -> `InterpreterSelectWithUnionQuery`, which is built directly and therefore keeps the old-analyzer path even when `enable_analyzer = 1`. Both values of that setting are pinned for the mutation rows, since only measuring them shows the carrier is not analyzer-gated. On pristine master all three new rows abort the server with `Trying to get name of not a column: ExpressionList`, and they still do with only the `useGroupingSetKey` clause removed from this branch, so each is load-bearing on its own rather than riding on the top-level rows already in the file. The subquery reads `numbers` rather than the mutated table, so its value does not depend on how many times the mutation ran, and `ORDER BY 1 DESC LIMIT 1` keeps it a single row while still using more than one grouping set. A single grouping set would be silently vacuous: it takes the ordinary aggregation-key path and never reaches the failing code. The two mutations read different ranges so that the second one lands a distinct value: with the same assignment its assertion held even when that mutation was removed entirely.
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 25/25 (100.00%) · Uncovered code |
CI finish ledger - abd94f4CI fully finished on this head: 174 check-runs, all completed (156 success, 18 skipped),
The failures carried on earlier heads are resolved by re-measurement rather than by a
Session id: cron:our-pr-ci-monitor:20260730-033000 |
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed two errors raised when a query with
GROUP BY GROUPING SETS(or a scalar or mutation subquery containing one) runs withforce_aggregation_in_order = 1:Trying to get name of not a column: ExpressionListunder the old analyzer (enable_analyzer = 0), andMemory bound merging of aggregated results is not supported for grouping sets.for a distributed query withenable_memory_bound_merging_of_aggregation_results = 1under the analyzer.Closes: #97988
Description
force_aggregation_in_orderbuilt in-order aggregation state forGROUPING SETSon bothplanning paths, although
AggregatingStep::transformPipelinereturns from its grouping-setsbranch before any
AggregatingInOrderTransformis created. One bug per path follows; both arefixed here:
getSortDescriptionFromGroupBycallsgetColumnNameon everyGROUP BYchild,and with
GROUPING SETSthose children areExpressionListnodes, so it raisesLOGICAL_ERROR(an exception in a release build, a server abort in debug and sanitizerbuilds). The sibling
optimize_aggregation_in_orderbranch already guarded this withuseGroupingSetKey.Planner::addAggregationSteppassed the setting through unguarded, so the stalein-order state reached
AggregatingStep.enableMemoryBoundMergingthen treated the localstep as in-order-capable, called
MergingAggregatedStep::applyOrder, and a distributed querywith
enable_memory_bound_merging_of_aggregation_results = 1failed inMergingAggregatedStep::transformPipelinewithMemory bound merging of aggregated results is not supported for grouping sets.Reported by review of the first half; it predates this PR.On each path the eligibility is derived once and used both where the sort descriptions are built
and where
AggregatingStepis constructed, so the step cannot reportexplicit_sorting_required_for_aggregation_in_orderwith empty sort descriptions.The reported query was a mutation subquery, which the test covers alongside a scalar one. It pins
enable_analyzerat both values there:ExecuteScalarSubqueriesVisitorbuilds anInterpreterSelectWithUnionQuerydirectly, so that carrier keeps the old-analyzer path even withthe analyzer enabled.
The guard keys on
GROUPING SETSalone rather than on any ofROLLUP/CUBE/GROUPING SETS:only
GROUPING SETScannot support memory bound merging, andROLLUP/CUBEuse it today.Because the result rows are identical either way, the test pins the mechanism with
EXPLAIN PIPELINEin both directions, and a mutation using the wider predicate reddens it.