tustvold commented on code in PR #4182:
URL: https://github.com/apache/arrow-rs/pull/4182#discussion_r1188826227


##########
arrow-cast/src/cast.rs:
##########
@@ -460,6 +462,56 @@ where
     }
 }
 
+/// Cast the array from interval year month to month day nano
+fn cast_interval_year_month_to_interval_month_day_nano(
+    array: &dyn Array,
+    _cast_options: &CastOptions,
+) -> Result<ArrayRef, ArrowError> {
+    let array = array
+        .as_any()
+        .downcast_ref::<PrimitiveArray<IntervalYearMonthType>>()
+        .ok_or_else(|| {
+            ArrowError::ComputeError(
+                "Internal Error: Cannot cast interval to IntervalArray of 
expected type"
+                    .to_string(),
+            )
+        })?;
+
+    let iter = array
+        .iter()
+        .map(|v| v.and_then(|v| Some(IntervalMonthDayNanoType::make_value(v, 
0, 0))));
+    Ok(Arc::new(unsafe {
+        PrimitiveArray::<IntervalMonthDayNanoType>::from_trusted_len_iter(iter)
+    }))
+}
+
+/// Cast the array from interval day time to month day nano
+fn cast_interval_day_time_to_interval_month_day_nano(
+    array: &dyn Array,
+    _cast_options: &CastOptions,
+) -> Result<ArrayRef, ArrowError> {
+    let array = array
+        .as_any()
+        .downcast_ref::<PrimitiveArray<IntervalDayTimeType>>()
+        .ok_or_else(|| {
+            ArrowError::ComputeError(
+                "Internal Error: Cannot cast interval to IntervalArray of 
expected type"
+                    .to_string(),
+            )
+        })?;

Review Comment:
   ```suggestion
           let array = array.as_primitive::<IntervalDayTimeType>();
   ```
   



##########
arrow-cast/src/cast.rs:
##########
@@ -460,6 +462,56 @@ where
     }
 }
 
+/// Cast the array from interval year month to month day nano
+fn cast_interval_year_month_to_interval_month_day_nano(
+    array: &dyn Array,
+    _cast_options: &CastOptions,
+) -> Result<ArrayRef, ArrowError> {
+    let array = array
+        .as_any()
+        .downcast_ref::<PrimitiveArray<IntervalYearMonthType>>()
+        .ok_or_else(|| {
+            ArrowError::ComputeError(
+                "Internal Error: Cannot cast interval to IntervalArray of 
expected type"
+                    .to_string(),
+            )
+        })?;

Review Comment:
   ```suggestion
       let array = array.as_primitive::<IntervalYearMonthType>();
   ```



##########
arrow-cast/src/cast.rs:
##########
@@ -8761,4 +8819,98 @@ mod tests {
         );
         assert!(casted_array.is_err());
     }
+
+    /// helper function to test casting from interval year month to interval 
month day nano
+    fn cast_from_interval_year_month_to_interval_month_day_nano(
+        array: Vec<i32>,
+        cast_options: &CastOptions,
+    ) -> Result<PrimitiveArray<IntervalMonthDayNanoType>, ArrowError> {
+        let array = PrimitiveArray::<IntervalYearMonthType>::from(array);
+        let array = Arc::new(array) as ArrayRef;
+        let casted_array = cast_with_options(
+            &array,
+            &DataType::Interval(IntervalUnit::MonthDayNano),
+            cast_options,
+        )?;
+        casted_array
+            .as_any()
+            .downcast_ref::<IntervalMonthDayNanoArray>()
+            .ok_or_else(|| {
+                ArrowError::ComputeError(
+                    "Failed to downcast to 
IntervalMonthDayNanoArray".to_string(),
+                )
+            })
+            .cloned()

Review Comment:
   ```suggestion
           Ok(casted_array.as_primitive::<IntervalMonthDayNanoType>().clone())
   ```



##########
arrow-cast/src/cast.rs:
##########
@@ -460,6 +462,56 @@ where
     }
 }
 
