17
17
18
18
//! Module for transforming a typed arrow `Array` to `VariantArray`.
19
19
20
- use arrow:: datatypes:: {
21
- self , ArrowPrimitiveType , ArrowTimestampType , Date32Type , TimestampMicrosecondType ,
22
- TimestampNanosecondType ,
23
- } ;
20
+ use arrow:: datatypes:: { self , ArrowPrimitiveType , ArrowTimestampType , Date32Type } ;
24
21
use parquet_variant:: Variant ;
25
22
26
23
/// Options for controlling the behavior of `cast_to_variant_with_options`.
@@ -41,6 +38,13 @@ pub(crate) trait PrimitiveFromVariant: ArrowPrimitiveType {
41
38
fn from_variant ( variant : & Variant < ' _ , ' _ > ) -> Option < Self :: Native > ;
42
39
}
43
40
41
+ /// Extension trait for Arrow timestamp types that can extract their native value from a Variant
42
+ /// We can't use [`PrimitiveFromVariant`] directly because we might need to use methods that
43
+ /// are only available on [`ArrowTimestampType`] (such as with_timezone_opt)
44
+ pub ( crate ) trait TimestampFromVariant : ArrowTimestampType {
45
+ fn from_variant ( variant : & Variant < ' _ , ' _ > ) -> Option < Self :: Native > ;
46
+ }
47
+
44
48
/// Macro to generate PrimitiveFromVariant implementations for Arrow primitive types
45
49
macro_rules! impl_primitive_from_variant {
46
50
( $arrow_type: ty, $variant_method: ident $( , $cast_fn: expr) ?) => {
@@ -52,6 +56,18 @@ macro_rules! impl_primitive_from_variant {
52
56
}
53
57
}
54
58
} ;
59
+ ( $arrow_type: ty $( , $variant_method: ident => $cast_fn: expr ) + ) => {
60
+ impl TimestampFromVariant for $arrow_type {
61
+ fn from_variant( variant: & Variant <' _, ' _>) -> Option <Self :: Native > {
62
+ $(
63
+ if let Some ( value) = variant. $variant_method( ) {
64
+ return Some ( $cast_fn( value) ) ;
65
+ }
66
+ ) +
67
+ None
68
+ }
69
+ }
70
+ } ;
55
71
}
56
72
57
73
impl_primitive_from_variant ! ( datatypes:: Int32Type , as_int32) ;
@@ -70,41 +86,13 @@ impl_primitive_from_variant!(
70
86
as_naive_date,
71
87
Date32Type :: from_naive_date
72
88
) ;
73
-
74
- pub ( crate ) trait TimestampFromVariant : ArrowTimestampType {
75
- fn from_variant ( variant : & Variant < ' _ , ' _ > ) -> Option < Self :: Native > ;
76
- }
77
-
78
- macro_rules! impl_timestamp_from_variant {
79
- ( $timestamp_type: ty,
80
- $( $variant_pattern: pat => $conversion: expr ) ,+ $( , ) ?
81
- ) => {
82
- impl TimestampFromVariant for $timestamp_type {
83
- fn from_variant( variant: & Variant <' _, ' _>) -> Option <Self :: Native > {
84
- match variant {
85
- $(
86
- $variant_pattern => $conversion,
87
- ) +
88
- _ => None ,
89
- }
90
- }
91
- }
92
- } ;
93
- }
94
-
95
- impl_timestamp_from_variant ! (
96
- TimestampMicrosecondType ,
97
- Variant :: TimestampMicros ( t) => Some ( t. timestamp_micros( ) ) ,
98
- Variant :: TimestampNtzMicros ( t) => Some ( t. and_utc( ) . timestamp_micros( ) ) ,
99
- ) ;
100
-
101
- impl_timestamp_from_variant ! (
102
- TimestampNanosecondType ,
103
- Variant :: TimestampMicros ( t) => Some ( t. timestamp_micros( ) ) . map( |t| t * 1000 ) ,
104
- Variant :: TimestampNtzMicros ( t) => Some ( t. and_utc( ) . timestamp_micros( ) ) . map( |t| t * 1000 ) ,
105
- Variant :: TimestampNanos ( t) => t. timestamp_nanos_opt( ) ,
106
- Variant :: TimestampNtzNanos ( t) => t. and_utc( ) . timestamp_nanos_opt( ) ,
107
- ) ;
89
+ impl_primitive_from_variant ! (
90
+ datatypes:: TimestampMicrosecondType ,
91
+ as_timestamp_micros => |t| t) ;
92
+ impl_primitive_from_variant ! (
93
+ datatypes:: TimestampNanosecondType ,
94
+ as_timestamp_micros => |t| 1000 * t,
95
+ as_timestamp_nanos => |t| t) ;
108
96
109
97
/// Convert the value at a specific index in the given array into a `Variant`.
110
98
macro_rules! non_generic_conversion_single_value {
0 commit comments