Repository navigation
Make ReadFromMergeTree::applyFilters rebuild an index analysis made before filter pushdown - #120011
Conversation
|
Workflow [PR], commit [1eeb87a] Summary: ✅
AI ReviewSummaryThis PR fixes the stale Findings
Final VerdictLLVM Coverage ReportMeasured on commit 1eeb87a.
Changed lines: Changed C/C++ lines covered: 13/15 (86.67%) · Uncovered code |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units119 translation units recompiled, 1424 s compile time in total, 119 of them have a recent master baseline. |
…n of `estimate-keep-index-analysis` `joinOrder.h` on `ijoin-add-block-to-join-worker-id` already declares `estimateReadRowsCount` with the `keep_index_analysis` default argument in `QueryPlanOptimizations`, so the declaration `uhj` carried from an older version of #120011 redefined the default arguments and broke every translation unit that includes the header. The file is now identical to the parent branch. Co-Authored-By: Claude Fable 5.1 <[email protected]>
47abb80 to
d382e68
Compare
84ca22f to
e6d5aed
Compare
461e3d4 to
6ec533b
Compare
2cd3284 to
3adc7e0
Compare
1b1b578 to
725da1e
Compare
65e42fc to
5952587
Compare
725da1e to
04146ad
Compare
There was a problem hiding this comment.
Note
Copilot was unable to run its full agentic suite in this review.
Copilot review overview
Review effort: Lite
Findings: 1
Open (2)
What changed in this PR
This PR prevents stale MergeTree index analysis (computed before filter pushdown) from being reused after filters are applied, ensuring range pruning matches the final pushed-down filters.
Changes:
- Track whether
ReadFromMergeTree::indexeswas built byapplyFiltersvia a new boolean flag. - Invalidate precomputed
indexesand associated analysis whenapplyFiltersruns and detects the indexes were created before filter pushdown. - Preserve the tracking flag when adopting filters from another
ReadFromMergeTree.
| File | Description |
|---|---|
| src/Processors/QueryPlan/ReadFromMergeTree.h | Adds state to track the origin of computed index analysis and propagates it in adoptFiltersFrom. |
| src/Processors/QueryPlan/ReadFromMergeTree.cpp | Drops stale index analysis during applyFilters and marks newly built indexes as originating from filter pushdown. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| /// filters. Drop them and the range analysis made with them; `indexes` are rebuilt below. | ||
| if (indexes && !indexes_built_by_apply_filters) | ||
| { | ||
| indexes.reset(); |
| /// Pre-computed value, needed to trigger sets creating for PK | ||
| mutable std::optional<Indexes> indexes; | ||
| /// True if `applyFilters` built `indexes` from the filters pushed down to this step. | ||
| bool indexes_built_by_apply_filters = false; |
estimateReadRowsCount keep an index analysis taken before filter pushdown off the read stepReadFromMergeTree::applyFilters rebuild an index analysis made before filter pushdown
|
|
||
| /// If `indexes` were built before the filters were pushed down to this step, they do not use those | ||
| /// filters. Drop them and the range analysis made with them; `indexes` are rebuilt below. | ||
| if (indexes && !indexes_built_by_apply_filters) |
There was a problem hiding this comment.
This is the right invalidation point, but the PR still needs a focused regression test for the scenario it claims to fix. The bug here is subtle: a pre-pushdown estimateReadRowsCount builds empty-filter indexes, execution later keeps them, and max_rows_to_read = 11 fails even though the pushed-down filter should prune to 10 rows. Nothing in this diff pins that contract, so a future refactor can silently reintroduce the same stale-analysis reuse. Please add a small 0_stateless test that drives the chooseJoinAlgorithm / estimateReadRowsCount path from the PR description and asserts the query stays under the row limit.
22221e6 to
7d093b9
Compare
…r pushdown off the read step `estimateReadRowsCount` runs index analysis on a `ReadFromMergeTree` step and memoizes it in the step's `indexes`. `ReadFromMergeTree::applyFilters` builds the indexes only when they do not exist yet, so an estimate taken before the filters reach the step leaves a `KeyCondition` built from an empty filter on it, and the executed read prunes nothing. Every caller on master estimates after `optimizePrimaryKeyConditionAndLimit`, so none of them hits this. A caller that estimates before the plan is optimized passes `keep_index_analysis = false`, and the analysis then goes into a local `Indexes` object. `selectRangesToReadForEstimation` takes the same flag. An analysis that already exists is reused either way, and the default path does exactly what it did before. Co-Authored-By: Claude Fable 5.1 <[email protected]>
Comment-only change from the retro `reduce-complexity`, `humanize` and `plain-prose` pass over this pull request. The `reduce-complexity` pass found nothing to apply. Every sentence in the edited comments now stays within 25 words. The `selectRangesToReadForEstimation` doc keeps the link between `analyzed_result_ptr` not being set and the re-analysis at execution. The body comment in `selectRangesToReadForEstimation` names `keep_index_analysis` instead of "either way". The blocks wrap at the width of the neighbouring comments. No statement, signature or default argument changes. Co-Authored-By: Claude Fable 5.1 <[email protected]>
… before filter pushdown `applyFilters` used to keep any `indexes` it found on the step. Pull request #115605 adds an estimate that runs before filter pushdown, in `chooseJoinAlgorithm` of the old analyzer. That estimate builds `indexes` without any filter, so the executed read would prune nothing. Now `applyFilters` records that it built `indexes`. If it finds `indexes` that it did not build, it drops them, together with the range analysis made with them. It then builds `indexes` from the pushed-down filters, and the read analyzes its ranges again. `estimateReadRowsCount` and `ReadFromMergeTree::selectRangesToReadForEstimation` no longer take the `keep_index_analysis` flag. It is not needed any more. On master, `applyFilters` never finds `indexes` that it did not build, so master behaves the same. Related: #115605 Co-Authored-By: Claude Opus 5.5 <[email protected]>
The comment above the check states the rule and no longer guesses where the stale `indexes` come from. The comment on `indexes_built_by_apply_filters` no longer repeats what `adoptFiltersFrom` does. Co-Authored-By: Claude Opus 5.5 <[email protected]>
7d093b9 to
1eeb87a
Compare
nickitat
left a comment
There was a problem hiding this comment.
I think, the current master behavior should be preserved - index analysis might be very costly, we cannot repeat it multiple times. Also, I'm not sure why would we want to use it instead of the current approach that looks at the cached build side size?
The rebuild only served the estimate of the right table size in the old analyzer's `chooseJoinAlgorithm`. #115605 no longer makes that estimate. No other caller leaves `indexes` for `applyFilters` to find, so `ReadFromMergeTree` goes back to the code on master. Related: #120011 Co-Authored-By: Claude Opus 5.5 <[email protected]>
…nAlgorithm` The estimate ran index analysis before the filters were pushed down to the read. Index analysis can be expensive, and the read had to run it again to use the pushed-down filters. The new analyzer takes the right table size from the join-order pass and from the hash table statistics cache, so it does not have this cost. User `SELECT` queries no longer reach the old analyzer. It still runs joins inside `IN` subqueries of `CHECK` constraints during `INSERT`. Without an estimate, `preferParallelHashLayout` picks the two-level layout whenever the join kind supports it. On master, the old analyzer always uses `ConcurrentHashJoin` when `parallel_hash` is allowed. Review: #120011 (review) Related: #120011 Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
@harikrishnan94 why did we merge it in the end? |
|
We didn't. |
I see, thanks for explanation |


Related: #115605
estimateReadRowsCountruns index analysis on aReadFromMergeTreestep and stores it on that step. Every caller on master runs after the filters are pushed down, so the storedKeyConditionmatches the read.ReadFromMergeTree::applyFilterskeeps that object when it is already set (if (!indexes)).#115605 uses
estimateReadRowsCountto compare the right table's size withparallel_hash_join_thresholdand select the serial or parallel hash layout. In the legacy analyzer'schooseJoinAlgorithm, this runs beforejoined_planis optimized. That path is still used for joins insideINsubqueries inCHECKconstraints duringINSERT.Without this change, the estimate can leave
indexesthat omit filters pushed down later.ReadFromMergeTree::applyFilterskeeps those existingindexes, so the executed read loses pruning from those filters. This can cause unnecessary reads and amax_rows_to_readexception even when the filtered read would fit the limit. This change lets that estimate run before filter pushdown without disabling pruning when the read executes.ReadFromMergeTree::applyFiltersrecords whether it builtindexes. If it did not, they were built before the filters were pushed down. It drops them and the range analysis made with them, then builds them from the filters. The step keeps an analysis it gets through its constructor. On master the new branch never runs.No caller on master leaves
indexesforapplyFiltersto find. The only one is the estimate that #115605 adds tochooseJoinAlgorithminsrc/Interpreters/ExpressionAnalyzer.cpp. UserSELECTqueries no longer reach that path.Changelog category (leave one):
Workflow [PR]
Sync PR [sync-upstream/pr/120011]
Version info
26.10.1.1-master(included in26.10and later)