Skip to content

Make regexpExtract return the whole match for patterns without a capturing group - #106374

Merged
antonio2368 merged 3 commits into
ClickHouse:masterfrom
groeneai:groeneai/fix-56393-regexpextract-no-group
Jun 9, 2026
Merged

antonio2368 merged 3 commits into
ClickHouse:masterfrom
groeneai:groeneai/fix-56393-regexpextract-no-group

Conversation

@groeneai

@groeneai groeneai commented Jun 3, 2026 •

Copy link
Copy Markdown
Collaborator

The two-argument form regexpExtract(haystack, pattern) defaults the group index to 1. A pattern with no capturing group only has group 0 (the whole match), so the default index 1 was out of range and the function threw INDEX_OF_POSITIONAL_ARGUMENT_IS_OUT_OF_RANGE instead of returning anything useful. For example regexpExtract('100-200', '\d+') failed.

Now, when the index is omitted and the pattern has no capturing group, the function falls back to group 0 and returns the whole match. An explicitly supplied out-of-range index still throws, and patterns with at least one capturing group keep the default index 1.

Closes: #56393

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 regexpExtract(haystack, pattern) so that patterns without a capturing group return the whole match instead of throwing INDEX_OF_POSITIONAL_ARGUMENT_IS_OUT_OF_RANGE.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Version info

  • Merged into: 26.6.1.537

…uring group

The two-argument form regexpExtract(haystack, pattern) defaults the group
index to 1. A pattern with no capturing group only has group 0 (the whole
match), so the default 1 is out of range and the function threw
INDEX_OF_POSITIONAL_ARGUMENT_IS_OUT_OF_RANGE instead of returning anything
useful. For example regexpExtract('100-200', '\d+') failed.

When the index is omitted and the pattern has no capturing group, fall back
to group 0 and return the whole match. An explicitly supplied out-of-range
index still throws, and patterns with at least one capturing group keep the
default index 1.

Closes: ClickHouse#56393

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@groeneai

groeneai commented Jun 3, 2026

Copy link
Copy Markdown
Collaborator Author

Pre-PR validation:

  • Reproduced before fix: regexpExtract('100-200', '\d+') threw Code: 353. INDEX_OF_POSITIONAL_ARGUMENT_IS_OUT_OF_RANGE.
  • Root cause: omitted index defaults to 1; a pattern with no capturing group has only group 0, so 1 >= capture+1 (==1) throws.
  • Fix: when the index is omitted and capture == 0, fall back to group 0. The omitted-index path only ever reaches vectorConstant (the all-const case is funneled there by useDefaultImplementationForConstants), so vectorVector/constantVector always carry an explicit index column and need no change.
  • After fix: regexpExtract('100-200', '\d+') returns 100; regexpExtract('100-200', '\d+', 1) still throws (explicit out-of-range index); patterns with a group keep default group 1.
  • New test 04305_regexp_extract_no_capture_group plus existing 02562_regexp_extract pass; new test ran 30x clean with TZ=UTC (the only random-settings failures were a local Invalid time zone: zoneinfo/UTC env issue, unrelated to the change).

Session: cron:clickhouse-ci-task-worker:20260603-095100

@groeneai

groeneai commented Jun 3, 2026

Copy link
Copy Markdown
Collaborator Author

cc @rschu1ze @antonio2368 — could you review? Small fix so the two-argument regexpExtract returns the whole match for patterns without a capturing group instead of throwing.

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

clickhouse-gh Bot commented Jun 6, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [1d2730e]

Summary: ❌

job_name test_name status info comment
Integration tests (arm_binary, distributed plan, 4/4) FAIL
test_database_delta/test.py::test_check_database_unity FAIL cidb

AI Review

Summary

This PR changes the omitted-index behavior of regexpExtract(haystack, pattern) so patterns without capturing groups return group 0 while patterns with capturing groups keep the existing default group 1, and explicit out-of-range indexes still throw INDEX_OF_POSITIONAL_ARGUMENT_IS_OUT_OF_RANGE. I reviewed the current diff, prior PR threads, the linked issue, the constant/non-constant dispatch paths, the updated FunctionDocumentation, and the focused regression test; I found no unresolved correctness, compatibility, or evidence issues.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Jun 6, 2026
Comment thread src/Functions/regexpExtract.cpp Resolved
The two-argument form `regexpExtract(haystack, pattern)` now uses group
`0` when `pattern` has no capturing group and group `1` otherwise.
Reflect this conditional default in the registered `FunctionDocumentation`
and add a no-capture-group example.

Address ClickHouse#106374 (comment)... bot review.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@groeneai

groeneai commented Jun 6, 2026

Copy link
Copy Markdown
Collaborator Author

