Skip to content

Add regexpPosition function (PostgreSQL regexp_instr compatibility) - #104172

Merged
alexey-milovidov merged 13 commits into
ClickHouse:masterfrom
aagarwal-gtr:regexp-instr
May 17, 2026
Merged

alexey-milovidov merged 13 commits into
ClickHouse:masterfrom
aagarwal-gtr:regexp-instr

Conversation

@aagarwal-gtr

@aagarwal-gtr aagarwal-gtr commented May 6, 2026 •

Copy link
Copy Markdown
Contributor

Implements PostgreSQL's regexp_instr (issue #103996), exposed under the ClickHouse-idiomatic name regexpPosition with regexpInstr and regexp_instr registered 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:

  • I went with regexpPosition name to parallel the existing position / regexpExtract family rather than matchFirstIndex, since the function returns the N-th match, not always the first.
  • Positions are byte-based, consistent with sibling regex functions (regexpExtract, etc.). PostgreSQL's regexp_instr is character-based; this divergence is documented in the function description. Open question: Do we need a regexpPositionUTF8 variant as followup, or should this be UTF8 instead?
  • Pattern and flags are required-constant and remaining numeric arguments may be vector or const.

Changelog category:

  • New Feature

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

Added function regexpPosition (with PostgreSQL-compatible aliases regexpInstr and regexp_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 written (mandatory for new features)
    Documentation is provided via the FunctionDocumentation block registered in src/Functions/regexpPosition.cpp (description, syntax, arguments, return value, example).

Version info

  • Merged into: 26.5.1.729

@CLAassistant

CLAassistant commented May 6, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@rschu1ze rschu1ze added the can be tested Allows running workflows for external contributors label May 6, 2026
@clickhouse-gh

clickhouse-gh Bot commented May 6, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [3d300b4]

Summary: ✅


AI Review

Summary

This PR adds regexpPosition with PostgreSQL-compatible aliases and comprehensive stateless coverage for argument validation, flag handling, occurrence/subexpression behavior, and prior constant-haystack regressions. I reviewed the current diff and prior discussion threads; I did not find any remaining correctness, safety, performance, or metadata issues that require changes before merge.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-feature Pull request with new product feature label May 6, 2026
Comment thread src/Functions/regexpPosition.cpp Resolved
Comment thread src/Functions/regexpPosition.cpp Resolved
Comment thread src/Functions/regexpPosition.cpp Resolved
Comment thread src/Functions/regexpPosition.cpp Resolved
Comment thread src/Functions/regexpPosition.cpp Resolved
@alexey-milovidov

Copy link
Copy Markdown
Member

The FileCacheTest.SLRUFreeSpaceKeepingProtectedOnly failure on this PR is a known flaky test that was fixed in #104418. Could you please update the branch with the latest master to pick up the fix?

@clickhouse-gh

clickhouse-gh Bot commented May 12, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.10% 84.10% +0.00%
Functions 91.10% 91.10% +0.00%
Branches 76.50% 76.50% +0.00%

Changed lines: 93.37% (183/196) · Uncovered code

Full report · Diff report

@aagarwal-gtr

Copy link
Copy Markdown
Contributor Author

Hi @alexey-milovidov / @rschu1ze would appreciate a review when you get a chance!

@alexey-milovidov

Copy link
Copy Markdown
Member

Open question: Do we need a regexpPositionUTF8 variant as followup, or should this be UTF8 instead?

Yes, this is ok.


const String prepared_pattern = applyFlagsToPattern(pattern, flags, getName());

const OptimizedRegularExpression regexp(prepared_pattern);

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.

Let's check if we should use ObjectCache here, see similar regexp functions.

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 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) use LocalCacheTable to avoid recompiling on every row.
  • Constant patterns (regexpExtract, extractGroups, extractAll, extract, countMatches, splitByRegexp) just call Regexps::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 alexey-milovidov self-assigned this May 17, 2026

@alexey-milovidov alexey-milovidov left a comment

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.

Overall looks good. Thank you!

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue May 17, 2026
Merged via the queue into ClickHouse:master with commit 05e3bb1 May 17, 2026
165 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 May 18, 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 pr-feature Pull request with new product feature pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants