Skip to content

Text index: accept arbitrary LIKE/ILIKE patterns for the array tokenizer - #115649

Merged
ahmadov merged 10 commits into
masterfrom
ahmadov/text-index-array-patterns
Sep 10, 2026
Merged

ahmadov merged 10 commits into
masterfrom
ahmadov/text-index-array-patterns

Conversation

@ahmadov

@ahmadov ahmadov commented Aug 20, 2026 •

Copy link
Copy Markdown
Member

The array tokenizer stores the whole value as a single token, so a pattern matches a token exactly when it matches the value. Any pattern is therefore answered by a dictionary scan instead of a full scan. Previously only %needle% with an alphanumeric needle qualified.

Closes #114641.

Changelog category (leave one):

  • Performance Improvement

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

Qualify arbitrary LIKE/ILIKE patterns for dictionary scan when a text index is built via the array tokenizer.


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

@clickhouse-gh

clickhouse-gh Bot commented Aug 20, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [ce14c29]

Summary: ✅


AI Review

Summary

This PR teaches the text-index array tokenizer to answer arbitrary LIKE / ILIKE patterns, including the startsWith / endsWith rewrites, from the index dictionary. The plain LIKE work looks good, but the current ILIKE admission still widens a known unsound lowerUTF8 / upperUTF8 exact-read path to new pattern shapes, so I do not think this is ready to merge unchanged.

Findings

❌ Blockers

  • [src/Storages/MergeTree/MergeTreeIndexConditionText.cpp:1552] The new allow_arbitrary_patterns call now makes prefix/suffix/wildcard-free ILIKE patterns exact on the array tokenizer, but the branch still accepts lowerUTF8 / upperUTF8 via preprocessor->isLowerOrUpper(). #116970 already shows those preprocessors are not semantics-preserving for raw ILIKE (lowerUTF8('abKoop zzz') becomes abkoop zzz, so ILIKE 'abkoo%' is an exact index hit even though the row-level predicate is false). Because this path is Exact, the original predicate is removed and the phantom row becomes the final result.
    Suggested fix: keep only ASCII lower / upper in the exact ILIKE path, or downgrade lowerUTF8 / upperUTF8 to hint mode until #116970 is fixed.

