Repository navigation
Fix handling of trailing backslash in regular expressions - #118229
PedroTadim merged 13 commits into
Conversation
|
Workflow [PR], commit [1511647] Summary: ✅
AI ReviewSummaryThis PR fixes the trailing-backslash misclassification in Tests
Final VerdictLLVM Coverage ReportMeasured on commit 1511647.
Changed lines: Changed C/C++ lines covered: 11/11 (100.00%) · Uncovered code |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units8 translation units recompiled, 15 s compile time in total, 8 of them have a recent master baseline. |
|
Hi @PedroTadim |
|
|
||
| auto & value = const_value.safeGet<String>(); | ||
| /// Compile the pattern as `match` execution does, so an invalid regexp | ||
| /// raises an exception instead of being silently pruned. |
There was a problem hiding this comment.
Future readers will not understand what "instead of being silently pruned" means. Could you rewrite (clarify), please?
There was a problem hiding this comment.
Please check if these other skip index types are also affected:
bloom_filter index
ngrambf_v1 index
tokenbf_v1 index
text index
There was a problem hiding this comment.
Future readers will not understand what "instead of being silently pruned" means. Could you rewrite (clarify), please?
Agreed. I’ll reword this to clarify that the regexp is validated before its required substring is used to build the skip-index condition.
There was a problem hiding this comment.
Please check if these other skip index types are also affected:
bloom_filter index ngrambf_v1 index tokenbf_v1 index text index
Checked all four. ngrambf_v1 and tokenbf_v1 share MergeTreeConditionBloomFilterText, so both are affected and covered by the same fix; I’ll add regression coverage for tokenbf_v1 as well. The generic bloom_filter condition does not use this match regexp-analysis path. The text index has a separate match path, but it already explicitly compiles the regexp before deriving index tokens, so the invalid pattern is already rejected there.
| ++pos; | ||
| if (pos == end) | ||
| { | ||
| is_trivial = false; |
There was a problem hiding this comment.
Is this not the place where we should throw an exception?
There was a problem hiding this comment.
I checked this path. OptimizedRegularExpression::analyze() catches exceptions from analyzeImpl() and returns an empty analysis result, so throwing directly here would not propagate the regexp error. I plan to keep this as is_trivial = false, which makes the normal execution path reach regexp compilation and reject the invalid pattern.
| auto & value = const_value.safeGet<String>(); | ||
| /// Validate the regexp before using its required substring to build | ||
| /// the skip-index condition. | ||
| static thread_local Regexps::LocalCacheTable regexp_cache; |
There was a problem hiding this comment.
I don't think we need a local cache table, at least not in the scope of this PR. If there is a performance problem it fixes, then we can implement that in a separate PR.
There was a problem hiding this comment.
Agreed. Removed the local cache and kept the validation direct. Any caching optimization can be handled separately if needed.
| ++pos; | ||
| if (pos == end) | ||
| { | ||
| is_trivial = false; |
There was a problem hiding this comment.
#118634 implements this place slightly differently. Which version is right?
There was a problem hiding this comment.
Thanks. I've aligned this with #118634 and now use finish_non_trivial_char() for a trailing lone backslash. This keeps the pattern on the non-trivial regexp path and lets RE2 reject it normally.
| -- A trailing unescaped backslash is an invalid RE2 regexp and must not | ||
| -- be treated as a trivial substring. | ||
|
|
||
| -- These patterns were incorrectly handled as trivial substring searches. |
There was a problem hiding this comment.
The new regression only proves the four entrypoints named in the issue, but the fix is in OptimizedRegularExpression::analyzeImpl, so the affected surface is larger. extract (src/Functions/extract.cpp:127-140), regexpExtract (src/Functions/regexpExtract.cpp:140-168), and regexpPosition (src/Functions/regexpPosition.cpp:94-97) all build the same OptimizedRegularExpression and then consume regexp.match(...). Before this change, those paths would also have answered abc\\ as a literal substring instead of raising.
Please add at least one focused regression for the other direct consumers of this shared fast path, e.g. extract('abcd', 'abc\\'), regexpExtract('abcd', 'abc\\'), or regexpPosition('xabcy', 'abc\\'), so the evidence matches the actual surface being changed.
Closes: #117853
Changelog category (leave one):
Changelog entry (a [user-readable short description] of the changes that goes into CHANGELOG.md):
Fix handling of regular expressions ending with an unescaped backslash in match, extractAll, countMatches, and splitByRegexp. Such invalid patterns are no longer incorrectly treated as trivial substring matches.
Workflow [PR]
Sync PR [sync-upstream/pr/118229]