Repository navigation
Cherry pick #102921 to 26.3: Disable trivial LIMIT optimization with row policies/additional_table_filters - #104042
Merged
robot-ch-test-poll4 merged 12 commits intoMay 5, 2026
Conversation
…_filters This caused single-threaded index analysis when row policies were active, even though the row policy filter needs to scan many parts.
Split `buildAdditionalFiltersIfNeeded` into a pure `parseAdditionalFilterAstIfNeeded` (parse + match + assign `additional_filter_ast`) and a side-effectful builder (actions DAG + prewhere column restoration + PreparedSets registration). The parse is now invoked once, early — right after `table_expression_query_info` is prepared — so that `additional_filter_ast` is populated before both the trivial-count and trivial-limit decisions. Previously both checks read a field that was only assigned later inside `buildAdditionalFiltersIfNeeded`, making the per-table `additional_filter_ast` checks effectively dead. As a consequence: - The trivial-limit optimization now correctly treats a per-table `additional_table_filters` match the same way it treats a matching row policy (introduced in 7479c7c). - The pessimistic outer guard that disabled trivial-count whenever *any* `additional_table_filters` entry was present is dropped — the per-table check inside `applyTrivialCountIfPossible` is now live and precise, so the optimization fires again when the filter targets a different table. The existing two call sites of the (now trimmed) `buildAdditionalFiltersIfNeeded` keep their ordering and semantics; they simply read the already-parsed AST. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Repro: 02346_additional_filters_distr
`select * from dist_02346 order by x
settings additional_table_filters={'dist_02346' : 'x > 3 and x < 7'}`
Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Mirror the check in buildRowPolicyFilterIfNeeded: a row policy that evaluates to always-true adds no predicate, so it should not prevent the trivial LIMIT optimization. Without this, queries where row policy combines to always-true would lose multi-threaded reading unnecessarily. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Share the effective row-policy filter check across applyTrivialCountIfPossible, buildRowPolicyFilterIfNeeded, and the trivial-LIMIT gate so the three sites agree on whether a row policy applies. Previously the early trivial-LIMIT check could drift from the later planning logic, leaving trivial-LIMIT incorrectly firing (or not firing) in edge cases. The helper also guards getDatabaseName with hasDatabase to handle storages with empty database (e.g. Distributed remote queries). Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
…ion-fix Disable trivial LIMIT optimization with row policies/additional_table_filters
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.
Original pull-request #102921
Do not merge this PR manually
This pull-request is a first step of an automated backporting.
It contains changes similar to calling
git cherry-picklocally.If you intend to continue backporting the changes, then resolve all conflicts if any.
Otherwise, if you do not want to backport them, then just close this pull-request.
The check results does not matter at this step - you can safely ignore them.
Troubleshooting
If the conflicts were resolved in a wrong way
If this cherry-pick PR is completely screwed by a wrong conflicts resolution, and you want to recreate it:
pr-cherrypicklabel from the PRYou also need to check the Original pull-request for
pr-backports-createdlabel, and delete if it's presented thereThe PR source
The PR is created in the CI job