Repository navigation
Description
Wrong result: NOT hasToken over a Nullable(String) column with a text index returns the NULL rows — direct read from text index loses NULL semantics
Summary
With a text skip index on a Nullable(String) column, a query filtering by
NOT hasToken(str, '...') returns the rows where str IS NULL. Correct SQL
semantics: hasToken(NULL, '...') is NULL, NOT NULL is NULL, and a
NULL predicate filters the row out — which is exactly what happens when the
index is not used.
The culprit is the direct-read optimization
(query_plan_direct_read_from_text_index, enabled by default): it evaluates
hasToken from the index posting lists alone, where a NULL row simply has no
tokens, so the function result is materialized as 0 instead of NULL.
Negation then turns that 0 into 1. The same wrong result is expected for
the other text-search functions evaluated via direct read (e.g.
hasAllTokens/hasAnyTokens) under negation or any null-sensitive transform
(isNull(hasToken(...)) composed inside larger expressions, comparisons with
<=>, etc.).
Found by the AST-fuzzer correctness-oracle suite from PR
#99980: the TLP WHERE oracle
partitioned a seed from the text-index stateless tests into
p / NOT p / p IS NULL and the NULL rows showed up in two partitions
(69 mismatches in one fuzz batch).
Minimal repro
CREATE TABLE tab
(
id UInt32,
str Nullable(String),
INDEX idx(str) TYPE text(tokenizer = 'splitByNonAlpha')
)
ENGINE = MergeTree ORDER BY id;
INSERT INTO tab VALUES (1, 'hello world'), (2, NULL), (3, 'foo bar'), (4, NULL), (5, 'hello there');
SELECT id FROM tab WHERE NOT hasToken(str, 'hello');
-- default settings: 2 3 4 (WRONG: NULL rows 2 and 4 returned)
-- SETTINGS query_plan_direct_read_from_text_index=0: 3 (correct)
-- SETTINGS use_skip_indexes=0: 3 (correct)
Reproduced on 26.6.1.1 (master 76c2edca2fd, release build) with
clickhouse local. Not dependent on compatibility (the fuzzer hit happened
to run with --compatibility=25.8, but the repro fires with defaults).
TLP view of the same inconsistency (rows 2 and 4 satisfy two disjoint
partitions simultaneously):
SELECT id FROM tab WHERE hasToken(str, 'hello') -- 1 5
UNION ALL
SELECT id FROM tab WHERE NOT hasToken(str, 'hello') -- 2 3 4 (wrong)
UNION ALL
SELECT id FROM tab WHERE isNull(hasToken(str, 'hello')); -- 2 4
-- 7 rows total; the table has 5.
Fix area
The direct-read-from-text-index planner optimization (the code behind
query_plan_direct_read_from_text_index,
src/Storages/MergeTree/MergeTreeIndexText* /
src/Processors/QueryPlan/Optimizations/ direct-read rewrite): when the
indexed column is Nullable, the function result column reconstructed from
posting lists must carry the null mask of the source column (NULL row →
NULL result, not 0). Alternatively, the rewrite must be disabled for
Nullable columns when the text-search function appears under a
null-sensitive parent (NOT, isNull, <=>, ...) — but propagating the
null mask is the correct general fix.
Original fuzzer hit (for reference)
2026-06-09 22:24:52, fuzz-lab interim fleet instance 14 (TSan build),
run dir tmp/fuzz_lab/runs/20260609-2141-76c2edca2fd/inst/14/
(mismatch_artifacts/run_1/ holds the full client log + seed shard).
TLP WHERE oracle: reference SELECT id FROM tab = 5 rows, partitioned
union = 7 rows, Only in partitioned: 2, 4.
Activity
qoega commentedon Jun 10, 2026
@groeneai could you please reproduce and fix this one? Repro is in the issue body — deterministic on master
76c2edca2fd, default settings.groeneai commentedon Jun 10, 2026
Reproduced on master and fixed in #107078.
Root cause:
hasToken/hasAllTokens/hasAnyTokensuse theExactdirect-read mode, which replaces the function with a non-NullableUInt8virtual column built from posting-list membership. A NULL row has no tokens, so it materializes as0(indistinguishable from a non-matching row); the later cast toNullable(UInt8)only adds an all-false null map, soNOT 0 = 1keeps the NULL row. The text index stores no per-row null map (NULL rows are skipped at build time), so it can't be reconstructed from the index alone.Fix: for a
Nullable/LowCardinality(Nullable)indexed column, demoteExacttoNoneso the real function is evaluated per row (granule pruning still applies). Added regression test02346_text_index_bug106922coveringNullable(String), all-NULL parts,LowCardinality(Nullable), preprocessor, and PREWHERE.qoega commentedon Jun 12, 2026
Update: the same direct-read-from-text-index defect produces oracle mismatches via
match,like, andhasPhrase— not onlyhasToken. Observed on the02346_text_index_partially_materializedtables (tab_fully/tab_partially), so the fix should also cover partially-materialized text indexes (ngramstokenizer in addition tosplitByNonAlpha).Suggested scope:
hasToken,match,like,hasPhrase(and likelysearchAny/searchAll)0for rows with no tokens (incl. NULL), so a negated predicate flips it to1and selects those rows.groeneai commentedon Jun 12, 2026
@qoega Confirmed and fixed for the expanded scope. You were right: the original fix only covered the
Exactdirect-read mode (hasToken/hasAnyTokens/hasAllTokens), butmatch/like/hasPhrase/startsWith/endsWithuse theHintmode, which buildsand(virtual_column, real_func). For a NULL row that isand(0, NULL) = 0, so a negated predicate flips it to1and leaks the row, exactly as you observed.PR #107078 now demotes any non-
Nonedirect-read mode (Exact and Hint) toNonefor aNullable/LowCardinality(Nullable)haystack, so the real function is evaluated per row (granule pruning still applies).Before/after on a
Nullable(String)index (NULL rows are 2 and 4; reference isquery_plan_direct_read_from_text_index=0):NOT hasPhrase(str,'hello world')2,3,4,53,5NOT endsWith(str,'world')2,3,4,53,5NOT (str LIKE '%world')2,3,4,53,5NOT match(str,'hello')33Verified on both
splitByNonAlphaandngrams(3), and on a partially-materializedngramsindex (matching thetab_fully/tab_partiallyshape). The regression test02346_text_index_bug106922now covers all of these and fails on a binary without the fix.Note: standalone
searchAny/searchAllare not functions in this build (only themultiSearch*/multiMatch*family), and those route throughNonemode already, so they were never affected. If you have an oracle repro that still mismatches after this update, please share the exact query + table definition and I'll dig in.Fix text index direct read losing NULL semantics for Nullable columns
Fix text index direct read losing NULL semantics for Nullable columns
Fix text index direct read losing NULL semantics for Nullable columns
qoega commentedon Jul 13, 2026
Reconfirmed still-open on today's master (fresh master + 1095 commits, tag
54200dd4a43, 2026-07-13). Minimal repro from the original issue still returns[2,3,4](leaks NULL rows 2 and 4) withquery_plan_direct_read_from_text_index=1(the default), vs[3]with it off. So the fix expansion to cover Nullable direct-read is still needed. No urgency, just flagging it survived several master merges.Fix text index direct read losing NULL semantics for Nullable columns
Fix text index direct read losing NULL semantics for Nullable columns
Fix text index direct read losing NULL semantics for Nullable columns
Fix text index direct read losing NULL semantics for Nullable columns
Fix text index direct read losing NULL semantics for Nullable columns
Fix text index direct read losing NULL semantics for Nullable columns
Fix text index direct read losing NULL semantics for Nullable columns
Fix text index direct read losing NULL semantics for Nullable columns
Fix text index direct read losing NULL semantics for Nullable columns
Fix text index direct read losing NULL semantics for Nullable columns
Fix text index direct read losing NULL semantics for Nullable columns
Add `v3_with_defined` text index format to make `hasToken` on nullabl…