Repository navigation
Fix lazy final text index direct read - #106894
Conversation
192ac1e to
155ce7d
Compare
|
Workflow [PR], commit [7549a0c] Summary: ✅
AI ReviewSummaryThe PR fixes wrong results for PR Metadata💡 The Fixed wrong results when querying a `ReplacingMergeTree` table with `FINAL` and a filter on a text index while `query_plan_optimize_lazy_final` was enabled. The lazy `FINAL` optimization built reading steps that did not reproduce the direct read from the text index, so the filter dropped all matching rows.
Closes: https://github.com/ClickHouse/ClickHouse/issues/106809Final VerdictStatus: ✅ Approve Only non-blocking cleanup: remove the changelog placeholder and use the full |
155ce7d to
69d7a00
Compare
There was a problem hiding this comment.
Pull request overview
Fixes a correctness bug in the lazy FINAL query-plan optimization where direct reads from a text index were not reproduced in synthetic reading steps, causing filters rewritten to __text_index_* virtual columns to drop all matching rows.
Changes:
- Forward text-index
IndexReadTasksfrom the originalReadFromMergeTreestep into the synthetic non-FINALstep created by lazyFINAL, so__text_index_*virtual columns are produced consistently. - Disable lazy-
FINALsplitting for intersecting-parts handling when direct text-index read tasks are present (fallback to regularFINAL), avoiding an incompatible execution path. - Add a stateless regression test (with reference output) covering both single-part and mixed intersecting/non-intersecting part scenarios.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| tests/queries/0_stateless/02346_text_index_bug106809.sql | Adds regression coverage for lazy FINAL + text-index direct read correctness. |
| tests/queries/0_stateless/02346_text_index_bug106809.reference | Expected outputs for the new regression test. |
| src/Processors/QueryPlan/ReadFromMergeTree.h | Exposes get/set accessors for text-index IndexReadTasks so other plan steps can reproduce direct index reads. |
| src/Processors/QueryPlan/Optimizations/optimizeLazyFinal.cpp | Copies text-index read tasks into synthetic reads and disables lazy FINAL split when tasks are present for intersecting-part handling. |
…-index-direct-read
| -- virtual column the rewritten filter relies on. With the optimization enabled the | ||
| -- counts below must stay correct. | ||
|
|
||
| SET allow_experimental_full_text_index = 1; |
| -- counts below must stay correct. | ||
|
|
||
| SET allow_experimental_full_text_index = 1; | ||
| SET query_plan_optimize_lazy_final = 1; |
There was a problem hiding this comment.
Also not needed, the actual and the expected test results must match regardless of the setting.
There was a problem hiding this comment.
To trigger the issue we need to ensure that both optimizations take place. Otherwise the issue don't happen
|
|
||
| SET allow_experimental_full_text_index = 1; | ||
| SET query_plan_optimize_lazy_final = 1; | ||
| SET max_rows_for_lazy_final = 10000000; |
There was a problem hiding this comment.
Why do we need this setting and the next?
|
|
||
| DROP TABLE IF EXISTS tab; | ||
|
|
||
| SELECT 'Single part (non-intersecting): the synthetic read must still use the direct read'; |
There was a problem hiding this comment.
Readers are not able to parse this sentence, let's rewrite.
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered by tests: 15/15 (100.00%) | Lost baseline coverage: none · Uncovered code |
|
Found via ClickGap automated review. Please close or comment if this is incorrect or needs adjustment. TL;DRFix lazy final text index direct read ClickGap verified: reproduced it with the script below on ReproductionBisect
Affects
Suggested next stepThe bug reproduces as far back as the oldest tested release ( Component: cc fix PR author: @Ergus ClickGap · Finding: Triage metadata
|
Backport #106894 to 26.6: Fix lazy final text index direct read
Cherry pick #106894 to 26.4: Fix lazy final text index direct read
Backport #106894 to 26.4: Fix lazy final text index direct read
ClickHouse#106894 fixed lazy FINAL dropping rows when a query reads directly from a text index. Its test uses ReplacingMergeTree(version) with no is_deleted column, so it never exercises addIsDeletedFilter alongside the copied setIndexReadTasks. Of the 12 stateless tests that set query_plan_optimize_lazy_final, only this one has a text index and only 04092 / 04207 have an is_deleted column, so that intersection was untested. Append a third section covering it: one merged non-intersecting part, a replacement deletion, and assertions that the tombstoned row is dropped through WHERE and PREWHERE. optimizeLazyFinal.cpp:321 bails out of lazy FINAL entirely when index read tasks exist and any part is intersecting, so the combination is reachable only from an all-non-intersecting part set; that is why the fixture merges into a single part and why the mixed-parts shape is not covered here (04092 already covers it). One assertion checks that the plan really took that shape, because the value assertions alone also pass through a regular FINAL read. It names the EXPLAIN options it depends on, since a bare EXPLAIN takes them from the session setting explain_query_plan_default and the token it matches is only printed by the pretty renderer. Three settings are pinned in the file's existing top-level block. use_skip_indexes_if_final is randomized and at 0 disables skip indexes under FINAL, so the existing query_plan_direct_read_from_text_index pin was already defeated in roughly half of all runs; use_skip_indexes_if_final_exact_mode must stay in sync with it, since the pair (1, 0) returns deliberately approximate results under FINAL and CI never generates that pair on its own; enable_analyzer is required because lazy FINAL is gated on the new analyzer. Expected values of the existing sections are unchanged. min_bytes_for_wide_part and index_granularity are pinned to their defaults on the new table only, so that the three rows stay in a single granule: three randomized MergeTree settings together write a Wide part with one row per granule, and a randomized index_granularity of 1 or 2 splits the rows the same way. The text index then selects a granule subset and lazy FINAL declines. Extends the existing test instead of adding a separate one, as requested on ClickHouse#109705
…ndex-direct-read Fix lazy final text index direct read
…oint sorting key `trySplitNonIntersectingParts` returns "not replaced" for a primary key the parts splitter cannot compare (`isSafePrimaryKey`: `Nullable`, `Float*`, `BFloat16`, `Variant`, `Dynamic`, `JSON`, also nested), for mixed `ASC`/`DESC` key columns, and when a full non-intersecting replacement selects nothing. All three return before the checks that keep a direct read from a text index (ClickHouse#106894) and a query that stops reading early (ClickHouse#110576) away from the set-building plan, so `optimizeLazyFinal` built that plan anyway. Its read inherits the `__text_index_*` column name from `getAllColumnNames` but not the index read tasks that produce it: the filter drops rows, and when the `WHERE` repeats the `PREWHERE` predicate, `set_plan.optimize` rewrites the copy to the same name and `createReadTasksForTextIndex` throws `Column __text_index_... already added for reading`. Apply the same two conditions right after the split. On every path that reaches the new check with either condition true the reading step is still untouched, so the query keeps the regular `FINAL` read, as it already does for a comparable key. The in-split checks stay, as they must run before the partial split narrows the reading step. With both conditions false the control flow is unchanged. On 26.8.10.6, `SELECT count() FROM tab FINAL WHERE str = 'ccc'` with only `query_plan_optimize_lazy_final = 1`, over a `ReplacingMergeTree` with a `Nullable(UInt64)` sorting key and a text index on `str`, returns 0 instead of 1. Found by `AST fuzzer (amd_debug, targeted, old_compatibility)` (STID 4792-5e4c) on ClickHouse#122492: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=122492&sha=f6b330abde632fb2c271eb66e098d4230b4a7004&name_0=PR&name_1=AST%20fuzzer%20%28amd_debug%2C%20targeted%2C%20old_compatibility%29 Tests: `02346_text_index_bug106809` gains a `Nullable`-key section (its `PREWHERE` + `WHERE` row is 1 because `PREWHERE` is evaluated before `FINAL`), and `04539_lazy_final_read_in_order_limit_disabled` gains `Float64`-key rows for both gates plus a positive control. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed wrong results when querying a
ReplacingMergeTreetable withFINALand a filter on a text index whilequery_plan_optimize_lazy_finalwas enabled. The lazyFINALoptimization built reading steps that did not reproduce the direct read from the text index, so the filter dropped all matching rows.Fixes: #106809
Version info
26.7.1.357(included in26.7and later)26.6.2.32,26.4.5.143