Repository navigation
Keep a direct read from a text index local in plan-based parallel replicas - #112723
alexey-milovidov wants to merge 9 commits into
Conversation
…licas Direct read from a text index registers a synthetic `__text_index_..._has_<hash>` virtual column and an index read task on the `ReadFromMergeTree` step. Capturing such a read into a plan-based parallel-replicas fragment loses the index read tasks (`ReadFromMergeTree::clone` copies the rewritten column list but not the tasks), so re-optimizing the local fragment registered the virtual column a second time and threw the logical error `Column ... already added for reading` when the same predicate appeared in both `PREWHERE` and `WHERE`. Keep such a plan local, like the `FINAL` and `IN (subquery)` cases. Found by AST fuzzer (amd_debug) on a mutation of 04338_text_index_codec_setting_change_merge in an unrelated PR: #111990 https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=111990&sha=e26559ad01883fd20334df73425012376b5a01d7&name_0=PR&name_1=AST%20fuzzer%20%28amd_debug%29 Co-Authored-By: Claude Fable 5 <[email protected]>
|
Workflow [PR], commit [b305123] Summary: ✅
AI ReviewSummaryThis PR tightens Findings❌ Blockers
Final Verdict❌ Request changes. The text-index and pinned-snapshot fixes look correct, but plan-based parallel replicas still appears to ship LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 25/26 (96.15%) · Uncovered code |
…ext-index-direct-read
… the control query `04658_text_index_direct_read_pr_plan_based` asserted that a read without a direct read from a text index is still split for parallel replicas, using `SELECT count() FROM t WHERE s != ''` as the control query. With the randomized `optimize_trivial_count_with_sparsity_filter = 1` and `compute_exact_num_defaults_for_sparse_columns = 1`, `applyTrivialCountWithSparsityFilterIfPossible` answers that query from the sparse-serialization defaults, so there is no `ReadFromMergeTree` left to distribute and no `ReadFromParallelReplicas` in the plan - the test printed `0` instead of `1` and failed in every stateless job. Use `sum(length(s))`, which has to read the column, as the control query. A/B verified locally against a three-replica localhost cluster: with the culprit settings the old form prints `0` and the new form prints `1`. https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=112723&sha=11119c375c78cd874658b7cbd51a4b4b4dc2bf4e&name_0=PR Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
…or shipping `planHasTextIndexDirectRead` walked the whole plan, so a direct read from a text index that was never going to be shipped - the right (build) side of a JOIN, which `collectReadsToDistribute` does not follow - disabled plan-based parallel replicas for the rest of the query. Check only the reads returned by `collectReadsToDistribute` instead. When one of those has index read tasks the whole plan still stays local rather than dropping that single read out of the eligible set: a union with a mix of local and distributed branches is not supported and can produce wrong results, same as the `FINAL` case. Test: the left side of a JOIN whose right side does a direct text-index read is still distributed. Verified locally against a three-replica localhost cluster - the `EXPLAIN` has `ReadFromParallelReplicas` over `t_plain_pr_plan_based` while the text-index read keeps its `__text_index_idx_hasToken_...` PREWHERE column locally. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
🕵 CI triage for the previous run (report): Caused by this PR — fixed. Unrelated.
@groeneai, could you investigate |
|
Both are already fixed on master, and both fixes are yours. Details, plus one residual risk worth knowing about.
|
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 13/13 (100.00%) · Uncovered code |
…ext-index-direct-read
The last check of `04658_text_index_direct_read_pr_plan_based` asserts that a direct text-index read on the side that `collectReadsToDistribute` never follows does not disable plan-based parallel replicas for the other side. Which side that is depends on the join order, and the stateless test harness randomizes it through `query_plan_optimize_join_order_randomize`: with a seed that swaps the tables, the text-index read becomes the eligible one, the whole plan legitimately stays local, and the test printed `0` instead of `1`. Pin `query_plan_optimize_join_order_randomize`, `query_plan_optimize_join_order_limit` and `query_plan_join_swap_table` for that query. Verified locally: the test fails with the two seeds reported by CI (`183078`, `481645`) before this change and passes with them after it.
|
🕵 Thanks for the analysis — confirmed on both counts, and this PR is green now that master is merged. On the residual: no separate PR is needed. The re-land of #107363 is #112803, and it already carries the durable fix that #112753 proposed — it adds |
…ext-index-direct-read
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction. Compile time of recompiled translation units7 translation units recompiled, 16 s compile time in total, 7 of them have a recent master baseline. |
…rect text-index read `collectReadsToDistribute` follows only the coordinated side of a JOIN, so the guard in `insertParallelReplicasSplit` never sees the broadcast side. When `liftSplitAboveJoin` then ships the whole join subtree, a broadcast-side `ReadFromMergeTree` with direct text-index tasks is serialized without them, and the remote replica fails with `Column __text_index_... not found in table` (NOT_FOUND_COLUMN_IN_BLOCK): the PREWHERE references the synthetic virtual column that only the index read tasks can produce. Teach `subtreeHasUnshippableRead` to treat a read with non-empty `getIndexReadTasks` as unshippable, so the split stays below the join: the coordinated read is still distributed and the join with its broadcast side stays local. Extend the test with an executed (not just explained) JOIN where dropping the broadcast-side filter would change the result. Co-Authored-By: Claude Fable 5 <[email protected]>
…ased parallel replicas A read with a pinned block-number boundary (`select_sequential_consistency` clamps the read to the parts confirmed by the insert quorum) must not be shipped into a plan-based parallel-replicas fragment: `ReadFromMergeTree` serialization does not carry the pin and `deserialize` rebuilds the read with `max_block_numbers_to_read = nullptr`, so a follower replica silently reads parts the initiator's snapshot excludes and returns rows the quorum never confirmed. Reject `hasPinnedBlockNumbers` in `mergeTreeReadCanBeShipped`, mirroring the rejection in `make_distributed_plan` (`makeDistributed.cpp`); the predicate gates both the coordinated reads (`collectReadsToDistribute`) and the broadcast side of a lifted join (`subtreeHasUnshippableRead`). A/B verified locally against a 3-replica localhost cluster with a two-replica quorum fixture (`SYSTEM STOP FETCHES` + failed quorum insert): without the guard the pinned read is split and a remote-only execution returns (435, 30) instead of the quorum-consistent (45, 10); with the guard it stays local and returns (45, 10), and a plain read is still distributed. Co-Authored-By: Claude Fable 5 <[email protected]>
| return false; | ||
| } | ||
|
|
||
| /// Direct read from a text index (`query_plan_direct_read_from_text_index`) replaces a text-search |
There was a problem hiding this comment.
@groeneai Please shorten this comment to a single line.
There was a problem hiding this comment.
Proposed:
/// A direct text-index read cannot be shipped: its index read tasks are not serialized, so a replica cannot resolve `__text_index_*`.134 chars, within the 140 column limit. I do not have push access to this branch, so this is a suggestion rather than a commit.
One note on which half I kept. The clause saying ReadFromMergeTree::clone copies the rewritten column list but not the index read tasks is no longer true at this head: it copies them at ReadFromMergeTree.cpp:3777, added in 764308f and pulled into this branch by the 08-02 master merge, i.e. after this comment was written. Serialization still omits them, so the remote-resolution reason is the one that survives.
Add to 04836 and 04837 the scenarios that the rejected-behavior tests of the superseded PRs covered and the new positive tests did not: the AST fuzzer shape with a mixed PREWHERE conjunction and a xor in WHERE, a text index read shipped through a subquery under a shuffle join (both from ClickHouse#108818), and a join whose side carries a direct text-index read with a filter-sensitive result (from ClickHouse#112723). Every query is repeated with the distributed mode off and must return identical results. Related: ClickHouse#108818 Related: ClickHouse#112723 Co-Authored-By: Claude Fable 5 <[email protected]>
|
I'm trying to add proper support in #114242. Closing this for now. |
…plicas select_sequential_consistency clamps a ReplicatedMergeTree read to the parts the initiator saw confirmed by the insert quorum, via max_block_numbers_to_read. The pin is not serialized: ReadFromMergeTree::deserialize rebuilds a shipped read with max_block_numbers_to_read = nullptr. mergeTreeReadCanBeShipped did not consult it, so the read was split for parallel replicas and each follower silently read its own unclamped part set, returning rows the quorum never confirmed with no exception. Reject hasPinnedBlockNumbers in mergeTreeReadCanBeShipped, mirroring the rejection for the same property in make_distributed_plan (checkDistributedReadSupported, makeDistributed.cpp). The predicate gates both collectReadsToDistribute (the coordinated read) and subtreeHasUnshippableRead (the broadcast side of a lifted join), so one check covers both carriers. Returning false rather than throwing matches how this path already keeps a refreshable-MV target read local, and how the same file keeps FINAL reads and IN (subquery) plans local: make_distributed_plan is an explicit opt-in, plan-based parallel replicas falls back to a local read. A/B on a two-replica quorum fixture (SYSTEM STOP FETCHES plus a timed-out insert_quorum = 2, insert_quorum_parallel = 0 insert, so /quorum/status stays set), remote-only, with the guard reverted and nothing else changed: the pinned read is split and returns (435, 30) instead of the quorum-consistent (45, 10), and a join reading the pinned table on its broadcast side returns 30 rows where the truth is 10. The fixture is asserted armed before measuring, since SYSTEM STOP FETCHES is in-memory only and a restart evaporates the pin. The join arm coordinates a plain MergeTree, which has no sequential-consistency branch and therefore keeps its split marker, so the broadcast-side check is reached rather than short-circuited by the coordinated read being rejected first. It asserts step order rather than step presence: refusing the lift keeps the split below the join, so ReadFromParallelReplicas appears either way and only its position distinguishes a local join over a distributed read from a fully shipped join. The fix and its regression test are Alexey Milovidov's, from commit b305123 on ClickHouse#112723, which was closed unmerged in favour of ClickHouse#114242. That PR reworks text-index distributed reads and does not carry this guard. Co-Authored-By: Claude Fable 5 <[email protected]>
Related: #111990
Related: #108818
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix the logical error
Column ... already added for readingwhen plan-based parallel replicas (parallel_replicas_plan_based) captured a plan doing a direct read from a text index (query_plan_direct_read_from_text_index), e.g. when the same text-search predicate appears in bothPREWHEREandWHERE. Such a plan is now executed locally.Description
Direct read from a text index replaces a text-search function (
hasToken,hasAnyTokens, ...) with a synthetic__text_index_..._has_<hash>virtual column and registers an index read task on theReadFromMergeTreestep. That per-step state does not survive capturing the read into a plan-based parallel-replicas fragment:ReadFromMergeTree::clonecopies the rewritten column list but not the index read tasks, so re-optimizing the local fragment considered direct read "not yet applied", registered the virtual column a second time, and threw the logical errorColumn ... already added for reading(an exception, and an abort in debug builds) when the same predicate appeared in bothPREWHEREandWHERE. A remote replica also cannot resolve the synthetic column against the table (themake_distributed_plananalogue is #108818).The text-index rewrite runs earlier in the same optimization pass, so the tasks are already registered when
applyParallelReplicasruns; keep such a plan local, following the existing precedent forFINALreads andIN (subquery).Found by AST fuzzer (amd_debug) in the unrelated #111990 on a mutation of
04338_text_index_codec_setting_change_merge:CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=111990&sha=e26559ad01883fd20334df73425012376b5a01d7&name_0=PR&name_1=AST%20fuzzer%20%28amd_debug%29
Verified A/B against a local server with the localhost 3-replica cluster: without the fix the new test throws the exact logical error from CI; with the fix all queries return correct results, the direct-read plan stays local, a plain read is still split for parallel replicas, and
04409_pr_plan_basedand02346_text_index_parallel_replicasstill pass.Documentation entry for user-facing changes