Skip to content

Keep the direct read assertion of 02346_text_index_bug106460 off the trivial count plan - #121282

Merged
rschu1ze merged 1 commit into
ClickHouse:masterfrom
groeneai:text-index-bug106460-oracle-off-trivial-count
Oct 1, 2026
Merged

rschu1ze merged 1 commit into
ClickHouse:masterfrom
groeneai:text-index-bug106460-oracle-off-trivial-count

Conversation

@groeneai

@groeneai groeneai commented Sep 21, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #118496
Related: #111494

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):

Pin query_plan_optimize_count_from_text_index = 0 in 02346_text_index_bug106460 so its direct-read assertion no longer depends on whether a background cleanup has already reclaimed the applied patch part.

Description

The -- materialized section asserts that after OPTIMIZE FINAL materializes a pending lightweight
update, the direct read is available again:

SELECT count() > 0 FROM (EXPLAIN actions = 1 SELECT count() FROM tab WHERE hasToken(s, 'tok1'))
WHERE explain LIKE '%\_\_text_index%';

It intermittently reads 0 while the data line above stays correct.

Root cause. The two passes read the pending-patch state differently.
optimizeDirectReadFromTextIndex tests patches per selected part, so after the merge it fires,
emitting the __text_index_* virtual column. optimizeTrivialCountFromTextIndex runs after it and
declines on the table-level hasPatchParts(). OPTIMIZE FINAL applies the patch but leaves the
patch part active until a background cleanup drops it; the count pass then applies, replaces the
ReadFromMergeTree step with ReadFromTextIndexCount, and the artifact the assertion greps for
leaves the plan although the direct read ran. The asserted property still holds. The longer it waits the
likelier it fails, so waiting for the patch to vanish would make the flake permanent.

Change. Pin the trivial-count pass off for this file, as 24 sibling text_index tests do; the
assertion then reads 1 in both cleanup states and stops sampling the cleanup thread.

Validation. Forcing the cleanup makes it deterministic: the current file fails with exactly
the CI diff (-1/+0), the patched file passes. Flipping the pin back reproduces 0; with the
pin kept but the direct read disabled it still reads 0, so the assertion still detects the original
defect; a misspelled setting raises Code: 115. The reference is unchanged; 50 randomized and 50
non-randomized runs are green.

Left alone: the count pass's patch gate is table-level while the direct read's is per-part, which only
declines an optimization and is that owner's call; and an EXPLAIN-grepping assertion is only
as stable as the later passes that may rewrite its step.


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

Version info

  • Merged into: 26.10.1.1184-master (included in 26.10 and later)

…trivial count plan

The `-- materialized` section of this test asserts that once `OPTIMIZE FINAL`
has materialized a pending lightweight update, the text-index direct read is
available again, by grepping `EXPLAIN actions = 1` for the `__text_index_*`
virtual column. That assertion intermittently reads 0 while the data line above
it stays correct.

Two optimizer passes read the pending-patch state through different predicates.
`optimizeDirectReadFromTextIndex` tests patches per selected part, so once the
merge has baked the patch in, the rewrite fires and emits the virtual column.
`optimizeTrivialCountFromTextIndex` runs after it and declines on the
table-level `hasPatchParts()`. `OPTIMIZE FINAL` applies the patch but leaves the
patch part itself active until a background cleanup reclaims it; from that
moment the count pass applies, takes its fully-materialized branch, replaces the
whole `ReadFromMergeTree` step with `ReadFromTextIndexCount` (whose header is a
single `count()` aggregate column), and the virtual column leaves the plan even
though the direct read did run and did read the index. The property the test was
added for holds in both outcomes; only the artifact the assertion looks for
disappears. The pass predates the test by about four weeks, so the assertion was
written against a plan shape the pass could already remove.

The direction matters: the longer the assertion waits, the likelier it fails, so
a fix shaped as "wait until the patch is materialized and gone" would convert a
rare flake into a permanent failure.

Pin the trivial-count pass off for this file, which 24 sibling text-index tests
including the canonical direct-read test already do. The assertion then reads 1
whether or not the patch part has been reclaimed, so it stops sampling the
cleanup thread, and it keeps asserting the direct read specifically rather than
accepting either plan shape. `query_plan_optimize_count_from_text_index` is the
surgical term of the pass's three-way conjunction: pinning
`query_plan_direct_read_from_text_index` would remove the very artifact being
asserted, and `optimize_trivial_count_query` would also disable the unrelated
part-metadata trivial count.

