15
15
// specific language governing permissions and limitations
16
16
// under the License.
17
17
18
+ use std:: env:: var;
18
19
use crate :: variant_get:: output:: OutputBuilder ;
19
20
use crate :: VariantArray ;
20
21
use arrow:: array:: { Array , ArrayRef , ArrowPrimitiveType , AsArray , BinaryViewArray , NullBufferBuilder , PrimitiveArray } ;
21
22
use arrow:: compute:: { cast_with_options, CastOptions } ;
22
23
use arrow_schema:: { ArrowError , FieldRef } ;
23
- use parquet_variant:: VariantPath ;
24
+ use parquet_variant:: { Variant , VariantPath } ;
24
25
use std:: marker:: PhantomData ;
25
26
use std:: sync:: Arc ;
26
27
@@ -64,7 +65,7 @@ impl<'a, T: ArrowPrimitiveType> OutputBuilder for PrimitiveOutputBuilder<'a, T>
64
65
let mut values = Vec :: with_capacity ( variant_array. len ( ) ) ;
65
66
let typed_value =
66
67
cast_with_options ( typed_value, & self . as_type . data_type ( ) , & self . cast_options ) ?;
67
- // downcast to the primitive type
68
+ // downcast to the primitive array (e.g. Int32Array, Float64Array, etc)
68
69
let typed_value = typed_value. as_primitive :: < T > ( ) ;
69
70
70
71
for i in 0 ..variant_array. len ( ) {
@@ -74,19 +75,30 @@ impl<'a, T: ArrowPrimitiveType> OutputBuilder for PrimitiveOutputBuilder<'a, T>
74
75
continue ;
75
76
}
76
77
77
- nulls. append_non_null ( ) ;
78
78
79
+ // if the typed value is null, decode the variant and extract the value
79
80
if typed_value. is_null ( i) {
80
- todo ! ( )
81
- /*
82
- // fall back to the value (variant) field
83
- if let Some(value) = variant_array.value(i) {
84
- // extract the path from the variant and try to convert it to the desired type
85
- let extracted = value.get_path(&self.path)
86
- }
87
- */
81
+ // todo follow path
82
+ let Some ( variant) = variant_array. value ( i) else {
83
+ nulls. append_null ( ) ;
84
+ values. push ( T :: default_value ( ) ) ; // not used, placeholder
85
+ continue ;
86
+ } ;
87
+
88
+ let Some ( value) = extract_primitive_from_variant :: < T > ( & variant) else {
89
+ return Err ( ArrowError :: CastError ( format ! (
90
+ "Failed to extract primitive of type {:?} from variant {:?} at path {:?}" ,
91
+ self . as_type. data_type( ) ,
92
+ variant,
93
+ self . path
94
+ ) ) ) ;
95
+ } ;
96
+
97
+ nulls. append_non_null ( ) ;
98
+ values. push ( value)
88
99
} else {
89
100
// otherwise we have a typed value, so we can use it directly
101
+ nulls. append_non_null ( ) ;
90
102
values. push ( typed_value. value ( i) ) ;
91
103
}
92
104
}
@@ -125,3 +137,8 @@ impl<'a, T: ArrowPrimitiveType> OutputBuilder for PrimitiveOutputBuilder<'a, T>
125
137
todo ! ( )
126
138
}
127
139
}
140
+
141
+ /// extract a primitive value from a variant
142
+ fn extract_primitive_from_variant < T : ArrowPrimitiveType > ( variant : & Variant ) -> Option < T :: Native > {
143
+ todo ! ( )
144
+ }
0 commit comments