Skip to content

Decline a text-index lookup that cannot cover a padded needle - #118972

Merged
alexey-milovidov merged 7 commits into
masterfrom
text-index-padded-needle-118669
Sep 22, 2026
Merged

alexey-milovidov merged 7 commits into
masterfrom
text-index-padded-needle-118669

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Sep 9, 2026 •

Copy link
Copy Markdown
Member

A FixedString needle compared with equals or IN drops the trailing zero bytes of both sides, so it matches a String value spelled with any number of trailing zero bytes, and a term-preserving tokenizer (array, or splitByString with a separator that does not contain the zero byte) stores every spelling as a different term. #117314 strips the needle's padding before extracting the search terms, which fixed the plain row (#117021) and moved the loss onto the row whose own value carries literal zeros: the index looked up a term that granule never stored and pruned it.

truth (use_skip_indexes = 0): [1,3]
with the index:               [3]     <- row 1 silently lost

The needle is now spelled the way the index stores a value that compares equal to it, decided on the concrete tokenizer instance with its pre- and postprocessor rather than on the tokenizer kind:

  • String column, equals or IN: the stripped needle is tokenized next to its padded spellings, and stripping is sound when its terms survive in them, since equals only requires all needle terms to be present in the granule. splitByString(['\0']), splitByNonAlpha and ngrams keep pruning; array and splitByString([',']) cannot cover the spellings with one lookup and decline the atom, only when the needle really carries padding, so ordinary needles keep their pruning.
  • String column, hasAny or hasAll: these cast the needle to String, which drops its padding, and compare the value as it is, so only the stripped spelling matches and one lookup always covers it.
  • FixedString(N) column: the values and their terms are stored padded to N while the comparison strips both sides, so the only matching value is the stripped needle re-padded to N. Before this change the needle was looked up as typed, and a plain String needle or a FixedString needle of another width silently dropped the matching rows (s = 'hello' on a FixedString(6) column with the array tokenizer returned nothing). A needle longer than the column declines the index.

The IN set path follows the same rule. The test asserts pruning versus declining with force_data_skipping_indices.

Closes: #118669
Related: #117314

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

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

Fixed a text index dropping matching rows when a FixedString is involved in the comparison: a term-preserving tokenizer over a String column lost the rows whose value carries literal trailing zero bytes when the search constant is a FixedString, and an index over a FixedString column lost all matching rows for a String constant or a FixedString constant of another width.

🤖 Generated with Claude Code


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

Version info

  • Merged into: 26.10.1.459-master (included in 26.10 and later)
  • Backported to: 26.9.12.5, 26.8.19.7