Forcing the cleanup with `SYSTEM STOP CLEANUP` + `SYSTEM START CLEANUP` makes the
failure deterministic: the unpatched file then fails with exactly the CI diff,
and the patched file passes. Flipping the pin back reproduces 0; keeping the pin
while disabling the direct read still reads 0, so the assertion remains a
detector of the defect this test exists for; a misspelled setting name raises
`Code: 115` rather than degrading to a silent no-op. The reference file is
unchanged, every other asserted value in the file is unaffected, and 50 runs with
settings randomization plus 50 without are green.
@groeneai groeneai added can be tested Allows running workflows for external contributors groeneai-origin-oranjeai PR origin: delegated from the OranjeAI sibling fleet labels Sep 21, 2026
@groeneai

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

Pre-publication review by an independent model (engine: codex; 0 findings) plus my own cold
review of the resulting diff (1 nit). Bounded: 1 full pass + 1 fix round + 1 delta recheck.

# Sev Finding Verdict Evidence / action
1 💡 The added comment says a bare count() over a text-index predicate is "answered from the posting lists", but for this test's single-token predicate the count comes from the dictionary cardinality with posting deserialization skipped (ReadFromTextIndexCount.cpp:111,157-162) DISAGREE The wording is inherited verbatim from the in-tree precedent 04365_token_index_lowcardinality_constant.sql:9-11, and the step's own header comment calls the same thing "the text-index posting metadata", which the dictionary cardinality is part of. The clause the comment turns on (the step is replaced, so the virtual column leaves the plan, optimizeTrivialCountFromTextIndex.cpp:434-437) is exact. Left as is for consistency with the sibling tests.

