Repository navigation
Fix extract and match results for a regexp with an empty flag group (?) - #126107
Conversation
re2 accepts an empty flag list, `(?)`: a no-op that captures nothing and
pushes nothing, so a quantifier after it applies to the atom before it
(`abcd(?)?e` is `abcd?e`). Two ClickHouse recognizers of inline flag
groups required at least one flag character and missed it:
- `analyzeImpl` (OptimizedRegularExpression.cpp) took `(?)` for a
capturing group. `RegexpFunctionRewritePass` then stripped the trailing
`.*$` of an `extract` pattern that has no capture group, so
`extract('abcd', '(?)b.*$')` returned `b` instead of `bcd`. The same
misreading kept `abcd` as a required substring of `abcd(?)?e`, so
`match('abce', 'abcd(?)?e')` returned 0 without asking re2.
- `findEndOfPureFlagGroup` (RegexpUtils.cpp) did not see `(?)` as
zero-width, so the primary-key prefix of `^ab(?)*c` stayed `ab` and
`WHERE match(s, '^ab(?)*c')` lost the rows starting with `ac`.
Both recognizers now accept the empty flag list. This completes ClickHouse#118361
(`(?i)` is not a capture group) and the fix for ClickHouse#117256 (`^ab(?i)*c`
primary-key prefix), which handled the non-empty forms only.
Closes ClickHouse#121275
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Review follow-up. re2 pushes nothing for a pure flag group, so a
quantifier that allows zero occurrences after `(?)` or `(?s)` applies to
the atom before it. The required-substring analysis in `analyzeImpl`
missed two shapes of that atom:
- a group: it looked only at the character right after the group's `)`,
so for `(?:abcd)(?s)*x` it kept `abcd` in the literal and the `*`
trimmed one byte, leaving the required substring `abc`;
- a multi-byte character: the quantifier trimmed one byte, so `abcdé?x`
required `abcd` plus the first byte of `é`.
Both made `match` reject matching rows (`match('x', '(?:abcd)(?s)*x')`
and `match('abcdx', 'abcdé?x')` returned 0). The group check now looks
past a chain of flag groups, as `skipZeroWidthConstructs` does for the
primary-key prefix, and the trim drops the whole UTF-8 character, as
`dropLastLiteral` does.
The `^ab(?)*c` count query now runs with `force_primary_key = 1`, so
the test also fails if the key prefix stops being extracted.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Review follow-up. The previous commit re-implemented in `analyzeImpl`
three helpers that `RegexpUtils.cpp` already has for the primary-key
prefix (`skipZeroWidthConstructs`, `isZeroAllowingQuantifier`,
`dropLastLiteral`), and the copy left out the empty `\Q\E` quote. re2
pushes nothing for an empty quote, so a quantifier after it applies to
the atom before it, but `analyzeImpl` ended the literal at the escape:
`match('abcx', 'abcd\Q\E?x')`, `match('x', '(?:abcd)\Q\E*x')` and
`match('abcdx', 'abcdé\Q\E?x')` returned 0.
Export the three helpers from `RegexpUtils.h` and call them from
`analyzeImpl` (the group check, the generic quantifier trim, and a new
check in the escape branch for an empty `\Q\E`), so the required-substring
analysis and the primary-key prefix share one definition of zero-width
constructs.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Restore master's wording of the ()? group comment; the extra clause restated skipZeroWidthConstructs. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 3 findings over 3 rounds, the last round clean), plus my own review.
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-11:20261015-191200 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-40:20261015-183800 |
|
Workflow [PR], commit [1a25c2e] Summary: ✅
AI ReviewSummaryThe PR fixes Final Verdict✅ Approve. |
|
cc @vitlibar @ahmadov, could you review this? re2 accepts an empty flag group |
70b72ed
Backport #126107 to 26.10: Fix `extract` and `match` results for a regexp with an empty flag group `(?)`
…gexp with an empty flag group `(?)`
Closes: #121275
Related: #118361
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix
extractreturning a truncated match, andmatchmissing matching rows (also through primary-key analysis), for a regular expression with an empty flag group(?). Also fixmatchmissing rows when?,*or{0,n}applies to a multi-byte character, such asabcdé?x, or comes after a flag group or an empty\Q\Equote, such as(?:abcd)(?s)*xorabcd\Q\E?x. Closes #121275.Description
On 26.8.18.2,
SELECT extract(materialize('abcd'), '(?)b.*$')returnsbinstead ofbcd. The same pattern shape also breaksmatch:match('abce', 'abcd(?)?e')returns 0 although re2 matches it, and withORDER BY s, primary-key analysis ofWHERE match(s, '^ab(?)*c')can skip granules holding matching rows such asac.match('x', '(?:abcd)(?s)*x'),match('abcdx', 'abcdé?x')andmatch('abcx', 'abcd\Q\E?x')also return 0 instead of 1. Also wrong on 26.3.42.3 and master.re2 accepts an empty flag list,
(?): it is zero-width and captures nothing, so a quantifier after it applies to the atom before it (abcd(?)?eisabcd?e). Two recognizers of inline flag groups required at least one flag character:analyzeImpl(OptimizedRegularExpression.cpp) read(?)as a capturing group.RegexpFunctionRewritePassthen stripped the.*$of anextractpattern that has no capture group, and the required-substring analysis keptabcdforabcd(?)?e.findEndOfPureFlagGroup(RegexpUtils.cpp) did not see(?)as zero-width, so the primary-key prefix of^ab(?)*cstayedab.analyzeImplalso applied a quantifier more narrowly than re2: it dropped only the last byte before it (abcdé?xkeptabcdplus half ofé), did not look through flag groups after a group ((?:abcd)(?s)*xkeptabc), and ended the literal at an empty\Q\Equote, which is zero-width too (abcd\Q\E?xkeptabcd).Both recognizers now accept the empty flag list, and
analyzeImpluses the helpers that the primary-key prefix already uses (skipZeroWidthConstructs,isZeroAllowingQuantifier,dropLastLiteral, now exported fromRegexpUtils.h). This completes #118361 and the fix for #117256.New cases in tests
05098and05097fail on the unpatched build and pass with the fix.Workflow [PR]
Sync PR [sync-upstream/pr/126107]
Version info
26.11.1.29-master(included in26.11and later)