Repository navigation
Conversation
… keys are found `OptimizeTrivialGroupByLimitPass` caps an aggregate-free `GROUP BY ... LIMIT n` at `max_rows_to_group_by = n + offset` with `group_by_overflow_mode = 'any'`, but `AggregatingTransform` kept pulling the whole input after the cap was reached, although every later block was a no-op for the aggregation. So the source, `WHERE` and the key expressions still ran over all the data. - `Aggregator::checkLimits` now returns `false` in `ANY` mode when there are no aggregate functions, like `BREAK`: the rest of the input cannot change the result. - With the new `Aggregator::Params::stop_all_streams_on_overflow_any`, set by the planner for the trivial `GROUP BY ... LIMIT` shape, the first stream over the cap makes all the other streams stop as well, because it alone already holds enough keys for the `LIMIT`. A stream waiting for input (e.g. behind a filter that drops all its rows) sees the flag only when its next chunk arrives. Closes: ClickHouse#8777
|
Workflow [PR], commit [7ae1f3f] Summary: ❌
AI ReviewSummaryPR #126490 stops aggregate-free Final VerdictStatus: ✅ Approve |
…lized plans Two review findings: - The stop fired only when a stream exceeded `max_rows_to_group_by`, so an input with exactly `LIMIT + OFFSET` distinct keys was still read to the end. With the flag, a stream now stops as soon as it has `max_rows_to_group_by` keys. - `AggregatingStep` did not serialize the flag, so under `serialize_query_plan = 1` the remote node only stopped the streams over the cap. It is now the plan setting `aggregation_stop_all_streams_at_max_rows_to_group_by`, which needs the query-plan serialization version 21 (the bump of release 26.11). The flag is renamed to `stop_all_streams_at_max_rows_to_group_by`.
Review comment: ClickHouse#126490 (comment) A shared flag did not stop a sibling `AggregatingTransform` waiting in `NeedData`, e.g. behind a filter that drops all its rows: the executor prepares a processor again only after a port update, so that stream kept reading until the end of its input. Now a `StopAllStreamsTransform` passes each stream into its aggregation. When the aggregation stops reading early, it sends a chunk to a `StopAllStreamsSignal` processor connected to all the streams, which finishes the signal inputs of every `StopAllStreamsTransform`. Each of them then finishes its stream, which wakes the waiting aggregation. The data path stays per stream, so there is no shared processor on it.
…ection pipelines Found by the local codex review: - With external aggregation, a block bringing a stream to exactly `max_rows_to_group_by` keys could be spilled before the stop was checked, emptying the table, so the stream never stopped. The check moves to `Aggregator::checkLimits`, which runs before the spill decision, after the overflow check so that `OverflowAny` is still counted. - `AggregatingProjectionStep` created separate stop groups for the pipelines of the ordinary parts and of the projection parts, although both feed one aggregation. They now share one.
…h keys Found by the local codex review: `StopAllStreamsTransform` finished silently as soon as its input ended, possibly before the aggregation consumed the last chunk. If that chunk brought the aggregation to the cap, the other streams were not told to stop. Now at the end of the input it waits until the consumer either asks for more data (finish silently) or closes the port (stop all the streams).
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes53 object files changed (+135.01 KiB total), 1 added.
716 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Compile time of recompiled translation units200 translation units recompiled, 1387 s compile time in total, 199 of them have a recent master baseline. |
Review comment: ClickHouse#126490 (comment) `AggregatingInOrderTransform` does not check `max_rows_to_group_by`, so with `optimize_aggregation_in_order` an aggregate-free `GROUP BY ... LIMIT` read the whole input. For this shape it now gets `max_rows_to_group_by` as its limit hint without an `ORDER BY` prefix, so it may stop at any group, also inside a run of equal sorting keys, and the streams stop together through `StopAllStreamsTransform`.
…wn_limit The test measures the rows read on the shards to check the push down of `LIMIT`, which `optimize_trivial_group_by_limit_query` now reduces also with aggregation in order.
…ggregates-early-stop
…-aggregates-early-stop # Conflicts: # src/Core/ProtocolDefines.h
…-aggregates-early-stop # Conflicts: # src/Planner/Planner.cpp
…-aggregates-early-stop
…... LIMIT` The table with the projection did not fix `index_granularity`, unlike the other tables of the test. The flaky check randomized it to 12, so the insert of 5M rows wrote about 416k granules and took 256 s under the thread fuzzer, and the test ran longer than 180 s. ClickHouse#126490 Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
📊 Cloud Performance Report
PR verdict: not_sure. 1 measured difference(s) not attributable to this PR. clickbench Q28: The measured difference cannot be attributed to this PR from the available source evidence. I checked the tested merge 968fd2dc against its first parent b79c41c2. In src/Planner/PlannerSteps.cpp, addAggregationStep sets stop_all_streams_at_max_rows_to_group_by only when aggregates_size == 0, group_by_overflow_mode is ANY and getTrivialGroupByLimit applies. Q28 has AVG/COUNT/MIN plus HAVING and ORDER BY, so none of those conditions hold and the flag stays false. In src/Processors/QueryPlan/AggregatingStep.cpp, the StopAllStreams transforms … clickbenchFlagged queries (1 of 43)
Change = percent below ×2; the ratio of medians (×N faster/slower) beyond, where percent understates the scale. q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on. tpch_adapted_1_official🟢 No significant changes Debug info
|
|
@raimannma I think the
|
Thanks, not sure if I understood you right. @rschu1ze |
Closes: #110047
optimize_trivial_group_by_limit_queryalready caps an aggregate-freeGROUP BY ... LIMIT natn + offsetkeys, but the aggregation kept reading its whole input after the cap was reached. NowAggregator::checkLimitsstops a stream once it is over the cap when there are no aggregate functions, and for the trivialGROUP BY ... LIMITshape the first stream over the cap stops all the others, since it alone holds enough keys for theLIMIT. On a 200M-rowMergeTreetable,SELECT k FROM t GROUP BY k LIMIT 10reads about 2M rows instead of 200M (5-70x faster depending on the key andWHERE); queries outside this shape are unchanged.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
A
GROUP BY ... LIMITquery without aggregate functions, such asSELECT a, b FROM t GROUP BY a, b LIMIT 10, now stops reading the table as soon as it has found enough distinct keys.Workflow [PR]
Sync PR [sync-upstream/pr/126490]