← Back to Pull requests

feat(spark): add Spark-compatible regexp_extract function #73

Closed
unknown closed this pull request without merging on Nov 19, 2026 12:48 UTC
View on GitHub
unknown unknown commented on Nov 12, 2026 16:15 UTC
Author

Closes #4.

Summary


  • Adds Spark's regexp_extract(str, regexp[, idx]), ported from DataFusion Comet's string_funcs/regexp_extract.rs. Part of #174.

  • Returns the text of group idx (default 1, 0 is the whole match) of the first match. NULL if any argument is NULL; empty string for no match or a group that did not participate.

  • An idx outside 0..=groupCount raises INVALID_PARAMETER_VALUE.REGEX_GROUP_INDEX, but only on rows that match; a row that does not match returns an empty string. The error class matches Spark 3.5 and later (3.4 used _LEGACY_ERROR_TEMP_2006/2007).

  • An invalid pattern raises INVALID_PARAMETER_VALUE.PATTERN, with the pattern quoted like Spark's toSQLValue. A NULL row is not checked, as in Spark, so an invalid pattern is only reported on a row it is applied to. For the same reason a literal pattern is not validated at planning time.

  • As in Spark, str and regexp of other types (numbers, binary, ...) are cast to strings, and numeric or string idx is cast to INT. Unsupported types are plan errors.

  • regexp and idx do not have to be literals. A literal pattern is compiled once and kept in a small cache on the UDF, keyed by pattern, so later batches reuse it. A column of patterns reuses the last compiled pattern across rows.

  • A caller that builds the physical expression directly, such as Comet, can pass Utf8, LargeUtf8 or Utf8View for str and a pattern column. The result is always Utf8.

  • Adds regex and regex-syntax (already in the lock file through regex) as direct dependencies.

  • Documents the function with #[user_doc].
  • Known gaps


    Patterns use the Rust regex engine, not java.util.regex. A shared Java-compatible regex helper is proposed in #255 and would also serve #257 and #259. Until then:
  • Look-around, backreferences, Java-only constructs (\Q...\E, \Z, \h, (?>...), (?d)) and patterns over the Rust compiled-size limit raise a not-implemented "Unsupported regex pattern" error, where Spark evaluates them. A rejected pattern is classified with regex_syntax: only the syntax errors Java also raises are INVALID_PARAMETER_VALUE.PATTERN.

  • Constructs that both engines accept but read differently are also reported as unsupported: the U flag, POSIX classes in brackets ([[:alpha:]]), the class operators -- and ~~, \< and \>, and Java's ASCII-only POSIX names in \p{..} (\p{Alpha}).

  • Constructs that Java rejects raise INVALID_PARAMETER_VALUE.PATTERN, as in Spark: the R flag, \b{start} and the other \b{...} boundaries, (?P<name>...), and group names that are not an ASCII letter followed by letters and digits.

  • Other \p{..} names that Rust knows and Java does not, such as \p{Letter} and \p{Latin}, are accepted where Spark raises an error.

  • With ANSI mode off, Spark wraps an out-of-range BIGINT idx, returns NULL for a malformed string idx, and casts NaN to 0. DataFusion's cast errors instead (#88).

  • Possessive quantifiers are read as nested greedy repetition (a++ as (a+)+), so (a++a) matches where Java does not.

  • \d, \w, \s match Unicode; Java's match ASCII only.

  • . matches \r; Java's does not.

  • $ matches only at the end of input; Java's also matches before a final line terminator.

  • (?i) uses Unicode case folding (k matches the Kelvin sign); Java folds ASCII only by default.
  • Each difference is recorded in the .slt file as a commented-out query with Spark 4.2.0's result.

    Testing


  • string/regexp_extract.slt. Expected values come from Spark's regexp-functions.sql.out golden file or were verified against Spark 4.2.0 with PySpark.

  • cargo clippy --all-targets --all-features -- -D warnings, cargo test -p datafusion-spark and cargo test -p datafusion-spark-sqllogictest pass.
  • Diff: 474 lines (+355 / -119) across 5 files Reviewer: kosiew Review duration: 9873m
    All Vouch review confidence checks passed
    Residual risk 0.18 = Change Risk (0.46) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.46 · 474 lines diff across 5 files
    0.46
    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.369
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #73"
    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: 45.7% (MEDIUM) · Higher = larger blast radius
    45.7%
    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: 18.1% · Safe to merge
    18.1%