This is an automated email from the ASF dual-hosted git repository.

github-merge-queue[bot] pushed a commit to branch 
gh-readonly-queue/main/pr-12318-ede2aeedef33116f1696a5d3130c34b55a04e9c9
in repository https://gitbox.apache.org/repos/asf/arrow-rs.git

commit aad006774ef332930ca4a374820f1ca7dd509fd3
Author: Michal Piatkowski <[email protected]>
AuthorDate: Fri Nov 27 13:27:16 2026 +0000

    perf(arrow-cast): bench Int64 to temporal (#12318)
    
    # Which issue does this PR close?
    
    <!--
    It is not necessary to raise an issue, but it is encouraged if it is a
    non-trivial
    enhancement/bug.
    
    If this PR relates to an existing issue, but does not fully resolve it,
    please change
    the wording below from "Closes #NNN" to "Relates to #NNN" or "Part of
    #NNN" to avoid
    false closing an issue.
    -->
    
    - Relates to https://github.com/apache/arrow-rs/issues/9128,
    https://github.com/apache/arrow-rs/issues/11717
    
    # Rationale for this change
    
    <!--
    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.
    
    Please try to explain the problem you are trying to solve in terms of
    the user-visible
    behavior, rather than the implementation.
    -->
    
    `make_array(array.to_data())` is a bit expensive (allocates buffer list,
    validates, drops).
    
    # What changes are included in this PR?
    
    <!--
    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.
    -->
    - ~~Special-case i64 casts to avoid `ArrayData`.~~
    - ~~Use `Array::slice` instead of `make_array(array.to_data)`~~
    superseded by https://github.com/apache/arrow-rs/pull/12420
    - Two new benches and a test for i64 duration cast.
    
    # Are these changes tested?
    
    <!--
    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 so identify these tests)?
    
    If this PR claims a performance improvement, please include evidence
    such as
    benchmark results.
    
    DO NOT simply paste a list of commands ran to verify the code; this is
    what our
    CI is for and it pollutes the PR. ONLY explain if testing was included
    with this
    PR, or why it wasn't.
    -->
    
    | bench | main |
    
https://github.com/apache/arrow-rs/pull/12318/changes/f626d103b74d4ec13ea6443c0758178167b142f5|
    change |
    |---|---:|---:|---:|
    | cast i64 to timestamp_ns 512 | 129.2 ns | 36.2 ns | −72% |
    | cast i64 to duration_ns 512 | 119.2 ns | 33.1 ns | −72% |
    
    (the array length does not matter, the overhead is per array cast)
    The bench applies within 5% after
    https://github.com/apache/arrow-rs/pull/12420.
    
    # Are there any significant user-facing changes?
    
    <!--
    Significant user-facing changes include:
    - Breaking public API change
    - Significant change to existing functionality (e.g. function returns
    something different)
    
    These need to be explicitly called out to ensure we have proper
    documentation if
    required and to ensure we follow proper semantic versioning.
    
    DO NOT restate the changes of this PR; if a bug was fixed and already
    explained
    above, we don't need this to be restated as a user-facing change.
    -->
    
    No.
---
 arrow-cast/tests/cast/interval.rs | 9 ++++++++-
 arrow/benches/cast_kernels.rs     | 6 ++++++
 2 files changed, 14 insertions(+), 1 deletion(-)

diff --git a/arrow-cast/tests/cast/interval.rs 
b/arrow-cast/tests/cast/interval.rs
index 5dc72dd5f9..197ae73416 100644
--- a/arrow-cast/tests/cast/interval.rs
+++ b/arrow-cast/tests/cast/interval.rs
@@ -333,11 +333,18 @@ fn test_cast_between_durations_and_numerics() {
     // numerics to durations
     let array = Int32Array::from(vec![Some(i32::MAX), Some(802034103), 
Some(10241096), None]);
     let b = cast(&array, &DataType::Duration(TimeUnit::Second)).unwrap();
-    let c = b.as_any().downcast_ref::<DurationSecondArray>().unwrap();
+    let c = b.as_primitive::<DurationSecondType>();
     assert_eq!(i32::MAX as i64, c.value(0));
     assert_eq!(802034103, c.value(1));
     assert_eq!(10241096, c.value(2));
     assert!(c.is_null(3));
+
+    let array = Int64Array::from(vec![Some(i64::MAX), Some(802034103), None]);
+    let b = cast(&array, &DataType::Duration(TimeUnit::Second)).unwrap();
+    let c = b.as_primitive::<DurationSecondType>();
+    assert_eq!(i64::MAX, c.value(0));
+    assert_eq!(802034103, c.value(1));
+    assert!(c.is_null(2));
 }
 
 /// helper function to test casting from duration to interval
diff --git a/arrow/benches/cast_kernels.rs b/arrow/benches/cast_kernels.rs
index 402f6878da..3fc2f01fbc 100644
--- a/arrow/benches/cast_kernels.rs
+++ b/arrow/benches/cast_kernels.rs
@@ -398,6 +398,12 @@ fn add_benchmark(c: &mut Criterion) {
     c.bench_function("cast timestamp_ms to i64 512", |b| {
         b.iter(|| cast_array(&time_ms_array, DataType::Int64))
     });
+    c.bench_function("cast i64 to timestamp_ns 512", |b| {
+        b.iter(|| cast_array(&i64_array, 
DataType::Timestamp(TimeUnit::Nanosecond, None)))
+    });
+    c.bench_function("cast i64 to duration_ns 512", |b| {
+        b.iter(|| cast_array(&i64_array, 
DataType::Duration(TimeUnit::Nanosecond)))
+    });
     c.bench_function("cast utf8 to date32 512", |b| {
         b.iter(|| cast_array(&utf8_date_array, DataType::Date32))
     });

Reply via email to