Skip to content

Let automatic parallel replicas handle direct read from a text index - #129339

Merged
nickitat merged 4 commits into
masterfrom
autopr-text-index-direct-read
Nov 14, 2026
Merged

nickitat merged 4 commits into
masterfrom
autopr-text-index-direct-read

Conversation

@nickitat

@nickitat nickitat commented Nov 4, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Improvement

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

Automatic parallel replicas can now enable parallel replicas for queries that use direct read from a text index.


Automatic parallel replicas never enabled parallel replicas for a query that reads directly from a text index (query_plan_direct_read_from_text_index, on by default). Every such query ended in AutoParallelReplicasPlansNotComparable.

The probe plan with parallel replicas is optimized with query_plan_optimize_primary_key off, so its reads have no index conditions. Direct read takes the conditions it rewrites from them, so the single-node plan got the __text_index_* virtual column while the probe plan kept hasToken, and the two plans hashed differently.

The builder of the probe plan now gets a lookup of the single-node read each probe read stands for, paired by table expression name as transplantAnalysisToAllReads pairs them. collectTextIndexReadInfos uses it for a read with no index conditions of its own. The probe reads themselves are left untouched: building their index conditions would execute IN subqueries the probe plan cannot adopt (e.g. the UUID-keyed set of query_plan_convert_join_to_in), and adopting the single-node read's indexes would make updatePrewhereInfo build the PREWHERE sets.

On a 20M-row table with query-based parallel replicas, a hasToken query with GROUP BY goes from PlansNotComparable to AutoParallelReplicasApplied on 3 replicas. The plan-based implementation is still blocked by #118265 and by the guard for #116360.


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

Version info

  • Merged into: 26.11.1.2307-master (included in 26.11 and later)
  • Backported to: 26.10.5.15

nickitat and others added 2 commits November 4, 2026 16:01
… a text index

The plan for automatic parallel replicas is optimized with
`query_plan_optimize_primary_key` off, so `optimizePrimaryKeyConditionAndLimit`
never builds the indexes of its reads. Direct read from a text index takes
the conditions it rewrites from those indexes, so the single-node plan got
`__text_index_*` virtual columns while the replicas plan kept `hasToken`.
The steps hashed differently, every such query ended in
`AutoParallelReplicasPlansNotComparable`, and parallel replicas were never
enabled for it.

Build the index conditions for a read of a table with a text index even when
the pass is off. Index analysis is lazy, so this builds conditions only, and
`adoptFiltersFrom` replaces them with the single-node read's if the replicas
plan is chosen.

On a 20M-row table with query-based parallel replicas, a `hasToken` query
with `GROUP BY` now gets `AutoParallelReplicasApplied` and runs on 3
replicas instead of `PlansNotComparable`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ing them

The previous commit ran `optimizePrimaryKeyConditionAndLimit` on reads of the
probe plan of automatic parallel replicas. That is not condition-only: building
`KeyCondition` executes the subquery of every `IN` whose set the probe plan
could not adopt from the single-node plan, such as the UUID-keyed set of
`query_plan_convert_join_to_in`. The subquery ran a second time even when the
candidate was rejected, so a query near `max_rows_to_read` could throw
`TOO_MANY_ROWS`. Adopting the single-node read's `indexes` into the probe read
is no better: `updatePrewhereInfo` takes their presence as the sign that
`applyFilters` ran and builds the PREWHERE sets.

Leave the probe reads untouched. The builder of the probe plan gets a lookup of
the single-node read each probe read stands for, paired by table expression
name as `transplantAnalysisToAllReads` pairs them, and only
`collectTextIndexReadInfos` uses it, for a read with no index conditions of its
own. Both plans then rewrite the same text-search functions, and nothing is
built that the single-node plan has not already built.

The test now also checks that a JOIN converted to `IN` next to a `hasToken`
predicate builds its set once and reads as many rows as without parallel
replicas; with the previous commit it built the set twice.

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

clickhouse-gh Bot commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [2cefcde]

Summary: ✅


AI Review

Summary

This PR enables automatic parallel replicas for query-based plans that read directly from a text index. The probe borrows the corresponding single-node read's index conditions without rebuilding its sets, and skips the incompatible trivial-count optimization. No actionable findings remain. The previous coverage finding is addressed by mode-1 execution assertions, and 05507_autopr_direct_read_from_text_index passed in Fast test at head 2cefcde902e31575fa552625465d9289cc7880e5. The documented plan-based limitations remain tracked in #118265 and #116360.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Nov 4, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-parallel-replicas Parallel reading from replicas for query speed (not data replication consistency). label Nov 4, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 2cefcde90 with master f0a9e6bff (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

80 object files changed (+39.32 KiB total), 0 added.

