comphead commented on code in PR #12422:
URL: https://github.com/apache/arrow-rs/pull/12422#discussion_r4670900191


##########
arrow-cast/benches/cast_timestamp.rs:
##########
@@ -51,11 +71,57 @@ fn benchmark<T: ArrowTimestampType>(c: &mut Criterion, 
unit: &str, units_per_sec
     group.finish();
 }
 
+/// Returns the names and units other than `unit`
+fn other_units(unit: TimeUnit) -> impl Iterator<Item = (&'static str, 
TimeUnit)> {
+    [
+        ("s", TimeUnit::Second),
+        ("ms", TimeUnit::Millisecond),
+        ("us", TimeUnit::Microsecond),
+        ("ns", TimeUnit::Nanosecond),
+    ]
+    .into_iter()
+    .filter(move |(_, u)| *u != unit)
+}
+
+fn duration_benchmark<T: ArrowPrimitiveType<Native = i64>>(
+    c: &mut Criterion,
+    unit: &str,
+    from: TimeUnit,
+    units_per_second: i64,
+) {
+    // Durations of up to roughly 63 years either way
+    let mut rng = StdRng::seed_from_u64(42);

Review Comment:
   These lines repeat lines 18 to 27 with the same seed and range. One helper 
for both functions, like `inputs` in `cast_decimal.rs`, would keep the 
`timestamp_*` and `duration_*` data identical by construction:
   
   ```rust
   fn arrays<T: ArrowPrimitiveType<Native = i64>>(units_per_second: i64) -> 
(PrimitiveArray<T>, PrimitiveArray<T>)
   ```



##########
arrow-cast/benches/cast_timestamp.rs:
##########
@@ -32,17 +32,37 @@ fn benchmark<T: ArrowTimestampType>(c: &mut Criterion, 
unit: &str, units_per_sec
         ("mixed_nulls", &mixed_nulls),
         ("timezone_control", &zoned),
     ];
-    let targets = [
-        ("date32", DataType::Date32),
-        ("time32_s", DataType::Time32(TimeUnit::Second)),
-        ("time32_ms", DataType::Time32(TimeUnit::Millisecond)),
-        ("time64_us", DataType::Time64(TimeUnit::Microsecond)),
-        ("time64_ns", DataType::Time64(TimeUnit::Nanosecond)),
+    let mut targets = vec![

Review Comment:
   Keeping `targets` as it was and adding the unit casts as their own loop, as 
`duration_benchmark` does, leaves the existing lines unchanged and drops the 
`.to_string()` calls and the `timezone_control` skip. The IDs and their order 
stay the same:
   
   ```rust
   for (to_unit, to) in other_units(T::UNIT) {
       let target = DataType::Timestamp(to, None);
       for (input, array) in [("no_nulls", &no_nulls), ("mixed_nulls", 
&mixed_nulls)] {
           group.bench_function(format!("timestamp_{to_unit}/{input}"), |b| {
               b.iter(|| black_box(cast(black_box(array), &target).unwrap()))
           });
       }
   }
   ```



##########
arrow-cast/tests/cast/date_time.rs:
##########
@@ -2723,3 +2723,66 @@ fn test_cast_temporal_scaling_boundaries() {
     check::<Date64Type, TimestampMicrosecondType>(MICROSECONDS / MILLISECONDS);
     check::<Date64Type, TimestampNanosecondType>(NANOSECONDS / MILLISECONDS);
 }
+
+#[test]
+fn test_cast_timestamp_and_duration_to_coarser_unit() {
+    // Truncates toward zero, as `/` does, and keeps nulls
+    let edges = [
+        i64::MIN,
+        i64::MIN + 1,
+        -1_000_000_001,
+        -1_000_000_000,
+        -999_999_999,
+        -1001,
+        -1000,
+        -999,
+        -1,
+        0,
+        1,
+        999,
+        1000,
+        1001,
+        999_999_999,
+        1_000_000_000,
+        1_000_000_001,
+        i64::MAX - 1,
+        i64::MAX,
+    ];
+    let values: Int64Array = edges
+        .into_iter()
+        .map(Some)
+        .chain([None])
+        .cycle()
+        .take(70)
+        .collect();
+    let units = [
+        TimeUnit::Second,
+        TimeUnit::Millisecond,
+        TimeUnit::Microsecond,
+        TimeUnit::Nanosecond,
+    ];
+    let tz = Some("+05:45".into());
+    for array in [values.clone(), values.slice(3, 60)] {
+        for (i, from) in units.into_iter().enumerate() {
+            for (j, to) in units.into_iter().enumerate().take(i) {

Review Comment:
   A few simplifications:
   
   - `time_unit_multiple(&from) / time_unit_multiple(&to)`, from the top of 
this file, gives the ratio without `j` and the inner `enumerate`.
   - `expected` depends only on `array` and `ratio`, so it can be built once, 
before the loop over type pairs.
   - The zoned pair runs the same division as the unzoned one, because `(Some, 
Some)` skips the adjustment, so `tz` can go.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to