Visitar URL original
[SPARK-60045][SQL] Avoid per-value regex compilation when parsing decimals in CSV/JSON/XML by dwsmith1983 · Pull Request #59273 · apache/spark · GitHub
Skip to content

[SPARK-60045][SQL] Avoid per-value regex compilation when parsing decimals in CSV/JSON/XML - #59273

Open
dwsmith1983 wants to merge 1 commit into
apache:masterfrom
dwsmith1983:perf/csv-decimal-parse-replace
Open

dwsmith1983 wants to merge 1 commit into
apache:masterfrom
dwsmith1983:perf/csv-decimal-parse-replace

Conversation

@dwsmith1983

Copy link
Copy Markdown
Contributor

What changes were proposed in this pull request?

ExprUtils.getDecimalParser strips grouping commas with s.replaceAll(",", "") for the default en-US locale. This PR uses s.replace(",", "") instead. The pattern is a literal comma, so the result is the same, but replace does not compile a regex.

Why are the changes needed?

Performance. String.replaceAll compiles a java.util.regex.Pattern on every call, and this parser runs once per decimal value in the CSV reader (UnivocityParser), the JSON reader (decimals given as strings), the XML reader and JSON/XML schema inference.

On a 2M row CSV file with 5 DECIMAL(18,2) columns (local[1], JDK 17, best of 3):

Before After
One parse (new BigDecimal(s.replaceAll/replace(",", ""))) 85.4 ns 14.8 ns
Read the file with the DECIMAL schema 1638 ms 842 ms
Read the same file with the columns as STRING (reference) 628 ms 671 ms

The DECIMAL read is 1.95x faster. Decimal parsing used to add about 1 s over reading the columns as strings and now adds about 0.17 s.

Does this PR introduce any user-facing change?

No.

How was this patch tested?

Existing tests already parse grouped values such as 1,000.01 and 158,058,049.001 through this code with the en-US locale. They pass: UnivocityParserSuite, CSVInferSchemaSuite, JsonInferSchemaSuite, JsonExpressionsSuite, XmlExpressionsSuite, and the decimal tests in CSVv1Suite, JsonV1Suite and XmlSuite.

The numbers above come from a local benchmark run before and after the change on the same commit.

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

No

@dwsmith1983

Copy link
Copy Markdown
Contributor Author

cc @MaxGekk @HyukjinKwon, a one-line change: replaceAll compiled a regex for every decimal value parsed.

This branch has not been deployed

No deployments
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