Object file Master PR Δ
src/CMakeFiles/dbms.dir/Processors/QueryPlan/Optimizations/considerEnablingParallelReplicas.cpp.o 485.41 KiB 502.11 KiB +16.70 KiB (+3.44%)

711 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.

Slowest function optimization changes (ThinLTO)

Median per-function time ratio to the master baseline: programs/clickhouse ×0.67, programs/clickhouse-keeper ×0.68 (each binary is linked separately, on a different machine and with different build flags); deltas below are relative to it.

Binary Function Master PR Δ vs median
programs/clickhouse DB::registerFunctionConversion(DB::FunctionFactory&) 5.9 s 13.7 s +9.7 s (+247%)
programs/clickhouse _ZN6detail10static_forITk7is_enumN2DB23AttributeUnderlyingTypeEZNS1_29callOnDictionaryAttributeType… 3.4 s 7.6 s +5.3 s (+234%)
Compile time of recompiled translation units

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

Job report

…allel replicas

Now that the plan for automatic parallel replicas rewrites its reads for direct read from a text index,
`optimizeTrivialCountFromTextIndex` matched those reads. They have no index analysis, so the pass ran it
over the rewritten filter, where the text index is of no use, and `force_data_skipping_indices` threw
`INDEX_NOT_USED`. When the single-node plan answers the count from the text index, automatic parallel
replicas does not consider the query, so the pass has nothing to do there.

Failures: `02346_text_index_bug110350`, `04654_text_index_merge_with_empty_part` with
`automatic_parallel_replicas_mode = 2`.
https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=129339&sha=latest&name_0=PR
#129339

05507 also runs the query in mode 1 after collecting statistics and checks that the plan with parallel
replicas is applied, is used, and returns the single-node result.

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

@groeneai groeneai left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. I built 222b0acd (debug) and compared it with master 21809eaa on a three-replica localhost cluster (parallel_replicas_plan_based = 0, parallel_replicas_local_plan = 1, cost-model settings as in 05507).

  • 05507 matches the reference. On master it fails at the last two checks (not_comparable = 1 with no statistics collected, then applied = 0). Its 05507_applied query in mode 1 also covers what the open bot thread on the test asks for.
  • 34 query shapes over the text index, each run without parallel replicas, then in mode 2, then in mode 1, give the same result and no exception: hasToken, hasAnyTokens, hasAllTokens, LIKE, ILIKE with use_text_index_like_evaluation_by_dictionary_scan, NOT, an OR with a non-index condition, explicit PREWHERE, a lower() preprocessor, a partially materialized index, a second text index on mapKeys(m) plus a minmax index on the same table, IN over the same table and over another one, a self-join, force_data_skipping_indices = 'idx_str', and each of query_plan_direct_read_from_text_index, use_skip_indexes_on_data_read, optimize_move_to_prewhere, use_query_condition_cache set to 0. 27 are adopted in mode 1 (AutoParallelReplicasApplied and ParallelReplicasUsedCount both 1), 1 (count() over ILIKE) is matched and then rejected by the cost model, and 4 (count() answered from the index, UNION ALL) are skipped before the probe plan is built.
  • The other 2 still end in PlansNotComparable: GROUP BY ... ORDER BY ... LIMIT 3, and the query_plan_convert_join_to_in join from the test. Both do the same with a plain filter (WHERE id % 1000 = 7) instead of the text index, so neither comes from this PR.
  • The finder's raw pointers to the single-node reads do not outlive the builder call: the finder lives only in the builder's local QueryPlanOptimizationSettings, and what the probe's IndexReadTask keeps is a value copy of MergeTreeIndexWithCondition.

…irect-read

# Conflicts:
#	src/Processors/QueryPlan/Optimizations/considerEnablingParallelReplicas.cpp
@nickitat
nickitat added this pull request to the merge queue Nov 14, 2026
Merged via the queue into master with commit c8a7358 Nov 14, 2026
205 checks passed
@nickitat
nickitat deleted the autopr-text-index-direct-read branch November 14, 2026 14:01
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 14, 2026
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Nov 14, 2026
nickitat added a commit that referenced this pull request Nov 14, 2026
Cherry pick #129339 to 26.10: Let automatic parallel replicas handle direct read from a text index
robot-clickhouse added a commit that referenced this pull request Nov 14, 2026
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Nov 14, 2026
clickhouse-gh Bot added a commit that referenced this pull request Nov 14, 2026
Backport #129339 to 26.10: Let automatic parallel replicas handle direct read from a text index
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-parallel-replicas Parallel reading from replicas for query speed (not data replication consistency). pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-improvement Pull request with some product improvements pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-synced-to-cloud The PR is synced to the cloud repo v26.10-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants