viirya commented on code in PR #4020:
URL: https://github.com/apache/arrow-rs/pull/4020#discussion_r1158899802


##########
arrow-cast/src/cast.rs:
##########
@@ -458,6 +460,93 @@ where
     }
 }
 
+/// Cast the array from interval to duration
+fn cast_interval_to_duration<D: ArrowTemporalType<Native = i64>>(
+    array: &dyn Array,
+    cast_options: &CastOptions,
+) -> Result<ArrayRef, ArrowError> {
+    let array = array
+        .as_any()
+        .downcast_ref::<IntervalMonthDayNanoArray>()
+        .unwrap();
+
+    let mut builder = PrimitiveBuilder::<D>::new();
+
+    let scale = match D::DATA_TYPE {
+        DataType::Duration(TimeUnit::Second) => 1_000_000_000,
+        DataType::Duration(TimeUnit::Millisecond) => 1_000_000,
+        DataType::Duration(TimeUnit::Microsecond) => 1_000,
+        DataType::Duration(TimeUnit::Nanosecond) => 1,
+        _ => unreachable!(),
+    };
+
+    for i in 0..array.len() {
+        if array.is_null(i) {
+            builder.append_null();
+        } else {
+            let v = array.value(i) / scale;
+            if v > i64::MAX as i128 {
+                if cast_options.safe {
+                    builder.append_null();
+                } else {
+                    return Err(ArrowError::ComputeError(format!(
+                        "Cannot cast to {:?}. Overflowing on {:?}",
+                        D::DATA_TYPE,
+                        v
+                    )));
+                }
+            } else {
+                builder.append_value(v as i64);
+            }
+        }
+    }
+
+    Ok(Arc::new(builder.finish()) as ArrayRef)
+}
+
+/// Cast the array from duration and interval
+fn cast_duration_to_interval<D: ArrowTemporalType<Native = i64>>(
+    array: &dyn Array,
+    cast_options: &CastOptions,
+) -> Result<ArrayRef, ArrowError> {
+    let array = array.as_any().downcast_ref::<PrimitiveArray<D>>().unwrap();
+
+    let mut builder = IntervalMonthDayNanoBuilder::new();
+
+    let scale = match array.data_type() {
+        DataType::Duration(TimeUnit::Second) => 1_000_000_000,
+        DataType::Duration(TimeUnit::Millisecond) => 1_000_000,
+        DataType::Duration(TimeUnit::Microsecond) => 1_000,
+        DataType::Duration(TimeUnit::Nanosecond) => 1,
+        _ => unreachable!(),
+    };
+
+    for i in 0..array.len() {
+        if array.is_null(i) {
+            builder.append_null();
+        } else {
+            let v = array.value(i) as i128;
+            let v = v.mul_checked(scale);
+            match v {
+                Ok(v) => builder.append_value(v),
+                Err(e) => {
+                    if cast_options.safe {
+                        builder.append_null()
+                    } else {
+                        return Err(ArrowError::ComputeError(format!(
+                            "Cannot cast to {:?}. Overflowing on {:?}",
+                            D::DATA_TYPE,
+                            e

Review Comment:
   `e` is `ArrowError`, so you will get nested `ArrowError`. 



-- 
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