Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable cast Date32 to Timestamp #3508

Merged
merged 2 commits into from
Jan 12, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 72 additions & 1 deletion arrow-cast/src/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ pub fn can_cast_types(from_type: &DataType, to_type: &DataType) -> bool {
(Timestamp(_, _), Int64) => true,
(Int64, Timestamp(_, _)) => true,
(Date64, Timestamp(_, None)) => true,
(Date32, Timestamp(_, None)) => true,
(Timestamp(_, _),
Timestamp(_, _)
| Date32
Expand Down Expand Up @@ -1943,7 +1944,24 @@ pub fn cast_with_options(
|x| x * (NANOSECONDS / MILLISECONDS),
),
)),

(Date32, Timestamp(TimeUnit::Second, None)) => Ok(Arc::new(
as_primitive_array::<Date32Type>(array)
.unary::<_, TimestampSecondType>(|x| (x as i64) * SECONDS_IN_DAY),
)),
(Date32, Timestamp(TimeUnit::Millisecond, None)) => Ok(Arc::new(
as_primitive_array::<Date32Type>(array).unary::<_, TimestampMillisecondType>(
|x| (x as i64) * MILLISECONDS_IN_DAY,
),
)),
(Date32, Timestamp(TimeUnit::Microsecond, None)) => Ok(Arc::new(
as_primitive_array::<Date32Type>(array).unary::<_, TimestampMicrosecondType>(
|x| (x as i64) * MICROSECONDS_IN_DAY,
),
)),
(Date32, Timestamp(TimeUnit::Nanosecond, None)) => Ok(Arc::new(
as_primitive_array::<Date32Type>(array)
.unary::<_, TimestampNanosecondType>(|x| (x as i64) * NANOSECONDS_IN_DAY),
)),
(Int64, Duration(TimeUnit::Second)) => {
cast_reinterpret_arrays::<Int64Type, DurationSecondType>(array)
}
Expand Down Expand Up @@ -7693,4 +7711,57 @@ mod tests {

test_cast_string_to_decimal256_overflow(overflow_array);
}

#[test]
fn test_cast_date32_to_timestamp() {
let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
let array = Arc::new(a) as ArrayRef;
let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
let c = b.as_any().downcast_ref::<TimestampSecondArray>().unwrap();
assert_eq!(1609459200, c.value(0));
assert_eq!(1640995200, c.value(1));
assert!(c.is_null(2));
}

#[test]
fn test_cast_date32_to_timestamp_ms() {
let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
let array = Arc::new(a) as ArrayRef;
let b = cast(&array, &DataType::Timestamp(TimeUnit::Millisecond, None)).unwrap();
let c = b
.as_any()
.downcast_ref::<TimestampMillisecondArray>()
.unwrap();
assert_eq!(1609459200000, c.value(0));
assert_eq!(1640995200000, c.value(1));
assert!(c.is_null(2));
}

#[test]
fn test_cast_date32_to_timestamp_us() {
let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
let array = Arc::new(a) as ArrayRef;
let b = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
let c = b
.as_any()
.downcast_ref::<TimestampMicrosecondArray>()
.unwrap();
assert_eq!(1609459200000000, c.value(0));
assert_eq!(1640995200000000, c.value(1));
assert!(c.is_null(2));
}

#[test]
fn test_cast_date32_to_timestamp_ns() {
let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
let array = Arc::new(a) as ArrayRef;
let b = cast(&array, &DataType::Timestamp(TimeUnit::Nanosecond, None)).unwrap();
let c = b
.as_any()
.downcast_ref::<TimestampNanosecondArray>()
.unwrap();
assert_eq!(1609459200000000000, c.value(0));
assert_eq!(1640995200000000000, c.value(1));
assert!(c.is_null(2));
}
}