Repository navigation
Support for LIKE ... ESCAPE ... clause - #99774
Conversation
Implements the SQL-standard ESCAPE clause for LIKE, ILIKE, NOT LIKE, and NOT ILIKE expressions. This allows users to specify a custom escape character for matching literal `%` and `_` wildcards in patterns. Examples: SELECT '10%' LIKE '10|%' ESCAPE '|' -- returns 1 SELECT '50%off' LIKE '50#%off' ESCAPE '#' -- returns 1 The implementation: - Adds ESCAPE keyword to the parser - Parses `expr LIKE pattern ESCAPE char` into a 3-argument `like` function - Rewrites the pattern at execution time from custom escape to standard backslash escapes, so the core matching logic is unchanged - Supports both operator syntax (LIKE ... ESCAPE) and functional syntax (like(haystack, pattern, escape_char)) - Handles round-trip formatting (parse -> format -> parse) Closes #99599 Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
- Parser: clarify pop/push-back logic with explicit `popped` flag - ASTFunction: remove redundant `is_like_operator` variable, reuse `is_like_with_escape` - Error messages: explain what escape sequences are valid after the escape character - FunctionsStringSearch: wrap ESCAPE handling in `if constexpr` to avoid dead code in non-LIKE instantiations; add comment explaining `ImplIsLike` SFINAE trait - Documentation: update `like`, `notLike`, `ilike`, `notILike` function docs with ESCAPE clause description, syntax, and examples - Tests: add coverage for functional syntax, non-constant needle, ILIKE with case difference, empty/multi-char escape errors, subquery, and `formatQuery` round-trip Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
|
Workflow [PR], commit [aa72411] Summary: ✅ AI ReviewSummaryThis PR adds Findings❌ Blockers
Final VerdictStatus: Minimum required actions:
|
This comment was marked as resolved.
This comment was marked as resolved.
|
I am 99% sure that the failure in AST fuzzer (amd_debug, targeted, old_compatibility) is related. |
Two bugs caused the AST fuzzer crash (PODArray assertion in debug build via the format-parse-format round-trip check): 1. Formatter: ASTFunction always formatted 3-arg LIKE as operator syntax (x LIKE y ESCAPE z), even when the 3rd argument was not a string literal (e.g. after fuzzer mutation). The parser only accepts a string literal after ESCAPE, so the round-trip failed. Now only uses operator syntax when the escape argument is a string literal; otherwise falls back to functional syntax like(x, y, z). 2. Parser: When ESCAPE was followed by a non-string-literal, the LIKE operator was popped from the stack and the position advanced past ESCAPE, but neither was restored on failure. Now saves position before consuming ESCAPE and restores both pos and operator stack on any failure in the ESCAPE clause. Made-with: Cursor
|
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. |
* In likePatternWithCustomEscapeToLikePattern, preserve legacy LIKE behavior when the explicit escape character is backslash: unknown escape sequences are kept as literal backslash + next character rather than throwing CANNOT_PARSE_ESCAPE_SEQUENCE. This makes LIKE p and LIKE p ESCAPE '\\' behaviorally equivalent for queries that worked before users added the explicit ESCAPE clause. Other escape characters keep the strict validation. * Enforce that the ESCAPE argument in the functional form is a single ASCII character (high bit clear), matching the documentation. Previously only the byte length was checked, allowing non-ASCII single-byte values to pass. * Replace the partial specialization of ImplIsLike on MatchImpl with a trait detection on Impl::is_like and drop the MatchImpl.h include from FunctionsStringSearch.h, removing transitive compile-time fanout of regex/match machinery into every consumer of the header. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
# Conflicts: # src/Functions/FunctionsStringSearch.h
The previous test used `unhex('FF')` after `ESCAPE`, but the parser
only accepts a string literal there, so the query failed with a
syntax error before reaching the runtime ASCII validation.
Use `'\xff'` instead, which is a single-byte string literal with the
high bit set and exercises the new ASCII check in
`FunctionsStringSearch.h`.
|
This was fixed by #105146. Let's update the branch. |
The previous implementation handled `ESCAPE` only when the immediate top operator was `like` / `ilike` / `notLike` / `notILike`. Patterns that included higher-priority operators (e.g. concatenation in `'ab' LIKE 'a' || 'b' ESCAPE '#'`) left the top operator as something other than a LIKE-family op, so the `ESCAPE` keyword was not consumed and parsing stopped at `ESCAPE`. Fold operators with priority strictly higher than `LIKE` (priority 9) before checking the top, so the corresponding `LIKE` becomes accessible. This is a precedence-correct reduction that `mergeElement` would perform anyway later, so it does not change the semantics of any existing expression. Adds tests covering `LIKE`, `NOT LIKE`, `ILIKE`, and `NOT ILIKE` with `ESCAPE` after concatenation and function-call sub-expressions.
… test In `04040_like_escape.sql`, `'a\b'` is the backspace character in ClickHouse string literals, so the two checks under "Backslash is literal when custom escape is used" did not actually exercise that contract. Switch the payload to `'a\\b'` (a literal backslash) so the regression path is covered. The expected results stay the same: - `'a\\b' LIKE 'a\\b' ESCAPE '|'` — strings are identical → 1 - `'a\\b' LIKE 'a|%b' ESCAPE '|'` — pattern becomes literal `a%b` → 0
LLVM Coverage Report
Changed lines: 89.52% (188/210) | lost baseline coverage: 4 line(s) · Uncovered code |
|
|
||
| -- NOT ILIKE with ESCAPE | ||
| SELECT '10%' NOT ILIKE '10|%' ESCAPE '|'; | ||
|
|
There was a problem hiding this comment.
@groeneai can you add a test case showing ClickHouse's text index is still used with a LIKE ESCAPE clause?
There was a problem hiding this comment.
Filed as #105848.
While writing the test I realized the text index was actually being bypassed for LIKE pattern ESCAPE 'c' queries — MergeTreeIndexConditionText::traverseAtomNode rejected the 3-arg like(col, pattern, escape_char) form before the LIKE handler could run (it had arguments_size != 2 -> return false). Result correctness was always fine because FunctionsStringSearch::executeImpl already normalizes the pattern with likePatternWithCustomEscapeToLikePattern at execution time; only the index path was missing the same fold.
#105848 applies the same fold at index-condition time, and the new test 04277_text_index_function_like_escape.sql uses EXPLAIN indexes = 1 to assert that the index narrows reads (1 part / 1024 granules out of 4 / 4096 for %World%) under both LIKE ... ESCAPE and ILIKE ... ESCAPE. It FAILS on master and PASSES with the fix.
|
Hi @qoega @alexey-milovidov @rschu1ze — while reviewing this PR I found the following:
Happy to discuss — close anything that's wrong or already addressed. |
Support for LIKE ... ESCAPE ... clause
Resolves #99599
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Added ESCAPE syntax that defines a custom escape character in a LIKE pattern, allowing you to treat % and _ as literal characters instead of wildcards.
Documentation entry for user-facing changes
Version info
26.6.1.123