Skip to content

Implement DataType::{Date32,Date64} => Variant::Date #8081

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

Merged
merged 1 commit into from
Aug 15, 2025
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
81 changes: 69 additions & 12 deletions parquet-variant-compute/src/cast_to_variant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,17 @@ use arrow::array::{
TimestampSecondArray,
};
use arrow::datatypes::{
i256, BinaryType, BinaryViewType, Decimal128Type, Decimal256Type, Decimal32Type, Decimal64Type,
Float16Type, Float32Type, Float64Type, Int16Type, Int32Type, Int64Type, Int8Type,
LargeBinaryType, Time32MillisecondType, Time32SecondType, Time64MicrosecondType,
Time64NanosecondType, UInt16Type, UInt32Type, UInt64Type, UInt8Type,
i256, BinaryType, BinaryViewType, Date32Type, Date64Type, Decimal128Type, Decimal256Type,
Decimal32Type, Decimal64Type, Float16Type, Float32Type, Float64Type, Int16Type, Int32Type,
Int64Type, Int8Type, LargeBinaryType, Time32MillisecondType, Time32SecondType,
Time64MicrosecondType, Time64NanosecondType, UInt16Type, UInt32Type, UInt64Type, UInt8Type,
};
use arrow::temporal_conversions::{
timestamp_ms_to_datetime, timestamp_ns_to_datetime, timestamp_s_to_datetime,
timestamp_us_to_datetime,
};
use arrow_schema::{ArrowError, DataType, TimeUnit};
use chrono::NaiveTime;
use chrono::{DateTime, NaiveDateTime, TimeZone, Utc};
use chrono::{DateTime, NaiveDate, NaiveDateTime, NaiveTime, TimeZone, Utc};
use half::f16;
use parquet_variant::{
Variant, VariantBuilder, VariantDecimal16, VariantDecimal4, VariantDecimal8,
Expand Down Expand Up @@ -485,6 +484,24 @@ pub fn cast_to_variant(input: &dyn Array) -> Result<VariantArray, ArrowError> {
builder.append_variant(variant);
}
}
DataType::Date32 => {
generic_conversion!(
Date32Type,
as_primitive,
|v: i32| -> NaiveDate { Date32Type::to_naive_date(v) },
input,
builder
);
}
DataType::Date64 => {
generic_conversion!(
Date64Type,
as_primitive,
|v: i64| { Date64Type::to_naive_date_opt(v).unwrap() },
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is probably good for now. I'll file a follow on ticket to track handling this case

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

input,
builder
);
}
dt => {
return Err(ArrowError::CastError(format!(
"Unsupported data type for casting to Variant: {dt:?}",
Expand All @@ -502,12 +519,13 @@ pub fn cast_to_variant(input: &dyn Array) -> Result<VariantArray, ArrowError> {
mod tests {
use super::*;
use arrow::array::{
ArrayRef, BinaryArray, BooleanArray, Decimal128Array, Decimal256Array, Decimal32Array,
Decimal64Array, FixedSizeBinaryBuilder, Float16Array, Float32Array, Float64Array,
GenericByteBuilder, GenericByteViewBuilder, Int16Array, Int32Array, Int64Array, Int8Array,
IntervalYearMonthArray, LargeStringArray, NullArray, StringArray, StringViewArray,
StructArray, Time32MillisecondArray, Time32SecondArray, Time64MicrosecondArray,
Time64NanosecondArray, UInt16Array, UInt32Array, UInt64Array, UInt8Array,
ArrayRef, BinaryArray, BooleanArray, Date32Array, Date64Array, Decimal128Array,
Decimal256Array, Decimal32Array, Decimal64Array, FixedSizeBinaryBuilder, Float16Array,
Float32Array, Float64Array, GenericByteBuilder, GenericByteViewBuilder, Int16Array,
Int32Array, Int64Array, Int8Array, IntervalYearMonthArray, LargeStringArray, NullArray,
StringArray, StringViewArray, StructArray, Time32MillisecondArray, Time32SecondArray,
Time64MicrosecondArray, Time64NanosecondArray, UInt16Array, UInt32Array, UInt64Array,
UInt8Array,
};
use arrow::buffer::NullBuffer;
use arrow_schema::{Field, Fields};
Expand Down Expand Up @@ -1763,6 +1781,45 @@ mod tests {
assert_eq!(location_obj2.get("y"), Some(Variant::from(-122.4f64)));
}

#[test]
fn test_cast_to_variant_date() {
// Date32Array
run_test(
Arc::new(Date32Array::from(vec![
Some(Date32Type::from_naive_date(NaiveDate::MIN)),
None,
Some(Date32Type::from_naive_date(
NaiveDate::from_ymd_opt(2025, 8, 1).unwrap(),
)),
Some(Date32Type::from_naive_date(NaiveDate::MAX)),
])),
vec![
Some(Variant::Date(NaiveDate::MIN)),
None,
Some(Variant::Date(NaiveDate::from_ymd_opt(2025, 8, 1).unwrap())),
Some(Variant::Date(NaiveDate::MAX)),
],
);

// Date64Array
run_test(
Arc::new(Date64Array::from(vec![
Some(Date64Type::from_naive_date(NaiveDate::MIN)),
None,
Some(Date64Type::from_naive_date(
NaiveDate::from_ymd_opt(2025, 8, 1).unwrap(),
)),
Some(Date64Type::from_naive_date(NaiveDate::MAX)),
])),
vec![
Some(Variant::Date(NaiveDate::MIN)),
None,
Some(Variant::Date(NaiveDate::from_ymd_opt(2025, 8, 1).unwrap())),
Some(Variant::Date(NaiveDate::MAX)),
],
);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could potentially add a negative test here, although it's difficult to construct an invalid Date64Type.

}

/// Converts the given `Array` to a `VariantArray` and tests the conversion
/// against the expected values. It also tests the handling of nulls by
/// setting one element to null and verifying the output.
Expand Down
Loading