Skip to content

Add tokensForLikePattern function - #97872

Merged
george-larionov merged 8 commits into
ClickHouse:masterfrom
amosbird:tokens-for-like-pattern
Mar 12, 2026
Merged

george-larionov merged 8 commits into
ClickHouse:masterfrom
amosbird:tokens-for-like-pattern

Conversation

@amosbird

@amosbird amosbird commented Feb 24, 2026 •

Copy link
Copy Markdown
Collaborator

Changelog category (leave one):

  • Improvement

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

Introduce tokensForLikePattern SQL function that tokenizes LIKE patterns while respecting wildcard semantics:

  • % and _ are treated as wildcards
  • Escaped wildcards (%, _) are treated as literals
  • Tokens adjacent to unescaped wildcards are discarded

The function is primarily intended for debugging and testing tokenization behavior for LIKE patterns. Part of #90268.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Note

Medium Risk
Adds a new SQL function and execution path for LIKE-aware tokenization, plus tweaks tokenizer behavior around separators/wildcards, which could affect pattern token extraction and related debugging workflows.

Overview
Introduces a new SQL function, tokensForLikePattern, that tokenizes LIKE pattern strings while respecting wildcard/escape semantics, primarily for debugging/testing tokenization behavior.

Refactors the existing tokens implementation to share a templated execution pipeline with the new mode, and enforces that only tokenizers advertising supportsStringLike() can be used (otherwise BAD_ARGUMENTS). Updates SplitByNonAlphaTokenizer::nextInStringLike to more clearly discard tokens adjacent to unescaped wildcards and to consume separators consistently. Adds docs and stateless tests covering behavior across tokenizers and unsupported cases, and updates the aspell ignore list.

Written by Cursor Bugbot for commit 710e282. This will update automatically on new commits. Configure here.

Version info

  • Merged into: 26.3.1.618

@clickhouse-gh

clickhouse-gh Bot commented Feb 24, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [710e282]

Summary: ❌

job_name test_name status info comment
Stateless tests (amd_debug, sequential) failure
03742_apply_row_policy_after_final FAIL cidb IGNORED
AST fuzzer (amd_debug, targeted) error IGNORED
AST fuzzer (amd_debug, targeted, old_compatibility) error IGNORED

AI Review

1) Summary

This PR adds tokensForLikePattern, reuses the existing tokens implementation via templated traits, and wires LIKE-aware token extraction through tokenizer-specific nextInStringLike paths with coverage in a new stateless test. I did not find blocker- or major-level correctness, safety, concurrency, or performance issues in the changed code; the SplitByNonAlphaTokenizer::nextInStringLike adjustments are consistent with the intended LIKE semantics.

2) Missing context (if any)

  • Full CI run is still in progress (several checks are pending), so final green status and full logs are not yet available.
  • No dedicated performance benchmark artifacts were provided for this change.

3) Findings (by severity)

No Blockers or Majors found.

4) Tests & Evidence

  • The PR adds tests/queries/0_stateless/03403_function_tokens_for_like_pattern.sql with reference output, covering splitByNonAlpha, ngrams, wildcard/escape behavior, empty/wildcard-only inputs, and rejection of unsupported tokenizers (splitByString, array) with BAD_ARGUMENTS.
  • Negative/error-path coverage is present for unsupported tokenizer modes.
  • Optional follow-up (not blocking): add one focused case for sparseGrams in tokensForLikePattern to lock behavior for escaped wildcard handling and ensure future refactors preserve current semantics.

5) ClickHouse Compliance Checklist (Yes/No + short note)

  • Data deletions logged? Yes (N/A) — no deletion logic changed.
  • Serialization formats versioned? Yes (N/A) — no serialization format changes.
  • Experimental setting gate present? Yes (N/A) — no new risky feature flag surface requiring rollout gating.
  • Settings exposed for constants/thresholds? Yes (N/A) — no new operational thresholds introduced.
  • Backward compatibility preserved? Yes — existing tokens behavior remains; new function is additive.
  • SettingsHistory.cpp updated for new/changed settings? Yes (N/A) — no settings changes.
  • Existing tests untouched (only additions)? Yes — only new stateless test files added.
  • Docs/user-facing notes updated? Yes — text index docs now mention tokensForLikePattern.
  • Core-area change got extra scrutiny? Yes — tokenizer behavior and function execution path reviewed.