Both bot review points addressed:

  1. Changelog category: PR body updated to Bug Fix (user-visible misbehavior in an official stable release), with the suggested entry wording.
  2. FunctionDocumentation for index: updated in src/Functions/regexpExtract.cpp (commit 838666483393) to describe the conditional default — group 1 when pattern has at least one capturing group, group 0 (whole match) when it has none. Added a no-capture-group row to the example so the docs match the new behavior.

Session: cron:clickhouse-ci-task-worker:20260606-123800

@clickhouse-gh clickhouse-gh Bot added pr-bugfix Pull request with bugfix, not backported by default and removed pr-improvement Pull request with some product improvements labels Jun 6, 2026
@groeneai

groeneai commented Jun 6, 2026

Copy link
Copy Markdown
Collaborator Author

CI Ledger — covered SHA: 838666483393e696cd3624d92c5a67212ce9f3e4

CI fully finished (Finish Workflow ✅ at 2026-06-06T16:50Z, last test completion 16:51:49Z, ≥20min CIDB ingestion buffer met).

Failure Classification Disposition
Stateless tests (arm_asan_ubsan, azure, parallel) / 04033_tpc_ds_q16 (ret=241 MEMORY_LIMIT_EXCEEDED, RSS 53.77 GiB > 53.69 GiB max) infra (chronic OOM) Pre-existing OOM cluster on arm_asan_ubsan + azure + parallel runner: 59 hits / 34 distinct PRs / 42 distinct tests in 30d. Tracked in chronic infra ticket. PR diff is in src/Functions/regexpExtract.cpp only — zero overlap with TPCDS / catalog_sales / OvercommitTracker.
Stateless tests (arm_asan_ubsan, azure, parallel) / 04049_with_func_alias_in_view (ret=210 NETWORK_ERROR "Connection reset by peer") infra (chronic) Same OOM/runner-pressure cluster; server died from sibling OOM in same runner.
Stateless tests (arm_asan_ubsan, azure, parallel) / 01661_extract_all_groups_throw_fast (ret=241 MEMORY_LIMIT_EXCEEDED in repeat(), RSS 51.81 GiB; test expects TOO_LARGE_ARRAY_SIZE but runner OOM'd first) infra (chronic) Same OOM cluster. Cross-PR: this exact test failed on PR #93833 (2026-05-31) and now ours — both arm_asan_ubsan azure parallel. Not regexpExtract-related.

PR-caused failure check: none. PR diff: src/Functions/regexpExtract.cpp (FunctionDocumentation index arg + body update) — has no interaction with repeat() / extractAllGroupsHorizontal memory paths, TPCDS catalog_sales, or with_func_alias_in_view. arm_tidy, all 11 build variants, Fast test, Bugfix validation (FT+IT), Style check, Code Review: all SUCCESS. Stateless tests on every other variant: pass.

Moving to on_review.

@antonio2368 antonio2368 self-assigned this Jun 8, 2026
Comment thread src/Functions/regexpExtract.cpp Outdated
const ColumnString::Offsets & offsets,
const std::string & pattern,
ssize_t index,
bool index_omitted,

@antonio2368 antonio2368 Jun 8, 2026 •

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.

What do you think about having index as a std::optional<ssize_t> and have something like

ssize_t resolved = index.value_or(capture == 0 ? 0 : 1);

@groeneai groeneai Jun 8, 2026 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Applied in 1d2730e. The omitted-vs-explicit state is now an std::optional<ssize_t> plumbed through to vectorConstant, and the default rule is index_arg.value_or(capture == 0 ? 0 : 1). Behavior unchanged; both regression tests (02562_regexp_extract, 04305_regexp_extract_no_capture_group) still pass.

Per @antonio2368 review on PR ClickHouse#106374: replace the
`bool index_omitted + ssize_t index` pair with
`std::optional<ssize_t>` and resolve the conditional default with
`value_or(capture == 0 ? 0 : 1)`.

Behavior unchanged: a pattern with no capturing group still falls
back to group 0 when the third argument is omitted; an explicitly
supplied index keeps its current validation.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.60% 84.60% +0.00%
Functions 92.30% 92.30% +0.00%
Branches 77.20% 77.20% +0.00%

Changed lines: Changed C/C++ lines covered by tests: 29/31 (93.55%) | Lost baseline coverage: none · Uncovered code

Full report · Diff report

@groeneai

groeneai commented Jun 9, 2026

Copy link
Copy Markdown
Collaborator Author

CI Ledger — covered SHA: 1d2730e40d9287eddec810174cc55ba03dba7892 (antonio2368's std::optional<ssize_t> suggestion applied)

CI fully finished. Gate (a) all CheckRuns COMPLETED ✓, gate (b) Finish Workflow=SUCCESS ✓, gate (c) ≥20-min CIDB ingestion buffer ✓.

Single failure, not PR-caused:

Check / Test Classification Disposition
Integration tests (arm_binary, distributed plan, 4/4) — test_database_delta/test.py::test_check_database_unity (Spark exec_in_container failure in execute_multiple_spark_queries) chronic flaky Cross-PR query (30d): 7 distinct PRs + 4 master hits. Master signature (head_ref=master): 2026-05-11 01:07Z + 17:41Z Integration tests (arm_binary, distributed plan, 4/4), 2026-05-13 + 2026-06-01 release branches. Same file:line stack signature every time. Unity-catalog test in test_database_delta — fully orthogonal to this PR's regexpExtract.cpp diff. Tracked in task 2026-06-08-ci-p2-chronic-flaky-testdatabasedelta (FOURTH re-emergence; prior closed triages on 2026-04-28, 2026-05-11, 2026-05-25).

All 11 builds, AST fuzzer (4 variants), BuzzHouse (4 variants), all stress tests, all other integration test variants (amd_asan_ubsan / amd_msan / amd_tsan / amd_llvm_coverage / amd_binary_excluded / arm_binary distributed plan 1-3/4), all functional tests across sanitizers, unit tests (asan/tsan/msan), Style check, Fast test, Bugfix validation, Code Review, LLVM Coverage, SQLLogic, Performance Comparison, Docs check, Compatibility, Install packages, Docker images, Upgrade check: all SUCCESS.

Awaiting reviewer re-review.

@antonio2368
antonio2368 added this pull request to the merge queue Jun 9, 2026
Merged via the queue into ClickHouse:master with commit 758954f Jun 9, 2026
164 of 166 checks passed
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jun 9, 2026
niyue pushed a commit to niyue/ClickHouse that referenced this pull request Jun 9, 2026
The two-argument form `regexpExtract(haystack, pattern)` now uses group
`0` when `pattern` has no capturing group and group `1` otherwise.
Reflect this conditional default in the registered `FunctionDocumentation`
and add a no-capture-group example.

Address ClickHouse#106374 (comment)... bot review.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
niyue pushed a commit to niyue/ClickHouse that referenced this pull request Jun 9, 2026
Per @antonio2368 review on PR ClickHouse#106374: replace the
`bool index_omitted + ssize_t index` pair with
`std::optional<ssize_t>` and resolve the conditional default with
`value_or(capture == 0 ? 0 : 1)`.

Behavior unchanged: a pattern with no capturing group still falls
back to group 0 when the third argument is omitted; an explicitly
supplied index keeps its current validation.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@clickgapai

Copy link
Copy Markdown
Contributor

Hi @groeneai @antonio2368 — while reviewing this PR I found the following:

Could you review the test PR and add the can be tested label if it looks good?
Happy to discuss — close anything that's wrong or already addressed.

pull Bot pushed a commit to AKJUS/ClickHouse that referenced this pull request Jun 11, 2026
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Jun 11, 2026
test_check_database_unity (and other test_database_delta tests that call
execute_spark_query) is chronically flaky: the spark-sql invocation
occasionally hangs for the full 600s timeout and the test fails with
subprocess.TimeoutExpired.

Root cause, from a jstack thread dump captured by the existing hang
diagnostics (PR ClickHouse#104801) on a CI failure (PR ClickHouse#106374,
Integration tests (arm_binary, distributed plan, 4/4)): the Spark main
thread parks for the whole timeout inside the Unity Catalog Java client:

  jdk.internal.net.http.HttpClientImpl.send
  io.unitycatalog.client.api.SchemasApi.getSchema
  io.unitycatalog.spark.UCProxy.loadNamespaceMetadata
  SupportsNamespaces.namespaceExists
  CreateNamespaceExec.run                         <- first CREATE SCHEMA

The UC client uses the JDK HttpClient with no per-request timeout, so a
single transient stall hangs forever. The hang is transient, not a UC
outage: in the same run the UC liveness probe answered in 7.8ms with
http_code 500, Ivy dependency resolution succeeded in 396ms (so this is
not a package-download problem), and the very next test
(test_multiple_schemes_tables, same CREATE SCHEMA path) ran 17s later and
passed in under a second once the stuck JVM was killed.

Retry the Spark invocation with a fresh JVM when it hangs. Only the
timeout is retried; real query errors are re-raised immediately. Per-attempt
timeout is 300s and at most two attempts are made, keeping the worst case
within the 900s per-test pytest limit.

The retry is opt-in (retry_on_timeout, default False) because the
pkill + metastore cleanup preamble does not reset Unity Catalog or Delta
committed state: blindly re-running a multi-statement batch that already
committed an earlier statement could fail on "already exists" or duplicate
INSERT rows. Non-idempotent callers keep the original single-attempt
behavior. test_check_database_unity opts in after being made idempotent
(CREATE SCHEMA/TABLE IF NOT EXISTS, INSERT OVERWRITE), so a fresh-JVM retry
after a partial commit converges to the same final state. The test asserts
only table count and CHECK DATABASE, so OVERWRITE vs INSERT changes no
assertion.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
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-bugfix Pull request with bugfix, not backported by default 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.

Poor usability of regexpExtract

5 participants