15
15
// specific language governing permissions and limitations
16
16
// under the License.
17
17
18
- use arrow:: array:: { ArrayRef , BinaryViewArray , NullBufferBuilder , PrimitiveBuilder } ;
18
+ use arrow:: array:: {
19
+ Array , ArrayRef , BinaryViewArray , NullBufferBuilder , PrimitiveArray , PrimitiveBuilder ,
20
+ } ;
19
21
use arrow:: compute:: CastOptions ;
20
22
use arrow:: datatypes:: { self , ArrowPrimitiveType , DataType } ;
21
23
use arrow:: error:: { ArrowError , Result } ;
@@ -30,6 +32,7 @@ use std::sync::Arc;
30
32
/// `VariantToArrowRowBuilder` (below) and `VariantToShreddedPrimitiveVariantRowBuilder` (in
31
33
/// `shred_variant.rs`).
32
34
pub ( crate ) enum PrimitiveVariantToArrowRowBuilder < ' a > {
35
+ Boolean ( VariantToBooleanArrowRowBuilder < ' a > ) ,
33
36
Int8 ( VariantToPrimitiveArrowRowBuilder < ' a , datatypes:: Int8Type > ) ,
34
37
Int16 ( VariantToPrimitiveArrowRowBuilder < ' a , datatypes:: Int16Type > ) ,
35
38
Int32 ( VariantToPrimitiveArrowRowBuilder < ' a , datatypes:: Int32Type > ) ,
@@ -41,6 +44,7 @@ pub(crate) enum PrimitiveVariantToArrowRowBuilder<'a> {
41
44
Float16 ( VariantToPrimitiveArrowRowBuilder < ' a , datatypes:: Float16Type > ) ,
42
45
Float32 ( VariantToPrimitiveArrowRowBuilder < ' a , datatypes:: Float32Type > ) ,
43
46
Float64 ( VariantToPrimitiveArrowRowBuilder < ' a , datatypes:: Float64Type > ) ,
47
+ Boolean ( VariantToBooleanArrowRowBuilder < ' a > ) ,
44
48
}
45
49
46
50
/// Builder for converting variant values into strongly typed Arrow arrays.
@@ -59,6 +63,7 @@ impl<'a> PrimitiveVariantToArrowRowBuilder<'a> {
59
63
pub fn append_null ( & mut self ) -> Result < ( ) > {
60
64
use PrimitiveVariantToArrowRowBuilder :: * ;
61
65
match self {
66
+ Boolean ( b) => b. append_null ( ) ,
62
67
Int8 ( b) => b. append_null ( ) ,
63
68
Int16 ( b) => b. append_null ( ) ,
64
69
Int32 ( b) => b. append_null ( ) ,
@@ -70,6 +75,8 @@ impl<'a> PrimitiveVariantToArrowRowBuilder<'a> {
70
75
Float16 ( b) => b. append_null ( ) ,
71
76
Float32 ( b) => b. append_null ( ) ,
72
77
Float64 ( b) => b. append_null ( ) ,
78
+ TimestampMicro ( b) => b. append_null ( ) ,
79
+ TimestampNano ( b) => b. append_null ( ) ,
73
80
}
74
81
}
75
82
@@ -87,12 +94,14 @@ impl<'a> PrimitiveVariantToArrowRowBuilder<'a> {
87
94
Float16 ( b) => b. append_value ( value) ,
88
95
Float32 ( b) => b. append_value ( value) ,
89
96
Float64 ( b) => b. append_value ( value) ,
97
+ Boolean ( b) => b. append_value ( value) ,
90
98
}
91
99
}
92
100
93
101
pub fn finish ( self ) -> Result < ArrayRef > {
94
102
use PrimitiveVariantToArrowRowBuilder :: * ;
95
103
match self {
104
+ Boolean ( b) => b. finish ( ) ,
96
105
Int8 ( b) => b. finish ( ) ,
97
106
Int16 ( b) => b. finish ( ) ,
98
107
Int32 ( b) => b. finish ( ) ,
@@ -104,6 +113,7 @@ impl<'a> PrimitiveVariantToArrowRowBuilder<'a> {
104
113
Float16 ( b) => b. finish ( ) ,
105
114
Float32 ( b) => b. finish ( ) ,
106
115
Float64 ( b) => b. finish ( ) ,
116
+ Boolean ( b) => b. finish ( ) ,
107
117
}
108
118
}
109
119
}
@@ -146,6 +156,7 @@ pub(crate) fn make_primitive_variant_to_arrow_row_builder<'a>(
146
156
use PrimitiveVariantToArrowRowBuilder :: * ;
147
157
148
158
let builder = match data_type {
159
+ DataType :: Boolean => Boolean ( VariantToBooleanArrowRowBuilder :: new ( cast_options, capacity) ) ,
149
160
DataType :: Int8 => Int8 ( VariantToPrimitiveArrowRowBuilder :: new (
150
161
cast_options,
151
162
capacity,
@@ -190,6 +201,7 @@ pub(crate) fn make_primitive_variant_to_arrow_row_builder<'a>(
190
201
cast_options,
191
202
capacity,
192
203
) ) ,
204
+ DataType :: Boolean => Boolean ( VariantToBooleanArrowRowBuilder :: new ( cast_options, capacity) ) ,
193
205
_ if data_type. is_primitive ( ) => {
194
206
return Err ( ArrowError :: NotYetImplemented ( format ! (
195
207
"Primitive data_type {data_type:?} not yet implemented"
@@ -297,6 +309,49 @@ fn get_type_name<T: ArrowPrimitiveType>() -> &'static str {
297
309
}
298
310
}
299
311
312
+ /// Builder for converting variant values to boolean values
313
+ /// Boolean is not primitive types in Arrow, so we need a separate builder
314
+ pub ( crate ) struct VariantToBooleanArrowRowBuilder < ' a > {
315
+ builder : arrow:: array:: BooleanBuilder ,
316
+ cast_options : & ' a CastOptions < ' a > ,
317
+ }
318
+
319
+ impl < ' a > VariantToBooleanArrowRowBuilder < ' a > {
320
+ fn new ( cast_options : & ' a CastOptions < ' a > , capacity : usize ) -> Self {
321
+ Self {
322
+ builder : arrow:: array:: BooleanBuilder :: with_capacity ( capacity) ,
323
+ cast_options,
324
+ }
325
+ }
326
+
327
+ fn append_null ( & mut self ) -> Result < ( ) > {
328
+ self . builder . append_null ( ) ;
329
+ Ok ( ( ) )
330
+ }
331
+
332
+ fn append_value ( & mut self , value : & Variant < ' _ , ' _ > ) -> Result < bool > {
333
+ if let Some ( v) = value. as_boolean ( ) {
334
+ self . builder . append_value ( v) ;
335
+ Ok ( true )
336
+ } else {
337
+ if !self . cast_options . safe {
338
+ // Unsafe casting: return error on conversion failure
339
+ return Err ( ArrowError :: CastError ( format ! (
340
+ "Failed to extract boolean from variant {:?} at path VariantPath([])" ,
341
+ value
342
+ ) ) ) ;
343
+ }
344
+ // Safe casting: append null on conversion failure
345
+ self . builder . append_null ( ) ;
346
+ Ok ( false )
347
+ }
348
+ }
349
+
350
+ fn finish ( mut self ) -> Result < ArrayRef > {
351
+ Ok ( Arc :: new ( self . builder . finish ( ) ) )
352
+ }
353
+ }
354
+
300
355
/// Builder for converting variant values to primitive values
301
356
pub ( crate ) struct VariantToPrimitiveArrowRowBuilder < ' a , T : ArrowPrimitiveType > {
302
357
builder : arrow:: array:: PrimitiveBuilder < T > ,
0 commit comments