6) Performance & Safety Notes

  • The template refactor keeps runtime behavior close to existing tokens; no obvious new hot-path allocations beyond expected token materialization into ColumnString.
  • Thread-safety handling for SparseGrams tokenizer cloning remains intact.
  • No new resource-lifetime or cleanup risks were identified in the diff.

7) User-Lens Review

  • The feature is intuitive for debugging LIKE tokenization and aligns with existing tokenizer concepts.
  • Error handling for unsupported tokenizers is explicit and actionable (BAD_ARGUMENTS with tokenizer name).

8) Final Verdict

  • Status: Approve

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Feb 24, 2026
@UnamedRus

UnamedRus commented Feb 24, 2026 •

Copy link
Copy Markdown
Contributor

Could it be used as tokenizer function for bf indexes lookup?

#68985

@amosbird

Copy link
Copy Markdown
Collaborator Author

Could it be used as tokenizer function for bf indexes lookup?

It's likely irrelevant. I’m not clear on the motivation TBH.

Introduce tokensForLikePattern SQL function that tokenizes LIKE patterns
while respecting wildcard semantics:
- % and _ are treated as wildcards
- Escaped wildcards (\%, \_) are treated as literals
- Tokens adjacent to unescaped wildcards are discarded

The function is primarily intended for debugging and testing tokenization
behavior for LIKE patterns.
@amosbird
amosbird force-pushed the tokens-for-like-pattern branch from 2962aac to e5fbdfd Compare February 24, 2026 17:13
Comment thread src/Interpreters/ITokenizer.cpp Resolved
@rschu1ze

Copy link
Copy Markdown
Member

