Skip to content

[SPARK-57173][SQL] Simplify regexp pattern-compile codegen by extracting a static Java helper#56223

Open
gengliangwang wants to merge 2 commits into
apache:masterfrom
gengliangwang:spark-regexp-compile-codegen
Open

[SPARK-57173][SQL] Simplify regexp pattern-compile codegen by extracting a static Java helper#56223
gengliangwang wants to merge 2 commits into
apache:masterfrom
gengliangwang:spark-regexp-compile-codegen

Conversation

@gengliangwang
Copy link
Copy Markdown
Member

What changes were proposed in this pull request?

Add ExpressionImplUtils.compileRegexPattern(String regex, int flags, String funcName), which wraps Pattern.compile and maps a PatternSyntaxException to the user-facing INVALID_PARAMETER_VALUE.PATTERN error. Route both the shared codegen (RegExpUtils.initLastMatcherCode, used by the whole regexp expression family -- RLike, RegExpReplace, RegExpExtract, RegExpExtractAll, RegExpInStr, etc.) and the eval helper (RegExpUtils.getPatternAndLastRegex) through it.

initLastMatcherCode previously emitted a 5-line inline try { Pattern.compile(...) } catch (PatternSyntaxException) block; it now emits a single helper call. The per-stage mutable-state caching (lastRegex / pattern) is preserved in the generated code.

Why are the changes needed?

Part of SPARK-56908 (umbrella). This block is emitted by every regexp expression in every stage that uses one; collapsing it to a single call shrinks the generated Java across the whole family, helping with the JVM 64KB method / constant-pool limits, Janino compile time, and JIT work.

Does this PR introduce any user-facing change?

No. The compiled behavior is identical; only the emitted Java source text changes.

How was this patch tested?

build/sbt "catalyst/testOnly *RegexpExpressionsSuite"

21/21 pass (exercised both with and without whole-stage codegen).

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

Generated-by: Claude Code (Opus 4.8)

…ing a static Java helper

### What changes were proposed in this pull request?

Add `ExpressionImplUtils.compileRegexPattern(String regex, int flags, String
funcName)`, which wraps `Pattern.compile` and maps a `PatternSyntaxException` to
the user-facing INVALID_PARAMETER_VALUE.PATTERN error. Route both the shared
codegen (`RegExpUtils.initLastMatcherCode`, used by the whole regexp expression
family -- RLike, RegExpReplace, RegExpExtract, RegExpExtractAll, RegExpInStr,
etc.) and the eval helper (`RegExpUtils.getPatternAndLastRegex`) through it.

`initLastMatcherCode` previously emitted a 5-line inline
`try { Pattern.compile } catch (PatternSyntaxException)` block; it now emits a
single helper call. The per-stage mutable-state caching (`lastRegex` /
`pattern`) is preserved in the generated code.

### Why are the changes needed?

Part of SPARK-56908 (umbrella). This block is emitted by every regexp expression
in every stage that uses one; collapsing it to a single call shrinks the
generated Java across the whole family.

### Does this PR introduce _any_ user-facing change?

No. The compiled behavior is identical; only the emitted Java source text changes.

### How was this patch tested?

```
build/sbt "catalyst/testOnly *RegexpExpressionsSuite"
```

21/21 pass (exercised both with and without whole-stage codegen).

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

Generated-by: Claude Code (Opus 4.8)

Co-authored-by: Isaac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant