Skip to content

chore(arrow-cast): support white space prefixed parse for ints and floats - #10374

Merged
Jefffrey merged 4 commits into
apache:mainfrom
Rich-T-kid:rich-T-kid/allow-white-space-aware-parse
Jul 20, 2026
Merged

chore(arrow-cast): support white space prefixed parse for ints and floats#10374
Jefffrey merged 4 commits into
apache:mainfrom
Rich-T-kid:rich-T-kid/allow-white-space-aware-parse

Conversation

@Rich-T-kid

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Rationale for this change

currently parsing ints and floats with white space prefiing the number caused the cast to fail

assert_eq!(Float64Type::parse("\n\t\n\t\n40.5123"), Some(40.5123));
assert_eq!(Float64Type::parse(" 1.5"), Some(1.5));
assert_eq!(Int32Type::parse(" 3"), Some(3));
assert_eq!(Int32Type::parse("          30"), Some(30));

What changes are included in this PR?

this PR adds a check to the string before parsing. it checks the first byte, if its not an ascii digit it will trim the left side of the string.

Performance was a consideration to avoid regressions here — see #9537 (comment). In the worst case where a string doesn't need trimming (no leading whitespace), the cost is just one slice operation to grab the first byte plus a range comparison to check if it's an ASCII digit.

benchmarks show no significant change
Image 7-18-26 at 1 02 PM

Are these changes tested?

yes. I created a test with 12 assertiosn to validate behavoir

Are there any user-facing changes?

yes. arrow-rs/datafusion cast operations will no longer fail when a number is prefixed with white space.

@github-actions github-actions Bot added the arrow Changes to the arrow crate label Jul 18, 2026
@Rich-T-kid

Copy link
Copy Markdown
Contributor Author

cc @alamb @aryan-212 @Rafferty97 since you had interest in #9538 & #9537

@Rich-T-kid

Copy link
Copy Markdown
Contributor Author

supporting post-fix white space should be pretty similar as well

@aryan-212

aryan-212 commented Jul 18, 2026

Copy link
Copy Markdown

@Rich-T-kid Since we are only concerned with ascii whitespaces, you can use trim_ascii_start() instead of trim_start() which is catered towards unicode whitespaces. Would be even faster, perhaps.
wdyt?

@Rich-T-kid

Copy link
Copy Markdown
Contributor Author

@Rich-T-kid Since we are only concerned with ascii whitespaces, you can use trim_ascii_start() instead of trim_start() which is catered towards unicode whitespaces. Would be even faster, perhaps. wdyt?

@aryan-212 thanks for the suggestion, ill update the PR

@Jefffrey

Copy link
Copy Markdown
Contributor

the screenshotted benchmarks are for string -> f32, but it seems f32/f16 paths are not changed only f64. i think we need to add to f32/f16 and rerun those benchmarks

@Rich-T-kid

Copy link
Copy Markdown
Contributor Author

the screenshotted benchmarks are for string -> f32, but it seems f32/f16 paths are not changed only f64. i think we need to add to f32/f16 and rerun those benchmarks

whoops 😅. I forgot to add this to the f32/16 branches.

@Jefffrey should be good now

@Jefffrey

Copy link
Copy Markdown
Contributor

run benchmark cast_kernels
env:
BENCH_FILTER: utf8 to f32

@adriangbot

This comment was marked as duplicate.

@adriangbot

Copy link
Copy Markdown

🤖 Arrow criterion benchmark completed (GKE) | trigger

Instance: c4a-highmem-16 (12 vCPU / 65 GiB)

