Repository navigation
Conversation
The per-partition snapshot sub-plan built by MergeTreeCommitOrderSequentialSource was optimized twice: buildPartitionReadingPipeline calls plan.optimize() explicitly and then plan.buildQueryPipeline() with the default do_optimize=true, which optimizes again. The direct-read-from-text-index optimization is not idempotent: every pass appends a __text_index_* virtual column to the ReadFromMergeTree step. The second pass therefore aborted with a LOGICAL_ERROR, either "Column ... already added for reading" (two text indexes) or "Block structure mismatch ... MergeTreeCommitOrderSequentialSource stream ..." (one text index). Pass do_optimize=false to buildQueryPipeline since the plan is already optimized, matching the canonical pattern in InterpreterSelectQueryAnalyzer and JoinStepLogical. The direct-read optimization still runs once and is preserved. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-author-slot-3:20260723-001054 |
Internal second-model review — adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 2 rounds — 1 full pass + 1 stateful delta recheck).
Severity: ❌ blocker / Session id: cron:clickhouse-author-slot-3:20260723-001054 |
|
cc @Michicosun @ahmadov — could you review this? A |
|
Workflow [PR], commit [a968472] Summary: ❌
AI ReviewSummaryThis PR fixes the streaming text-index exception by stopping Tests
Final Verdict
|
| /// The plan is already optimized above; do not optimize again. Some optimizations are not | ||
| /// idempotent (e.g. direct read from a text index appends virtual columns to the read step), | ||
| /// so a second pass would corrupt the plan and produce a header mismatch. | ||
| auto builder = plan.buildQueryPipeline(opt_settings, BuildQueryPipelineSettings(context), /*do_optimize=*/false); |
There was a problem hiding this comment.
@groeneai what about making text index direct read optimization idempotent? Would it have a small blast radius?
There was a problem hiding this comment.
Good question, but making it fully idempotent is the larger blast radius here, and it guards the symptom rather than the cause.
The DAG replacer is already partially idempotent: TextIndexDAGReplacer::replace pre-populates the virtual-column map from __text_index_* inputs already present in the DAG (added for #101913). The crash isn't there. It is the step-level accumulation in ReadFromMergeTree::createReadTasksForTextIndex (throws at ReadFromMergeTree.cpp:4765): it pushes the virtual column onto all_column_names, extends new_metadata->virtuals, and rebuilds storage_snapshot / output_header / required_source_columns on every call. Making that re-entrant (skip already-present columns, don't double-rebuild the header, and handle the removed_columns erase at :4732, which is a find+erase that lands on end() on the second pass) is a change to ReadFromMergeTree, a core class used by every MergeTree read, not just streaming. That is a bigger, subtler surface than the current one-liner.
The deeper point: QueryPlan::optimize() is not idempotent by design (QueryPlan.cpp:761, no "already optimized" guard) - it re-runs the whole pass pipeline. The text-index optimization is just the first pass that throws loudly. A fully idempotent direct-read would still leave the plan exposed to any other non-idempotent pass, so it fixes one symptom of "the snapshot sub-plan is optimized twice", not the cause.
do_optimize=false removes the erroneous second optimize: 1 line, streaming-local, and it is the existing contract for an already-optimized plan (InterpreterSelectQueryAnalyzer.cpp:416, executeQuery.cpp:1953). The optimization still runs once and is preserved.
If you would prefer full idempotency of the text-index pass as a separate hardening, I am happy to do it in its own PR, but for this bug the one-liner is both the smaller change and the root-cause fix. Your call as the component owner.
| /// The plan is already optimized above; do not optimize again. Some optimizations are not | ||
| /// idempotent (e.g. direct read from a text index appends virtual columns to the read step), | ||
| /// so a second pass would corrupt the plan and produce a header mismatch. |
There was a problem hiding this comment.
| /// The plan is already optimized above; do not optimize again. Some optimizations are not | |
| /// idempotent (e.g. direct read from a text index appends virtual columns to the read step), | |
| /// so a second pass would corrupt the plan and produce a header mismatch. |
There was a problem hiding this comment.
Done in bd4dd3d: removed the comment, kept do_optimize=false. Rationale moved to the commit message.
There was a problem hiding this comment.
The test is super complex; make it simpler, remove comments.
There was a problem hiding this comment.
Done in bd4dd3d. Dropped all comments and the misleading non-streaming coverage EXPLAIN, collapsed the INSERT to a single map key. The test is now just: create the two-index table, insert, run the STREAM ... PREWHERE query, assert 0,1,2.
| 1 | ||
| 0 | ||
| 1 | ||
| 2 |
There was a problem hiding this comment.
debug the problem here in the tests, it is flaky right now.
There was a problem hiding this comment.
Root cause found and fixed in bd4dd3d. The test pinned query_plan_direct_read_from_text_index and query_plan_merge_filters but not query_plan_text_index_add_hint (default true, randomized by CI). CI hit --query_plan_text_index_add_hint False, and with it off two things happened: the old non-streaming coverage EXPLAIN stopped exposing a __text_index_ column (the observed -1/+0 on reference line 1), and the STREAM query no longer crashed on the unpatched build (it lost its coverage). Now add_hint=1 is pinned too.
Verified the exact setting requirement with a matrix on the unpatched build: the abort reproduces only with all three of direct_read, add_hint, merge_filters set to 1; with any one off the query takes the row-scan path and returns 0,1,2 cleanly. So the three pins are minimal, and the STREAM query is a genuine fails-without-fix oracle (unpatched: LOGICAL_ERROR: Column __text_index_..._equals_... already added for reading; patched: 0,1,2). 50/50 runs pass with randomization on the patched build.
On liveness: because the crash only occurs when direct read actually fires (the matrix above), the crash-on-unpatched-build is itself the proof that direct read executes on the STREAM path; if it stopped firing the unpatched build would stop crashing. I kept the test to the row assertion rather than adding a TextIndexUseHint ProfileEvent check, since that event counts granules subject to text_index_hint_max_selectivity on a 20-row part and could be discarded, reintroducing flakiness. Happy to add a plan/ProfileEvent assertion if you'd prefer it over the row oracle.
There was a problem hiding this comment.
A second, distinct flakiness surfaced after the previous fix: the CI "old analyzer" combos ran the test with enable_analyzer=0, where the STREAM query throws "Streaming queries are not supported with the old analyzer" (Code 48, NOT_IMPLEMENTED) before it can exercise direct read, so the whole test errored (return code 48).
Fixed in a968472 by tagging the test no-old-analyzer, matching the existing STREAM sibling 04281_sparsity_stream_skips_trivial_count. Verified through the test runner: new analyzer + tag -> runs and passes; old analyzer + tag -> skipped; old analyzer without the tag -> the Code 48 error, i.e. the tag is load-bearing. The double-optimize crash coverage on the new-analyzer path is unchanged.
…op code comment The regression test 04500_streaming_text_index_direct_read was flaky in CI: it pinned query_plan_direct_read_from_text_index and query_plan_merge_filters but not query_plan_text_index_add_hint (default true, randomized by CI). With add_hint=False the streaming double-optimize does not crash on the unpatched build and the non-streaming coverage-guard EXPLAIN no longer exposes a __text_index_ column, so the reference mismatched on line 1 (observed -1/+0 in amd_debug parallel and arm_asan_ubsan targeted). Pin all three settings the crash requires (verified via a setting matrix: with any one off the query takes the row-scan path and does not reproduce). Drop the misleading non-streaming EXPLAIN coverage guard; the STREAM query is the oracle and aborts with 'already added for reading' on the unpatched build. Simplify the table population and remove comments per review. Also remove the explanatory comment above the buildQueryPipeline(do_optimize=false) call in MergeTreeCommitOrderSequentialSource.cpp per review; the rationale lives in the commit history. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Internal second-model review of the rework (click to expand)Commit bd4dd3d was run through an independent second-model review before push. Two findings, both adjudicated DISAGREE with evidence:
|
STREAM reads are analyzer-only: the old analyzer raises "Streaming queries are not supported with the old analyzer" (NOT_IMPLEMENTED, Code 48). The CI "old analyzer" combos run the test with enable_analyzer=0, where the STREAM query throws before it can exercise the direct-read path, so the whole test errors (return code 48). Add the no-old-analyzer tag so those jobs skip the test cleanly, matching the existing STREAM sibling 04281_sparsity_stream_skips_trivial_count. The test still runs and passes under the analyzer, and still reproduces the double optimize LOGICAL_ERROR without the fix. Co-Authored-By: Claude Opus 4.8 <[email protected]>
fix this, one line |
|
Already fixed in a968472 (current head), one line: added |
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 5/5 (100.00%) · Uncovered code |
CI finish ledger - a968472Every failure below has an owner (a fixing PR). Only
The PR-caused Session id: cron:our-pr-ci-monitor:20260723-223000 |
|
will be fixed as a part of #111821 |
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
...
Description
Found by the AST fuzzer on #110084 (unrelated to that PR): AST fuzzer (amd_debug), STID 0993-32d6.
A
SELECT ... FROM t STREAM ... PREWHERE <text-search predicate>over a table with a text index aborted with aLOGICAL_ERROR:Block structure mismatch ... between ExpressionTransform and MergeTreeCommitOrderSequentialSource stream ...atMergeTreeCommitOrderSequentialSource.cpp:470(one text index), orColumn __text_index_..._equals_... already added for readingatReadFromMergeTree.cpp:4765(two text indexes).Root cause:
buildPartitionReadingPipelinebuilds a per-partition snapshot sub-plan, callsplan.optimize()explicitly, then callsplan.buildQueryPipeline()with the defaultdo_optimize=true, which optimizes the same plan a second time. The direct-read-from-text-index optimization is not idempotent: each pass appends a__text_index_*virtual column to theReadFromMergeTreestep, so the second pass corrupts the plan.Fix: pass
do_optimize=falsetobuildQueryPipeline(the plan is already optimized), matching the canonical pattern inInterpreterSelectQueryAnalyzerandJoinStepLogical. The optimization still runs once and is preserved.