A `FixedString` needle compares equal to a `String` value both with and
without its trailing zero bytes, and a term-preserving tokenizer
(`array`, or `splitByString` with a separator that does not contain the
zero byte) stores those two spellings as two different terms. #117314
strips the needle's padding before extracting the search terms, which
fixed the plain row (#117021) and moved the loss onto the row whose own
value carries literal zeros: the index looked up a term that granule
never stored and pruned it, so the query returned fewer rows than the
same query with `use_skip_indexes = 0`.

No single lookup covers both spellings, so a needle that really carries
padding now leaves the index unusable for that atom, which is what the
`FixedString`-column case already did. The decision becomes explicit:
strip for a tokenizer that treats the zero byte as a separator, keep the
padding for a `FixedString` column whose terms carry it too, and decline
for the term-preserving-over-`String` combination - and only when the
needle actually has padding to strip, so ordinary needles keep their
pruning. The `IN`-set path, which strips per element, follows the same
rule.
@clickhouse-gh

clickhouse-gh Bot commented Sep 9, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [a7877f0]

Summary: ✅


AI Review

Summary

This PR fixes FixedString padding normalization in text-index lookups across equals, IN, array/map predicates, and FixedString-indexed columns. The core FixedString(N) re-padding path looks right, but the latest simplification regressed the earlier tokenizer-instance logic: safe parameterized tokenizers such as splitByString(['\0']) now always decline padded FixedString needles, and the new regression test was updated to expect that fallback, so I cannot approve the patch in its current form.

Missing context / blind spots
  • ⚠️ The full PR matrix was still pending while I reviewed this on 2026-09-22 (Build (arm_tidy) and the overall PR report were not complete), so I could not use finished functional CI beyond reading the added stateless test.
Findings

⚠️ Majors

  • [src/Storages/MergeTree/MergeTreeIndexConditionText.cpp:1096-1101, 1246-1251, 2173-2188] The padding-normalization decision is now keyed only on tokenizer->getType() plus has_preprocessor, which is too coarse for parameterized tokenizers. A supported index like tokenizer = splitByString(['\0']) on a String column removes the zero padding before term extraction, so s = toFixedString('hello', 10) and the matching IN set case should still prune; with the current enum-only check they both decline instead. That violates the PR's own contract of deciding from the concrete tokenizer/pre/postprocessor instance and silently drops text-index pruning on an otherwise supported configuration. Suggested fix: restore an instance-level capability check (or expose one on ITokenizer) for both the scalar and IN paths instead of collapsing every SplitByString/SplitByRegexp variant into one bucket.
Tests
  • ⚠️ [tests/queries/0_stateless/05223_text_index_fixed_string_padded_needle.sql:88-103] The added regression test currently expects INDEX_NOT_USED for splitByString(['\0']), so it locks in the conservative fallback instead of detecting the parameterized-tokenizer regression. Once the capability check is fixed, this case should require successful pruning with force_data_skipping_indices.
Final Verdict

Changes requested.

LLVM Coverage Report

Measured on commit a7877f0.

Metric Baseline Current Δ
Lines 88.10% 88.10% +0.00%
Functions 91.70% 91.70% +0.00%
Branches 79.40% 79.30% -0.10%

Changed lines: Changed C/C++ lines covered: 104/110 (94.55%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Sep 9, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-text-index Full-text search: inverted/text index in MergeTree (tokenizers, posting lists, GIN-style index bu... label Sep 9, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 9, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing a7877f0df with master e96f004b9 (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

31 translation units recompiled, 304 s compile time in total, 31 of them have a recent master baseline.

Job report

alexey-milovidov and others added 2 commits September 17, 2026 07:01
…es a matching value

The padding decision was keyed on `ITokenizer::Type`, which is too coarse for
parameterized tokenizers: `splitByString(['\0'])` tolerates the padding while
`splitByString([','])` does not, and both fell into one bucket. The decision
now runs on the concrete tokenizer instance with its pre- and postprocessor:
the stripped needle is tokenized next to its padded spellings (one trailing
zero byte and the padding it really carries), and stripping is sound when the
terms of the stripped needle survive in the padded spellings, since `equals`
only requires all needle terms to be present in the granule. This keeps
`ngrams` pruning too, whose grams of a prefix are grams of the whole.

Probing the comparison semantics along the way corrected two assumptions:

- `equals` and `IN` with a `FixedString` on either side drop the trailing
  zeros of both sides, so a `String` value spelled with any number of trailing
  zero bytes matches, not just two spellings. `hasAny` and `hasAll` instead
  cast the needle to `String`, which drops its padding, and compare the value
  as it is, so only the stripped spelling matches and one lookup always
  covers it; they no longer decline.

- A `FixedString(N)` indexed column stores every value padded to N, and so do
  its terms, but the comparison strips both sides, so the only value that
  compares equal to a needle is the needle without its own padding re-padded
  to N. The previous code kept the needle as typed, which looked up a term
  the index never stored for a plain `String` needle or a `FixedString` needle
  of another width and silently dropped the matching rows:

      FixedString(6) column, tokenizer = array
      s = 'hello'                      truth [1,3]   with the index []
      s = toFixedString('hello', 10)   truth [1,3]   with the index []

  The needle is now re-padded to the column width, and a needle longer than
  the column declines the index. The `IN` set path follows the same rule and
  derives the element type from the set column.

The regression test now builds the index on the `Array(String)` column so the
`hasAny` and `hasAll` predicates really reach it, asserts pruning versus
declining with `force_data_skipping_indices`, and covers `splitByString` with
and without the zero byte as a separator, the `FixedString` column with
`String`, same-width, wider and too-long needles, and `Array(FixedString)`.
Renumbered to 05223 since 05167 was taken on master.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@rschu1ze

Copy link
Copy Markdown
Member

@ahmadov Could you review this PR please?

- Add FixedStringPaddingSemantics and FixedStringNeedleContext data
  structures to make the implementation simpler.
- Rename functions to match ClickHouse naming style.
We might need to modify the internal data of `FixedString`,
however for other cases we can avoid copy.
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Sep 22, 2026
Merged via the queue into master with commit f6f6dd7 Sep 22, 2026
176 checks passed
@alexey-milovidov
alexey-milovidov deleted the text-index-padded-needle-118669 branch September 22, 2026 21:32
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 22, 2026
@clickgapai

Copy link
Copy Markdown
Contributor

@alexey-milovidov @ahmadov — ClickGap found the following in this PR:

Close anything that's wrong or already addressed.

@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Oct 5, 2026
robot-clickhouse-ci-1 added a commit that referenced this pull request Oct 6, 2026
Cherry pick #118972 to 26.8: Decline a text-index lookup that cannot cover a padded needle
robot-clickhouse added a commit that referenced this pull request Oct 6, 2026
robot-ch-test-poll2 added a commit that referenced this pull request Oct 6, 2026
Cherry pick #118972 to 26.9: Decline a text-index lookup that cannot cover a padded needle
robot-clickhouse added a commit that referenced this pull request Oct 6, 2026
@robot-clickhouse robot-clickhouse added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Oct 6, 2026
clickhouse-gh Bot added a commit that referenced this pull request Oct 6, 2026
Backport #118972 to 26.8: Decline a text-index lookup that cannot cover a padded needle
clickhouse-gh Bot added a commit that referenced this pull request Oct 6, 2026
Backport #118972 to 26.9: Decline a text-index lookup that cannot cover a padded needle
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Oct 7, 2026
Master now carries its own index-domain conversion for the bloom filter text
index (`convertConstantToIndexDomain` plus `const_source_type` on the `mapKeys`
redirects) and a rewritten `FixedString` needle normalization for the `text`
index (ClickHouse#118972). Both supersede parts of this branch, so this merge keeps
master's code and re-applies only the cases that still fail on it:

- `normalizeConstantForIndexDomain` now wraps master's converter and only adds
  the width of a `FixedString` column (trim, then re-pad to N) and the decline
  for a widthless column whose trimmed constant ends inside a UTF-8 sequence.
  It is applied to `has`, `mapContainsKey`, `mapContains`, `mapContainsValue`,
  `hasAny` and `hasAll`, which master leaves unnormalized, and to `equals` /
  `notEquals` in place of the bare conversion. The `IPv6` text parser and the
  redirected-map-key flag are dropped: master's conversion handles both.
- The `text` index replaces its type-only `tokenizerSplitsAtZeroByte` with the
  shared per-value `tokensSurviveTrailingNuls`, so `ngrams` and `sparseGrams`
  decline a stripped needle that ends mid-sequence (both the direct and the
  `IN` path go through `tryNormalizeNeedlePadding`). `asciiCJK` and `icu`
  keep master's answer: asciiCJK drops a sequence cut by the buffer end and
  icu emits word tokens only, measured identical to master.
- The padding helpers stay in the anonymous namespace, as on master, since the
  `text` index no longer has its own copy.

The test moves to the free 05332 prefix, loses the three IPv6 spelling rows
that only exercised master's parser, and gains a `text` `IN` row. On the
master snapshot 29 of its 59 cells differ; all pass on this branch.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-text-index Full-text search: inverted/text index in MergeTree (tokenizers, posting lists, GIN-style index bu... pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-bugfix Pull request with bugfix, not backported by default pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-synced-to-cloud The PR is synced to the cloud repo v26.8-must-backport

Projects

None yet

7 participants