Skip to content

Add a regression test for NOT NOT over a text index direct read - #120836

Merged
alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:groeneai/pin-not-not-has-phrase-text-index-direct-read
Sep 19, 2026
Merged

alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:groeneai/pin-not-not-has-phrase-text-index-direct-read

Conversation

@groeneai

@groeneai groeneai commented Sep 18, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #118321
Related: #116381

Changelog category (leave one):

  • CI Fix or Improvement (changelog entry is not required)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Description

Test only, no src/ change.

AST fuzzer (amd_release, oracle) on #118321 reported an Identity WHERE (NOT(NOT p)) mismatch seeded from 02346_text_index_partially_materialized.sql: hasPhrase(text, 'ello50 wor') returned 30 rows while NOT (NOT hasPhrase(text, 'ello50 wor')) returned 0.

Root cause: over a text index declared support_phrase_search = 0, hasPhrase gets the Hint direct-read mode, the only mode that keeps the original predicate beside the index virtual column, so the index tokenizer reaches the row-level function only as an injected third argument. Index analysis used to cancel a pair of not unconditionally, so NOT (NOT hasPhrase(...)) became the rewrite target itself and replaceNodes returned early on it without descending into its children. The bare two-argument call ran instead, and it tokenizes with splitByNonAlpha, under which 'ello50 wor' is not a phrase of 'hello50 world50'.

#116381 already fixed this on master, so nothing changes here. 05043_not_in_value_position_key_condition pins the index-analysis consequence of that fix; the direct-read rewrite had no test of its own.

The test asserts the fuzzer's identity arms (p, NOT (NOT p), p AND 1, p OR 0) and PREWHERE NOT (NOT p) over such an index, a ground-truth arm with the tokenizer spelled out and the index off, and a guard that EXPLAIN actions = 1 contains the __text_index_ virtual column, so no arm can pass vacuously. The randomized settings the mechanism needs are pinned in the test body.

Validation: with the one line of #116381 reverted locally, not not p and prewhere not not p report 0 instead of 10 (20 of 20 randomized runs fail); on master 50 of 50 randomized runs pass.

CI report of the fuzzer run: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=118321&sha=518eff241122633d224affa709900640c1544c07&name_0=PR&name_1=AST%20fuzzer%20%28amd_release%2C%20oracle%29


Workflow [PR]
Sync PR [sync-upstream/pr/120836]

Version info

  • Merged into: 26.10.1.155 (included in 26.10 and later)

`AST fuzzer (amd_release, oracle)` on ClickHouse#118321 reported an `Identity WHERE (NOT(NOT p))` mismatch
seeded from `02346_text_index_partially_materialized.sql`: `hasPhrase(text, 'ello50 wor')` counted
30 rows while `NOT (NOT hasPhrase(text, 'ello50 wor'))` counted 0.

`hasPhrase` with two arguments tokenizes with `splitByNonAlpha`, under which `'ello50 wor'` is not a
phrase of `'hello50 world50'`; the index tokenizer reaches the row-level function only as a third
argument injected by `processTextIndexFunction`. Because the index declares
`support_phrase_search = 0`, `getDirectReadMode` returns `Hint`, the only mode that keeps the
original predicate beside the index virtual column, so that injection is observable. Before ClickHouse#116381,
index analysis cancelled a pair of `not` unconditionally, so `not(not(hasPhrase(...)))` canonicalised
to the bare atom, the outer `not` node became the replacement key, and `replaceNodes` returned early
on it without descending into its children, so the injected three-argument call never reached the
executed predicate. `EXPLAIN actions = 1` shows both sides:

  before  Filter column: __text_index_idx_hasPhrase_5d43... AND NOT NOT hasPhrase(text, 'ello50 wor')
  after   Filter column: NOT NOT (__text_index_idx_hasPhrase_5d43... AND hasPhrase(text, 'ello50 wor', 'ngrams(3)'))

ClickHouse#116381 fixed this on master by blocking only the unsound half of the cancellation, so this commit
adds no source change. `05043_not_in_value_position_key_condition` pins the index-analysis consequence
of that fix; the direct-read rewrite had no test of its own. `hasAnyTokens` and `hasAllTokens` take
the same injected tokenizer argument, but over a plain text index `getDirectReadMode` gives them
`Exact` mode, which drops the original predicate, so this seam is reachable only for a non-positional
`hasPhrase` and no sibling arm is added.