+/// Cast the array from interval year month to month day nano
+fn cast_interval_year_month_to_interval_month_day_nano(
+    array: &dyn Array,
+    _cast_options: &CastOptions,
+) -> Result<ArrayRef, ArrowError> {
+    let array = array
+        .as_any()
+        .downcast_ref::<PrimitiveArray<IntervalYearMonthType>>()
+        .ok_or_else(|| {
+            ArrowError::ComputeError(
+                "Internal Error: Cannot cast interval to IntervalArray of 
expected type"
+                    .to_string(),
+            )
+        })?;
+
+    let iter = array
+        .iter()
+        .map(|v| v.and_then(|v| Some(IntervalMonthDayNanoType::make_value(v, 
0, 0))));
+    Ok(Arc::new(unsafe {
+        PrimitiveArray::<IntervalMonthDayNanoType>::from_trusted_len_iter(iter)
+    }))
+}
+
+/// Cast the array from interval day time to month day nano
+fn cast_interval_day_time_to_interval_month_day_nano(
+    array: &dyn Array,
+    _cast_options: &CastOptions,
+) -> Result<ArrayRef, ArrowError> {
+    let array = array
+        .as_any()
+        .downcast_ref::<PrimitiveArray<IntervalDayTimeType>>()
+        .ok_or_else(|| {
+            ArrowError::ComputeError(
+                "Internal Error: Cannot cast interval to IntervalArray of 
expected type"
+                    .to_string(),
+            )
+        })?;
+
+    let iter = array.iter().map(|v| {

Review Comment:
   I think this needs to perform checked conversion, and can make use of 
unary_opt and try_unary to achieve this based on the cast options



##########
arrow-cast/src/cast.rs:
##########
@@ -8761,4 +8819,98 @@ mod tests {
         );
         assert!(casted_array.is_err());
     }
+
+    /// helper function to test casting from interval year month to interval 
month day nano
+    fn cast_from_interval_year_month_to_interval_month_day_nano(
+        array: Vec<i32>,
+        cast_options: &CastOptions,
+    ) -> Result<PrimitiveArray<IntervalMonthDayNanoType>, ArrowError> {
+        let array = PrimitiveArray::<IntervalYearMonthType>::from(array);
+        let array = Arc::new(array) as ArrayRef;
+        let casted_array = cast_with_options(
+            &array,
+            &DataType::Interval(IntervalUnit::MonthDayNano),
+            cast_options,
+        )?;
+        casted_array
+            .as_any()
+            .downcast_ref::<IntervalMonthDayNanoArray>()
+            .ok_or_else(|| {
+                ArrowError::ComputeError(
+                    "Failed to downcast to 
IntervalMonthDayNanoArray".to_string(),
+                )
+            })
+            .cloned()
+    }
+
+    #[test]
+    fn test_cast_from_interval_year_month_to_interval_month_day_nano() {
+        // from interval year month to interval month day nano
+        let array = vec![1234567];
+        let casted_array = 
cast_from_interval_year_month_to_interval_month_day_nano(
+            array,
+            &DEFAULT_CAST_OPTIONS,
+        )
+        .unwrap();
+        assert_eq!(
+            casted_array.data_type(),
+            &DataType::Interval(IntervalUnit::MonthDayNano)
+        );
+        assert_eq!(casted_array.value(0), 97812474910747780469848774134464512);
+
+        let array = vec![i32::MAX];
+        let casted_array = 
cast_from_interval_year_month_to_interval_month_day_nano(
+            array.clone(),
+            &DEFAULT_CAST_OPTIONS,
+        )
+        .unwrap();
+        assert!(casted_array.is_valid(0));
+    }
+
+    /// helper function to test casting from interval day time to interval 
month day nano
+    fn cast_from_interval_day_time_to_interval_month_day_nano(
+        array: Vec<i64>,
+        cast_options: &CastOptions,
+    ) -> Result<PrimitiveArray<IntervalMonthDayNanoType>, ArrowError> {
+        let array = PrimitiveArray::<IntervalDayTimeType>::from(array);
+        let array = Arc::new(array) as ArrayRef;
+        let casted_array = cast_with_options(
+            &array,
+            &DataType::Interval(IntervalUnit::MonthDayNano),
+            cast_options,
+        )?;
+        casted_array
+            .as_any()
+            .downcast_ref::<IntervalMonthDayNanoArray>()
+            .ok_or_else(|| {
+                ArrowError::ComputeError(
+                    "Failed to downcast to 
IntervalMonthDayNanoArray".to_string(),
+                )
+            })

Review Comment:
   ```suggestion
           Ok(casted_array.as_primitive::<IntervalMonthDataNanoType>().clone())
   ```



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