Skip to content

Text index: support '<string>%' and '%<string>' patterns for LIKE/ILIKE optimization - #114728

Merged
ahmadov merged 8 commits into
masterfrom
ahmadov/text-index-like-prefix-suffix
Sep 4, 2026
Merged

ahmadov merged 8 commits into
masterfrom
ahmadov/text-index-like-prefix-suffix

Conversation

@ahmadov

@ahmadov ahmadov commented Aug 13, 2026 •

Copy link
Copy Markdown
Member

Currently, text index is leveraged only for a LIKE pattern %<alpha-numerical>% (dictionary scan), other similar patterns (<alpha-numerical>% and %<alpha-numerical>) fall back to the full-table scan.
With this PR, those two patterns (a.k.a. startsWith and endsWith) are supported now. Compare to the pattern %<alpha-numerical>%, the text index is used in HINT mode - it means rows will be filtered out using the text index (dictionary scan) then the original filter condition will be run again to match. So, the query would be re-written internally

from:

SELECT count() FROM tab WHERE col LIKE 'foo%';

to:

SELECT count() FROM tab WHERE __text_index_index_virtual_column AND col LIKE 'foo%';

The virtual column is filled from the posting lists of the tokens the dictionary scan matched.

Fixes #112597.

Changelog category (leave one):

  • Performance Improvement

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

Utilize text index for the supported LIKE/ILIKE patterns.


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

@mintlify

mintlify Bot commented Aug 13, 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 13, 2026, 11:28 PM

@clickhouse-gh

clickhouse-gh Bot commented Aug 17, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [c41a767]

Summary: ✅


AI Review

Summary

This PR extends text-index dictionary-scan optimization to prefix/suffix LIKE and ILIKE patterns, plus the corresponding startsWith / endsWith cases, and it fixes the earlier array-tokenizer and hint-selectivity issues. One correctness blocker still remains: nullable infix LIKE / ILIKE can still take exact direct read, which changes NOT (...) semantics by collapsing NULL to 0.

Findings

❌ Blockers

  • [src/Storages/MergeTree/MergeTreeIndexConditionText.cpp:1409,1444] Exact direct read is still allowed whenever is_infix is true, even if the indexed expression is nullable. In the filter DAG that replaces like / ilike with a UInt8 virtual column, NULL rows become 0, so NOT (tag LIKE '%ClickHouse%') and NOT (tag ILIKE '%clickhouse%') evaluate as true instead of staying NULL. The new regression coverage only checks NOT LIKE / NOT ILIKE, which do not exercise this not(like(...)) / not(ilike(...)) path.
    Suggested fix: require the same non-nullable guard for exact infix patterns before choosing TextIndexDirectReadMode::Exact, or preserve the original predicate as a fallback; then add a focused stateless case for NOT (tag LIKE '%ClickHouse%') / NOT (tag ILIKE '%clickhouse%') on Nullable(String).
Final Verdict

❌ Not ready to merge until nullable infix direct read preserves NULL semantics under NOT (...).

LLVM Coverage Report

Measured on commit c41a767.

Metric Baseline Current Δ
Lines 88.70% 88.70% +0.00%
Functions 92.10% 92.10% +0.00%
Branches 81.00% 81.00% +0.00%

Changed lines: Changed C/C++ lines covered: 152/154 (98.70%) · Uncovered code

Full report · Diff report

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

clickhouse-gh Bot commented Aug 17, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

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

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

Job report

@ahmadov
ahmadov marked this pull request as ready for review August 17, 2026 19:33
@ahmadov
ahmadov marked this pull request as draft August 17, 2026 19:33
@ahmadov
ahmadov force-pushed the ahmadov/text-index-like-prefix-suffix branch from 029ce21 to f196f2c Compare August 17, 2026 22:23
const auto & like_pattern = value_field.safeGet<String>();
auto patterns = stringLikeToPatterns(value_field, /*case_insensitive=*/ false);
const bool is_infix = isInfixPattern(like_pattern);
if (patterns.size() == 1 && (is_infix || 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.

The new nullable guard only covers affix hints. Infix patterns still bypass it here, so Nullable(String) can take the exact virtual-column path for LIKE '%foo%' and ILIKE '%foo%'.

That breaks three-valued logic under NOT: a NULL row has no postings, so the reader fills the UInt8 virtual column with 0, and optimizeDirectReadFromTextIndex then replaces the original predicate with that column alone in exact mode (src/Processors/QueryPlan/Optimizations/optimizeDirectReadFromTextIndex.cpp:791-813). NOT 0 becomes 1, while NOT (NULL LIKE '%foo%') / NOT (NULL ILIKE '%foo%') should stay NULL.

Please block exact pattern direct-read on nullable columns as well, or keep the original predicate/fallback in the plan so NULL can survive as NULL.

@ahmadov
ahmadov marked this pull request as ready for review August 18, 2026 09:15
@ahmadov

ahmadov commented Aug 18, 2026 •

Copy link
Copy Markdown
Member Author

Perf report:

image

@clickhouse-gh

clickhouse-gh Bot commented Aug 18, 2026 •

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

⚠️ AI verdict: not_sure — no significant changes across 38 queries analysed

1 query inconclusive (baseline era shift, segment too thin). K_source=6 K_base=30 flagged=0/65

clickbench

⚠️ 1 inconclusive

Flagged queries (1 of 43)
Query Verdict Baseline median (ms) PR median (ms) Change q-value Hint
⚠️ 18 not_sure 1080 1137 +5.3% <0.0001

Change = percent below ×2; the ratio of medians (×N faster/slower) beyond, where percent understates the scale. q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on.

tpch_adapted_1_official

🟢 No significant changes

Debug info
  • StressHouse run: 2ff3ef8b-54ba-4a1a-9d0b-183afe3f73f2
  • MIRAI run: 70311ba2-e47f-448d-83eb-ce8c341dca67
  • PR check IDs:
    • clickbench_1109841_1788487918
    • clickbench_1109970_1788488028
    • clickbench_1110242_1788488124
    • tpch_adapted_1_official_1110486_1788488334
    • tpch_adapted_1_official_1110599_1788488362
    • tpch_adapted_1_official_1110700_1788488373

…like-prefix-suffix

# Conflicts:
#	docs/reference/engines/table-engines/mergetree-family/textindexes.mdx
Fix ClickHouse spelling in the affix test fixture
Comment thread src/Core/Settings.cpp Resolved
@clickgapai

Copy link
Copy Markdown
Contributor

@ahmadov @rschu1ze — ClickGap found the following in this PR:

Close anything that's wrong or already addressed.

ahmadov added a commit that referenced this pull request Sep 24, 2026
…ng>' patterns for LIKE/ILIKE optimization
ahmadov added a commit that referenced this pull request Sep 24, 2026
…-index-like-affix

Backport #114728 to 26.8: Text index: support '<string>%' and '%<string>' patterns for LIKE/ILIKE optimization
kewin-robetti pushed a commit to viasoftkorp/ClickHouse that referenced this pull request Sep 25, 2026
…nd '%<string>' patterns for LIKE/ILIKE optimization
kewin-robetti pushed a commit to viasoftkorp/ClickHouse that referenced this pull request Sep 25, 2026
…nd '%<string>' patterns for LIKE/ILIKE optimization
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]>

This branch was successfully deployed

1 active deployment
staging - docs — c41a767b Deployed Sep 3, 2026 by mintlify[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

Support prefix/postfix patterns in inverted-index-LIKE-fastpath

4 participants