Repository navigation
Fix ReadInOrder for merge table with filter push-down to prewhere - #105867
Merged
Merged
Conversation
When a query through the `merge` table function has a WHERE clause that can be pushed down to the prewhere of the underlying `MergeTree`, the read-in-order optimization was silently disabled. The plan ended up sorting rows even though the underlying parts were already sorted by the requested prefix of the primary key. Two pieces together broke the sort-key match: 1. `buildSortingDAG` collected expression and prewhere chains only above `ReadFromMerge`. After filter push-down, the column-renaming and `materialize` aliasing performed by `convertAndFilterSourceStream` was hidden inside the merge node, so the outer plan no longer contained the alias chain from `__table1.X` back to the storage's sort-key column. 2. `matchTrees` treated `materialize`/`identity` as monotonic functions with `is_strict = false`. Even when the chain was visible, that non-strict match truncated the matched sort-key prefix to a single column. Fix: - Walk into each child plan of `ReadFromMerge` while building the per-table DAG. The outer DAG is cloned and merged on top of the inner DAG; `outer_fixed_columns` pointers are remapped through the clone mapping and the `mergeInplace` input map. - Teach `matchTrees` to look through single-argument `materialize` / `identity` calls and pass the matched child through, like `ALIAS`. Closes: #86313 Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Contributor
|
Workflow [PR], commit [b4d7e56] Summary: ❌
AI ReviewSummaryThis PR fixes Final VerdictStatus: ✅ Approve |
Contributor
LLVM Coverage Report
Changed lines: 77.53% (69/89) | lost baseline coverage: 1 line(s) · Uncovered code |
Avogar
approved these changes
May 27, 2026
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.
Claude
When a query through the
mergetable function has a WHERE clause that can be pushed down to the prewhere of the underlyingMergeTree, the read-in-order optimization was silently disabled. The plan ended up sorting rows even though the underlying parts were already sorted by the requested prefix of the primary key.Two pieces together broke the sort-key match:
buildSortingDAGcollected expression and prewhere chains only aboveReadFromMerge. After filter push-down, the column-renaming andmaterializealiasing performed byconvertAndFilterSourceStreamwas hidden inside the merge node, so the outer plan no longer contained the alias chain from__table1.Xback to the storage's sort-key column.matchTreestreatedmaterialize/identityas monotonic functions withis_strict = false. Even when the chain was visible, that non-strict match truncated the matched sort-key prefix to a single column.Fix:
ReadFromMergewhile building the per-table DAG. The outer DAG is cloned and merged on top of the inner DAG;outer_fixed_columnspointers are remapped through the clone mapping and themergeInplaceinput map.matchTreesto look through single-argumentmaterialize/identitycalls and pass the matched child through, likeALIAS.Closes: #86313
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix read-in-order for
Mergetables (with a new analyzer).Version info
26.6.1.190