Repository navigation
Push LIMIT into aggregation-in-order for early termination - #104859
Conversation
|
Workflow [PR], commit [5564b30] Summary: ✅
AI ReviewSummaryThis PR adds default-on Findings
Final VerdictStatus: Minimum required action: update the stale |
…ns including nulls_direction and collator
4e8d160 to
eb6ba21
Compare
…cks rows_before_limit_at_least
…-down into aggregation-in-order
ExpressionStep with ARRAY JOIN does not preserve row count (its TransformTraits already expose preserves_number_of_rows = !hasArrayJoin), so unconditionally treating it as transparent could push LIMIT past row- multiplying actions and under-read. ExtremesStep preserves row count and order by traits, but it observes the stream before LIMIT is applied; crossing it would feed it a truncated prefix and produce wrong extremes output.
|
Hi @thevar1able Let's merge with master to solve the conflicts and retry CI |
| - [GROUP BY optimization](/sql-reference/statements/select/group-by#group-by-optimization-depending-on-table-sorting-key) | ||
| )", 0) \ | ||
| DECLARE(Bool, optimize_aggregation_in_order_limit, true, R"( | ||
| When enabled and aggregation in order is active, pushes LIMIT into the aggregation step to enable early termination after producing enough groups. This reduces the amount of data read when ORDER BY matches the GROUP BY key prefix. May reduce the value reported by `rows_before_limit_at_least`; use `exact_rows_before_limit` if exact counts are needed. |
There was a problem hiding this comment.
This default-on optimization changes the public rows_before_limit_at_least contract for GROUP BY ... ORDER BY ... LIMIT: once aggregation stops early, the counter can report only the prefix read instead of the full number of pre-limit groups. The setting text notes that, but docs/en/interfaces/formats/Template/Template.md and the matching TemplateRowInputFormat.cpp description still say that when a query contains GROUP BY, rows_before_limit_at_least is exact. Please update those docs in this PR, or keep the optimization default-off until the public format documentation no longer promises an exact count.
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered by tests: 182/183 (99.45%) | Lost baseline coverage: none · Uncovered code |
|
Hi @thevar1able @Ergus — while reviewing this PR I found the following:
Could you review the test PR and add the |
| <query>SELECT CounterID, EventDate, count() FROM hits_100m_single GROUP BY CounterID, EventDate ORDER BY CounterID, EventDate LIMIT 10 FORMAT Null</query> | ||
|
|
||
| <!-- ORDER BY is a prefix of GROUP BY (GROUP BY has extra non-PK column) --> | ||
| <query>SELECT CounterID, EventDate, RegionID, count() FROM hits_100m_single GROUP BY CounterID, EventDate, RegionID ORDER BY CounterID, EventDate LIMIT 10 FORMAT Null</query> |
There was a problem hiding this comment.
@thevar1able the optimization should be applied as well when the order by is a subset of group by? Should a performance test be added for that? if the columns are not in the same order in both, it should also be applied. Can you confirm this?
Add queries for the cases raised in review of ClickHouse#104859: `ORDER BY` as a single-column subset of `GROUP BY`, `GROUP BY` columns permuted relative to the table sorting key, and a permuted `GROUP BY` with an extra non-key column combined with a subset `ORDER BY`. Related: ClickHouse#104859 (comment) Co-Authored-By: Claude Fable 5 <[email protected]>
The initial implementation #78553 belongs to a different optimization (top-K heap pruning during aggregation, #96630, not yet merged), not to the aggregation-in-order LIMIT push-down in #104859. Co-Authored-By: Claude Fable 5 <[email protected]>
…n-order LIMIT push-down Address review feedback from @Ergus on PR ClickHouse#104859: - `04339_aggregation_in_order_limit_filter`: a row-count-reducing filter (both `HAVING` fused into the aggregation and a `WHERE` in an outer subquery, i.e. a real `FilterStep`) between the aggregation and the outer `LIMIT`. The optimization must not push the limit past the filter; the test asserts the result is identical with `optimize_aggregation_in_order_limit` on and off. - `04340_aggregation_in_order_limit_parallel_replicas`: correctness under `enable_parallel_replicas = 1`, comparing results with the setting on and off. Under parallel replicas the cross-replica merge is a `MergingAggregated` over non-final per-replica `Aggregating` steps, so the optimization (which requires a final in-order `AggregatingStep` and refuses to cross a row-reducing `MergingAggregated`) does not fire on the distributed path. The test pins this down. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…rder-limit-pushdown Push LIMIT into aggregation-in-order for early termination
The initial implementation ClickHouse#78553 belongs to a different optimization (top-K heap pruning during aggregation, ClickHouse#96630, not yet merged), not to the aggregation-in-order LIMIT push-down in ClickHouse#104859. Co-Authored-By: Claude Fable 5 <[email protected]>
Add queries for the cases raised in review of ClickHouse#104859: `ORDER BY` as a single-column subset of `GROUP BY`, `GROUP BY` columns permuted relative to the table sorting key, and a permuted `GROUP BY` with an extra non-key column combined with a subset `ORDER BY`. Related: ClickHouse#104859 (comment) Co-Authored-By: Claude Fable 5 <[email protected]>
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Push LIMIT into aggregation-in-order to enable early termination when GROUP BY key matches ORDER BY key and table sorting key, significantly reducing the number of rows read.
Resolves #105196
Version info
26.7.1.267(included in26.7and later)