Skip to content

Plan-based parallel replicas: keep a read local when a replica cannot build its text index transform - #132916

Draft
groeneai wants to merge 1 commit into
ClickHouse:masterfrom
groeneai:plan-based-pr-keep-local-denied-text-index-transform
Draft

groeneai wants to merge 1 commit into
ClickHouse:masterfrom
groeneai:plan-based-pr-keep-local-denied-text-index-transform

Conversation

@groeneai

@groeneai groeneai commented Nov 19, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #128413

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Description

02346_text_index_transform_access fails in the new Stateless tests (arm_binary, ParallelReplicas, plan-based, s3 storage, parallel) job (CI report): a reader without the privilege for a function of a text index preprocessor/postprocessor gets FUNCTION_NOT_ALLOWED from a replica, while the same read works locally and with query-based parallel replicas.

The text-index rewrite merges the index transform into the read's filter before applyParallelReplicas cuts the fragment. The transform is built under the global context on purpose (its privileges are checked when the index is declared), but a replica rebuilds every function of the shipped plan under the reader's rights, so checkAccessOnBuild throws there.

The fragment is now kept local when a replica would refuse to build one of its functions, the same way this file keeps Top-K, direct-read and mergeTreeProjection reads local. The probe resolves each function by the name the plan is serialized with and calls checkAccessOnBuild on a copy of the context, so the query's reported privileges do not change. A reader who has the privilege still gets parallel replicas. make_distributed_plan ships its worker fragments by another path, not changed here.

parallel_replicas_plan_based is experimental, hence Not for changelog. The replicas refused this read before #127443 too, so it is not the cause.

Validation: the new test fails on master and passes 50/50 with randomized settings; 02346_text_index_transform_access under the plan-based profile with parallel_replicas_local_plan = 0 goes from 20/20 FAIL to 20/20 OK; every *_pr_plan_based_* test passes.


Workflow [PR]
Sync PR [sync-upstream/pr/132916]

… build its text index transform

The text-index rewrite (`processAndOptimizeTextIndexFunctions`) runs before
`applyParallelReplicas` and merges the index `preprocessor`/`postprocessor`
into the read's filter or PREWHERE. The transform is built under the global
context on purpose: its privileges are checked when the index is declared,
so a reader without them can still search through the index. A replica,
however, rebuilds every function of a shipped plan with
`FunctionFactory::get` + `build` under the reader's rights, so
`checkAccessOnBuild` threw `FUNCTION_NOT_ALLOWED` there (seen as
`02346_text_index_transform_access` failing in the plan-based parallel
replicas CI job). The replicas refused this read before ClickHouse#127443 as well,
from the function creator.

Keep the fragment local when a replica would refuse to build one of its
functions: walk every DAG the fragment serializes (`checkQueryPlanActionDAGs`),
every function of each node (`allNodeFunctions`, recursing into lambda
DAGs), resolve it by the name the plan is serialized with and call
`checkAccessOnBuild`. The probe runs on a copy of the context with its own
`QueryPrivilegesInfo`, so `system.query_log` privileges do not change. A
reader who has the privilege still ships the read.

An IN-subquery set needs no extra check: its own fragment goes through the
same visitor, and the set plan an outer fragment ships is the unoptimized
one (`addPlansForSets` runs after `applyParallelReplicas`), which does not
hold the transform.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 0 findings; bounded: 1 full pass + 1 fix round + 1 delta recheck). Two points from my own review, both with no code change:

# Sev Finding Verdict Evidence / action
1 💡 The probe catches any Exception from tryGet + checkAccessOnBuild (applyParallelReplicas.cpp) DISAGREE A replica rebuilds the function with FunctionFactory::get + build, and build calls checkAccessOnBuild first (ActionsDAG.cpp:5516-5520, IFunction.cpp:939), so whatever the probe throws, the replica would throw too; staying local is the right outcome for all of it.
2 💡 FunctionFactory::tryGet records names into the query's used_functions DISAGREE Only functions of the shipped plan, which the query executes anyway: used_functions and used_privileges are identical on master and on this PR for the same queries.

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are terminal per finding.

