Repository navigation
perf(arrow-cast): optimize parsing of decimals from strings - #10668
Conversation
Rewrite parse_string_to_decimal_native to accumulate digits in u64 chunks that are folded into the target native type with checked arithmetic (one wide multiply per 19 digits), instead of splitting the string and round-tripping through i256 and intermediate allocations. String-to-decimal128 casts are ~8x faster and decimal256 casts ~6.5x faster; the parser microbenchmarks improve 64-92% across all cases. The checked arithmetic also fixes two bugs in the previous implementation: Decimal256 values whose unscaled magnitude exceeds the i256 range could silently wrap to an arbitrary in-range value instead of reporting overflow, and inputs with more than 76 fractional digits were rejected even when the scaled value fits the target type. Also use PrimitiveBuilder instead of an intermediate Vec and unsafe from_trusted_len_iter in the strict (safe=false) cast path. The safe path keeps the unsafe trusted-len construction: it measures 15-19% faster than a PrimitiveBuilder equivalent, and its justification comment now records that. Also document the accepted syntax and rounding behavior, and add benchmark coverage for string-to-decimal parsing.
|
i wonder if this will fix/affect some of the other issues we have open for decimal parsing/casting, see: edit: seems like none, those other issues are for a different decimal parsing function |
Yeah, I believe those issues are distinct. Although I think it might make sense to unify some of the code here, both to simplify the implementation and to address some inconsistencies (e.g., rounding vs. truncating when given more decimal digits than required for the target type's scale). I can take a look at some of that once this PR lands. |
8012d45 to
7f9a781
Compare
|
run benchmark parse_decimal |
This comment was marked as duplicate.
This comment was marked as duplicate.
|
🤖 Arrow criterion benchmark completed (GKE) | trigger Instance: Comparing neilc/perf-decimal-parser (7f9a781) to 4f0c3c8 (merge-base) diff Run configurationrun benchmark parse_decimalCPU Details (lscpu)Details
Resource Usagebase (merge-base)
branch
File an issue against this benchmark runner |
|
run benchmark cast_kernels |
This comment was marked as duplicate.
This comment was marked as duplicate.
|
🤖 Arrow criterion benchmark completed (GKE) | trigger Instance: Comparing neilc/perf-decimal-parser (7f9a781) to 4f0c3c8 (merge-base) diff Run configurationrun benchmark cast_kernels
env:
BENCH_FILTER: "string to decimal"CPU Details (lscpu)Details
Resource Usagebase (merge-base)
branch
File an issue against this benchmark runner |
Jefffrey
left a comment
There was a problem hiding this comment.
my local run of the parse_decimal benchmark, with the new benches:
string decimal128 integer
time: [15.733 ns 15.758 ns 15.782 ns]
+ change: [−86.119% −86.045% −85.974%] (p = 0.00 < 0.05)
+ Performance has improved.
Found 9 outliers among 100 measurements (9.00%)
2 (2.00%) low severe
4 (4.00%) low mild
3 (3.00%) high mild
string decimal128 exact scale
time: [15.383 ns 15.443 ns 15.508 ns]
+ change: [−86.121% −86.036% −85.955%] (p = 0.00 < 0.05)
+ Performance has improved.
Found 12 outliers among 100 measurements (12.00%)
1 (1.00%) low mild
8 (8.00%) high mild
3 (3.00%) high severe
string decimal128 padded scale
time: [15.591 ns 15.615 ns 15.638 ns]
+ change: [−87.092% −87.039% −86.988%] (p = 0.00 < 0.05)
+ Performance has improved.
Found 3 outliers among 100 measurements (3.00%)
1 (1.00%) low severe
2 (2.00%) low mild
string decimal128 rounded scale
time: [16.002 ns 16.066 ns 16.134 ns]
+ change: [−90.851% −90.820% −90.786%] (p = 0.00 < 0.05)
+ Performance has improved.
string decimal128 signed
time: [15.932 ns 15.987 ns 16.045 ns]
+ change: [−92.027% −91.995% −91.961%] (p = 0.00 < 0.05)
+ Performance has improved.
string decimal128 38 digits
time: [27.153 ns 27.196 ns 27.239 ns]
+ change: [−77.117% −77.040% −76.963%] (p = 0.00 < 0.05)
+ Performance has improved.
Found 1 outliers among 100 measurements (1.00%)
1 (1.00%) high mild
string decimal256 76 digits
time: [61.585 ns 61.706 ns 61.824 ns]
+ change: [−64.960% −64.841% −64.712%] (p = 0.00 < 0.05)
+ Performance has improved.
Found 2 outliers among 100 measurements (2.00%)
2 (2.00%) low mild
string decimal256 rounded scale
time: [61.868 ns 61.990 ns 62.116 ns]
+ change: [−85.577% −85.534% −85.487%] (p = 0.00 < 0.05)
+ Performance has improved.
Found 4 outliers among 100 measurements (4.00%)
1 (1.00%) low severe
2 (2.00%) low mild
1 (1.00%) high mildvery impressive results
| v.map(|v| { | ||
| parse_string_to_decimal_native::<T>(v, scale as usize) | ||
| .map_err(|_| { | ||
| let mut builder = PrimitiveBuilder::<T>::with_capacity(from.len()); |
There was a problem hiding this comment.
note: this change isnt captured in cast_kernels benchmark since we only benchmark for safe = true
| if fractionals == scale { | ||
| first_discarded_digit.get_or_insert(digit); | ||
| index += 1; | ||
| continue; |
There was a problem hiding this comment.
i do wonder if instead of continuing via the loop to verify we have a valid number, we specialize to another loop that checks if the remainder characters is an ascii digit; it might be able to vectorize better?
There was a problem hiding this comment.
Yeah that occurred to me -- it seemed a bit marginal because strings with long strings of digits beyond the target scale seems like a bit of a corner case. But it's probably worth investigating; I'll take a look as a follow-on PR if that's okay.
|
thanks @neilconway |
|
Thank you @Jefffrey and @neilconway |
…0668) # Which issue does this PR close? - Closes apache#10664 - Closes apache#10665 - Closes apache#10666 # Rationale for this change The previous implementation of `parse_string_to_decimal_native` used a string-manipulation approach: it trimmed the input string, split it based on `"."`, parsed both halves with `i256::from_string`, converted the result back to a string with `format!`, and parsed that _again_ with `i256::from_string`. Instead, we use a single pass over the input bytes. A simple state machine walks over the input digits, accumulating a running sum. This avoids all of the string manipulation and heap allocation of the previous approach. We further optimize this by accumulating the running sum in a `u64`, and then periodically folding that partial value into the running `decimal` value (we do this often enough that there is no risk of overflowing the `u64`). That trades a bit of redundant computation for doing more work in `u64` and less work in `decimal`; based on benchmarking, this is a clear win. Finally, we don't need to accumulate digits from the suffix of the string. Values beyond the target type's scale don't contribute to the result value; only the first such digit influences rounding behavior. This new approach also fixes two correctness bugs (apache#10664 and apache#10665) in the previous implementation. **Benchmarks** (M4 Max) Parser microbenchmarks (arrow-cast/benches/parse_decimal.rs): - string decimal128 integer: ~111 ns → 15.8 ns, −85.8% - string decimal128 exact scale: ~109 ns → 15.2 ns, −86.0% - string decimal128 padded scale: ~116 ns → 15.3 ns, −86.8% - string decimal128 rounded scale: ~175 ns → 15.7 ns, −91.0% - string decimal128 signed: 201.8 ns → 15.7 ns, −92.2% - string decimal128 38 digits: 116.8 ns → 27.2 ns, −76.8% - string decimal256 76 digits: 174.4 ns → 61.9 ns, −64.5% - string decimal256 rounded scale: 440.0 ns → 62.3 ns, −85.8% End-to-end cast kernel (arrow/benches/cast_kernels.rs, 512-row string array, safe mode): - cast string to decimal128(38, 3): 558 µs → 68.4 µs, −87.0% - cast string to decimal256(76, 3): 519 µs → 80.1 µs, −84.4% # What changes are included in this PR? * Rewrite `parse_string_to_decimal_native` as described above * Add benchmark coverage * Add unit tests * Clarify / extend comments on accepted syntax and rounding behavior * Replace a `Vec` + `unsafe` with `PrimitiveBuilder`, which saves an allocation * Improve error message reporting # Are these changes tested? Yes; existing tests pass, and new tests have been added. I also checked the new implementation against a naive oracle built using the [num-bigint](https://docs.rs/num-bigint/latest/num_bigint/) crate; the new implementation was consistent with `num-bigint` for 120M randomly generated inputs. # Are there any user-facing changes? No, aside from fixed bugs. # AI usage Iterated with the help of Claude Fable; I reviewed and understand the resulting code. --------- Co-authored-by: Jeffrey Vo <[email protected]>
arrow-cast previously contained two different string-to-decimal parsers: `parse_string_to_decimal_native`, used by `cast`, and `parse_decimal`, used by the CSV and JSON readers. Aside from redundancy, these code paths behaved differently (e.g., truncating vs rounding for digits beyond the target type's scale, whitespace trimming, support for e-notation, etc.), so decimal conversion behaved differently depending on how the decimal value arrived into arrow-rs. This branch replaces these two parsers with a single unified parser; `parse_decimal(s, precision_scale)` is now the public entry point and `parse_string_to_decimal_native` is a thin, deprecated wrapper over it. The new parser is based on the one-pass, u64-chunked parser in apache#10668, extended with support for e-notation and negative scales. This fixes a lot of bugs and ensures consistent behavior, but it does result in some behavior changes and a small performance regression for the CSV/JSON path; more details below. Bugs fixed: (all in the JSON/CSV path) - divide-by-zero panic or wrong values for some inputs in exponent notation (apache#10788, apache#5762) - overflow on inputs with >= 256 digits or long exponents (apache#10787) - negative scales were ignored (apache#10791) - `0e0`/`-0e0` were rejected (apache#10789) while `e5` and `-.` parsed as 0 (apache#10790) Behaviour changes: - CSV and JSON readers now round half away from zero instead of truncating digits beyond the scale (apache#9410, apache#9422, apache#7355) - `cast` from strings accepts exponent notation (apache#5068) and negative scales, which `can_cast_types` already advertised (apache#10792), and validates the target precision and scale before parsing any values - CSV and JSON readers now trim whitespace (apache#10793). Only ASCII whitespace characters are trimmed, which matches the behavior of the CSV float/int parsers; previously, the `cast` path trimmed Unicode whitespace as well, but it will no longer do so. - parse errors use `ArrowError::ParseError` with unified messages - `variant_get` validates the target precision for string inputs (apache#10794) Tests: new tests cover rounding, exponents, negative scale, whitespace, long inputs and the four widths, a seeded differential test checks 20k random inputs against a BigInt reference (num-bigint was added as a dev-dependency), and the CSV/JSON readers gain end-to-end tests for the new behavior and bugfixes listed above. Performance: - `cast` string-to-decimal: ~unchanged. The cast path already used the fast single-pass parser from apache#10668; the unified parser benchmarks within Criterion noise (~4%) of it. - CSV/JSON reader path: short inputs cost 1-2 ns more per value, while long Decimal256 inputs are ~35% faster. End-to-end, CSV reads of decimal columns are ~8% slower. I suspect there is room for further optimization here (which will now benefit both code paths!) to reach or exceed the previous performance, but I'd like to land the unified parser first before we tackle further optimizations. Closes apache#10787, closes apache#10788, closes apache#10789, closes apache#10790, closes apache#10791, closes apache#10792, closes apache#10793, closes apache#10794
…0668) # Which issue does this PR close? - Closes apache#10664 - Closes apache#10665 - Closes apache#10666 # Rationale for this change The previous implementation of `parse_string_to_decimal_native` used a string-manipulation approach: it trimmed the input string, split it based on `"."`, parsed both halves with `i256::from_string`, converted the result back to a string with `format!`, and parsed that _again_ with `i256::from_string`. Instead, we use a single pass over the input bytes. A simple state machine walks over the input digits, accumulating a running sum. This avoids all of the string manipulation and heap allocation of the previous approach. We further optimize this by accumulating the running sum in a `u64`, and then periodically folding that partial value into the running `decimal` value (we do this often enough that there is no risk of overflowing the `u64`). That trades a bit of redundant computation for doing more work in `u64` and less work in `decimal`; based on benchmarking, this is a clear win. Finally, we don't need to accumulate digits from the suffix of the string. Values beyond the target type's scale don't contribute to the result value; only the first such digit influences rounding behavior. This new approach also fixes two correctness bugs (apache#10664 and apache#10665) in the previous implementation. **Benchmarks** (M4 Max) Parser microbenchmarks (arrow-cast/benches/parse_decimal.rs): - string decimal128 integer: ~111 ns → 15.8 ns, −85.8% - string decimal128 exact scale: ~109 ns → 15.2 ns, −86.0% - string decimal128 padded scale: ~116 ns → 15.3 ns, −86.8% - string decimal128 rounded scale: ~175 ns → 15.7 ns, −91.0% - string decimal128 signed: 201.8 ns → 15.7 ns, −92.2% - string decimal128 38 digits: 116.8 ns → 27.2 ns, −76.8% - string decimal256 76 digits: 174.4 ns → 61.9 ns, −64.5% - string decimal256 rounded scale: 440.0 ns → 62.3 ns, −85.8% End-to-end cast kernel (arrow/benches/cast_kernels.rs, 512-row string array, safe mode): - cast string to decimal128(38, 3): 558 µs → 68.4 µs, −87.0% - cast string to decimal256(76, 3): 519 µs → 80.1 µs, −84.4% # What changes are included in this PR? * Rewrite `parse_string_to_decimal_native` as described above * Add benchmark coverage * Add unit tests * Clarify / extend comments on accepted syntax and rounding behavior * Replace a `Vec` + `unsafe` with `PrimitiveBuilder`, which saves an allocation * Improve error message reporting # Are these changes tested? Yes; existing tests pass, and new tests have been added. I also checked the new implementation against a naive oracle built using the [num-bigint](https://docs.rs/num-bigint/latest/num_bigint/) crate; the new implementation was consistent with `num-bigint` for 120M randomly generated inputs. # Are there any user-facing changes? No, aside from fixed bugs. # AI usage Iterated with the help of Claude Fable; I reviewed and understand the resulting code. --------- Co-authored-by: Jeffrey Vo <[email protected]>
…0668) # Which issue does this PR close? - Closes apache#10664 - Closes apache#10665 - Closes apache#10666 # Rationale for this change The previous implementation of `parse_string_to_decimal_native` used a string-manipulation approach: it trimmed the input string, split it based on `"."`, parsed both halves with `i256::from_string`, converted the result back to a string with `format!`, and parsed that _again_ with `i256::from_string`. Instead, we use a single pass over the input bytes. A simple state machine walks over the input digits, accumulating a running sum. This avoids all of the string manipulation and heap allocation of the previous approach. We further optimize this by accumulating the running sum in a `u64`, and then periodically folding that partial value into the running `decimal` value (we do this often enough that there is no risk of overflowing the `u64`). That trades a bit of redundant computation for doing more work in `u64` and less work in `decimal`; based on benchmarking, this is a clear win. Finally, we don't need to accumulate digits from the suffix of the string. Values beyond the target type's scale don't contribute to the result value; only the first such digit influences rounding behavior. This new approach also fixes two correctness bugs (apache#10664 and apache#10665) in the previous implementation. **Benchmarks** (M4 Max) Parser microbenchmarks (arrow-cast/benches/parse_decimal.rs): - string decimal128 integer: ~111 ns → 15.8 ns, −85.8% - string decimal128 exact scale: ~109 ns → 15.2 ns, −86.0% - string decimal128 padded scale: ~116 ns → 15.3 ns, −86.8% - string decimal128 rounded scale: ~175 ns → 15.7 ns, −91.0% - string decimal128 signed: 201.8 ns → 15.7 ns, −92.2% - string decimal128 38 digits: 116.8 ns → 27.2 ns, −76.8% - string decimal256 76 digits: 174.4 ns → 61.9 ns, −64.5% - string decimal256 rounded scale: 440.0 ns → 62.3 ns, −85.8% End-to-end cast kernel (arrow/benches/cast_kernels.rs, 512-row string array, safe mode): - cast string to decimal128(38, 3): 558 µs → 68.4 µs, −87.0% - cast string to decimal256(76, 3): 519 µs → 80.1 µs, −84.4% # What changes are included in this PR? * Rewrite `parse_string_to_decimal_native` as described above * Add benchmark coverage * Add unit tests * Clarify / extend comments on accepted syntax and rounding behavior * Replace a `Vec` + `unsafe` with `PrimitiveBuilder`, which saves an allocation * Improve error message reporting # Are these changes tested? Yes; existing tests pass, and new tests have been added. I also checked the new implementation against a naive oracle built using the [num-bigint](https://docs.rs/num-bigint/latest/num_bigint/) crate; the new implementation was consistent with `num-bigint` for 120M randomly generated inputs. # Are there any user-facing changes? No, aside from fixed bugs. # AI usage Iterated with the help of Claude Fable; I reviewed and understand the resulting code. --------- Co-authored-by: Jeffrey Vo <[email protected]>
…10850) # Which issue does this PR close? - Closes #10787, closes #10788, closes #10789, closes #10790, closes #10791, closes #10792, closes #10793, closes #10794 # Rationale for this change arrow-cast had two different string-to-decimal parsers: `parse_string_to_decimal_native`, used by `cast`, and `parse_decimal`, used by the CSV and JSON readers. Aside from redundancy, these code paths behaved differently (e.g., truncating vs rounding for digits beyond the target type's scale, whitespace trimming, support for e-notation, etc.), so decimal conversion behaved differently depending on how the decimal value arrived into arrow-rs. This PR replaces these parsers with a single unified parser; `parse_decimal(s, precision_scale)` is now the public entry point and `parse_string_to_decimal_native` is a thin, deprecated wrapper over it. The new parser is based on the one-pass, u64-chunked parser in #10668, extended with support for e-notation and negative scales. This fixes a lot of bugs and ensures consistent behavior, but it does result in some behavior changes and a small performance regression for the CSV/JSON path; more details below. Bugs fixed: (all in the JSON/CSV path) - divide-by-zero panic or wrong values for some inputs in exponent notation (#10788, #5762) - overflow on inputs with >= 256 digits or long exponents (#10787) - negative scales were ignored (#10791) - `0e0`/`-0e0` were incorrectly rejected (#10789), while `e5` and `-.` were incorrectly accepted (#10790) Behaviour changes: - CSV and JSON readers now round half away from zero instead of truncating digits beyond the scale (#9410, #9422, #7355) - `cast` from strings accepts exponent notation (#5068) and negative scales, which `can_cast_types` already advertised (#10792), and validates the target precision and scale before parsing any values - CSV and JSON readers now trim whitespace (#10793). Only ASCII whitespace characters are trimmed, which matches the behavior of the CSV float/int parsers; previously, the `cast` path trimmed Unicode whitespace as well, but it will no longer do so. - parse errors use `ArrowError::ParseError` with unified messages - `variant_get` validates the target precision for string inputs (#10794) Performance: - `cast` string-to-decimal: ~unchanged. The cast path already used the fast single-pass parser from #10668; the unified parser benchmarks within Criterion noise (~4%) of it. - CSV/JSON reader path: short inputs cost 1-2 ns more per value, while long Decimal256 inputs are ~35% faster. End-to-end, CSV reads of decimal columns are ~8% slower. I suspect there is room for further optimization here (which will now benefit both code paths!) to reach or exceed the previous performance, but I'd like to land the unified parser first before we tackle further optimizations. # What changes are included in this PR? See above. # Are these changes tested? New tests added to cover rounding, exponents, negative scale, whitespace, long inputs and the four widths, a seeded differential test checks 20k random inputs against a BigInt reference (num-bigint was added as a dev-dependency), and the CSV/JSON readers gain end-to-end tests for the new behavior and bugfixes listed above. # Are there any user-facing changes? Yes; a deprecated public API, and user-visible behavioral changes in decimal parsing. # AI usage Iterated primarily with CC Fable 5; code reviewed by Codex GPT 5.6. I read, understand, and revised the resulting code.
…0668) # Which issue does this PR close? - Closes apache#10664 - Closes apache#10665 - Closes apache#10666 # Rationale for this change The previous implementation of `parse_string_to_decimal_native` used a string-manipulation approach: it trimmed the input string, split it based on `"."`, parsed both halves with `i256::from_string`, converted the result back to a string with `format!`, and parsed that _again_ with `i256::from_string`. Instead, we use a single pass over the input bytes. A simple state machine walks over the input digits, accumulating a running sum. This avoids all of the string manipulation and heap allocation of the previous approach. We further optimize this by accumulating the running sum in a `u64`, and then periodically folding that partial value into the running `decimal` value (we do this often enough that there is no risk of overflowing the `u64`). That trades a bit of redundant computation for doing more work in `u64` and less work in `decimal`; based on benchmarking, this is a clear win. Finally, we don't need to accumulate digits from the suffix of the string. Values beyond the target type's scale don't contribute to the result value; only the first such digit influences rounding behavior. This new approach also fixes two correctness bugs (apache#10664 and apache#10665) in the previous implementation. **Benchmarks** (M4 Max) Parser microbenchmarks (arrow-cast/benches/parse_decimal.rs): - string decimal128 integer: ~111 ns → 15.8 ns, −85.8% - string decimal128 exact scale: ~109 ns → 15.2 ns, −86.0% - string decimal128 padded scale: ~116 ns → 15.3 ns, −86.8% - string decimal128 rounded scale: ~175 ns → 15.7 ns, −91.0% - string decimal128 signed: 201.8 ns → 15.7 ns, −92.2% - string decimal128 38 digits: 116.8 ns → 27.2 ns, −76.8% - string decimal256 76 digits: 174.4 ns → 61.9 ns, −64.5% - string decimal256 rounded scale: 440.0 ns → 62.3 ns, −85.8% End-to-end cast kernel (arrow/benches/cast_kernels.rs, 512-row string array, safe mode): - cast string to decimal128(38, 3): 558 µs → 68.4 µs, −87.0% - cast string to decimal256(76, 3): 519 µs → 80.1 µs, −84.4% # What changes are included in this PR? * Rewrite `parse_string_to_decimal_native` as described above * Add benchmark coverage * Add unit tests * Clarify / extend comments on accepted syntax and rounding behavior * Replace a `Vec` + `unsafe` with `PrimitiveBuilder`, which saves an allocation * Improve error message reporting # Are these changes tested? Yes; existing tests pass, and new tests have been added. I also checked the new implementation against a naive oracle built using the [num-bigint](https://docs.rs/num-bigint/latest/num_bigint/) crate; the new implementation was consistent with `num-bigint` for 120M randomly generated inputs. # Are there any user-facing changes? No, aside from fixed bugs. # AI usage Iterated with the help of Claude Fable; I reviewed and understand the resulting code. --------- Co-authored-by: Jeffrey Vo <[email protected]>
…0668) # Which issue does this PR close? - Closes apache#10664 - Closes apache#10665 - Closes apache#10666 # Rationale for this change The previous implementation of `parse_string_to_decimal_native` used a string-manipulation approach: it trimmed the input string, split it based on `"."`, parsed both halves with `i256::from_string`, converted the result back to a string with `format!`, and parsed that _again_ with `i256::from_string`. Instead, we use a single pass over the input bytes. A simple state machine walks over the input digits, accumulating a running sum. This avoids all of the string manipulation and heap allocation of the previous approach. We further optimize this by accumulating the running sum in a `u64`, and then periodically folding that partial value into the running `decimal` value (we do this often enough that there is no risk of overflowing the `u64`). That trades a bit of redundant computation for doing more work in `u64` and less work in `decimal`; based on benchmarking, this is a clear win. Finally, we don't need to accumulate digits from the suffix of the string. Values beyond the target type's scale don't contribute to the result value; only the first such digit influences rounding behavior. This new approach also fixes two correctness bugs (apache#10664 and apache#10665) in the previous implementation. **Benchmarks** (M4 Max) Parser microbenchmarks (arrow-cast/benches/parse_decimal.rs): - string decimal128 integer: ~111 ns → 15.8 ns, −85.8% - string decimal128 exact scale: ~109 ns → 15.2 ns, −86.0% - string decimal128 padded scale: ~116 ns → 15.3 ns, −86.8% - string decimal128 rounded scale: ~175 ns → 15.7 ns, −91.0% - string decimal128 signed: 201.8 ns → 15.7 ns, −92.2% - string decimal128 38 digits: 116.8 ns → 27.2 ns, −76.8% - string decimal256 76 digits: 174.4 ns → 61.9 ns, −64.5% - string decimal256 rounded scale: 440.0 ns → 62.3 ns, −85.8% End-to-end cast kernel (arrow/benches/cast_kernels.rs, 512-row string array, safe mode): - cast string to decimal128(38, 3): 558 µs → 68.4 µs, −87.0% - cast string to decimal256(76, 3): 519 µs → 80.1 µs, −84.4% # What changes are included in this PR? * Rewrite `parse_string_to_decimal_native` as described above * Add benchmark coverage * Add unit tests * Clarify / extend comments on accepted syntax and rounding behavior * Replace a `Vec` + `unsafe` with `PrimitiveBuilder`, which saves an allocation * Improve error message reporting # Are these changes tested? Yes; existing tests pass, and new tests have been added. I also checked the new implementation against a naive oracle built using the [num-bigint](https://docs.rs/num-bigint/latest/num_bigint/) crate; the new implementation was consistent with `num-bigint` for 120M randomly generated inputs. # Are there any user-facing changes? No, aside from fixed bugs. # AI usage Iterated with the help of Claude Fable; I reviewed and understand the resulting code. --------- Co-authored-by: Jeffrey Vo <[email protected]>
…pache#10850) # Which issue does this PR close? - Closes apache#10787, closes apache#10788, closes apache#10789, closes apache#10790, closes apache#10791, closes apache#10792, closes apache#10793, closes apache#10794 # Rationale for this change arrow-cast had two different string-to-decimal parsers: `parse_string_to_decimal_native`, used by `cast`, and `parse_decimal`, used by the CSV and JSON readers. Aside from redundancy, these code paths behaved differently (e.g., truncating vs rounding for digits beyond the target type's scale, whitespace trimming, support for e-notation, etc.), so decimal conversion behaved differently depending on how the decimal value arrived into arrow-rs. This PR replaces these parsers with a single unified parser; `parse_decimal(s, precision_scale)` is now the public entry point and `parse_string_to_decimal_native` is a thin, deprecated wrapper over it. The new parser is based on the one-pass, u64-chunked parser in apache#10668, extended with support for e-notation and negative scales. This fixes a lot of bugs and ensures consistent behavior, but it does result in some behavior changes and a small performance regression for the CSV/JSON path; more details below. Bugs fixed: (all in the JSON/CSV path) - divide-by-zero panic or wrong values for some inputs in exponent notation (apache#10788, apache#5762) - overflow on inputs with >= 256 digits or long exponents (apache#10787) - negative scales were ignored (apache#10791) - `0e0`/`-0e0` were incorrectly rejected (apache#10789), while `e5` and `-.` were incorrectly accepted (apache#10790) Behaviour changes: - CSV and JSON readers now round half away from zero instead of truncating digits beyond the scale (apache#9410, apache#9422, apache#7355) - `cast` from strings accepts exponent notation (apache#5068) and negative scales, which `can_cast_types` already advertised (apache#10792), and validates the target precision and scale before parsing any values - CSV and JSON readers now trim whitespace (apache#10793). Only ASCII whitespace characters are trimmed, which matches the behavior of the CSV float/int parsers; previously, the `cast` path trimmed Unicode whitespace as well, but it will no longer do so. - parse errors use `ArrowError::ParseError` with unified messages - `variant_get` validates the target precision for string inputs (apache#10794) Performance: - `cast` string-to-decimal: ~unchanged. The cast path already used the fast single-pass parser from apache#10668; the unified parser benchmarks within Criterion noise (~4%) of it. - CSV/JSON reader path: short inputs cost 1-2 ns more per value, while long Decimal256 inputs are ~35% faster. End-to-end, CSV reads of decimal columns are ~8% slower. I suspect there is room for further optimization here (which will now benefit both code paths!) to reach or exceed the previous performance, but I'd like to land the unified parser first before we tackle further optimizations. # What changes are included in this PR? See above. # Are these changes tested? New tests added to cover rounding, exponents, negative scale, whitespace, long inputs and the four widths, a seeded differential test checks 20k random inputs against a BigInt reference (num-bigint was added as a dev-dependency), and the CSV/JSON readers gain end-to-end tests for the new behavior and bugfixes listed above. # Are there any user-facing changes? Yes; a deprecated public API, and user-visible behavioral changes in decimal parsing. # AI usage Iterated primarily with CC Fable 5; code reviewed by Codex GPT 5.6. I read, understand, and revised the resulting code.
…0668) # Which issue does this PR close? - Closes apache#10664 - Closes apache#10665 - Closes apache#10666 # Rationale for this change The previous implementation of `parse_string_to_decimal_native` used a string-manipulation approach: it trimmed the input string, split it based on `"."`, parsed both halves with `i256::from_string`, converted the result back to a string with `format!`, and parsed that _again_ with `i256::from_string`. Instead, we use a single pass over the input bytes. A simple state machine walks over the input digits, accumulating a running sum. This avoids all of the string manipulation and heap allocation of the previous approach. We further optimize this by accumulating the running sum in a `u64`, and then periodically folding that partial value into the running `decimal` value (we do this often enough that there is no risk of overflowing the `u64`). That trades a bit of redundant computation for doing more work in `u64` and less work in `decimal`; based on benchmarking, this is a clear win. Finally, we don't need to accumulate digits from the suffix of the string. Values beyond the target type's scale don't contribute to the result value; only the first such digit influences rounding behavior. This new approach also fixes two correctness bugs (apache#10664 and apache#10665) in the previous implementation. **Benchmarks** (M4 Max) Parser microbenchmarks (arrow-cast/benches/parse_decimal.rs): - string decimal128 integer: ~111 ns → 15.8 ns, −85.8% - string decimal128 exact scale: ~109 ns → 15.2 ns, −86.0% - string decimal128 padded scale: ~116 ns → 15.3 ns, −86.8% - string decimal128 rounded scale: ~175 ns → 15.7 ns, −91.0% - string decimal128 signed: 201.8 ns → 15.7 ns, −92.2% - string decimal128 38 digits: 116.8 ns → 27.2 ns, −76.8% - string decimal256 76 digits: 174.4 ns → 61.9 ns, −64.5% - string decimal256 rounded scale: 440.0 ns → 62.3 ns, −85.8% End-to-end cast kernel (arrow/benches/cast_kernels.rs, 512-row string array, safe mode): - cast string to decimal128(38, 3): 558 µs → 68.4 µs, −87.0% - cast string to decimal256(76, 3): 519 µs → 80.1 µs, −84.4% # What changes are included in this PR? * Rewrite `parse_string_to_decimal_native` as described above * Add benchmark coverage * Add unit tests * Clarify / extend comments on accepted syntax and rounding behavior * Replace a `Vec` + `unsafe` with `PrimitiveBuilder`, which saves an allocation * Improve error message reporting # Are these changes tested? Yes; existing tests pass, and new tests have been added. I also checked the new implementation against a naive oracle built using the [num-bigint](https://docs.rs/num-bigint/latest/num_bigint/) crate; the new implementation was consistent with `num-bigint` for 120M randomly generated inputs. # Are there any user-facing changes? No, aside from fixed bugs. # AI usage Iterated with the help of Claude Fable; I reviewed and understand the resulting code. --------- Co-authored-by: Jeffrey Vo <[email protected]>
…pache#10850) # Which issue does this PR close? - Closes apache#10787, closes apache#10788, closes apache#10789, closes apache#10790, closes apache#10791, closes apache#10792, closes apache#10793, closes apache#10794 # Rationale for this change arrow-cast had two different string-to-decimal parsers: `parse_string_to_decimal_native`, used by `cast`, and `parse_decimal`, used by the CSV and JSON readers. Aside from redundancy, these code paths behaved differently (e.g., truncating vs rounding for digits beyond the target type's scale, whitespace trimming, support for e-notation, etc.), so decimal conversion behaved differently depending on how the decimal value arrived into arrow-rs. This PR replaces these parsers with a single unified parser; `parse_decimal(s, precision_scale)` is now the public entry point and `parse_string_to_decimal_native` is a thin, deprecated wrapper over it. The new parser is based on the one-pass, u64-chunked parser in apache#10668, extended with support for e-notation and negative scales. This fixes a lot of bugs and ensures consistent behavior, but it does result in some behavior changes and a small performance regression for the CSV/JSON path; more details below. Bugs fixed: (all in the JSON/CSV path) - divide-by-zero panic or wrong values for some inputs in exponent notation (apache#10788, apache#5762) - overflow on inputs with >= 256 digits or long exponents (apache#10787) - negative scales were ignored (apache#10791) - `0e0`/`-0e0` were incorrectly rejected (apache#10789), while `e5` and `-.` were incorrectly accepted (apache#10790) Behaviour changes: - CSV and JSON readers now round half away from zero instead of truncating digits beyond the scale (apache#9410, apache#9422, apache#7355) - `cast` from strings accepts exponent notation (apache#5068) and negative scales, which `can_cast_types` already advertised (apache#10792), and validates the target precision and scale before parsing any values - CSV and JSON readers now trim whitespace (apache#10793). Only ASCII whitespace characters are trimmed, which matches the behavior of the CSV float/int parsers; previously, the `cast` path trimmed Unicode whitespace as well, but it will no longer do so. - parse errors use `ArrowError::ParseError` with unified messages - `variant_get` validates the target precision for string inputs (apache#10794) Performance: - `cast` string-to-decimal: ~unchanged. The cast path already used the fast single-pass parser from apache#10668; the unified parser benchmarks within Criterion noise (~4%) of it. - CSV/JSON reader path: short inputs cost 1-2 ns more per value, while long Decimal256 inputs are ~35% faster. End-to-end, CSV reads of decimal columns are ~8% slower. I suspect there is room for further optimization here (which will now benefit both code paths!) to reach or exceed the previous performance, but I'd like to land the unified parser first before we tackle further optimizations. # What changes are included in this PR? See above. # Are these changes tested? New tests added to cover rounding, exponents, negative scale, whitespace, long inputs and the four widths, a seeded differential test checks 20k random inputs against a BigInt reference (num-bigint was added as a dev-dependency), and the CSV/JSON readers gain end-to-end tests for the new behavior and bugfixes listed above. # Are there any user-facing changes? Yes; a deprecated public API, and user-visible behavioral changes in decimal parsing. # AI usage Iterated primarily with CC Fable 5; code reviewed by Codex GPT 5.6. I read, understand, and revised the resulting code.
…0668) # Which issue does this PR close? - Closes apache#10664 - Closes apache#10665 - Closes apache#10666 # Rationale for this change The previous implementation of `parse_string_to_decimal_native` used a string-manipulation approach: it trimmed the input string, split it based on `"."`, parsed both halves with `i256::from_string`, converted the result back to a string with `format!`, and parsed that _again_ with `i256::from_string`. Instead, we use a single pass over the input bytes. A simple state machine walks over the input digits, accumulating a running sum. This avoids all of the string manipulation and heap allocation of the previous approach. We further optimize this by accumulating the running sum in a `u64`, and then periodically folding that partial value into the running `decimal` value (we do this often enough that there is no risk of overflowing the `u64`). That trades a bit of redundant computation for doing more work in `u64` and less work in `decimal`; based on benchmarking, this is a clear win. Finally, we don't need to accumulate digits from the suffix of the string. Values beyond the target type's scale don't contribute to the result value; only the first such digit influences rounding behavior. This new approach also fixes two correctness bugs (apache#10664 and apache#10665) in the previous implementation. **Benchmarks** (M4 Max) Parser microbenchmarks (arrow-cast/benches/parse_decimal.rs): - string decimal128 integer: ~111 ns → 15.8 ns, −85.8% - string decimal128 exact scale: ~109 ns → 15.2 ns, −86.0% - string decimal128 padded scale: ~116 ns → 15.3 ns, −86.8% - string decimal128 rounded scale: ~175 ns → 15.7 ns, −91.0% - string decimal128 signed: 201.8 ns → 15.7 ns, −92.2% - string decimal128 38 digits: 116.8 ns → 27.2 ns, −76.8% - string decimal256 76 digits: 174.4 ns → 61.9 ns, −64.5% - string decimal256 rounded scale: 440.0 ns → 62.3 ns, −85.8% End-to-end cast kernel (arrow/benches/cast_kernels.rs, 512-row string array, safe mode): - cast string to decimal128(38, 3): 558 µs → 68.4 µs, −87.0% - cast string to decimal256(76, 3): 519 µs → 80.1 µs, −84.4% # What changes are included in this PR? * Rewrite `parse_string_to_decimal_native` as described above * Add benchmark coverage * Add unit tests * Clarify / extend comments on accepted syntax and rounding behavior * Replace a `Vec` + `unsafe` with `PrimitiveBuilder`, which saves an allocation * Improve error message reporting # Are these changes tested? Yes; existing tests pass, and new tests have been added. I also checked the new implementation against a naive oracle built using the [num-bigint](https://docs.rs/num-bigint/latest/num_bigint/) crate; the new implementation was consistent with `num-bigint` for 120M randomly generated inputs. # Are there any user-facing changes? No, aside from fixed bugs. # AI usage Iterated with the help of Claude Fable; I reviewed and understand the resulting code. --------- Co-authored-by: Jeffrey Vo <[email protected]>
…pache#10850) # Which issue does this PR close? - Closes apache#10787, closes apache#10788, closes apache#10789, closes apache#10790, closes apache#10791, closes apache#10792, closes apache#10793, closes apache#10794 # Rationale for this change arrow-cast had two different string-to-decimal parsers: `parse_string_to_decimal_native`, used by `cast`, and `parse_decimal`, used by the CSV and JSON readers. Aside from redundancy, these code paths behaved differently (e.g., truncating vs rounding for digits beyond the target type's scale, whitespace trimming, support for e-notation, etc.), so decimal conversion behaved differently depending on how the decimal value arrived into arrow-rs. This PR replaces these parsers with a single unified parser; `parse_decimal(s, precision_scale)` is now the public entry point and `parse_string_to_decimal_native` is a thin, deprecated wrapper over it. The new parser is based on the one-pass, u64-chunked parser in apache#10668, extended with support for e-notation and negative scales. This fixes a lot of bugs and ensures consistent behavior, but it does result in some behavior changes and a small performance regression for the CSV/JSON path; more details below. Bugs fixed: (all in the JSON/CSV path) - divide-by-zero panic or wrong values for some inputs in exponent notation (apache#10788, apache#5762) - overflow on inputs with >= 256 digits or long exponents (apache#10787) - negative scales were ignored (apache#10791) - `0e0`/`-0e0` were incorrectly rejected (apache#10789), while `e5` and `-.` were incorrectly accepted (apache#10790) Behaviour changes: - CSV and JSON readers now round half away from zero instead of truncating digits beyond the scale (apache#9410, apache#9422, apache#7355) - `cast` from strings accepts exponent notation (apache#5068) and negative scales, which `can_cast_types` already advertised (apache#10792), and validates the target precision and scale before parsing any values - CSV and JSON readers now trim whitespace (apache#10793). Only ASCII whitespace characters are trimmed, which matches the behavior of the CSV float/int parsers; previously, the `cast` path trimmed Unicode whitespace as well, but it will no longer do so. - parse errors use `ArrowError::ParseError` with unified messages - `variant_get` validates the target precision for string inputs (apache#10794) Performance: - `cast` string-to-decimal: ~unchanged. The cast path already used the fast single-pass parser from apache#10668; the unified parser benchmarks within Criterion noise (~4%) of it. - CSV/JSON reader path: short inputs cost 1-2 ns more per value, while long Decimal256 inputs are ~35% faster. End-to-end, CSV reads of decimal columns are ~8% slower. I suspect there is room for further optimization here (which will now benefit both code paths!) to reach or exceed the previous performance, but I'd like to land the unified parser first before we tackle further optimizations. # What changes are included in this PR? See above. # Are these changes tested? New tests added to cover rounding, exponents, negative scale, whitespace, long inputs and the four widths, a seeded differential test checks 20k random inputs against a BigInt reference (num-bigint was added as a dev-dependency), and the CSV/JSON readers gain end-to-end tests for the new behavior and bugfixes listed above. # Are there any user-facing changes? Yes; a deprecated public API, and user-visible behavioral changes in decimal parsing. # AI usage Iterated primarily with CC Fable 5; code reviewed by Codex GPT 5.6. I read, understand, and revised the resulting code.
Which issue does this PR close?
Decimal256#10665Rationale for this change
The previous implementation of
parse_string_to_decimal_nativeused a string-manipulation approach: it trimmed the input string, split it based on".", parsed both halves withi256::from_string, converted the result back to a string withformat!, and parsed that again withi256::from_string.Instead, we use a single pass over the input bytes. A simple state machine walks over the input digits, accumulating a running sum. This avoids all of the string manipulation and heap allocation of the previous approach.
We further optimize this by accumulating the running sum in a
u64, and then periodically folding that partial value into the runningdecimalvalue (we do this often enough that there is no risk of overflowing theu64). That trades a bit of redundant computation for doing more work inu64and less work indecimal; based on benchmarking, this is a clear win.Finally, we don't need to accumulate digits from the suffix of the string. Values beyond the target type's scale don't contribute to the result value; only the first such digit influences rounding behavior.
This new approach also fixes two correctness bugs (#10664 and #10665) in the previous implementation.
Benchmarks (M4 Max)
Parser microbenchmarks (arrow-cast/benches/parse_decimal.rs):
End-to-end cast kernel (arrow/benches/cast_kernels.rs, 512-row string array, safe mode):
What changes are included in this PR?
parse_string_to_decimal_nativeas described aboveVec+unsafewithPrimitiveBuilder, which saves an allocationAre these changes tested?
Yes; existing tests pass, and new tests have been added. I also checked the new implementation against a naive oracle built using the num-bigint crate; the new implementation was consistent with
num-bigintfor 120M randomly generated inputs.Are there any user-facing changes?
No, aside from fixed bugs.
AI usage
Iterated with the help of Claude Fable; I reviewed and understand the resulting code.