Repository navigation
Read a character class of a regexp the way RE2 does - #118342
Conversation
The analyzer that extracts a required substring for the `match` prefilter tracks
character classes, and it read two of RE2's rules wrong.
A `]` written first in a class - right after `[` or `[^` - is a literal member
of it, so `[]a]` is the class of `]` and `a`. The analyzer closed the class at
that `]` instead and analyzed the rest of the body as a top-level literal run,
so `match(materialize('ab'), '[]a]b')` required the substring `a]b` and answered
0 for a haystack RE2 matches.
A `[` inside an already-open class is a literal member as well. The analyzer
opened another class for it and never got back to depth zero, so a top-level
`|` after `[[]` was not recognized as an alternative and the literal run before
the class stayed a required substring: `match(materialize('zb'), 'abc[[]|b')`
answered 0, and so did `match(materialize('x'), 'abc[[]|')`, whose right
alternative is empty and matches everything.
`extract`, `extractAll` and the `replaceRegexp*` family run RE2 without the
prefilter and answered correctly all along, which is what makes the wrong answer
of `match` and `countMatches` visible side by side.
Closes: #117254
Closes: #117255
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
Workflow [PR], commit [e4932ee] Summary: ✅
AI ReviewSummaryThis PR fixes the required-substring analyzer so RE2 character classes with a leading literal Missing context / blind spots
Tests
Final VerdictNeeds one more focused regression test for the LLVM Coverage ReportMeasured on commit e4932ee.
Changed lines: Changed C/C++ lines covered: 31/31 (100.00%) · Uncovered code |
|
TL;DR: the two reported shapes are fixed correctly, but the new
|
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units7 translation units recompiled, 6 s compile time in total, 7 of them have a recent master baseline. |
The tracker that reads a `[` inside an open class also decides whether
`optimize_rewrite_regexp_functions` may turn `replaceRegexpAll` into
`replaceRegexpOne`: the rewrite declines for a pattern with a top-level
alternation, and with the alternation hidden it replaced only the first match.
SELECT replaceRegexpAll(materialize('[[a'), '[[]|a$', 'Z'); -- 'Z[a', now 'ZZZ'
Closes: #117243
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
… literal `[` Reading every `[` inside an open character class as a literal member of it was broader than RE2's grammar: RE2 first looks for a POSIX named class such as `[:alpha:]` and consumes it as a part of the enclosing class. For `[[:alpha:]]b` the analyzer therefore closed the class at the `:]`, took the trailing `]b` as a top-level literal run, and the prefilter demanded a substring the pattern does not - dropping rows that RE2 matches. Consume the named class as a whole, following RE2's rule that the `[` is a literal only when there is no closing `:]` anywhere in the rest of the regexp. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
…ass-literals-117254
A control binary built without the fix answers 0 for
`match('zabcdefg', 'z[[:alpha:]]bcdefg')`, because the required substring is
taken as `]bcdefg`; the case without a closing `:]`, where the `[` really is a
literal member, is covered too.
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
A named class next to ordinary members, one inside an alternative (the alternatives also drive text-index pruning), and `[[:]]`, which has no closing `:]` and is a literal `[` for RE2 too. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
| -- A named class next to other members, and one inside an alternative, where the analyzer also | ||
| -- feeds the alternatives to the text-index pruning. | ||
| SELECT match(materialize('7xyz'), '[a[:digit:]z]xyz'), match(materialize('qxyz'), '[a[:digit:]z]xyz'); | ||
| SELECT match(materialize('Qx'), 'foo|[[:alpha:]]x'), match(materialize('foo'), 'foo|[[:alpha:]]x'), match(materialize('bar'), 'foo|[[:alpha:]]x'); |
There was a problem hiding this comment.
This still does not exercise the path that consumes RegexpAnalysisResult::alternatives. match never reads that vector: OptimizedRegularExpression::getAnalyzeResult exposes only required_substring, and MatchImpl::matchOneRow falls straight back to RE2 when that required substring is empty. So this query stays green even if analyze('foo|[[:alpha:]]x') still extracts the right branch as ]x instead of x, and the regression would only show up under text-index pruning.
Please add either a focused gtest_optimize_re expectation for foo|[[:alpha:]]x -> {"foo", "x"} or a text-index match(... SETTINGS use_skip_indexes = 1) / multiMatchAny case on this exact POSIX-class shape.
|
@alexey-milovidov — ClickGap found the following in this PR:
Close anything that's wrong or already addressed. |
The analyzer that extracts a required substring for the
matchprefilter tracks character classes, and it read two of RE2's rules wrong.A
]written first in a class - right after[or[^- is a literal member of it, so[]a]is the class of]anda. The analyzer closed the class at that]instead and analyzed the rest of the body as a top-level literal run, so the prefilter required a substring the pattern does not:A
[inside an already-open class is a literal member as well. The analyzer opened another class for it and never got back to depth zero, so a top-level|after[[]was not recognized as an alternative and the literal run before the class stayed a required substring:The same hidden alternation also let
optimize_rewrite_regexp_functionsturnreplaceRegexpAllintoreplaceRegexpOne, which replaced only the first match:extract,extractAlland thereplaceRegexp*family run RE2 without the prefilter and answered correctly all along, which is what makes the wrong answer ofmatchandcountMatchesvisible side by side. Both the constant-pattern and the non-constant-pattern paths are affected, and no setting is involved.The one
[inside a class that is not a literal is the opening one of a POSIX named class such as[[:alpha:]]or[[:^digit:]]: RE2 consumes[:name:]as a part of the enclosing class, and reads the[as a literal only when there is no closing:]anywhere in the rest of the pattern. The analyzer follows that rule too, so[[:alpha:]]bkeeps requiringband not]b.Closes: #117254
Closes: #117255
Closes: #117243
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes to CHANGELOG.md):
Fixed
matchandcountMatchesreturning wrong results for a pattern whose character class starts with a literal]([]a]b) or contains a literal[next to an alternation (abc[[]|b): the required-substring prefilter demanded a substring the pattern does not require. The same defect madeoptimize_rewrite_regexp_functionsturn such areplaceRegexpAllintoreplaceRegexpOne.Workflow [PR]
Sync PR [sync-upstream/pr/118342]
Version info
26.9.1.1194(included in26.9and later)