Repository navigation
Support SIMILAR TO pattern matching predicate - #101514
Conversation
|
Workflow [PR], commit [2d1b654] Summary: ✅
AI ReviewSummaryThis PR adds Final Verdict
LLVM Coverage ReportMeasured on commit 2d1b654.
Changed lines: Changed C/C++ lines covered: 562/595 (94.45%) · Uncovered code |
@clickhouse-gh's s suggestion seems off? Lookahead would be an overkill here for a thin layer of translating SQL metacharacters into RE2. We just need to figure out "maybe it's a class expression" or not. Also, bracket expressions don't nest in SQL standards and RE2. The translation layer shouldn't do too much here... Added test cases to show the suggested bug doesn't exist: |
|
The Stress test (arm_msan) failure is fixed by #101239, which should be merged first. After it is merged, please update the branch to include the fix. |
The docs claimed that `^` is always a literal character in a `SIMILAR TO` pattern, but a leading `^` inside a bracket expression still negates the class (as `04051_similar_to` already tests). Qualify the statement in `docs/reference/operators/index.mdx` and mirror it in the `similarTo` and `notSimilarTo` function documentation.
…lar-to # Conflicts: # docs/reference/operators/index.mdx # src/Parsers/ExpressionListParsers.cpp
…lar-to
Two conflicts, both in `Common/likePatternToRegexp.{h,cpp}`, where `master`
added `likePatternHasUnknownBackslashEscape` for the text/bloom-filter text
indexes right next to this PR's `similarToPatternWithCustomEscapeToSimilarToPattern`.
Resolved as a union of both declarations and definitions.
Also fixed two things that auto-merged without a textual conflict:
- `master` added a new 3-argument `Regexps::createRegexp` call site in
`ITokenizer.cpp` (this PR widens the template to 4 arguments); made it
4-argument.
- `introduced_in` for `similarTo` / `notSimilarTo` bumped `{26, 8}` -> `{26, 9}`
to match `master`'s `VERSION_MINOR`.
Restored the `contrib/llvm-project` and `contrib/musl` gitlinks to `master`'s
values - an earlier criss-cross merge had reverted them on the branch.
…lar-to Conflict in `src/Interpreters/ITokenizer.cpp`: `master` reworked `SplitByRegexpTokenizer` to build its `OptimizedRegularExpression` directly instead of going through `Regexps::createRegexp`, so the 4-argument `createRegexp` adaptation this branch had applied there is no longer needed - took `master`'s version wholesale.
…ualify the `ESCAPE` prose
Two unresolved review threads:
1. The \`SIMILAR TO\` additions to \`docs/reference/statements/select/where.mdx\`
sat inside the file's \`AUTOGENERATED_START\`/\`AUTOGENERATED_END\` region, so
\`check_autogenerated_regions\` rejected them. That region is a verbatim copy
of the \`.description\` raw string of \`factory.registerStatement("WHERE", ...)\`
in \`src/Parsers/ParserSelectQuery.cpp\`, so the content now lives there and the
generated page is left untouched for the docs autogeneration job to refresh.
2. The \`ESCAPE\` prose claimed that an escape followed by an ordinary character is
always an error. That holds only for a *custom* escape character: the default
backslash keeps the legacy \`LIKE\` behaviour and denotes a literal backslash, as
\`04051_similar_to.sql\` pins (\`SELECT '\\\\mabc' SIMILAR TO '%\\\\mabc%'\` returns 1).
Qualified in \`src/Functions/similarTo.cpp\` and \`docs/reference/operators/index.mdx\`.
…lar-to # Conflicts: # src/Storages/MergeTree/MergeTreeIndexConditionText.cpp
… backslash in `SIMILAR TO`
`similarToPatternWithCustomEscapeToSimilarToPattern` still accepted the
custom escape character before `^`, `$`, `.` and `\`, silently rewriting
`a#.b` (under `ESCAPE '#'`) to the same meaning as `a.b`. The documented
`SIMILAR TO` contract describes these characters as ordinary literals
outside bracket expressions and says that escaping an ordinary character
is an error, so the validator now accepts only the characters that are
special in the `SIMILAR TO` grammar (`%`, `_`, `|`, `*`, `+`, `?`, `{`,
`}`, `(`, `)`, `[`, `]`) and the escape character itself, consistent with
`LIKE ... ESCAPE`.
Adds focused regressions for `a#.b`, `a#^b`, `a#$b` and `a#\b`, and
spells the rule out in the `similarTo` documentation and the operators
reference.
Co-Authored-By: Claude Fable 5.1 <[email protected]>
…lar-to Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…lar-to # Conflicts: # src/Common/QueryFuzzer.cpp # src/Functions/Regexps.h
…cs tables Inside a Markdown table the `|` has to be written as `\|`, which readers can mistake for a ClickHouse string escape. Use examples that need no escaping instead. Co-Authored-By: Claude Opus 5.5 <[email protected]>
alexey-milovidov
left a comment
There was a problem hiding this comment.
The implementation is straightforward. Thanks!
Master (ClickHouse#101514) added a similar_to template parameter to createRegexp, so the two LIKE-pattern calls of this branch no longer compiled after the master merge. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Master added the pattern bypass cache (ClickHouse#113572), which conflicted in ProfileEvents.cpp and MergeTreeIndexText.cpp; both sides' events are kept. Its key does not hold text_index_like_max_matched_tokens, which can decide the bypass for hasTokenPrefix, hasTokenLike and hasTokenMatch, so a query with those patterns skips the cache. Master (ClickHouse#101514) also added a similar_to parameter to createRegexp; the branch's calls pass it. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Support
SIMILAR TOpattern matching predicateDocumentation entry for user-facing changes
SQL standard has SIMILAR TO predicate that combines SQL LIKE wildcards (%, _) with regular expression metacharacters
|, *, +, ?, [...], {m,n}, (...). Note that excluded are^, $, ..It is supported by PostgreSQL and others. See the PostgreSQL documentation on pattern matching for reference.
I also added: negation and substring pattern shortcut. Syntax highlighting is also hooked in to highlight the metacharacters.
Parameters:
None.
Example use:
[[:a]]is invalid, but up to RE2 to process.Addresses #99608.
Note
Medium Risk
Adds new SQL operator/function surface area and a new pattern-to-regexp translator, which can affect query parsing and string filtering semantics; risk is moderate due to edge cases in escaping/bracket handling and regex compilation paths.
Overview
Adds SQL-standard
SIMILAR TOpattern matching (andNOT SIMILAR TO) implemented via newsimilarToPatternToRegexp()conversion and newsimilarTo/notSimilarTofunctions wired intoMatchImpl/Regexps::createRegexp.Extends the parser to recognize and pretty-print
SIMILAR TO/NOT SIMILAR TO, updates CNF NOT-pushing inversions, and adds syntax-highlighting support for SIMILAR TO metacharacters.Updates call sites to the new
Regexps::createRegexp<like, similar_to, ...>template signature and expands query fuzzer swap sets plus a new stateless test suite covering SIMILAR TO behavior and corner cases.Reviewed by Cursor Bugbot for commit e17d9fd. Bugbot is set up for automated code reviews on this repo. Configure here.
Workflow [PR]
Sync PR [sync-upstream/pr/101514]
Version info
26.10.1.1492-master(included in26.10and later)