Skip to content

Fix handling of trailing backslash in regular expressions - #118229

Merged
PedroTadim merged 13 commits into
ClickHouse:masterfrom
alexprabhat99:fix-117853-trailing-backslash-regexp
Sep 10, 2026
Merged

PedroTadim merged 13 commits into
ClickHouse:masterfrom
alexprabhat99:fix-117853-trailing-backslash-regexp

Conversation

@alexprabhat99

@alexprabhat99 alexprabhat99 commented Sep 5, 2026 •

Copy link
Copy Markdown
Contributor

Closes: #117853

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

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]

@clickhouse-gh

clickhouse-gh Bot commented Sep 5, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [1511647]

Summary: ✅


AI Review

Summary

This PR fixes the trailing-backslash misclassification in OptimizedRegularExpression and adds the missing regexp validation on the bloom-filter skip-index path, with regressions for match, extractAll, countMatches, splitByRegexp, and the text-index/tokenizer cases discussed in review. The implementation now looks consistent with that intent, but the proof is still incomplete because the changed fast path is also used by other user-visible regexp functions that are not covered here.

Tests
  • ⚠️ [tests/queries/0_stateless/05059_regexp_trailing_backslash.sql:5] The new regression file does not cover other direct consumers of the same fast path such as extract, regexpExtract, and regexpPosition (src/Functions/extract.cpp:127-140, src/Functions/regexpExtract.cpp:140-168, src/Functions/regexpPosition.cpp:94-97). Before the analyzeImpl fix, those entrypoints would also have answered abc\\ as a literal match instead of rejecting it. Add the smallest focused cases for at least one of those functions so the correctness evidence matches the shared implementation.
Final Verdict

⚠️ Changes requested: broaden the regression coverage to the other direct users of this shared regexp fast path before merge.

LLVM Coverage Report

Measured on commit 1511647.

Metric Baseline Current Δ
Lines 88.90% 88.90% +0.00%
Functions 91.80% 91.80% +0.00%
Branches 81.30% 81.30% +0.00%

Changed lines: Changed C/C++ lines covered: 11/11 (100.00%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Sep 5, 2026
@PedroTadim PedroTadim self-assigned this Sep 5, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 6, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 1511647b7 with master e76c8434d (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of 3fb3b1306; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

8 translation units recompiled, 15 s compile time in total, 8 of them have a recent master baseline.

Job report

@alexprabhat99

Copy link
Copy Markdown
Contributor Author

Hi @PedroTadim
All CI checks have passed successfully. Could you please review this when you have a chance? Thank you.


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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Future readers will not understand what "instead of being silently pruned" means. Could you rewrite (clarify), please?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check if these other skip index types are also affected:

bloom_filter index
ngrambf_v1 index
tokenbf_v1 index
text index

@alexprabhat99 alexprabhat99 Sep 7, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this not the place where we should throw an exception?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#118634 implements this place slightly differently. Which version is right?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@PedroTadim
PedroTadim added this pull request to the merge queue Sep 10, 2026
Merged via the queue into ClickHouse:master with commit 90ed928 Sep 10, 2026
175 checks passed
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-regular-function Regular scalar functions: string processing, data conversion, arithmetic, math, comparison, condi... pr-bugfix Pull request with bugfix, not backported by default pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

5 participants