perf: optimize date subtraction to avoid intermediate array allocation#22591
Open
lyne7-sc wants to merge 5 commits into
Open
perf: optimize date subtraction to avoid intermediate array allocation#22591lyne7-sc wants to merge 5 commits into
lyne7-sc wants to merge 5 commits into
Conversation
Jefffrey
reviewed
May 30, 2026
| { | ||
| /// Extract the native value from a scalar by converting to a single-element | ||
| /// array and reading index 0. Returns `None` for null scalars. | ||
| fn scalar_to_native<P: ArrowPrimitiveType>( |
Contributor
There was a problem hiding this comment.
I wonder if we're better off destructuring the scalar value directly instead of needing to roundtrip through an array just to extract the scalar value 🤔
Tradeoff being that it'd be more verbose in terms of code
| let result: Int64Array = left.unary(|l| day_diff_fn(l, right_val)); | ||
| Ok(ColumnarValue::Array(Arc::new(result))) | ||
| } | ||
| None => Ok(ColumnarValue::Array(new_null_array( |
Contributor
There was a problem hiding this comment.
Can return a null scalar here instead of a null array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Which issue does this PR close?
Rationale for this change
When evaluating
Date32 - Date32orDate64 - Date64, DataFusion needs to return an Int64 representing the difference in days(after #19563). The current implementation went through Arrow's sub_wrapping, then converted to Int64 days. This allocates an unnecessary intermediate array and bypasses vectorized kernels.Since Date32/Date64 are just i32/i64 under the hood, we can compute the day difference directly on the native values.
What changes are included in this PR?
apply_date_subtraction + duration_to_dayswithsubtract_date_to_days, which operates directly on primitive values usingbinary()/unary()Benchmarks
Are these changes tested?
Yes, new UTs and exist slt
Are there any user-facing changes?
No.