Repository navigation
Add regexpPosition function (PostgreSQL regexp_instr compatibility) - #104172
Conversation
|
Workflow [PR], commit [3d300b4] Summary: ✅ AI ReviewSummaryThis PR adds Final VerdictStatus: ✅ Approve |
|
The |
LLVM Coverage Report
Changed lines: 93.37% (183/196) · Uncovered code |
|
Hi @alexey-milovidov / @rschu1ze would appreciate a review when you get a chance! |
Yes, this is ok. |
|
|
||
| const String prepared_pattern = applyFlagsToPattern(pattern, flags, getName()); | ||
|
|
||
| const OptimizedRegularExpression regexp(prepared_pattern); |
There was a problem hiding this comment.
Let's check if we should use ObjectCache here, see similar regexp functions.
There was a problem hiding this comment.
I took a look. There's no pre-existing generic ObjectCache for compiled regexes. The closest things in Functions/Regexps.h are:
Regexps::createRegexp<...>(pattern), which is just a helper, not a cache.Regexps::LocalCacheTable, a per-call cache keyed by the pattern string.
The convention across the other regex functions seems to split on whether the pattern is per-row or constant:
- Per-row patterns (
match,like,MatchImpl.h) useLocalCacheTableto avoid recompiling on every row. - Constant patterns (
regexpExtract,extractGroups,extractAll,extract,countMatches,splitByRegexp) just callRegexps::createRegexp<false, false, false>(pattern)once outside the row loop, with no caching.
regexpPosition requires a constant pattern (enforced at regexpPosition.cpp:80), so it falls in the second bucket and the regex already gets compiled exactly once per query. A LocalCacheTable here would just be a hash+lookup against a single key, so I don't think it buys us anything.
The one place I diverge from the siblings is that I construct OptimizedRegularExpression directly instead of going through Regexps::createRegexp. That was on purpose. createRegexp hard-enables RE_DOT_NL, and an earlier review on this PR asked me to drop the unconditional RE_DOT_NL so the user's s flag actually does something for PostgreSQL regexp_instr semantics.
If you'd rather have the consistency with the other const-pattern functions, I can route through Regexps::createRegexp<false, false, false> and make applyFlagsToPattern always emit an explicit (?...) / (?-s...) prefix so the inline modifier overrides the compile-time RE_DOT_NL. I'd double-check the inline-vs-compile precedence in RE2 before pushing. Happy to do that as a follow-up commit if you want, let me know.
alexey-milovidov
left a comment
There was a problem hiding this comment.
Overall looks good. Thank you!
Implements PostgreSQL's
regexp_instr(issue #103996), exposed under the ClickHouse-idiomatic nameregexpPositionwithregexpInstrandregexp_instrregistered as case-insensitive aliases for PG compatibility.Returns the 1-based byte position of the N-th match of a pattern in a haystack. Supports an optional start position, a "return position right after the match" mode, regex flags (
i,c,m/n,s), and capture-group selection. Returns 0 when there is no match or the requested capture group did not participate.A few notes for review:
regexpPositionname to parallel the existingposition/regexpExtractfamily rather thanmatchFirstIndex, since the function returns the N-th match, not always the first.regexpExtract, etc.). PostgreSQL'sregexp_instris character-based; this divergence is documented in the function description. Open question: Do we need aregexpPositionUTF8variant as followup, or should this be UTF8 instead?Changelog category:
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Added function
regexpPosition(with PostgreSQL-compatible aliasesregexpInstrandregexp_instr) that returns the byte position of the N-th regex match in a string. Supports start offset, return-after-match mode, regex flags, and capture-group selection.Documentation entry for user-facing changes
Documentation is provided via the
FunctionDocumentationblock registered insrc/Functions/regexpPosition.cpp(description, syntax, arguments, return value, example).Version info
26.5.1.729