💡 Nits

  • [docs/reference/engines/table-engines/mergetree-family/textindexes.mdx:1413] [dismissed by author -- https://github.com/Text index: accept arbitrary LIKE/ILIKE patterns for the array tokenizer #115649#discussion_r3934821664] The new “any pattern qualifies” sentence is still broader than the implementation: arbitrary patterns are disabled for nullable haystacks, and LIKE still requires no preprocessor or postprocessor. As written, the docs promise direct-read behavior users still will not get.
  • [src/Core/Settings.cpp:8985] [dismissed by author -- https://github.com/Text index: accept arbitrary LIKE/ILIKE patterns for the array tokenizer #115649#discussion_r3951139806] The structured DECLARE text for use_text_index_like_evaluation_by_dictionary_scan still lists only %value%, value%, %value, startsWith, and endsWith. After this PR, the array tokenizer also accelerates wildcard-free and multi-needle patterns, so the autogenerated settings docs will stay stale unless this string is updated too.
Tests
  • ⚠️ Add a focused stateless repro for ILIKE with tokenizer = array plus lowerUTF8 / upperUTF8, using one of the #116970 compatibility characters (K, ſ) and one of the newly admitted affix or wildcard-free patterns. That is the smallest proof that the new exact-read surface does not keep widening the existing phantom-row bug.
Final Verdict

Request changes.

LLVM Coverage Report

Measured on commit ce14c29.

Metric Baseline Current Δ
Lines 88.90% 88.90% +0.00%
Functions 91.70% 91.80% +0.10%
Branches 81.30% 81.30% +0.00%

Changed lines: Changed C/C++ lines covered: 59/59 (100.00%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label Aug 20, 2026
@clickhouse-gh

clickhouse-gh Bot commented Aug 20, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing ce14c2978 with master fe1a91eac (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of 129e4b59b; 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

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

Job report

@EmeraldShift

Copy link
Copy Markdown
Contributor

...[T]he planner still rewrites the common prefix% / %suffix LIKE forms into startsWith / endsWith before this new path can see them

Can the array text index just support these functions? 😉 It should be an efficient index scan, as if the tokens were an ordinary MergeTree table with a KeyCondition. I wonder how much of this infrastructure is just portable to the Text index file structures? That would be really neat!

@mintlify

mintlify Bot commented Aug 24, 2026 •

Copy link
Copy Markdown
Contributor

Preview deployment for your docs. Learn more about Mintlify Previews.

Project Status Preview Updated (UTC)
ClickHouse-docs 🟢 Ready View Preview Aug 24, 2026, 12:03 PM

@ahmadov

ahmadov commented Aug 25, 2026 •

Copy link
Copy Markdown
Member Author

...[T]he planner still rewrites the common prefix% / %suffix LIKE forms into startsWith / endsWith before this new path can see them

Can the array text index just support these functions? 😉 It should be an efficient index scan, as if the tokens were an ordinary MergeTree table with a KeyCondition. I wonder how much of this infrastructure is just portable to the Text index file structures? That would be really neat!

@EmeraldShift, yes that is the end goal of this PR. I'll add support for those functions as well.

@ahmadov

ahmadov commented Aug 26, 2026

Copy link
Copy Markdown
Member Author

depends on #116554 to support startsWith and endsWith functions without additional cost.

@clickhouse-gh clickhouse-gh Bot added the submodule changed At least one submodule changed in this PR. label Sep 4, 2026
The `array` tokenizer stores the whole value as a single token, so a
pattern matches a token exactly when it matches the value. Any pattern is
therefore answered by a dictionary scan instead of a full scan.
Previously only `%needle%` with an alphanumeric needle qualified.

Closes #114641.
@ahmadov
ahmadov force-pushed the ahmadov/text-index-array-patterns branch from f88d83e to f2d625a Compare September 4, 2026 11:38
@ahmadov
ahmadov marked this pull request as ready for review September 4, 2026 11:39
@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 4, 2026
@zlareb1

This comment was marked as low quality.

@clickhouse-gh

clickhouse-gh Bot commented Sep 4, 2026 •

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

✅ AI verdict: no_change — no significant changes across 39 queries analysed

This PR extends the text-index dictionary-scan optimization to accept arbitrary LIKE/ILIKE patterns for the array tokenizer, plus docs and new tests; no general query-execution path is touched. The two flagged ClickBench queries (Q33, Q34) were correctly held as no_change within master's variance band: neither uses a text index, and their CPU time is flat while wall-clock shows a small ~-5% swing consistent with run-to-run noise. The deterministic envelope suppression is sound here. No real PR-attributable regression or improvement is present in these results; the new text_index_like_array performance test is the workload that would actually reflect this change.

clickbench

🟢 No significant changes

tpch_adapted_1_official

🟢 No significant changes

Debug info
  • StressHouse run: eb03b016-da21-48dc-b782-0bd0a3e36388
  • MIRAI run: 8415dce0-1943-4343-ad88-ec0b3e2a97e5
  • PR check IDs:
    • clickbench_55165_1788950280
    • clickbench_55171_1788950280
    • clickbench_55182_1788950280
    • tpch_adapted_1_official_55189_1788950280
    • tpch_adapted_1_official_55222_1788950280
    • tpch_adapted_1_official_55236_1788950280

@zlareb1

This comment was marked as low quality.

@rschu1ze rschu1ze self-assigned this Sep 6, 2026
Comment thread src/Core/Settings.cpp Resolved
@ahmadov ahmadov removed the submodule changed At least one submodule changed in this PR. label Sep 8, 2026
@@ -1449,7 +1549,7 @@ bool MergeTreeIndexConditionText::traverseFunctionNode(
return false;

const auto & like_pattern = value_field.safeGet<String>();
auto patterns = stringLikeToPatterns(value_field, /*case_insensitive=*/ true);
auto patterns = stringLikeToPatterns(value_field, /*case_insensitive=*/ true, /*allow_arbitrary_patterns=*/ affix_patterns_allowed);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This widens the existing ILIKE + UTF-8 case-mapping bug from '%needle%' to the new array-tokenizer shapes. preprocessor->isLowerOrUpper() still admits lowerUTF8 / upperUTF8, but #116970 shows those transforms are not semantics-preserving for raw ILIKE: for example, lowerUTF8('abKoop zzz') becomes abkoop zzz, so s ILIKE 'abkoo%' is an exact index hit even though the row-level predicate is false.

Because the array tokenizer makes this path Exact, the original predicate is removed and the phantom row becomes final. Please either restrict this exact path to ASCII lower / upper, or keep lowerUTF8 / upperUTF8 in hint mode until #116970 is fixed.

@ahmadov
ahmadov added this pull request to the merge queue Sep 9, 2026
@alexey-milovidov
alexey-milovidov removed this pull request from the merge queue due to a manual request Sep 9, 2026
@ahmadov
ahmadov added this pull request to the merge queue Sep 10, 2026
Merged via the queue into master with commit afcf4b1 Sep 10, 2026
175 checks passed
@ahmadov
ahmadov deleted the ahmadov/text-index-array-patterns branch September 10, 2026 09:06
@ahmadov

ahmadov commented Sep 10, 2026

Copy link
Copy Markdown
Member Author

Perf report:
image

@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 10, 2026
jonathan-albrecht-ibm pushed a commit to linux-on-ibm-z/ClickHouse that referenced this pull request Nov 9, 2026
Issue ClickHouse#118197 (duplicate ClickHouse#118037): on a `text(tokenizer = array)` index,
`startsWith`/`endsWith` are answered by a dictionary scan with the LIKE
pattern `needle%` / `%needle` in Exact mode, so the original predicate is
dropped. Before ClickHouse#115649 the needle went into the pattern unescaped, and a
`%` in it acted as a wildcard: `startsWith(tag, 'foobar%')` returned every
row starting with `foobar`. ClickHouse#115649 escapes `%`, `_` and `\` on master,
but no test pinned it: the only such case, `startsWith(name, '100%')` in
02346_text_index_function_like_array_patterns, returns the same rows with
or without the escaping.

The new test covers `%` next to the added wildcard and at the far edge,
`LIKE 'foobar\%%'` (rewritten to `startsWith` by
`optimize_rewrite_like_perfect_affix`), `_` and a trailing backslash, in
both directions. Each indexed query is compared with `use_skip_indexes = 0`,
and EXPLAIN checks that the text index answers it alone.

Validated: matches the reference on master; with `escapeForLikePattern`
removed from the affix branch all nine indexed results change (the last
one throws CANNOT_PARSE_ESCAPE_SEQUENCE); on 26.8.21.12, which has the
ClickHouse#114728 backport without ClickHouse#115649, the five `%` cases return extra rows.

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-performance Pull request with some performance improvements 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.

Text index: support arbitrary LIKE patterns with tokenizer = 'array'

5 participants