@@ -23,18 +23,17 @@ use arrow::array::{
23
23
TimestampSecondArray ,
24
24
} ;
25
25
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 ,
30
30
} ;
31
31
use arrow:: temporal_conversions:: {
32
32
timestamp_ms_to_datetime, timestamp_ns_to_datetime, timestamp_s_to_datetime,
33
33
timestamp_us_to_datetime,
34
34
} ;
35
35
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 } ;
38
37
use half:: f16;
39
38
use parquet_variant:: {
40
39
Variant , VariantBuilder , VariantDecimal16 , VariantDecimal4 , VariantDecimal8 ,
@@ -485,6 +484,24 @@ pub fn cast_to_variant(input: &dyn Array) -> Result<VariantArray, ArrowError> {
485
484
builder. append_variant ( variant) ;
486
485
}
487
486
}
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
+ }
488
505
dt => {
489
506
return Err ( ArrowError :: CastError ( format ! (
490
507
"Unsupported data type for casting to Variant: {dt:?}" ,
@@ -502,12 +519,13 @@ pub fn cast_to_variant(input: &dyn Array) -> Result<VariantArray, ArrowError> {
502
519
mod tests {
503
520
use super :: * ;
504
521
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 ,
511
529
} ;
512
530
use arrow:: buffer:: NullBuffer ;
513
531
use arrow_schema:: { Field , Fields } ;
@@ -1763,6 +1781,45 @@ mod tests {
1763
1781
assert_eq ! ( location_obj2. get( "y" ) , Some ( Variant :: from( -122.4f64 ) ) ) ;
1764
1782
}
1765
1783
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
+
1766
1823
/// Converts the given `Array` to a `VariantArray` and tests the conversion
1767
1824
/// against the expected values. It also tests the handling of nulls by
1768
1825
/// setting one element to null and verifying the output.
0 commit comments