Skip to content

Commit ace8dad

Browse files
Implement DataType::{Date32,Date64} => Variant::Date (#8081)
# Which issue does this PR close? - Closes #8054 # Rationale for this change Adds Date32, Date64 conversions to the cast_to_variant kernel # What changes are included in this PR? - adds fallibility to cast macro - conversion of DataType:::{Date32, Date64}=> Variant::Date # Are these changes tested? Yes, additional unit tests have been added. Currently there is no negative test for a Date64Array with a date element out-of-range. # Are there any user-facing changes? Yes, adds new type conversions to kernel
1 parent e4f74d8 commit ace8dad

File tree

1 file changed

+69
-12
lines changed

1 file changed

+69
-12
lines changed

parquet-variant-compute/src/cast_to_variant.rs

Lines changed: 69 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,17 @@ use arrow::array::{
2323
TimestampSecondArray,
2424
};
2525
use arrow::datatypes::{
26-
i256, BinaryType, BinaryViewType, Decimal128Type, Decimal256Type, Decimal32Type, Decimal64Type,
27-
Float16Type, Float32Type, Float64Type, Int16Type, Int32Type, Int64Type, Int8Type,
28-
LargeBinaryType, Time32MillisecondType, Time32SecondType, Time64MicrosecondType,
29-
Time64NanosecondType, UInt16Type, UInt32Type, UInt64Type, UInt8Type,
26+
i256, BinaryType, BinaryViewType, Date32Type, Date64Type, Decimal128Type, Decimal256Type,
27+
Decimal32Type, Decimal64Type, Float16Type, Float32Type, Float64Type, Int16Type, Int32Type,
28+
Int64Type, Int8Type, LargeBinaryType, Time32MillisecondType, Time32SecondType,
29+
Time64MicrosecondType, Time64NanosecondType, UInt16Type, UInt32Type, UInt64Type, UInt8Type,
3030
};
3131
use arrow::temporal_conversions::{
3232
timestamp_ms_to_datetime, timestamp_ns_to_datetime, timestamp_s_to_datetime,
3333
timestamp_us_to_datetime,
3434
};
3535
use arrow_schema::{ArrowError, DataType, TimeUnit};
36-
use chrono::NaiveTime;
37-
use chrono::{DateTime, NaiveDateTime, TimeZone, Utc};
36+
use chrono::{DateTime, NaiveDate, NaiveDateTime, NaiveTime, TimeZone, Utc};
3837
use half::f16;
3938
use parquet_variant::{
4039
Variant, VariantBuilder, VariantDecimal16, VariantDecimal4, VariantDecimal8,
@@ -485,6 +484,24 @@ pub fn cast_to_variant(input: &dyn Array) -> Result<VariantArray, ArrowError> {
485484
builder.append_variant(variant);
486485
}
487486
}
487+
DataType::Date32 => {
488+
generic_conversion!(
489+
Date32Type,
490+
as_primitive,
491+
|v: i32| -> NaiveDate { Date32Type::to_naive_date(v) },
492+
input,
493+
builder
494+
);
495+
}
496+
DataType::Date64 => {
497+
generic_conversion!(
498+
Date64Type,
499+
as_primitive,
500+
|v: i64| { Date64Type::to_naive_date_opt(v).unwrap() },
501+
input,
502+
builder
503+
);
504+
}
488505
dt => {
489506
return Err(ArrowError::CastError(format!(
490507
"Unsupported data type for casting to Variant: {dt:?}",
@@ -502,12 +519,13 @@ pub fn cast_to_variant(input: &dyn Array) -> Result<VariantArray, ArrowError> {
502519
mod tests {
503520
use super::*;
504521
use arrow::array::{
505-
ArrayRef, BinaryArray, BooleanArray, Decimal128Array, Decimal256Array, Decimal32Array,
506-
Decimal64Array, FixedSizeBinaryBuilder, Float16Array, Float32Array, Float64Array,
507-
GenericByteBuilder, GenericByteViewBuilder, Int16Array, Int32Array, Int64Array, Int8Array,
508-
IntervalYearMonthArray, LargeStringArray, NullArray, StringArray, StringViewArray,
509-
StructArray, Time32MillisecondArray, Time32SecondArray, Time64MicrosecondArray,
510-
Time64NanosecondArray, UInt16Array, UInt32Array, UInt64Array, UInt8Array,
522+
ArrayRef, BinaryArray, BooleanArray, Date32Array, Date64Array, Decimal128Array,
523+
Decimal256Array, Decimal32Array, Decimal64Array, FixedSizeBinaryBuilder, Float16Array,
524+
Float32Array, Float64Array, GenericByteBuilder, GenericByteViewBuilder, Int16Array,
525+
Int32Array, Int64Array, Int8Array, IntervalYearMonthArray, LargeStringArray, NullArray,
526+
StringArray, StringViewArray, StructArray, Time32MillisecondArray, Time32SecondArray,
527+
Time64MicrosecondArray, Time64NanosecondArray, UInt16Array, UInt32Array, UInt64Array,
528+
UInt8Array,
511529
};
512530
use arrow::buffer::NullBuffer;
513531
use arrow_schema::{Field, Fields};
@@ -1763,6 +1781,45 @@ mod tests {
17631781
assert_eq!(location_obj2.get("y"), Some(Variant::from(-122.4f64)));
17641782
}
17651783

1784+
#[test]
1785+
fn test_cast_to_variant_date() {
1786+
// Date32Array
1787+
run_test(
1788+
Arc::new(Date32Array::from(vec![
1789+
Some(Date32Type::from_naive_date(NaiveDate::MIN)),
1790+
None,
1791+
Some(Date32Type::from_naive_date(
1792+
NaiveDate::from_ymd_opt(2025, 8, 1).unwrap(),
1793+
)),
1794+
Some(Date32Type::from_naive_date(NaiveDate::MAX)),
1795+
])),
1796+
vec![
1797+
Some(Variant::Date(NaiveDate::MIN)),
1798+
None,
1799+
Some(Variant::Date(NaiveDate::from_ymd_opt(2025, 8, 1).unwrap())),
1800+
Some(Variant::Date(NaiveDate::MAX)),
1801+
],
1802+
);
1803+
1804+
// Date64Array
1805+
run_test(
1806+
Arc::new(Date64Array::from(vec![
1807+
Some(Date64Type::from_naive_date(NaiveDate::MIN)),
1808+
None,
1809+
Some(Date64Type::from_naive_date(
1810+
NaiveDate::from_ymd_opt(2025, 8, 1).unwrap(),
1811+
)),
1812+
Some(Date64Type::from_naive_date(NaiveDate::MAX)),
1813+
])),
1814+
vec![
1815+
Some(Variant::Date(NaiveDate::MIN)),
1816+
None,
1817+
Some(Variant::Date(NaiveDate::from_ymd_opt(2025, 8, 1).unwrap())),
1818+
Some(Variant::Date(NaiveDate::MAX)),
1819+
],
1820+
);
1821+
}
1822+
17661823
/// Converts the given `Array` to a `VariantArray` and tests the conversion
17671824
/// against the expected values. It also tests the handling of nulls by
17681825
/// setting one element to null and verifying the output.

0 commit comments

Comments
 (0)