With the one line of ClickHouse#116381 reverted locally, `not not p` and `prewhere not not p` report 0 instead
of 10 (20 of 20 randomized runs fail); on master 50 of 50 randomized runs pass.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=118321&sha=518eff241122633d224affa709900640c1544c07&name_0=PR&name_1=AST%20fuzzer%20%28amd_release%2C%20oracle%29
Related: ClickHouse#118321
Related: ClickHouse#116381

Co-Authored-By: Claude Opus 5 <[email protected]>
@groeneai groeneai added can be tested Allows running workflows for external contributors groeneai-origin-request PR origin: a maintainer pinged or directed groeneai labels Sep 18, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Two rounds. Round 1: review by an independent model (engine: codex, 2 findings) plus a cold review of
the resulting test by a first reviewer (3 findings). Round 2, after the fix round: the same
independent review re-run on the new head, 0 findings, plus a second cold review by a different
reviewer (2 nits, no blocker, no major).

# Sev Finding Verdict Evidence / action
1 ⚠️ "hasToken, hasAllTokens and hasAnyTokens reach the same seam in Hint mode" (test header comment, and the commit message) AGREE, fixed in r1 False on two counts: needApplyTokenizer is exactly {hasAllTokens, hasAnyTokens, hasPhrase} (optimizeDirectReadFromTextIndex.cpp:607), and tokenizerArgumentMatchesIndex rejects hasToken because its third argument is a start position (MergeTreeIndexConditionText.cpp:272); and getDirectReadMode returns Exact, not Hint, for all three over a plain text index (:300), where Exact drops the original predicate instead of keeping it beside the virtual column (optimizeDirectReadFromTextIndex.cpp:1018). Sentence corrected.
2 ⚠️ The PREWHERE arm can pass even if its direct-read path silently stops firing, so it needs its own EXPLAIN assertion DISAGREE The arm pins that the executed predicate is the injected three-argument call, and a lost injection always reports 0 here: the bare two-argument form tokenizes with splitByNonAlpha, under which 'ello50 wor' is not a phrase of 'hello50 world50'. Measured on the pre-fix binary: prewhere not not p reports 0, deterministically and in 20 of 20 randomized runs. In the inject-only path the finding cites, the injected form is still what executes (optimizeDirectReadFromTextIndex.cpp:1198), so a 10 there is a true pass, and pre-fix that path injects nothing at all (processTextIndexFunction returns early for a one-child node, :732). An EXPLAIN arm would pin which clause carries the virtual column, which the code allocates to one clause or the other (:1250) and which this test does not contract.
3 💡 Commit message wrote getDirectReadMode() where the in-tree rulebook asks for f rather than f() (.claude/CLAUDE.md:11) AGREE, fixed in r1 Message reworded.
4 💡 ngrams(3), splitByNonAlpha and Hint were not in inline code blocks in the test comments (.claude/CLAUDE.md:5) AGREE, fixed in r1 Comments reworded.
5 💡 The PR body said "six randomized settings" while only five of the six pinned settings are randomized by the test runner AGREE, fixed in r2 Bare use_skip_indexes is absent from the randomization table in tests/clickhouse-test; the five that are randomized sit at :1817, :1834, :1839, :1893 and :1894, and the test's own trailing comments already marked exactly those five. Body sentence corrected.

Round 2 re-derived every source fact behind the round-1 correction independently, before reading the
recorded evidence, so the fix does not trade one false claim for another. One qualifier is worth
stating because it looks like slack and is not: hasAnyTokens and hasAllTokens do reach Hint
mode, through the mapValues(m) map-element-value shape (MergeTreeIndexConditionText.cpp:1121),
JSONAllValues (:1127) and the map.key_<serialized> subcolumn shape (:1151). They never reach
it over a plain text index on a String column, which is exactly what the corrected sentence says.

Checked and found sound rather than changed: the absent no-parallel-replicas tag, because
mergeTreeReadCanBeShipped refuses to ship a MergeTree read that holds index read tasks
(applyParallelReplicas.cpp:121) and the untagged direct-read tests in this family pass about 3,090
times each in the ParallelReplicas jobs over the last 10 days; the distributed plan flavour
(serialize_query_plan = 1), where the whole 02346_text_index_* family is green; the per-setting
body SET pins, which match every neighbouring test in the family; and the absent no-fasttest tag,
since only 9 of the 106 02346_text_index_* tests carry one. No arm can pass vacuously: every
expected 10 requires the injected three-argument form, so losing the rewrite yields 0 and a loud
failure rather than a quiet pass, and the index read arm additionally pins that __text_index_ is
in the plan.

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are terminal
per finding.

