Repository navigation
Support constant haystack with non-constant needle in LIKE/ILIKE/match - #100479
alexey-milovidov merged 30 commits into
Conversation
The `like`, `ilike`, `notLike`, `notILike`, and `match` functions
previously threw an exception when the haystack (first argument) was
a constant and the needle (second argument) came from a column:
SELECT * FROM t WHERE 'foo' ILIKE concat('%', col, '%');
-- Error: doesn't support non-constant needles in constant haystack
This implements the `constantVector` method in `MatchImpl` following
the same logic as the existing `vectorVector` path: per-row pattern
compilation with substring fast-path and regexp cache.
Fixes ClickHouse#84187
|
Workflow [PR], commit [38d0e35] Summary: ✅
AI ReviewSummaryThis PR implements non-constant needle support for constant haystacks in Final VerdictStatus: ✅ Approve |
- Remove unused `ErrorCodes::ILLEGAL_COLUMN` declaration from `MatchImpl.h` (style check failure). - Update `03278_enum_string_functions` test: constant LIKE with `randomString(0)` no longer throws, it returns 0. - Simplify `04053` test reference and remove ambiguous edge cases.
ClickHouse's LIKE treats '%%' differently from '%' — fix the expected output for the empty-string edge case.
…-haystack-nonconstant-needle
|
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 flaky check failure is fixed in #102148, let's update the branch. |
…aystack-nonconstant-needle
`vectorConstant` and `vectorFixedConstant` already short-circuit `needle == '%' || needle == '%%'` (for `LIKE`) and `needle == '.*' || needle == '.*?'` (for `match`) to `!negate` independently of the haystack. `constantVector` was missing the same shortcut, so `'' LIKE '%%'` fell through `likePatternIsSubstring` into a Volnitsky search with an empty required substring on an empty haystack, which returns `haystack_end` and therefore `0` — inconsistent with the column ⇒ const direction and with SQL semantics. Adds the shortcut and updates the reference for `'' LIKE '%%'` from `0` to `1`. Addresses review comment on PR ClickHouse#100479.
…rFixedVector` `constantVector` (and `vectorConstant`/`vectorFixedConstant`) already short-circuit `needle == '%' || needle == '%%'` (for `LIKE`) and `needle == '.*' || needle == '.*?'` (for `match`) to `!negate` independently of the haystack. `vectorVector` and `vectorFixedVector` were missing the same shortcut, so when both the haystack and the needle came from columns and a row had `needle == '%%'`, that row fell through `likePatternIsSubstring` into a Volnitsky search with an empty `required_substr` and returned `0` for an empty haystack — inconsistent with the column ⇒ const direction (which the prior `705aef7d474` fixed) and with SQL semantics. Adds the shortcut and extends `04053_like_constant_haystack_nonconstant_needle` to cover the column-column path for both `String` and `FixedString` haystacks, and the `match` `.*` / `.*?` shortcut across all four argument-shape combinations. Addresses follow-up review comment on PR ClickHouse#100479.
…aystack-nonconstant-needle
The merge brought in updated `.github/workflows/*` and `ci/workflows/*` from master, which the fork-push OAuth token cannot push. Restore those paths to the PR-branch's pre-merge state so the PR keeps its own workflow snapshot and the push succeeds.
These changes to CI workflow / infrastructure files were unrelated to the purpose of this pull request and were introduced accidentally (stale base branch). Restoring them to match `master`.
…aystack-nonconstant-needle
The merge with master pulled in many tests using the 04053 prefix, so the new test is renumbered to the next available number. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…ctor` Master enabled the `cppcoreguidelines-init-variables` clang-tidy check. Initialize these locals to `false` as in the other `MatchImpl` paths. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…aystack-nonconstant-needle
…-pattern shortcut Addresses @antaljanosbenjamin's review on `MatchImpl.h`: - The per-row matching logic was duplicated three times (in `vectorVector`, `vectorFixedVector` and `constantVector`). Extracted it into a single `matchOneRow` helper so the three non-constant-needle paths stay in sync. - `matchOneRow` takes the needle as a `std::string_view` instead of copying it into a `String` on every row; a `String` is only materialized on the slow path when populating the per-thread regexp cache (`getOrSet`). - Generalized the "matches everything" trivial-pattern check: instead of only recognizing `%` and `%%`, the new `impl::likePatternMatchesEverything` treats any non-empty run of `%` (e.g. `%%%`, `%%%%%%`) as matching every haystack. Applied consistently across all argument shapes (`vectorConstant`, `vectorFixedConstant`, and the three non-constant-needle paths). Extended `04309_like_constant_haystack_nonconstant_needle` to cover longer runs of `%` as a needle in both the constant-haystack and column-column directions. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Merged
Verified locally against the freshly built binary: The remaining red checks are unrelated to this PR:
@groeneai, could you take a look at the |
…aystack-nonconstant-needle
…able The `match` function now supports a non-constant needle (pattern from a column), so its embedded documentation should no longer advertise the `pattern` argument as `const String`. Change the argument type to `String` and note that it can be a constant or come from a column. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
CI status updateMerged Review feedback
CI failures — all unrelated to this PRThe only red checks are the targeted SELECT arrayRemove([[[isNotDistinctFrom(16, ...)]], ...], [[arrayRemove(['hello', 'world'], concat('a', 1, equals(16, isNullable(8)))), ...]])It aborts in @groeneai, could you investigate the Verified locally: the new test |
|
@alexey-milovidov, the fix is open and approved: #105248 "Fix arrayRemove logical error when comparing incompatible Variant types". State: Same root cause covers all 6 sub-STIDs you listed (1fb5 / 3a34 / 42b3 / 3f35 / 30fe + the new CIDB 30d for the exact STID The PR is not stuck on us; it has been approved and is just waiting for a final merge step (looks like the Session: cron:clickhouse-ci-task-worker:20260608-133300 |
…aystack-nonconstant-needle
After merging master, the number `04309` is now also used by `04309_distributed_aggregation_persisted_exchange` and `04309_explain_insert_format`. Renumber to the next free prefix. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Merged latest
CI failures — all unrelated to this change
@groeneai, please investigate the AST fuzzer failure (STID: 2508-1fb5, #95839, a nested |
|
CI status on current head Fix is already open and approved: #105248 (tracking issue #95839). No unresolved review threads, approved by @antaljanosbenjamin — this is ready to merge. |
…aystack-nonconstant-needle
|
Merged latest `master` (the branch was ~1002 commits behind and red); the merge was conflict-free and The only remaining non-sync failures are the two |
…aystack-nonconstant-needle
|
Merged latest The reason for this re-merge: the only remaining red checks were the two No unresolved review threads; approved by @antaljanosbenjamin. Ready to merge once CI is green. |
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered by tests: 89/91 (97.80%) | Lost baseline coverage: none · Uncovered code |
|
Hi @Onyx2406 @alexey-milovidov @antaljanosbenjamin — while reviewing this PR I found the following:
Could you review the test PR and add the |
`MatchImpl` supports a constant haystack with a non-constant needle (since the merge of #100479 into `master`), so the earlier exclusion of `LIKE`, `ILIKE`, `NOT LIKE`, `NOT ILIKE`, and `REGEXP` from the `SOME` / `ALL` array quantifier no longer applies: `'abc' LIKE SOME(['a%', 'b%'])` rewrites to `arrayExists(x -> 'abc' LIKE x, ['a%', 'b%'])` and evaluates without throwing. Route these predicates through the same `arrayExists` / `arrayAll` lambda rewrite as the keyword comparison predicates, correct the now-false parser comment and the operator documentation, and replace the `UNKNOWN_FUNCTION` assertion in the test with positive coverage (`LIKE` / `ILIKE` / `NOT LIKE` / `NOT ILIKE` / `REGEXP` with `SOME` / `ALL`, including `NULL` patterns and equivalence to the explicit lambda form). Review addressed: #107454 (comment) Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
`MatchImpl` supports a constant haystack with a non-constant needle (since the merge of ClickHouse#100479 into `master`), so the earlier exclusion of `LIKE`, `ILIKE`, `NOT LIKE`, `NOT ILIKE`, and `REGEXP` from the `SOME` / `ALL` array quantifier no longer applies: `'abc' LIKE SOME(['a%', 'b%'])` rewrites to `arrayExists(x -> 'abc' LIKE x, ['a%', 'b%'])` and evaluates without throwing. Route these predicates through the same `arrayExists` / `arrayAll` lambda rewrite as the keyword comparison predicates, correct the now-false parser comment and the operator documentation, and replace the `UNKNOWN_FUNCTION` assertion in the test with positive coverage (`LIKE` / `ILIKE` / `NOT LIKE` / `NOT ILIKE` / `REGEXP` with `SOME` / `ALL`, including `NULL` patterns and equivalence to the explicit lambda form). Review addressed: ClickHouse#107454 (comment) Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Functions
like,ilike,notLike,notILike, andmatchnow support constant haystack with non-constant needle (e.g.'foo' LIKE pattern_column), which previously threwILLEGAL_COLUMN.Documentation entry for user-facing changes
What this PR does
The
(I)LIKEandmatchfunctions threw an exception when the haystack (first argument) was a constant string and the needle/pattern (second argument) came from a column:This is valid SQL and works in other databases. The
constantVectormethod inMatchImplwas a stub that unconditionally threw. This PR implements it.Root cause
MatchImpl::constantVectorinsrc/Functions/MatchImpl.hwas:The other argument combinations (
vectorConstant,vectorVector,vectorFixedVector) were all implemented — onlyconstantVectorwas missing.Fix
Implements
constantVectorfollowing the same logic as the existingvectorVectorpath:%substring%, use Volnitsky string searchLocalCacheTablecache, pre-filter with required substring, then run RE2 matchThe haystack is constant across all rows, so it is read once and reused.
Test plan
New stateless test
04053_like_constant_haystack_nonconstant_needle.sql:LIKE,ILIKE,NOT LIKE,NOT ILIKEwith constant haystackmatch()with constant haystackFixes #84187
Version info
26.6.1.785