Skip to content

Cherry pick #102921 to 25.8: Disable trivial LIMIT optimization with row policies/additional_table_filters - #104040

Merged
robot-ch-test-poll4 merged 12 commits into
backport/25.8/102921from
cherrypick/25.8/102921
May 5, 2026
Merged

robot-ch-test-poll4 merged 12 commits into
backport/25.8/102921from
cherrypick/25.8/102921

Conversation

@robot-clickhouse-ci-2

Copy link
Copy Markdown
Contributor

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-pick locally.
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:

  • delete the pr-cherrypick label from the PR
  • delete this branch from the repository

You also need to check the Original pull-request for pr-backports-created label, and delete if it's presented there

The PR source

The PR is created in the CI job

azat and others added 11 commits April 21, 2026 16:13
…_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]>
…ion-fix

Disable trivial LIMIT optimization with row policies/additional_table_filters
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only! do not test disable testing on pull request pr-bugfix Pull request with bugfix, not backported by default labels May 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

do not test disable testing on pull request pr-bugfix Pull request with bugfix, not backported by default pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants