Repository navigation
Conversation
|
Have you run the benchmarks for this? |
sorry, new here, could you tell me how do I run them? 😅 |
I think "cargo bench -p arrow-cast" should be sufficient. |
|
run benchmark cast_kernels |
|
Thank you @Rafferty97 and @aryan-212 -- I kicked off some benchmark runs to verify the performance implications of this change |
alamb
left a comment
There was a problem hiding this comment.
Thank you for this PR @aryan-212 and (especially) for the help reviwing @Rafferty97
| impl Parser for Float16Type { | ||
| fn parse(string: &str) -> Option<f16> { | ||
| lexical_core::parse(string.as_bytes()) | ||
| lexical_core::parse(string.trim().as_bytes()) |
There was a problem hiding this comment.
I wonder if there will be any performance implications 🤔
| #[test] | ||
| fn test_parse_trimmed_whitespace() { | ||
| // Float types | ||
| assert_eq!(Float16Type::parse(" 1.5 "), Some(f16::from_f32(1.5))); |
There was a problem hiding this comment.
Cn you please add some tests with only leading whitespace and some tests with only trailing whitespace?
|
🤖 Arrow criterion benchmark running (GKE) | trigger |
|
🤖 Arrow criterion benchmark completed (GKE) | trigger Details
Resource Usagebase (merge-base)
branch
|
I don't think there is any significant perf regression, isn't it? |
My reading of this result is that this code is 16% slower when parsing strings to floating point. I will rerun the benchmarks to see if this is reproducable |
|
run benchmark cast_kernels |
|
🤖 Arrow criterion benchmark running (GKE) | trigger |
|
🤖 Arrow criterion benchmark completed (GKE) | trigger Details
Resource Usagebase (merge-base)
branch
|
|
🤔 same slowdown:
|
|
hmmm, should I make changes in some other layer where the performance hit wouldn't be this much? Any pointers if possible? |
Well we can use the existing DataFusion CLI v52.3.0
> select ' 4.5'::float;
Optimizer rule 'simplify_expressions' failed
caused by
Arrow error: Cast error: Cannot cast string ' 4.5' to value of Float32 type
> select trim(' 4.5')::float;
+----------------------+
| btrim(Utf8(" 4.5")) |
+----------------------+
| 4.5 |
+----------------------+
1 row(s) fetched.
Elapsed 0.022 seconds.Interestingly DuckDB does automatically trim andrewlamb@Andrews-MacBook-Pro-3:~/Software/influxdb_pro/ent$ duckdb
DuckDB v1.5.0 (Variegata)
Enter ".help" for usage hints.
memory D select ' 4.5'::float;
┌────────────────────────┐
│ CAST(' 4.5' AS FLOAT) │
│ float │
├────────────────────────┤
│ 4.5 │
└────────────────────────┘As does postgres 🤔 andrewlamb@Andrews-MacBook-Pro-3:~/Software/influxdb_pro/ent$ psql -h localhost -U postgres
psql (14.22 (Homebrew), server 11.16 (Debian 11.16-1.pgdg90+1))
Type "help" for help.
postgres=# select ' 4.5'::float;
float8
--------
4.5
(1 row) |
|
Is it possible to improve the performance of trim? For example, trim just based on ascii whitespace rather than having to do utf8 checking? |
|
run benchmark cast_kernels |
|
Hi @aryan-212, thanks for the request (#9537 (comment)). Only whitelisted users can trigger benchmarks. Allowed users: Dandandan, Jefffrey, Omega359, adriangb, alamb, comphead, etseidl, gabotechs, geoffreyclaude, klion26, kosiew, rluvaton, xudong963, zhuqi-lucas. |
|
@alamb replaced |
|
run benchmark cast_kernels |
|
run benchmark cast_kernels |
|
🤖 Arrow criterion benchmark running (GKE) | trigger File an issue against this benchmark runner |
|
🤖 Arrow criterion benchmark completed (GKE) | trigger Details
Resource Usagebase (merge-base)
branch
File an issue against this benchmark runner |
|
Thank you for your contribution. Unfortunately, this pull request is stale because it has been open 60 days with no activity. Please remove the stale label or comment or this will be closed in 7 days. |
|
Maybe we can try to separate the normal path(no whitespace) and the exception path(with leading/trailing whitespace), if this is accepted, then maybe we also can improve the parse logic for int/decimal by changing the code in |
This is a good idea @klion26 -- basically don't slow down the common case (non whitespace) but do an extra check/trim on the error path. |
|
Marking as draft as I think this PR is no longer waiting on feedback and I am trying to make it easier to find PRs in need of review. Please mark it as ready for review when it is ready for another look |
# 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 half of #9538. # Rationale for this change currently parsing ints and floats with white space prefiing the number caused the cast to fail ```rust 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)); ``` <!-- 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 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 <img width="1155" height="493" alt="Image 7-18-26 at 1 02 PM" src="https://github.com/user-attachments/assets/911560a4-bc4f-4dde-83a0-858e4a470bf8" /> <!-- 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. I created a test with 12 assertiosn to validate behavoir <!-- 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. arrow-rs/datafusion cast operations will no longer fail when a number is prefixed with white space. <!-- 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: Richard Baah <[email protected]>
|
Thank you for the contribution @aryan-212 -- very much appreciate to get it going |
Which issue does this PR close?
Rationale for this change
The
Parser::parseimplementations for numeric types did not trim whitespace before parsing. This caused values like " 42 " or " 1.5 " to fail parsing and returnNone, even though they represent valid numbers.What changes are included in this PR?
.trim()calls before parsing in FloatType Parser implementations.string.trim()at the top of the parser_primitive! macro, which covers all integer and duration types.Are these changes tested?
Yes. Added test_parse_trimmed_whitespace covering:
Datafusion changes
For the following SQL :-
in datafusion we used to get
now after these changes, we get
this behaviour is now aligned with Databricks
Are there any user-facing changes?
Yes. Numeric parsing now accepts strings with leading/trailing whitespace. This is a relaxation of the previous behaviour (previously
None, nowSome(value)), so it is not a breaking change.