Skip to content

Cherry pick #110710 to 25.8: Fix text-index direct-read double-registration over Merge/Distributed - #111092

Closed
robot-ch-test-poll2 wants to merge 6 commits into
backport/25.8/110710from
cherrypick/25.8/110710
Closed

robot-ch-test-poll2 wants to merge 6 commits into
backport/25.8/110710from
cherrypick/25.8/110710

Conversation

@robot-ch-test-poll2

Copy link
Copy Markdown
Contributor

Original pull-request #110710

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

groeneai and others added 6 commits July 16, 2026 15:05
optimizeDirectReadFromTextIndex can visit the same ReadFromMergeTree step
more than once. When an identical text-index predicate is present in both
PREWHERE and WHERE and the query is walked through a
Merge -> Distributed -> MergeTree plan, the inner plan is optimized again
while building the pipeline. On the first visit the pass rewrites PREWHERE
and creates the index read tasks; on the second visit PREWHERE is already
rewritten, so the WHERE branch runs and re-registers the same synthetic
__text_index_..._has_<hash> read column, aborting with
"Column ... already added for reading".

Skip the pass for a step whose index read tasks are already populated: the
optimization has already run for it and there is nothing left to do.

Closes: #110697
The regression test's EXPLAIN visibility check asserts the text-index
direct-read column appears in the plan. Under random setting
prefer_localhost_replica=0 the Merge -> Distributed read is dispatched to
the remote replica, so the MergeTree-level __text_index_..._has_<hash>
column is absent from the initiator plan and the check spuriously reports 0.
The double-registration bug being tested only engages on the local
(initiator) optimization path, so pinning prefer_localhost_replica=1 keeps
the test faithful to the bug while fixing the flakiness.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
…ix 04549 flakiness

Address AI review on the earlier fix: the unconditional early return in
processAndOptimizeTextIndexFunctions skipped the whole pass on any re-visit,
which also dropped the tokenizer/preprocessor rewrite of a different text-index
predicate pushed into a Merge child via ReadFromMerge::addFilter (semantic, not
just perf). Instead, gate only the direct-read replacement on both the PREWHERE
and WHERE branches by whether the step already carries index read tasks. A
re-visit no longer re-registers the synthetic __text_index_..._has_<hash> column
(no "already added for reading"), while the semantic rewrite still runs.

Also stabilize the regression test 04549: pin
query_plan_direct_read_from_text_index=1 on the EXPLAIN visibility query (the
runner randomizes it; when off the optimization is disabled and the synthetic
column is absent, so the visibility check spuriously returned 0). The crash-fix
count() queries stay unpinned so both randomized values exercise the fix.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
…ewrite

The extra query used has(...), which processTextIndexFunction does not rewrite,
so it did not exercise the tokenizer/preprocessor rewrite path the fix preserves.

Add a String column with its own text index and assert that a direct-read PREWHERE
predicate combined with a DIFFERENT tokenizing WHERE predicate (hasAnyTokens) returns
the same result for query_plan_direct_read_from_text_index = 0 and = 1, and that the
WHERE predicate keeps its 3-argument tokenizer rewrite in EXPLAIN even when the step
is re-visited after the PREWHERE already registered a direct-read column.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
The double-registration abort is thrown during pipeline build
(ReadFromMerge::initializePipeline -> buildQueryPipeline), only when direct
read from the text index is enabled. The runner randomizes
query_plan_direct_read_from_text_index to 0 on ~5% of runs, which disables the
optimization and silently skips the crash path, so the crash-repro count()
query must pin the setting to exercise the fix deterministically on every run.
The pinned EXPLAIN visibility query does not cover this: EXPLAIN only builds and
optimizes the plan and never calls buildQueryPipeline. The count()=1 crash-fix
assertions are unchanged; the 0/1 comparison queries are kept for semantic
coverage of the second-pass tokenizer rewrite.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
…here-where-dup-110697

Fix text-index direct-read double-registration over Merge/Distributed
@robot-ch-test-poll2 robot-ch-test-poll2 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 Jul 20, 2026
@alexey-milovidov

Copy link
Copy Markdown
Member

Closing: this backport is not applicable to 25.8. The bug fixed by #110710 lives in the text-index direct-read optimization (src/Processors/QueryPlan/Optimizations/optimizeDirectReadFromTextIndex.cpp, ReadFromMergeTree::getIndexReadTasks / createReadTasksForTextIndex), and none of that code exists on the 25.8 branch — the feature was introduced later, so the double-registration exception cannot occur there and the test cannot run there either. Per the instructions above, closing this cherry-pick PR declines the 25.8 backport; the 26.x backports are unaffected.

@maxknv
maxknv deleted the cherrypick/25.8/110710 branch November 13, 2026 13:22
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.

3 participants