@george-larionov may want to check (he's out at the moment).

@UnamedRus

Copy link
Copy Markdown
Contributor

I’m not clear on the motivation TBH.

TLDR, current tokenizer used for BF index lookup is too aggressive and it remove tokens even if they have escaped %, _ characters.
Which is exactly what your tokenizer is intended to preserve.

@amosbird

Copy link
Copy Markdown
Collaborator Author

Which is exactly what your tokenizer is intended to preserve.

But this PR doesn't provide a tokenizer. It just exports the existing nextInStringLike function.

@UnamedRus

Copy link
Copy Markdown
Contributor

But this PR doesn't provide a tokenizer

I see, but as i see you did implemented fix proposed in #68985, which should cover this particular issue?

@amosbird

amosbird commented Mar 2, 2026

Copy link
Copy Markdown
Collaborator Author

tokensForLikePattern is mainly a debugging tool for cases where LIKE patterns don't behave as expected with skip indexes. While working on it, I did find and fix a minor bug in nextInStringLike (here) — when returning a completed token at a non-alphanumeric separator, pos wasn't advanced, which caused tokens to be skipped in escaped-underscore patterns like \_not\_an\_example. This matches the second bug described in #68985 (by @FrankChen021).

That said, the original report in #68985 (unescaped _ not extracting tokens) is actually correct behavior — _ is a LIKE wildcard, so adjacent tokens should be discarded. This PR doesn't change that.

More broadly, this PR doesn't introduce a new tokenizer — it just exposes the existing nextInStringLike implementations as a SQL function for debugging and testing. Existing tokenizer implementations may still have other edge cases; the intention is to make those easier to discover.

@UnamedRus

Copy link
Copy Markdown
Contributor

That said, the original report in #68985 (unescaped _ not extracting tokens) is actually correct behavior — _ is a LIKE wildcard, so adjacent tokens should be discarded. This PR doesn't change that.

This is expected and fine.

when returning a completed token at a non-alphanumeric separator, pos wasn't advanced, which caused tokens to be skipped in escaped-underscore patterns like _not_an_example. T

Good, this is what was need.

@amosbird

amosbird commented Mar 2, 2026

Copy link
Copy Markdown
Collaborator Author

@george-larionov Could you help take another look?

Copilot AI left a comment

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.

Pull request overview

This PR introduces a new SQL function tokensForLikePattern to tokenize LIKE pattern strings while respecting wildcard/escape semantics, primarily for debugging/testing tokenization behavior, and adds a stateless test to validate expected outputs across tokenizers.

Changes:

  • Add tokensForLikePattern alongside tokens, including validation that the chosen tokenizer supports LIKE-pattern tokenization.
  • Extend ITokenizer with accessors for internal/external tokenizer names, and adjust LIKE tokenization behavior in SplitByNonAlphaTokenizer.
  • Add a stateless SQL test + reference output for tokensForLikePattern.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/Functions/tokens.cpp Implements and registers tokensForLikePattern, adds support-check/error for unsupported tokenizers, and adds function documentation.
src/Interpreters/ITokenizer.h Extends tokenizer interface with name accessors and implements them via ITokenizerHelper.
src/Interpreters/ITokenizer.cpp Fixes SplitByNonAlphaTokenizer::nextInStringLike to advance past delimiters when returning a token.
tests/queries/0_stateless/03814_tokens_for_like_pattern.sql Adds stateless coverage for LIKE-pattern tokenization across default + ngrams tokenizers and unsupported-tokenizer errors.
tests/queries/0_stateless/03814_tokens_for_like_pattern.reference Adds expected output for the new stateless test.

Comment thread src/Functions/tokens.cpp Resolved
Comment thread src/Interpreters/ITokenizer.cpp Resolved
Comment thread src/Interpreters/ITokenizer.cpp Resolved

@george-larionov george-larionov left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM, but I want to give @ahmadov a chance to take a look since he is working in the same area. Also, you will need to pull in the latest changes from your other PR.

@amosbird

amosbird commented Mar 4, 2026

Copy link
Copy Markdown
Collaborator Author

@george-larionov "CH Inc sync — tests failed" Is this relevant?

@ahmadov ahmadov left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM! I think this function is helpful for debugging purposes 👍

Comment thread src/Functions/tokens.cpp Resolved
@george-larionov

Copy link
Copy Markdown
Member

@george-larionov "CH Inc sync — tests failed" Is this relevant?

Only some fuzzer tests failed, which are unrelated to your changes, if you can address @ahmadov's comments I will merge.

@rschu1ze rschu1ze changed the title Add tokensForLikePattern function Add tokensForLikePattern function Mar 11, 2026
Comment thread src/Functions/tokens.cpp Resolved
Comment thread src/Functions/tokens.cpp Resolved
…nd unify tests

Replace template<bool for_like_pattern> with TokensTraits structs
(PlainTokensTraits, LikePatternTokensTraits) following the pattern
from hasAnyAllTokens.h.

Rename test from 03814_tokens_for_like_pattern to
03403_function_tokens_for_like_pattern to match the existing
03403_function_tokens numbering.

Unify test inputs so both tokens() and tokensForLikePattern() receive
the same string, making it easy to compare how each function tokenizes
the same input differently.
@amosbird

Copy link
Copy Markdown
Collaborator Author

Thanks @george-larionov, @ahmadov, and @rschu1ze for the reviews! I've addressed all the feedback.

@rschu1ze

rschu1ze commented Mar 11, 2026 •

Copy link
Copy Markdown
Member

You didn't pass the final boss yet ...

grafik

@clickhouse-gh

clickhouse-gh Bot commented Mar 11, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 83.80% 83.80% +0.00%
Functions 23.90% 23.90% +0.00%
Branches 76.30% 76.40% +0.10%

PR changed lines: PR changed-lines coverage: 98.95% (94/95)
Diff coverage report
Uncovered code

@george-larionov
george-larionov added this pull request to the merge queue Mar 12, 2026
Merged via the queue into ClickHouse:master with commit dd054d4 Mar 12, 2026
157 of 160 checks passed
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Mar 12, 2026
@amosbird amosbird mentioned this pull request Mar 13, 2026
1 task done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-improvement Pull request with some product 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.

7 participants