Skip to content

Make the regexp analyzer's POSIX named class lookup linear - #128394

Open
TheNetherWatcher wants to merge 2 commits into
ClickHouse:masterfrom
TheNetherWatcher:fix-regexp-posix-class-scan
Open

TheNetherWatcher wants to merge 2 commits into
ClickHouse:masterfrom
TheNetherWatcher:fix-regexp-posix-class-scan

Conversation

@TheNetherWatcher

@TheNetherWatcher TheNetherWatcher commented Oct 31, 2026 •

Copy link
Copy Markdown

Inside a character class, every [: makes skipPosixNamedClass look for a closing :], and that scan runs over the whole rest of the pattern. When no :] is left, it always runs to the end, so a pattern with many [: and no :] made the analysis quadratic: SELECT match(materialize('x'), concat('*[', repeat('[:', 250000), 'x]')) spends ~34 s before re2 is even built.

A failed scan stays failed for every later [:, so this finds the last :] once with rfind and passes it down. A [: after it returns pos + 1 right away, which is what the scan would have returned. When the scan does run, it is guaranteed to find a :], and the analyzer then continues after it, so each character is scanned at most once.

Timing of the analyzer's walk over the issue's pattern (old vs new function, -O2):

[: count before after
25,000 0.30 s < 0.001 s
100,000 4.6 s < 0.001 s
250,000 28.9 s < 0.001 s

Testing:

Closes: #119581
Related: #118342

Changelog category (leave one):

  • Performance Improvement

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Fixed quadratic time spent analyzing a regular expression whose character class contains many [: and no closing :]. For example, match with 250,000 of them took about 34 seconds before reporting the pattern as invalid.


Workflow [PR]
Sync PR [sync-upstream/pr/128394]

Inside a character class, every `[:` made skipPosixNamedClass scan the rest
of the pattern for `:]`. With no `:]` left, each scan ran to the end, so
analysis was quadratic. Find the last `:]` once and skip the scan for any
`[:` after it.
@CLAassistant

CLAassistant commented Oct 31, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@clickhouse-gh clickhouse-gh Bot added comp-regular-function Regular scalar functions: string processing, data conversion, arithmetic, math, comparison, condi... external Author is not a member of the ClickHouse organization labels Oct 31, 2026
@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Nov 14, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [a957efa]

Summary: ❌

job_name test_name status info comment
Performance Comparison (amd_release, master_head, 3/6) ERROR
Performance Comparison (amd_release, master_head, 4/6) ERROR
Performance Comparison (amd_release, master_head, 6/6) ERROR
Build profile diff ERROR

AI Review

Summary

This PR removes quadratic searches for a missing :] in the regular-expression analyzer while preserving parsing behavior. No actionable findings on head a957efaad8bf405d0929bd83df3a4484e9fb4d9b. The before/after measurements support the performance improvement, and the new stateless regression test passed in 0.18 seconds in the Fast test report.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label Nov 14, 2026
clang-tidy flagged regexp_.data() once the call was split over several lines,
because the NOLINT stayed on the first one. Take the pointer once and keep the
call on one line.

The valid-class queries timed out in the fast test: re2 accepts those patterns
and its own MaybeParsePosixClass also scans for :] at every [:, so building
the class is quadratic in re2 regardless of the analyzer. Use 1000 [: there;
the 250000 run stays in the first query, which re2 rejects at the leading *.

This branch has not been deployed

No deployments
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... external Author is not a member of the ClickHouse organization pr-performance Pull request with some performance improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

skipPosixNamedClass rescans the whole pattern per [: — analysis is now quadratic

3 participants