Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Commit

Permalink
Added cast to months_days_ns (#900)
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgecarleitao authored Mar 9, 2022
1 parent 4269aef commit 79fa592
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/compute/cast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ pub fn can_cast_types(from_type: &DataType, to_type: &DataType) -> bool {
(Timestamp(_, _), Date64) => true,
(Int64, Duration(_)) => true,
(Duration(_), Int64) => true,
(Interval(_), Interval(IntervalUnit::MonthDayNano)) => true,
(_, _) => false,
}
}
Expand Down Expand Up @@ -842,6 +843,13 @@ pub fn cast(array: &dyn Array, to_type: &DataType, options: CastOptions) -> Resu
(Int64, Duration(_)) => primitive_to_same_primitive_dyn::<i64>(array, to_type),
(Duration(_), Int64) => primitive_to_same_primitive_dyn::<i64>(array, to_type),

(Interval(IntervalUnit::DayTime), Interval(IntervalUnit::MonthDayNano)) => {
primitive_dyn!(array, days_ms_to_months_days_ns)
}
(Interval(IntervalUnit::YearMonth), Interval(IntervalUnit::MonthDayNano)) => {
primitive_dyn!(array, months_to_months_days_ns)
}

(_, _) => Err(ArrowError::NotYetImplemented(format!(
"Casting from {:?} to {:?} not supported",
from_type, to_type,
Expand Down
30 changes: 30 additions & 0 deletions src/compute/cast/primitive_to.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ use std::hash::Hash;

use num_traits::{AsPrimitive, Float, ToPrimitive};

use crate::datatypes::IntervalUnit;
use crate::error::Result;
use crate::types::{days_ms, months_days_ns};
use crate::{
array::*,
bitmap::Bitmap,
Expand Down Expand Up @@ -551,3 +553,31 @@ pub fn naive_timestamp_to_utf8<O: Offset>(
}
}
}

#[inline]
fn days_ms_to_months_days_ns_scalar(from: days_ms) -> months_days_ns {
months_days_ns::new(0, from.days(), from.milliseconds() as i64 * 1000)
}

/// Casts [`days_ms`]s to [`months_days_ns`]. This operation is infalible and lossless.
pub fn days_ms_to_months_days_ns(from: &PrimitiveArray<days_ms>) -> PrimitiveArray<months_days_ns> {
unary(
from,
days_ms_to_months_days_ns_scalar,
DataType::Interval(IntervalUnit::MonthDayNano),
)
}

#[inline]
fn months_to_months_days_ns_scalar(from: i32) -> months_days_ns {
months_days_ns::new(from, 0, 0)
}

/// Casts months represented as [`i32`]s to [`months_days_ns`]. This operation is infalible and lossless.
pub fn months_to_months_days_ns(from: &PrimitiveArray<i32>) -> PrimitiveArray<months_days_ns> {
unary(
from,
months_to_months_days_ns_scalar,
DataType::Interval(IntervalUnit::MonthDayNano),
)
}
25 changes: 24 additions & 1 deletion tests/it/compute/cast.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use arrow2::array::*;
use arrow2::compute::cast::{can_cast_types, cast, CastOptions};
use arrow2::datatypes::*;
use arrow2::types::NativeType;
use arrow2::types::{days_ms, months_days_ns, NativeType};

#[test]
fn i32_to_f64() {
Expand Down Expand Up @@ -512,6 +512,29 @@ fn date32_to_date64() {
);
}

#[test]
fn days_ms_to_months_days_ns() {
test_primitive_to_primitive(
&[days_ms::new(1, 1), days_ms::new(1, 2)],
DataType::Interval(IntervalUnit::DayTime),
&[
months_days_ns::new(0, 1, 1000),
months_days_ns::new(0, 1, 2000),
],
DataType::Interval(IntervalUnit::MonthDayNano),
);
}

#[test]
fn months_to_months_days_ns() {
test_primitive_to_primitive(
&[1, 2],
DataType::Interval(IntervalUnit::YearMonth),
&[months_days_ns::new(1, 0, 0), months_days_ns::new(2, 0, 0)],
DataType::Interval(IntervalUnit::MonthDayNano),
);
}

#[test]
fn date64_to_date32() {
test_primitive_to_primitive(
Expand Down

0 comments on commit 79fa592

Please sign in to comment.