CPU Details (lscpu)
Architecture:                            aarch64
CPU op-mode(s):                          64-bit
Byte Order:                              Little Endian
CPU(s):                                  16
On-line CPU(s) list:                     0-15
Vendor ID:                               ARM
Model name:                              Neoverse-V2
Model:                                   1
Thread(s) per core:                      1
Core(s) per cluster:                     16
Socket(s):                               -
Cluster(s):                              1
Stepping:                                r0p1
BogoMIPS:                                2000.00
Flags:                                   fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp cpuid asimdrdm jscvt fcma lrcpc dcpop sha3 sm3 sm4 asimddp sha512 sve asimdfhm dit uscat ilrcpc flagm sb paca pacg dcpodp sve2 sveaes svepmull svebitperm svesha3 svesm4 flagm2 frint svei8mm svebf16 i8mm bf16 dgh rng bti
L1d cache:                               1 MiB (16 instances)
L1i cache:                               1 MiB (16 instances)
L2 cache:                                32 MiB (16 instances)
L3 cache:                                80 MiB (1 instance)
NUMA node(s):                            1
NUMA node0 CPU(s):                       0-15
Vulnerability Gather data sampling:      Not affected
Vulnerability Indirect target selection: Not affected
Vulnerability Itlb multihit:             Not affected
Vulnerability L1tf:                      Not affected
Vulnerability Mds:                       Not affected
Vulnerability Meltdown:                  Not affected
Vulnerability Mmio stale data:           Not affected
Vulnerability Reg file data sampling:    Not affected
Vulnerability Retbleed:                  Not affected
Vulnerability Spec rstack overflow:      Not affected
Vulnerability Spec store bypass:         Mitigation; Speculative Store Bypass disabled via prctl
Vulnerability Spectre v1:                Mitigation; __user pointer sanitization
Vulnerability Spectre v2:                Mitigation; CSV2, BHB
Vulnerability Srbds:                     Not affected
Vulnerability Tsa:                       Not affected
Vulnerability Tsx async abort:           Not affected
Vulnerability Vmscape:                   Not affected
Details

group               main                                   rich-T-kid_allow-white-space-aware-parse
-----               ----                                   ----------------------------------------
cast utf8 to f32    1.01      5.7±0.04µs        ? ?/sec    1.00      5.7±0.03µs        ? ?/sec

Resource Usage

base (merge-base)

Metric Value
Wall time 10.0s
Peak memory 7.4 MiB
Avg memory 5.7 MiB
CPU user 7.6s
CPU sys 0.0s
Peak spill 0 B

branch

Metric Value
Wall time 15.0s
Peak memory 7.3 MiB
Avg memory 5.0 MiB
CPU user 10.0s
CPU sys 0.0s
Peak spill 0 B

File an issue against this benchmark runner

@Rich-T-kid

Copy link
Copy Markdown
Contributor Author

cast utf8 to f32 1.01 5.7±0.04µs ? ?/sec 1.00 5.7±0.03µs ? ?/sec

nice

@Jefffrey

Copy link
Copy Markdown
Contributor

run benchmark cast_kernels
env:
BENCH_FILTER: utf8 to f32

Comment thread arrow-cast/src/parse.rs Outdated
Comment thread arrow-cast/src/parse.rs
@adriangbot

This comment was marked as duplicate.

@adriangbot

Copy link
Copy Markdown

🤖 Arrow criterion benchmark completed (GKE) | trigger

Instance: c4a-highmem-16 (12 vCPU / 65 GiB)

CPU Details (lscpu)
Architecture:                            aarch64
CPU op-mode(s):                          64-bit
Byte Order:                              Little Endian
CPU(s):                                  16
On-line CPU(s) list:                     0-15
Vendor ID:                               ARM
Model name:                              Neoverse-V2
Model:                                   1
Thread(s) per core:                      1
Core(s) per cluster:                     16
Socket(s):                               -
Cluster(s):                              1
Stepping:                                r0p1
BogoMIPS:                                2000.00
Flags:                                   fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp cpuid asimdrdm jscvt fcma lrcpc dcpop sha3 sm3 sm4 asimddp sha512 sve asimdfhm dit uscat ilrcpc flagm sb paca pacg dcpodp sve2 sveaes svepmull svebitperm svesha3 svesm4 flagm2 frint svei8mm svebf16 i8mm bf16 dgh rng bti
L1d cache:                               1 MiB (16 instances)
L1i cache:                               1 MiB (16 instances)
L2 cache:                                32 MiB (16 instances)
L3 cache:                                80 MiB (1 instance)
NUMA node(s):                            1
NUMA node0 CPU(s):                       0-15
Vulnerability Gather data sampling:      Not affected
Vulnerability Indirect target selection: Not affected
Vulnerability Itlb multihit:             Not affected
Vulnerability L1tf:                      Not affected
Vulnerability Mds:                       Not affected
Vulnerability Meltdown:                  Not affected
Vulnerability Mmio stale data:           Not affected
Vulnerability Reg file data sampling:    Not affected
Vulnerability Retbleed:                  Not affected
Vulnerability Spec rstack overflow:      Not affected
Vulnerability Spec store bypass:         Mitigation; Speculative Store Bypass disabled via prctl
Vulnerability Spectre v1:                Mitigation; __user pointer sanitization
Vulnerability Spectre v2:                Mitigation; CSV2, BHB
Vulnerability Srbds:                     Not affected
Vulnerability Tsa:                       Not affected
Vulnerability Tsx async abort:           Not affected
Vulnerability Vmscape:                   Not affected
Details

group               main                                   rich-T-kid_allow-white-space-aware-parse
-----               ----                                   ----------------------------------------
cast utf8 to f32    1.00      5.7±0.03µs        ? ?/sec    1.00      5.7±0.03µs        ? ?/sec

Resource Usage

base (merge-base)

Metric Value
Wall time 10.0s
Peak memory 7.4 MiB
Avg memory 5.7 MiB
CPU user 7.8s
CPU sys 0.0s
Peak spill 0 B

branch

Metric Value
Wall time 10.0s
Peak memory 7.3 MiB
Avg memory 5.3 MiB
CPU user 7.0s
CPU sys 0.0s
Peak spill 0 B

File an issue against this benchmark runner

@Jefffrey
Jefffrey merged commit a16c458 into apache:main Jul 20, 2026
30 checks passed
@Jefffrey

Copy link
Copy Markdown
Contributor

thanks @Rich-T-kid & @aryan-212

@alamb

alamb commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

Nice!

alamb added a commit that referenced this pull request Jul 25, 2026
# Which issue does this PR close?

<!--
We generally require a GitHub issue to be filed for all bug fixes and
enhancements and this helps us generate change logs for our releases.
You can link an issue to this PR using the GitHub syntax.
-->

- Closes #9538.

# Rationale for this change

- see #9538
- see #10374

<!--
Why are you proposing this change? If this is already explained clearly
in the issue then this section is not needed.
Explaining clearly why changes are proposed helps reviewers understand
your changes and offer better suggestions for fixes.
-->

# What changes are included in this PR?
this PR adds support for suffix white space when calling parse
<!--
There is no need to duplicate the description in the issue here but it
is sometimes worth providing a summary of the individual changes in this
PR.
-->

# Are these changes tested?
yes
<!--
We typically require tests for all PRs in order to:
1. Prevent the code from being accidentally broken by subsequent changes
2. Serve as another way to document the expected behavior of the code

If tests are not included in your PR, please explain why (for example,
are they covered by existing tests)?

If this PR claims a performance improvement, please include evidence
such as benchmark results.
-->

# Are there any user-facing changes?
yes, users will be able to parse strings that have white space
prefixed/suffixed onto numbers/floats.
<!--
If there are user-facing changes then we may require documentation to be
updated before approving the PR.

If there are any breaking changes to public APIs, please call them out.
-->

---------

Co-authored-by: rich-T-kid <richardbaah@MacBook-Air-de-Richard.local>
Co-authored-by: Jeffrey Vo <jeffrey.vo.australia@gmail.com>
Co-authored-by: Andrew Lamb <andrew@nerdnetworks.org>
@Jefffrey Jefffrey added the enhancement Any new improvement worthy of a entry in the changelog label Jul 28, 2026
@alamb alamb changed the title support white space prefixed parse for ints and floats chore(arrow-cast): support white space prefixed parse for ints and floats Jul 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

arrow Changes to the arrow crate enhancement Any new improvement worthy of a entry in the changelog

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants