Repository navigation
Fix wrong aggregates from limit pushdown when ORDER BY is a strict prefix of GROUP BY - #117573
Conversation
…efix of GROUP BY `optimize_aggregation_in_order_limit` installed the limit hint whenever the query sort description was a prefix of the full group-by sort description. Each in-order stream terminates early at a boundary in full group-key order, so a group whose rows span multiple parts could be cut off in one stream while another stream still emitted its partial state; since groups tied on the strict prefix are interchangeable to the final sort, it could select a group with an incomplete aggregate value. Require the sort description to match the full group-by sort description instead. Closes: #116849 Co-Authored-By: Claude Fable 5 <[email protected]>
|
Workflow [PR], commit [1a68387] Summary: ✅
AI ReviewSummaryThis PR fixes the wrong-aggregate bug in Findings
Final VerdictChanges requested: the implementation looks correct, but the regression test still does not prove the fix deterministically in CI. LLVM Coverage Report |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units54 translation units recompiled, 473 s compile time in total, 54 of them have a recent master baseline. |
…avior The optimization now requires ORDER BY to match the full GROUP BY sort description, not just a prefix. Update the setting description in src/Core/Settings.cpp (the source of truth for the generated docs) and the settings-history entry accordingly. https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=117573&sha=cbb50fbdc19fe16409b6ac9c122ce0a4b6adf263&name_0=PR #117573 Co-Authored-By: Claude Fable 5 <[email protected]>
Bugfix validation reported that `05055_aggregation_in_order_limit_order_by_prefix` passes on the master binary. The check was wrapped into `SELECT count() FROM (...)`, and the outer query changes the plan: master runs the inner aggregation through the `Top-K` aggregation path with a `Default` read instead of aggregation in order, so the limit push-down never fires and the bug is hidden. Assert the aggregate value in the projection of the query itself, which keeps the `Limit -> Sorting -> Aggregating` in-order plan. Tiny `max_block_size` and `aggregation_in_order_max_block_bytes`, which the test harness randomizes, also hide the bug, so pin them. CI: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=117573&sha=ee9ff177aee08cb1c09614b962c3e4f4843b4f1f&name_0=PR&name_1=Bugfix%20validation%20%28functional%20tests%2C%20amd64%29 PR: #117573
`SettingsChangesHistory` is versioned history: the 26.7 row must keep describing
the prefix semantics that 26.7 and 26.8 actually shipped. Record the tightening
as a `{true, true}` row under 26.9, so that `compatibility` with 26.7 or 26.8
does not restore the incorrect strict-prefix push-down. This follows the
precedent of `apply_row_policy_after_final` (25.10) and
`statistics_max_set_size_for_exact_selectivity_estimation` (26.6-26.9).
PR: #117573
…BY` prefix boundary Instead of rejecting the `LIMIT` push-down when the `ORDER BY` is a strict prefix of the `GROUP BY` sort description, make the early termination correct for that case. `AggregatingInOrderTransform` used to stop as soon as it had emitted `limit_hint` groups, at a boundary of the full group key. A group that ties with the emitted groups on the `ORDER BY` prefix could then be cut off in one stream while another stream emitted its partial state, and the final sort could legally pick that partial group (issue #116849). Now a stream stops only when the next key differs from the current one in the first `limit_prefix_columns` group-by columns, i.e. the columns the query is ordered by. Every stream then contains all rows whose prefix is at most its cutoff; all groups whose prefix is at most the smallest cutoff are complete, and there are at least `limit` of them, so the groups a stream drops always sort strictly after enough complete groups. The optimization pass passes the length of the `ORDER BY` prefix along with the limit hint. The settings documentation, the settings history and `04489` are restored to the original prefix semantics. The regression test asserts the aggregate values in the projection, since wrapping the query into a subquery changes the plan and hides the bug, and also checks that the push-down still short-circuits reads when the prefix has enough distinct values. Closes: #116849 PR: #117573
… `group_by_key` path When the input is sorted only by a prefix of the `GROUP BY` key, `AggregatingInOrderTransform` keeps the groups of one run of the sorted columns in a hash table, and `cur_block_size` counts those runs rather than groups. The early-stop check therefore compared the number of runs with `limit_hint` and kept reading until `limit_hint` runs had passed, although a single run may already hold more than `limit_hint` complete groups. Use the size of the hash table as the number of groups accumulated for the current block on that path. Add a test with a table sorted by `a` only and `GROUP BY a, b ORDER BY a LIMIT 5`: the stream now stops after the first run of `a` and reads 32 rows instead of 128 of the 2000-row table.
`clickhouse-test` randomizes `max_threads` and `read_in_order_two_level_merge_threshold`. With `max_threads = 1` and a threshold that does not exceed the number of parts, reading in order merges the parts into a single stream before the aggregation, so there is only one in-order stream and the bug from issue #116849 cannot occur. Found by running the test against a build without the fix under randomized settings: 1 of 15 runs passed, and delta-debugging the session settings of that run left exactly these two. Pin `max_threads = 2` in the correctness queries and `read_in_order_two_level_merge_threshold` in the `read_rows` queries, whose expected counts assume one stream per part.
…en ORDER BY is a strict prefix of GROUP BY
Backport #117573 to 26.9: Fix wrong aggregates from limit pushdown when ORDER BY is a strict prefix of GROUP BY
Backport #117573 to 26.8: Fix wrong aggregates from limit pushdown when ORDER BY is a strict prefix of GROUP BY
…ushdown when ORDER BY is a strict prefix of GROUP BY
Since 26.7,
optimize_aggregation_in_order_limitpushes the queryLIMITinto in-order aggregation when theORDER BYis a prefix of theGROUP BYsort description. EachAggregatingInOrderTransformstream stopped as soon as it had emittedLIMITgroups, at a boundary of the full group key. When theORDER BYis a strict prefix, a group that ties with the emitted ones on that prefix could be cut off in one stream while another stream still emitted its partial state, and the final sort could legally pick the partial group, returning a wrong aggregate value.A stream now stops only at a boundary of the
ORDER BYprefix: onceLIMITgroups are emitted, it keeps consuming until the prefix columns change. Every stream then contains all rows whose prefix is at most its cutoff, all groups up to the smallest cutoff are complete, and there are at leastLIMITof them, so the dropped groups always sort strictly after enough complete groups. The optimization keeps working for a strict prefix. A stream may now emit more thanLIMITgroups before it stops (everything up to the prefix boundary); the extra groups are discarded by the finalLIMIT. When the prefix has few distinct values the stream reads to the end, which is the only correct option there. When theORDER BYcovers the wholeGROUP BYkey the behavior is unchanged.Closes: #116849
Caused by: #104859
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes to CHANGELOG.md):
Fix wrong aggregate values with
optimize_aggregation_in_order_limitwhen theORDER BYis a strict prefix of theGROUP BYkey and a group's rows span multiple parts. In-order aggregation streams now stop early only at a boundary of theORDER BYprefix. Closes #116849.Workflow [PR]
Sync PR [sync-upstream/pr/117573]
Version info
26.10.1.279-master(included in26.10and later)26.9.1.1620,26.8.10.5