Repository navigation
Use projection if it will help with sorting - #103723
Merged
Merged
Conversation
Contributor
|
Workflow [PR], commit [8195677] Summary: ❌
AI ReviewSummaryThis PR improves projection selection by allowing equal-cost normal projections when their sort order can help Findings
Tests
ClickHouse Rules
Final Verdict
|
azat
reviewed
Apr 30, 2026
Michicosun
force-pushed
the
optimize_read_in_order_after_projection_use
branch
from
April 30, 2026 07:23
be943b9 to
fbdea02
Compare
Contributor
LLVM Coverage Report
Changed lines: 97.64% (124/127) | lost baseline coverage: 2 line(s) · Uncovered code |
azat
reviewed
May 1, 2026
azat
approved these changes
May 1, 2026
1 task done
alexey-milovidov
added a commit
that referenced
this pull request
May 2, 2026
When `optimize_read_in_order` is the result of a normal projection being chosen to satisfy `ORDER BY` (PR #103723), `requestReadingInOrder` is called on the projection's read step. Projections typically have a single part, so any non-trivial filter that hits all rows in that part triggers the PK-selectivity guard, disabling read-in-order on the projection - which is the entire reason the projection was chosen. With a single selected part there is no inter-part parallelism to lose, so the guard is meaningless there. Add `selected_parts > 1` to the precondition. The 04053 regression test was a single-part insert, which now correctly skips the guard. Switch it to four parts (with `SYSTEM STOP MERGES`) so it actually exercises the multi-part case the optimization is meant for. Test: tests/queries/0_stateless/04109_read_in_order_with_normal_projection.sql Report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=100377&sha=c0ff2228cf01cd7c1cb58cb1ac6ab8f2564d30b1&name_0=PR&name_1=Stateless%20tests%20%28amd_debug%2C%20parallel%29
alexey-milovidov
added a commit
that referenced
this pull request
May 4, 2026
The previous guard `selected_parts > 1` was too broad: it disabled the PK-selectivity check for any single-part scan, including large single-part tables where reading via mark-range parallelism would still beat the ordered single-stream path. The original motivation for the carve-out was the normal-projection case (PR #103723): when a projection is chosen to satisfy `ORDER BY`, calling `requestReadingInOrder` on the projection's read step must still set up in-order reading - otherwise the projection's own ordering is wasted. Projection parts are detected by `AnalysisResult::readFromProjection`, which is exactly the property we need. Switch the precondition to `!readFromProjection` so that: - projection-driven reads keep read-in-order regardless of PK selectivity; - single-part non-projection scans with poor PK selectivity correctly fall back to parallel reading + sorting, matching the multi-part case. Review: #100377 (comment) Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
11 of 28 tasks
ianton-ru
pushed a commit
to Altinity/ClickHouse
that referenced
this pull request
Jul 13, 2026
…_order_after_projection_use Use projection if it will help with sorting
Merged
1 task
alexey-milovidov
pushed a commit
to tuanpach/ClickHouse
that referenced
this pull request
Jul 20, 2026
…ow expects Projection metadata is re-derived from the projection query text at every table load, so the believed column set of a projection can drift from the stored column set of existing projection parts. Two native triggers: parts written by <= 26.2 for a projection selecting an ALIAS column store only the sort key (26.3+ derive the alias source as a physical projection column), and ALTER TABLE ... MODIFY COLUMN re-pointing an ALIAS used by a projection is allowed without a projection rebuild. Since 26.5 the planner also selects normal projections for sorting without a filter (ClickHouse#103723, ClickHouse#104680), so a plain SELECT reads the drifted part through the projection, fills the missing column with the type default, and silently returns wrong data. Background merges that merge projection parts directly read the same default fill and bake the wrong values permanently into the merged projection part. The same shape breaks aggregate projections: the aggregate-state column name embeds the expanded alias (`sum(plus(b, 1))`), so a re-pointed alias leaves old parts without the state column the metadata now expects and reads return default aggregate states. A column is missing legitimately only when it is a parent-table column the parent part also lacks (added by `ALTER ADD COLUMN` after the part was written) - the default fill is then correct and identical on either read path. Everything else counts as drift: `analyzeProjectionCandidate` now reads such parts from the base table (covers normal and aggregate projections; virtual columns are exempt since the reading step provides them), and `prepareProjectionsToMergeAndRebuild` rebuilds the projection from the merged parent data instead of merging drifted projection parts directly (skipped under `deduplicate_merge_projection_mode = 'ignore'`, mirroring the expired-columns rule). Co-Authored-By: Claude Fable 5 <[email protected]>
This was referenced Aug 28, 2026
zeekay
pushed a commit
to hanzoai/datastore
that referenced
this pull request
Sep 11, 2026
When `optimize_read_in_order` is the result of a normal projection being chosen to satisfy `ORDER BY` (PR ClickHouse#103723), `requestReadingInOrder` is called on the projection's read step. Projections typically have a single part, so any non-trivial filter that hits all rows in that part triggers the PK-selectivity guard, disabling read-in-order on the projection - which is the entire reason the projection was chosen. With a single selected part there is no inter-part parallelism to lose, so the guard is meaningless there. Add `selected_parts > 1` to the precondition. The 04053 regression test was a single-part insert, which now correctly skips the guard. Switch it to four parts (with `SYSTEM STOP MERGES`) so it actually exercises the multi-part case the optimization is meant for. Test: tests/queries/0_stateless/04109_read_in_order_with_normal_projection.sql Report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=100377&sha=c0ff2228cf01cd7c1cb58cb1ac6ab8f2564d30b1&name_0=PR&name_1=Stateless%20tests%20%28amd_debug%2C%20parallel%29
zeekay
pushed a commit
to hanzoai/datastore
that referenced
this pull request
Sep 11, 2026
The previous guard `selected_parts > 1` was too broad: it disabled the PK-selectivity check for any single-part scan, including large single-part tables where reading via mark-range parallelism would still beat the ordered single-stream path. The original motivation for the carve-out was the normal-projection case (PR ClickHouse#103723): when a projection is chosen to satisfy `ORDER BY`, calling `requestReadingInOrder` on the projection's read step must still set up in-order reading - otherwise the projection's own ordering is wasted. Projection parts are detected by `AnalysisResult::readFromProjection`, which is exactly the property we need. Switch the precondition to `!readFromProjection` so that: - projection-driven reads keep read-in-order regardless of PK selectivity; - single-part non-projection scans with poor PK selectivity correctly fall back to parallel reading + sorting, matching the multi-part case. Review: ClickHouse#100377 (comment) Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
zeekay
pushed a commit
to hanzoai/datastore
that referenced
this pull request
Sep 11, 2026
…ow expects Projection metadata is re-derived from the projection query text at every table load, so the believed column set of a projection can drift from the stored column set of existing projection parts. Two native triggers: parts written by <= 26.2 for a projection selecting an ALIAS column store only the sort key (26.3+ derive the alias source as a physical projection column), and ALTER TABLE ... MODIFY COLUMN re-pointing an ALIAS used by a projection is allowed without a projection rebuild. Since 26.5 the planner also selects normal projections for sorting without a filter (ClickHouse#103723, ClickHouse#104680), so a plain SELECT reads the drifted part through the projection, fills the missing column with the type default, and silently returns wrong data. Background merges that merge projection parts directly read the same default fill and bake the wrong values permanently into the merged projection part. The same shape breaks aggregate projections: the aggregate-state column name embeds the expanded alias (`sum(plus(b, 1))`), so a re-pointed alias leaves old parts without the state column the metadata now expects and reads return default aggregate states. A column is missing legitimately only when it is a parent-table column the parent part also lacks (added by `ALTER ADD COLUMN` after the part was written) - the default fill is then correct and identical on either read path. Everything else counts as drift: `analyzeProjectionCandidate` now reads such parts from the base table (covers normal and aggregate projections; virtual columns are exempt since the reading step provides them), and `prepareProjectionsToMergeAndRebuild` rebuilds the projection from the merged parent data instead of merging drifted projection parts directly (skipped under `deduplicate_merge_projection_mode = 'ignore'`, mirroring the expired-columns rule). Co-Authored-By: Claude Fable 5 <[email protected]>
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.
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Slightly better query plan for projections in-order scan.
Version info
26.5.1.210