← Back to Pull requests

[SPARK-59531][SQL] Avoid a per-match MatchResult allocation in regexp_extract, regexp_extract_all, and regexp_instr #58815

Closed
unknown closed this pull request without merging on Sep 22, 2026 10:15 UTC
View on GitHub
unknown unknown commented on Sep 15, 2026 13:50 UTC
Author

What changes were proposed in this pull request?

RegExpExtractBase.extract, RegExpExtractBase.extractAll, and RegExpInStr call
Matcher.toMatchResult() on every match, then read groupCount/group(idx) (the extract
helpers) or start() (regexp_instr) from the returned snapshot:

if (matcher.find()) {
  val mr = matcher.toMatchResult
  checkGroupIndex(prettyName, mr.groupCount, idx)
  val group = mr.group(idx)
  ...
}

Matcher.toMatchResult() allocates an immutable MatchResult snapshot of the current match
state, including a copy of the groups int[]. This PR reads the same values directly from the
Matcher:

if (matcher.find()) {
  checkGroupIndex(prettyName, matcher.groupCount, idx)
  val group = matcher.group(idx)
  ...
}

and m.toMatchResult().start() -> m.start() in RegExpInStr (both the interpreted and the
codegen paths).

Why are the changes needed?

The values are read immediately after find(), with nothing else mutating the matcher in
between, so Matcher.groupCount(), Matcher.group(int), and Matcher.start() return exactly
the same values as the corresponding MatchResult methods for the current match. Snapshotting
into a MatchResult first only adds one allocation (the snapshot plus a copy of the groups
int[]) per match on the regexp_extract, regexp_extract_all, and regexp_instr hot paths.

Profiling a regexp_extract workload under JFR (settings=profile) shows
java.util.regex.Matcher.toMatchResult accounting for ~5% of sampled allocation pressure;
this change removes it. Wall-clock time is unchanged within run-to-run noise (this is an
allocation / GC-pressure reduction, not a CPU one).

Does this PR introduce _any_ user-facing change?

No.

How was this patch tested?

Existing tests, which cover regexp_extract, regexp_extract_all, and regexp_instr:

build/sbt 'catalyst/testOnly *RegexpExpressionsSuite'

All 24 tests pass.

Was this patch authored or co-authored using generative AI tooling?

Generated-by: Isaac

This pull request and its description were written by Isaac.

Diff: 265 lines (+198 / -67) across 3 files Reviewer: stevomitric Review duration: 9864m
All Vouch review confidence checks passed
Residual risk 0.16 = Change Risk (0.39) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.39 · 265 lines diff across 3 files
0.39
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
0.17
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
60%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.334
Shallow Review Depth shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #58815"
general weight: 0.35
Reviewers
Assignees
Unassigned
Labels
risk: low confidence: 60.5% reviewed
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 39.3% (MEDIUM) · Higher = larger blast radius
39.3%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 60.5% · Lower = rushed or shallow review
60.5%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 15.5% · Safe to merge
15.5%
Repository