Session id: cron:clickhouse-review-slot-8:20261119-090100

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @ahmadov, could you review this? With plan-based parallel replicas the text-index rewrite merges the index transform, built under the global context, into the shipped filter, and a replica rebuilding it under the reader's rights throws FUNCTION_NOT_ALLOWED; such a fragment is now kept local, like the Top-K and direct-read cases.

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. With parallel_replicas_local_plan = 0 every replica is remote: a hasAllTokens/hasToken read over a text index with postprocessor = demangle(val) (or preprocessor) under allow_introspection_functions = 0 fails 5/5 on master with FUNCTION_NOT_ALLOWED from a replica; 02346_text_index_transform_access under the plan-based profile fails 20/20.
b Root cause explained? The text-index rewrite merges the index transform, built under the global context, into the read's filter before applyParallelReplicas cuts the fragment; the replica rebuilds every function under the reader's rights (ActionsDAG::deserialize -> checkAccessOnBuild).
c Fix matches root cause? Yes. The fragment stays local exactly when a replica would refuse to build one of its functions, like the existing Top-K / direct-read / mergeTreeProjection rules. No tag, no blacklist entry; a granted reader still ships.
d Test intent preserved / new tests added? New 05838_pr_plan_based_text_index_transform_access.sql: granted read shipped, denied reads (postprocessor, preprocessor, hasToken, IN subquery) kept local and correct, query privileges unchanged. 02346 untouched.
e Both directions demonstrated? Yes. New test fails on master and passes on the fix (50/50 randomized); 02346 20/20 FAIL -> 20/20 OK; disabling the new check brings the failure back, and probing without the context copy flips the privileges assertion.
f Fix is general across code paths? Yes. One generic predicate over every DAG a shipped step serializes (filter, expression, join, PREWHERE, row-level filter, child plans), recursing into lambdas, for every function with a build-time access check. An IN subquery's own fragment is covered too.
g Fix generalizes across inputs (params/datatypes/wrappers)? Preprocessor (plain node) and postprocessor (lambda) shapes, hasAllTokens and hasToken, IN subquery with use_index_for_in_with_subqueries 0/1, use_skip_indexes_on_data_read 0/1, granted and denied. Column type wrappers do not affect the decision.
h Backward compatible? (maintainer-approved exception only) Yes. No setting, format or default change; a denied read under the experimental setting now runs on one node instead of failing.
i Invariants and contracts preserved? Yes. The check only reads the plan, on a copy of the context with its own QueryPrivilegesInfo, so used_privileges/missing_privileges are unchanged; the early return leaves the split marker unconverted, the same exit the other keep-local rules use.

Session id: cron:clickhouse-impl-slot-4:20261119-082700

@clickhouse-gh

clickhouse-gh Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [9681370]

Summary: ❌

job_name test_name status info comment
Stateless tests (arm_binary, ParallelReplicas, plan-based, s3 storage, parallel) FAIL
05829_autopr_skip_index_on_read_in_subquery FAIL cidb
05033_lazy_materialization_column_function_structure_equals NOT_FAILED cidb
04353_text_index_explicit_tokenizer_argument NOT_FAILED cidb

AI Review

Summary

Keeps a plan fragment local when a replica would reject a function injected by a text index transform, while retaining parallel execution for permitted readers. No actionable findings in the current diff or surrounding execution paths. The focused regression test covers preprocessors, lambda postprocessors, an IN subquery, plan placement, and privilege accounting.

Missing context / blind spots
  • ⚠️ Tests were not rerun locally because this checkout has no built binary. The author reports repeated baseline/fixed validation; the full CI suite is still pending. Completed CI results would provide independent runtime confirmation.
Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added pr-not-for-changelog This PR should not be mentioned in the changelog can be tested Allows running workflows for external contributors comp-parallel-replicas Parallel reading from replicas for query speed (not data replication consistency). labels Nov 19, 2026
@devcrafter devcrafter self-assigned this Nov 19, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 9681370aa with master 0f2074154 (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Object file sizes

4 object files changed (+21.75 KiB total), 0 added.

Object file Master PR Δ
src/CMakeFiles/dbms.dir/Processors/QueryPlan/Optimizations/applyParallelReplicas.cpp.o 286.55 KiB 308.29 KiB +21.74 KiB (+7.59%)

716 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only clickhouse-bundle) and not compared.

Compile time of recompiled translation units

1408 translation units recompiled, 41 s compile time in total, 1408 of them have a recent master baseline.

Job report

@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger - 9681370

Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
whose fixing-PR link will be posted here when it opens. Only CH Inc sync is exempt.

Check / test Reason Owner / fixing PR
Stateless tests (arm_binary, ParallelReplicas, plan-based, s3 storage, parallel) / 05829_autopr_skip_index_on_read_in_subquery not from this PR: a deterministic red of this test under the plan-based parallel replicas job, where automatic parallel replicas collects no statistics for its queries (#131450). Same red on 16 master commits since 2026-11-19 00:41Z (master report) and on 91 other PRs. The fix merged at 11:58Z, after this run started #132867 (mine, merged)
PR praktika rollup of the row above #132867 (mine, merged)

The other CIDB FAIL rows on this head are Config Workflow / Pre Hooks of the run before the can be tested label. Mergeable Check is red only because of the row above; CH Inc sync is green.

Session id: cron:our-pr-ci-monitor:20261119-123019

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-parallel-replicas Parallel reading from replicas for query speed (not data replication consistency). pr-not-for-changelog This PR should not be mentioned in the changelog

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants