Skip to content

Limit pushdown into UNION ALL - #100364

Merged
alexey-milovidov merged 9 commits into
masterfrom
limit-pushdown-union-all
Mar 29, 2026
Merged

alexey-milovidov merged 9 commits into
masterfrom
limit-pushdown-union-all

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Mar 22, 2026 •

Copy link
Copy Markdown
Member

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

Changelog category (leave one):

  • Performance Improvement

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

  • Merged into: 26.4.1.388

alexey-milovidov and others added 2 commits March 22, 2026 12:55
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]>
@clickhouse-gh

clickhouse-gh Bot commented Mar 22, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [47949c1]

Summary: ✅


AI Review

Summary

This PR adds LIMIT pushdown for UNION ALL by inserting branch-local LimitStep nodes with limit + offset, while preserving the outer LimitStep. The implementation includes a guard to skip pushdown when always_read_till_end is required and adds stateless coverage for key semantics (OFFSET, WITH TIES, WITH FILL, exact_rows_before_limit, and runtime checks). I did not find additional correctness, safety, or performance issues beyond already-addressed bot feedback.

Missing context
  • ⚠️ Full CI results/logs were not available yet (PR checks are still pending), so this review is based on code + tests in the PR diff.
ClickHouse Rules
Item Status Notes
Deletion logging ➖
Serialization versioning ➖
Core-area scrutiny ✅
No test removal ✅
Experimental gate ➖
No magic constants ✅
Backward compatibility ✅
SettingsChangesHistory.cpp ➖
PR metadata quality ✅
Safe rollout ✅
Compilation time ✅
Final Verdict
  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label Mar 22, 2026
alexey-milovidov and others added 6 commits March 25, 2026 22:20
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]>
@clickhouse-gh

clickhouse-gh Bot commented Mar 29, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.20% 84.20% +0.00%
Functions 24.60% 24.50% -0.10%
Branches 76.70% 76.80% +0.10%

Changed lines: 97.56% (40/41) · Uncovered code

Full report · Diff report

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, now ready.

@alexey-milovidov alexey-milovidov self-assigned this Mar 29, 2026
@alexey-milovidov
alexey-milovidov merged commit ec2c183 into master Mar 29, 2026
149 checks passed
@alexey-milovidov
alexey-milovidov deleted the limit-pushdown-union-all branch March 29, 2026 16:00
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Mar 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-performance Pull request with some performance improvements pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

LIMIT pushdown to UNION ALL queries

2 participants