Repository navigation
Fix LOGICAL_ERROR on a lazy FINAL text index read with different PREWHERE and WHERE predicates - #121041
Fix LOGICAL_ERROR on a lazy FINAL text index read with different PREWHERE and WHERE predicates#121041groeneai wants to merge 9 commits into
Conversation
…HERE and WHERE predicates A FINAL query with query_plan_optimize_lazy_final = 1 that reads a text index directly and carries two different text search predicates on the indexed column, one in PREWHERE and one in WHERE, aborted a debug build with Logical error: 'Query builder not found for text search query with function 'equals'' and failed the query in a release build. For non-intersecting parts, optimizeLazyFinal builds its own reading step. That step copies the original step's text index read tasks, which is what makes the rewritten filter's virtual column available at all (issue ClickHouse#106809), but it then registers only the WHERE FilterStep before applyFilters(), so it re-derives its skip index conditions from that one filter. The granule its analysis publishes in read_hints.index_granules therefore carries a TextIndexAnalyzer holding query builders only for the WHERE predicate, while MergeTreeReaderTextIndex resolves its virtual columns against the inherited condition template and so looks up the PREWHERE predicate's search query. TextIndexAnalyzer::getQueryBuilder rightly refuses a hash it never registered. read_hints.index_granules is keyed by index name only, which is how a granule crosses a step boundary that does not preserve the granule/condition pairing. Two identical predicates hash to one search query and cannot expose this, which is why only different ones do, and why the function name in the message is whichever query the reader happened to look up. Accept a precomputed granule only when its analyzer covers the search queries the reader resolves, otherwise read the granule from the reader's own condition. The check is at the granule's first use rather than at installation, so one check covers both ways a foreign granule reaches this reader: the constructor parameter and setPrecomputedGranule under use_skip_indexes_on_data_read = 1. A granule the reader reads itself covers its queries by construction, and whenever the analysis condition holds a superset of them, which is the ordinary case, the granule is reused exactly as before. The LOGICAL_ERROR is deliberately left in place: this removes the violation, not the check. Two other routes into the same message are already handled and are untouched here: shipping a direct text index read to parallel replicas (fixed by e0eccd2) and partition key constant folding (ClickHouse#110770). Found by AST fuzzer (amd_tsan) on PR ClickHouse#119839, which mutated the regression test of issue ClickHouse#106809 by adding the second predicate.
The test left two claims of the fix unmeasured. use_skip_indexes_on_data_read defaults to 1, so every row of the first group inherited the data-read transport (setPrecomputedGranule) that the third group already pins explicitly, and the constructor transport (IMergeTreeReader.cpp, fed from read_hints.index_granules) was never exercised. Pin the setting to 0 in the session block, so the two groups now cover one transport each and no assertion depends on a default. The constructor transport is defective too: the same query aborts on an unfixed binary with the setting pinned to 0. Every row drove a reader holding exactly one search query, where all_of and any_of are the same function, so the predicate's quantifier had no oracle. A PREWHERE with two text search predicates, one of which is also the WHERE predicate, produces a reader whose two queries the synthetic step's analyzer covers only one of. Add that row and its ground-truth control; both return 1, so a fix that stopped the abort by dropping rows would fail them. Weakening all_of to any_of aborts on that row alone, whether prewhere read steps are split or not.
The check at the granule's first use runs at most once per reader object, and reader objects outlive read tasks. When the next task is for the same part, MergeTreeReadPoolBase::createTask moves the readers over with previous_task->releaseReaders() and only refreshes their mark ranges, then the new task's initializeReadersChain calls setPrecomputedGranule on that already initialized reader. is_initialized is never reset, so from the second task of a part onward the reader kept the granule that call installed, and buildPostingsForQuery reached getQueryBuilder with the other step's analyzer again. Any part that yields more than one read task was still affected, which is the ordinary case for a part of any size. Apply the same predicate at installation, so an incompatible granule is never installed in the first place and the reader keeps the one it read for itself. The first-use check stays: it covers the constructor transport, where nothing re-installs. Resetting is_initialized instead would also stop the abort, but it would re-run classifyVirtualColumns and the stream initialization on every task of every multi-task part, including the ordinary case where the granule is compatible. A compatible granule is still installed unchanged, and no extra granule read appears on that path. The regression test gains a second fixture, one part of 8-row marks wide enough for the read pool to cut many tasks from it. Two settings the existing rows relied on defaults for are now pinned per row: a row limit with read_overflow_mode = throw disables reading the index on data read outright, and the stateless test profile sets one, so the rows that name that route did not take it; and max_threads above 1 is what selects the read pool that splits a part at all, since the in-order pool puts every mark range in a single task.
The two value queries of the reuse group pinned task sizing but inherited allow_prefetched_read_pool_for_remote_filesystem, which defaults to true. MergeTreePrefetchedReadPool::createTask builds a task from its own readers future whenever it has one, so it never consults previous_task and the same-part reader reuse that the installation-time check exists for does not happen. The expected output is the same either way, so the group passed while covering nothing on the flavors that select that pool: builds whose parts are all on remote storage, since the local sibling setting defaults to false, and not debug builds, where the test runner already forces the remote one off. Pin it off on those two rows only. The controls next to them keep running on whatever pool CI picks. The comment above the group also gave a false reason for one of the pins: merge_tree_min_rows_for_concurrent_read is randomized nowhere. It is pinned because its default is far above this part's row count, so without it the part yields a single read task and the coverage disappears with the test still green. Give each pinned setting its own reason instead.
Dropped the two-line rationale at setPrecomputedGranule and shortened the readRows note to one invariant line.
Internal second-model reviewIndependent passes over this diff (an internal reviewer and a second model, both without the implementation notes), then adjudication against the recorded measurements.
Checked and found sound: the guard fires on exactly the set of states that aborted before, because the per-column |
|
Workflow [PR], commit [c273288] Summary: ✅
AI ReviewSummaryThe PR fixes a Final VerdictStatus: ✅ Approve. |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units13 translation units recompiled, 91 s compile time in total, 13 of them have a recent master baseline. |
CI finish ledger - 163385aEvery failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
Session id: cron:our-pr-ci-monitor:20260919-213105 |
…ndex-foreign-granule-lazy-final # Conflicts: # src/Storages/MergeTree/MergeTreeReaderTextIndex.h
CI finish ledger - c3f85b4Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task whose fixing-PR link will be posted here when it opens. Only Every test that ran on this head passed (0 failed tests in CIDB). The red comes only from jobs that never ran, and from jobs that needed the release builds.
Session id: cron:our-pr-ci-monitor:20261007-180301 |
…ndex-foreign-granule-lazy-final
The fixture asserts that `tab_reuse` ends up as one part of at least 512 marks, so the queries below really get several read tasks. A compact part computes its granularity per written block and folds a short tail into the previous mark, so when the OPTIMIZE merge writes blocks of a randomized, small `merge_max_block_size` the merged part can get fewer marks. CI drew `merge_max_block_size = 265` with a compact part and `optimize_on_insert = 0` (Stateless tests (amd_tsan, s3 storage, targeted), head c9a76d4), and the merge produced 510 marks (511 with the final one), so the precondition returned 0. Pin the merge block at its default next to the existing `index_granularity` pin, so the merged part has 8-row marks under any draw. Replaying that run's exact settings, the old file fails 20/20 and the pinned one passes 20/20. Co-Authored-By: Claude Opus 5.5 <[email protected]>
CI finish ledger - a5fb123Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
CIDB has no other Session id: cron:our-pr-ci-monitor:20261022-083003 |
|
Fixing PR for the |
Related: #113664
Related: #106809
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed a
LOGICAL_ERROR(Query builder not found for text search query with function ...) for aFINALquery withquery_plan_optimize_lazy_final = 1that reads atextindex directly and carries different text search predicates inPREWHEREandWHERE.Description
Found by
AST fuzzer (amd_tsan)on PR #119839 (report, STID 3694-54bc): it mutated02346_text_index_bug106809.sqlby adding a second, differentPREWHEREpredicate. A debug build aborts; a release build fails the query.Root cause: for non-intersecting parts,
optimizeLazyFinalbuilds its own reading step, copying the original step's text index read tasks but re-deriving its skip index conditions from theWHEREfilter alone. Its granule therefore carries query builders only for theWHEREpredicate, while the reader looks up the inheritedPREWHEREone, andTextIndexAnalyzer::getQueryBuilderrefuses a hash it never registered. Identical predicates hash the same, so only different ones expose it. Third route into that message: #113664 was the shipped parallel-replicas fragment, fixed by not distributing such a read.Fix: accept a granule only when its analyzer covers this reader's own search queries, else read the granule from the reader's own condition. Both arrival points need it: the granule's first use (constructor transport), and
setPrecomputedGranule(use_skip_indexes_on_data_read = 1), which a read pool calls again for every further task of the same part, after initialization. TheLOGICAL_ERRORstays: this removes the violation, not the check. The synthetic step still analyzes theWHEREfilter alone, so it prunes fewer granules: a pre-existing missed optimization, left alone.Validation: the new test aborts a debug
masterserver, aborts again with only the first-use check, and passes here 50/50 under randomized settings. Forcing the predicate totruerestores the abort. 232*text_index*/*lazy_final*tests: 227 pass, 4 skip, 1 fails (02346_text_index_hits, taggedstateful, dies atINSERT ... FROM test.hitsbefore any index read).Reproducer
Workflow [PR]
Sync PR [sync-upstream/pr/121041]