Session id: cron:clickhouse-review-slot-10:20260918-134300

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. With the one line of #116381 reverted locally, 05227_not_not_has_phrase_text_index_direct_read fails on demand: not not p and prewhere not not p report 0 instead of 10. 20 of 20 attempted randomized runs failed, not a rate.
b Root cause explained? Yes. support_phrase_search = 0 gives hasPhrase the Hint direct-read mode, the only mode that keeps the original predicate beside the index virtual column, so the index tokenizer arrives only as an injected third argument. The unconditional not-pair cancellation in index analysis made the outer NOT NOT node the rewrite target, replaceNodes returned early on it without descending, and the bare two-argument call executed. EXPLAIN actions = 1 shows it directly: pre-fix __text_index_… AND NOT NOT hasPhrase(text, 'ello50 wor'), post-fix NOT NOT (__text_index_… AND hasPhrase(text, 'ello50 wor', 'ngrams(3)')).
c Fix matches root cause? Test only, zero src/ change. The fix is #116381, already merged; @ alexey-milovidov asked for the pin rather than a code change. No widened bound, no no-random-* tag, no shrunken dataset, no defensive guard.
d Test intent preserved / new tests added? New test, added with ./tests/queries/0_stateless/add-test as the in-tree rulebook requires. No existing test was modified: not 02346_text_index_partially_materialized.sql (the fuzzer's own seed) and not 05043_not_in_value_position_key_condition.sql (which pins the index-analysis half of #116381).
e Both directions demonstrated? Yes. Pre-fix binary (Build-ID aeb2ca03cbf19e00673a3bd2ccc4a809468efe4f): FAIL with not not p 0 / prewhere not not p 0, other five lines unchanged. Master binary (035565f499d453ccc45b0c584eaa626c4b9e2469): OK, byte-identical to the reference. SELECT lower(buildId()) matched on both servers.
f Fix is general across code paths? Both carriers of the invariant are asserted and both proven to redden: the WHERE filter DAG and the PREWHERE DAG. hasAnyTokens and hasAllTokens take the same injected tokenizer argument, but over a plain text index getDirectReadMode gives them Exact mode, which drops the original predicate, so this seam is reachable only for a non-positional hasPhrase; hasToken never reaches it in any mode, because its third argument is a start position rather than a tokenizer. Exact mode is safe by construction (the original node is dropped for the virtual column alone and the old cancellation was truthiness-preserving), which is why support_phrase_search = 0 is load-bearing in the fixture.
g Fix generalizes across inputs (params/datatypes/wrappers)? The defect is in index-analysis DAG canonicalisation, upstream of column types, so the axis under test is the boolean shape and the clause position: p, NOT (NOT p), p AND 1, p OR 0 in WHERE plus NOT (NOT p) in PREWHERE, against a ground-truth arm with the tokenizer spelled out and the index off. Type wrappers would exercise the text index's type support instead of this fix.
h Backward compatible? Vacuously: no src/ change, so no SettingsChangesHistory.cpp entry, no format version and no setting default moved.
i Invariants and contracts preserved? No production path is touched. The test creates and drops its own single table in the runner's per-run database, so the flaky check's nproc - 1 workers all running this file cannot collide; 50 concurrent runs passed and no no-parallel tag is needed. The index read arm asserts the rewrite actually fired, so the identity arms cannot pass vacuously.

Session id: cron:clickhouse-impl-slot-41:20260918-113200

@clickhouse-gh

clickhouse-gh Bot commented Sep 18, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [559bded]

Summary: ✅


AI Review

Summary

Adds a stateless regression test for NOT (NOT hasPhrase(...)) on a text index in hint-mode direct read, pins the randomized settings the scenario depends on, and checks both result correctness and that the plan still introduces a __text_index_ virtual column. I did not find a correctness, coverage, or PR-metadata issue in the current patch.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-ci label Sep 18, 2026
@groeneai
groeneai requested a review from Ergus September 18, 2026 14:25
@clickhouse-gh clickhouse-gh Bot added the comp-testing Test content only (no production code). label Sep 18, 2026
@alexey-milovidov alexey-milovidov self-assigned this Sep 19, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Sep 19, 2026
Merged via the queue into ClickHouse:master with commit 59577b6 Sep 19, 2026
176 of 177 checks passed
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 19, 2026
@clickhouse-gh clickhouse-gh Bot added comp-text-index Full-text search: inverted/text index in MergeTree (tokenizers, posting lists, GIN-style index bu... and removed comp-testing Test content only (no production code). labels Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-text-index Full-text search: inverted/text index in MergeTree (tokenizers, posting lists, GIN-style index bu... groeneai-origin-request PR origin: a maintainer pinged or directed groeneai pr-ci pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants