Repository navigation
Fix lazy FINAL over a text index read on a Nullable, floating-point or mixed-direction sorting key - #122534
Conversation
…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]>
…ex_bug106809` Such a key reaches a separate early return of `trySplitNonIntersectingParts`, which the `Nullable`-key section does not cover. Both new rows fail on master: a `LOGICAL_ERROR` for the `PREWHERE` + `WHERE` row, 0 instead of 1 for the `WHERE`-only row. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…2346_text_index_bug106809` The section's two rows check results only, so they would also pass if the text index stopped being read directly and the section no longer reached the fixed path. Add the `EXPLAIN` check that the `Nullable`-key section already has. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…39_lazy_final_read_in_order_limit_disabled` A sorting key mixing `ASC` and `DESC` columns takes its own early return in `trySplitNonIntersectingParts`. Its `02346` section always reads the text index directly, so it did not show that lazy `FINAL` still applies to such a key, or that a small `LIMIT` keeps its early exit there. Add the float-key rows' positive control and small `LIMIT` row for it. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 4 findings; bounded: 1 full pass + 3 fix rounds + 3 delta rechecks, the last of which found nothing new).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-9:20260927-141300 |
|
Workflow [PR], commit [f2de874] Summary: ✅
AI ReviewSummaryThis PR fixes the lazy Final VerdictStatus: ✅ Approve LLVM Coverage ReportMeasured on commit f2de874.
Changed lines: Changed C/C++ lines covered: 7/7 (100.00%) · Uncovered code |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units7 translation units recompiled, 14 s compile time in total, 7 of them have a recent master baseline. |
|
📊 Cloud Performance Report ✅ AI verdict: The PR adds one early-return guard in optimizeLazyFinal and new stateless tests. The guard skips lazy FINAL for queries that stop reading early or read the text index directly, and it only runs on FINAL reads of ReplacingMergeTree-style tables. The one flagged change, ClickBench Q23 at -45.7% (94 ms to 51 ms), is a plain query on the hits table with no FINAL, so it never reaches the changed code; it is downgraded to not_sure. CPU time dropped just as sharply, so something did change between the two builds, most likely other differences between the PR build and the 26.10.1.39897 master baseline rather than this change. clickbenchFlagged queries (1 of 43)
Change = percent below ×2; the ratio of medians (×N faster/slower) beyond, where percent understates the scale. q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on. tpch_adapted_1_official🟢 No significant changes Debug info
|
Related: #106809
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed wrong results and a
LOGICAL_ERROR(Column ... already added for reading) for aFINALquery withquery_plan_optimize_lazy_final = 1that reads atextindex directly, when theReplacingMergeTreesorting key isNullable, floating-point, or mixesASCandDESCcolumns.Description
On 26.8.10.6, with only
query_plan_optimize_lazy_final = 1set,SELECT count() FROM tab FINAL WHERE str = 'ccc'over aReplacingMergeTreewith aNullable(UInt64)sorting key and a text index onstrreturns 0 instead of 1, and... FINAL PREWHERE str = 'bbb' WHERE str = 'bbb'fails withLOGICAL_ERROR. Found byAST fuzzer (amd_debug, targeted, old_compatibility)on #122492 (report, job, STID 4792-5e4c; 6 PRs in 30 days).Root cause: for a key the parts splitter cannot compare and for mixed sort directions,
trySplitNonIntersectingPartsreturns "not replaced" before the checks that keep a text index read (#106894) and a query that stops reading early (#110576) away from the set-building plan. That plan reads the__text_index_*column without the read tasks that produce it, so rows are dropped, or aWHERErepeating thePREWHEREregisters the same column again and throws.Fix: apply the same two conditions right after the split. Such reads get the regular
FINALread, as with a comparable key, and a smallLIMITregains its early exit (LIMIT 10over 4M rows: 3.2M selected rows before, 0.69M after). LazyFINALstill applies to these keys otherwise. The same site covers a third return, a full replacement selecting nothing, which no text index read reached in my probes. My open #112526 re-checks the same conditions at two of the returns inside a larger change.Validation:
02346gainsNullable-key and mixedASC/DESC-key sections,04539early-exit rows and a positive control for a float and a mixed-direction key. Both fail on master and pass 50 randomized runs each; dropping either condition fails exactly one of them.Float32,BFloat16andLowCardinality(Nullable(String))keys and themerge(),remote(),cluster()andReplicatedReplacingMergeTreeroutes also fail on master and pass with the fix.Reproducer
🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/122534]
Version info
26.10.1.1000-master(included in26.10and later)