Repository navigation
Limit pushdown into UNION ALL - #100364
Merged
Merged
Conversation
When a `LIMIT` is applied to a `UNION ALL` query, each branch only needs to produce at most `limit + offset` rows. This optimization adds a `LimitStep` to each branch of the `UnionStep`, significantly reducing the number of rows processed. The outer `LimitStep` is preserved to enforce the final limit across all branches. Closes #23239 Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Verify that LIMIT is not pushed down into UNION ALL branches when WITH TIES or WITH FILL is used, as intermediate steps (Sorting, Filling) sit between Limit and Union preventing the optimization. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Contributor
|
Workflow [PR], commit [47949c1] Summary: ✅ AI ReviewSummaryThis PR adds Missing context
ClickHouse Rules
Final Verdict
|
When `exact_rows_before_limit` is enabled or the query uses `WITH TOTALS`, the outer `LimitStep` has `always_read_till_end = true`. Pushing per-branch limits into UNION ALL in this case would terminate sources early and produce incorrect `rows_before_limit_at_least` statistics. Add `alwaysReadTillEnd` getter to `LimitStep` and skip the pushdown when the flag is set. Add a regression test for the `exact_rows_before_limit` case. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
…with `UNION ALL` + `LIMIT` Address review feedback: add an EXPLAIN test verifying that `WITH TOTALS` (via `TotalsHavingStep`) prevents LIMIT pushdown into UNION ALL branches, a runtime correctness test checking `rows_before_limit_at_least` is correct when `exact_rows_before_limit = 1`, and a three-branch runtime correctness test. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Contributor
LLVM Coverage Report
Changed lines: 97.56% (40/41) · Uncovered code |
alexey-milovidov
left a comment
Member
Author
There was a problem hiding this comment.
Ok, now ready.
1 task
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When a
LIMITis applied to aUNION ALLquery, each branch only needs to produce at mostlimit + offsetrows. This optimization adds aLimitStepto each branch of theUnionStep, significantly reducing the number of rows processed.The outer
LimitStepis preserved to enforce the final limit across allbranches.
Closes #23239
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Optimize queries by pushing the LIMIT clause down into the UNION ALL.
Version info
26.4.1.388