Repository navigation
A flag group of a regexp is not a capture group - #118361
Conversation
`optimize_rewrite_regexp_functions` strips a trailing `.*$` from the pattern of
`extract`, which is sound only when the pattern has a capture group: `extract`
returns group 1 then, and that group precedes the stripped tail. The analyzer
answered that a pattern has a capture group when its only parentheses were flag
groups - it counted both a bare `(?i)` and a scoped `(?i:...)` - so the strip
fired while `extract` was returning the whole match, and the result was
truncated:
SELECT extract(materialize('aBcd'), '(?i)b.*$'); -- 'B', wrong
SELECT regexpExtract(materialize('aBcd'), '(?i)b.*$', 0); -- 'Bcd'
RE2 counts no capture group for either form, so `has_capture` no longer does
either. The rewrite keeps firing for a pattern with a real capture group, which
the test pins along with both flag-group forms.
Closes: #117242
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
Workflow [PR], commit [5fd771f] Summary: ❌
AI ReviewSummaryThis PR fixes flag groups being counted as captures and keeps nested or earlier captures from being dropped, but the broader Findings❌ Blockers
Final VerdictStatus: ❌ Block Minimum required actions:
LLVM Coverage ReportMeasured on commit 5fd771f.
Changed lines: Changed C/C++ lines covered: 21/21 (100.00%) · Uncovered code |
Build profile diff (arm_release)Comparing commit See the job log for details. |
`analyzeImpl` overwrote the accumulated `has_capture` with the capture bit of the group it had just descended into, so a capture seen earlier, or nested inside a non-capturing group, was lost: `(?i:(b)).*$` and `(a)(?:b).*$` were analyzed as having no capture group even though RE2 exposes group 1 for both. As a result `RegexpFunctionRewritePass::handleExtract` skipped the sound `.*$` strip for them. Accumulate instead of overwrite, and pin the rewrite with `EXPLAIN QUERY TREE` cases, because `extract` returns the same value with and without the rewrite for these patterns. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
The flaky check randomizes `optimize_rewrite_regexp_functions` and the `EXPLAIN QUERY TREE` part of the test asserts that the rewrite fires, so it failed whenever the setting was randomized to 0. https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=118361&sha=79d214d886c15ce235dd99e1866b177706e864ae&name_0=PR&name_1=Stateless%20tests%20%28amd_binary%2C%20flaky%20check%29 #118361 Co-Authored-By: Claude Fable 5.1 <[email protected]>
| has_capture = !is_non_capturing_group; | ||
| /// A capture anywhere counts: one seen before this group, one nested inside it, | ||
| /// or this group itself when it captures. | ||
| has_capture = has_capture || group_has_capture || !is_non_capturing_group; |
There was a problem hiding this comment.
result.has_capture feeds both the trailing .*$ rewrite you are fixing and the leading ^.* stripping in RegexpFunctionRewritePass::handleExtract. With this new accumulation, patterns that were previously left alone now start taking the prefix rewrite even though it changes which match becomes group 1.
This is the same last-vs-first capture change already documented by 04492_regexp_pattern_with_nul_byte.sql, just without a NUL byte. For example, extract(materialize('a1a2'), '^.*(?s:(a.))') should capture the last a. (a2), but the pass now rewrites it to (?s:(a.)), which captures the first (a1). The same regression appears for a capture that precedes a later non-capturing group, e.g. ^.*(a.)(?:x) on a1xa2x.
So the new has_capture value is correct for the trailing .*$ case, but it is not a safe blanket permission for stripping ^.*. Please either disable the prefix rewrite for these cases (or entirely), or strengthen the proof so group 1 cannot move, and add a focused regression test around one of the examples above.
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]>
optimize_rewrite_regexp_functionsstrips a trailing.*$from the pattern ofextract, which is sound only when the pattern has a capture group:extractreturns group 1 then, and that group precedes the stripped tail. The analyzer answered that a pattern has a capture group when its only parentheses were flag groups - it counted both a bare(?i)and a scoped(?i:...)- so the strip fired whileextractwas returning the whole match, and the result was silently truncated:RE2 counts no capture group for either form, so
has_captureno longer does either. The rewrite keeps firing for a pattern with a real capture group, which the test pins along with both flag-group forms; the unit test that recorded(?s)bobas having a capture is updated to the answer RE2 gives.While fixing this, the capture analysis turned out to lose captures in a second way:
analyzeImplreplaced the accumulatedhas_capturewith the capture bit of the group it had just descended into, so a capture seen earlier, or nested inside a non-capturing group, was dropped -(?i:(b)).*$and(a)(?:b).*$were analyzed as having no capture group even though RE2 exposes group 1 for both, and the sound.*$strip was skipped for them. The analyzer now accumulates, and the test pins the rewrite withEXPLAIN QUERY TREE, becauseextractreturns the same value for these patterns with and without it.Closes: #117242
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes to CHANGELOG.md):
Fixed
extractreturning a truncated result for a pattern that ends with.*$and whose only parentheses are an inline flag group, such as(?i)b.*$: the pattern has no capture group, so the trailing.*$must not be stripped.Workflow [PR]
Sync PR [sync-upstream/pr/118361]
Version info
26.10.1.308-master(included in26.10and later)