I re-derived the invariant-carrier sweep before reading the implementation's evidence, and checked
what a file-level setting pin usually gets wrong: blast radius (only two queries in this file can
reach the pass, and the other one's asserted value comes from the index on either plan), coverage
(the pass keeps its own test plus value coverage in five files that do not pin the setting), and
vacuity (pin kept, direct read disabled, the assertion still reads 0, so it stays a detector).
Trap for anyone repeating the sweep: a plain grep '__text_index' misses this test, because the
assertion spells the pattern LIKE-escaped.

Alternatives not taken: accepting either plan artifact stops the assertion saying which read
answered; pinning query_plan_direct_read_from_text_index removes the artifact being asserted;
optimize_trivial_count_query also disables the unrelated part-metadata trivial count; keeping the
patch part alive preserves the accidental condition instead of removing the dependence.

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

Session id: cron:clickhouse-review-slot-9:20260921-054900

@clickhouse-gh clickhouse-gh Bot closed this Sep 21, 2026
@clickhouse-gh clickhouse-gh Bot reopened this Sep 21, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. SYSTEM STOP CLEANUP tab; SYSTEM START CLEANUP tab; after OPTIMIZE FINAL (plus a 1 s cleanup_delay_period pin, probe-only) reclaims the applied patch part on demand, and the assertion then reads 0 every time instead of ~5 in 21k runs.
b Root cause explained? Yes. optimizeDirectReadFromTextIndex tests patches per selected part, so the direct read fires after the merge and emits __text_index_*. optimizeTrivialCountFromTextIndex, which runs later, declines on the table-level hasPatchParts(); once the applied patch part is reclaimed it applies, replaces the whole ReadFromMergeTree step with ReadFromTextIndexCount (header: one count() column), and the artifact the assertion greps for leaves the plan although the direct read ran.
c Fix matches root cause? Yes. It removes the assertion's dependence on that later pass, rather than preserving the accidental condition (remove_unused_patch_parts = 0), accepting either artifact, or blanket no-random-*. The carrier (background cleanup) is untouched, and the pass keeps its own 283-line test.
d Test intent preserved / new tests added? Preserved. The assertion still says "after OPTIMIZE FINAL the direct read is available", and with the pin kept but query_plan_direct_read_from_text_index = 0 it still reads 0, so it remains a detector of the original defect rather than an unconditional 1. No source change, so no new regression test is owed; the .reference file is unchanged, so every other assertion in the file is bit-identical.
e Both directions demonstrated? Yes, on the real test file through the real runner in the forced post-GC state: the current file fails with exactly the CI diff (-1/+0), the patched file reports [ OK ]. Also confirmed statement-level (4-arm A/B plus positive and negative controls).
f Fix is general across code paths? Yes. Swept every __text_index EXPLAIN assertion in tests/queries/ and tests/integration/: 29 files carry one, 32 already pin this setting, 18 do not, and exactly one of those is reachable by the pass (a bare trivial count() over an Exact-mode text predicate above a single ReadFromMergeTree) - this test. The other 17 are immune for a stated structural reason and were deliberately not pre-emptively pinned.
g Fix generalizes across inputs (params/datatypes/wrappers)? The input dimension that matters is the cleanup state: with the pin the assertion reads 1 both while the patch part is alive and after it is reclaimed, so it no longer samples the cleanup thread at all. The file's other predicate shapes (hasAnyTokens with a preprocessor, hasToken with a patch pending) were measured unchanged in value. Type-wrapper and boundary matrices are N/A for a test-only setting pin.
h Backward compatible? N/A - test-only. No setting default moves, so no SettingsChangesHistory.cpp entry, no format versioning, no docs.
i Invariants and contracts preserved? Yes. The invariant the file asserts is unchanged; only the plan it is asserted against changes. No production contract is touched, and the assertion's fail-closed property is strengthened: a removed or misspelled setting name raises Code: 115 UNKNOWN_SETTING instead of silently passing, which a comment or a tag would not.

Session id: cron:clickhouse-impl-slot-7:20260921-044300

@clickhouse-gh

clickhouse-gh Bot commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [ce277ee]

Summary: ❌

job_name test_name status info comment
Stress test (amd_tsan) FAIL
Cannot start clickhouse-server FAIL cidb IGNORED
Check failed FAIL cidb IGNORED

AI Review

Summary

This PR disables query_plan_optimize_count_from_text_index in 02346_text_index_bug106460.sql so the file keeps asserting the direct-read plan shape instead of intermittently flipping to ReadFromTextIndexCount after background patch cleanup. I did not find a correctness or coverage regression in the updated test: the file remains focused on direct-read behavior, the dedicated trivial-count optimizer coverage still lives in 02346_text_index_optimize_trivial_count.sql, the pattern matches sibling direct-read tests, and current Praktika is all green.

Final Verdict

✅ No findings. I would not post any inline review comments on the current diff.

@clickhouse-gh clickhouse-gh Bot added the pr-ci label Sep 21, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-testing Test content only (no production code). label Sep 21, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger - ce277ee

Every 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 CH Inc sync is exempt.

Check / test Reason Owner / fixing PR
Stress test (amd_tsan) / Cannot start clickhouse-server (plus its job-level Check failed, the same event) the server refuses to boot because a surviving query-fuzzer table r_prop_table1__fuzz_7 has ENGINE = Merge with a TTL clause on disk, so loading it throws Code: 36 Engine Merge doesn't support TTL clause; this diff is one stateless test file and cannot reach it a fix task is created (investigating at full effort, fixing-PR link to follow here)

50 checks succeeded and 131 were skipped on the live attempt; the 164 cancelled runs belong to a superseded attempt on this same head, and Mergeable Check and PR aggregate over the one failure above.

Session id: cron:our-pr-ci-monitor:20260921-103829

@groeneai

Copy link
Copy Markdown
Collaborator Author

Fixing PR for the single failure in the ledger above (Cannot start clickhouse-server, Code: 36 Engine Merge doesn't support TTL clause): #121335. It stops a TTL-less engine from inheriting a source table per-column TTL, which is the on-disk state that made the query-fuzzer ENGINE = Merge copy unloadable at startup.

@rschu1ze

Copy link
Copy Markdown
Member

Does the test still test what it's supposed to test?

@groeneai

Copy link
Copy Markdown
Collaborator Author

Yes. I ran the file from before and after this PR with clickhouse local on two master CI builds: 10bec7b8 (the last master build before #118496, so without the #106460 fix) and 5b339633 (the #118496 merge).

before this PR after this PR
without the fix fails fails, identical output
with the fix passes passes

Without the fix both versions fail on the same lines, the #106460 symptom: count(), sum(c) with direct read returns 0 0 instead of 100 1, and the id < 25 query returns no rows instead of three.

The setting only disables answering a bare count() from the index. That never happens while an update is pending, so none of the #106460 checks goes through it. In this file only two queries can use it: the -- materialized check, once the applied patch part has been cleaned up (that is the flaky failure), and the first preprocessor count(), which returns 10 either way.

The -- materialized check still fails when it should: with the direct read turned off for that query it reads 0. With the patch part cleanup forced before it, the file before this PR reads 0 there and the file after reads 1.

If you prefer, I can move the setting from the file onto that one EXPLAIN query.

@rschu1ze rschu1ze self-assigned this Oct 1, 2026
@rschu1ze
rschu1ze added this pull request to the merge queue Oct 1, 2026
Merged via the queue into ClickHouse:master with commit d68bca3 Oct 1, 2026
183 of 349 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 Oct 1, 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-oranjeai PR origin: delegated from the OranjeAI sibling fleet 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