1515// specific language governing permissions and limitations
1616// under the License.
1717
18- use arrow:: array:: { ArrayRef , BinaryViewArray , NullBufferBuilder , PrimitiveBuilder } ;
18+ use arrow:: array:: {
19+ Array , ArrayRef , BinaryViewArray , NullBufferBuilder , PrimitiveArray , PrimitiveBuilder ,
20+ } ;
1921use arrow:: compute:: CastOptions ;
2022use arrow:: datatypes:: { self , ArrowPrimitiveType , DataType } ;
2123use arrow:: error:: { ArrowError , Result } ;
@@ -30,6 +32,7 @@ use std::sync::Arc;
3032/// `VariantToArrowRowBuilder` (below) and `VariantToShreddedPrimitiveVariantRowBuilder` (in
3133/// `shred_variant.rs`).
3234pub ( crate ) enum PrimitiveVariantToArrowRowBuilder < ' a > {
35+ Boolean ( VariantToBooleanArrowRowBuilder < ' a > ) ,
3336 Int8 ( VariantToPrimitiveArrowRowBuilder < ' a , datatypes:: Int8Type > ) ,
3437 Int16 ( VariantToPrimitiveArrowRowBuilder < ' a , datatypes:: Int16Type > ) ,
3538 Int32 ( VariantToPrimitiveArrowRowBuilder < ' a , datatypes:: Int32Type > ) ,
@@ -41,6 +44,7 @@ pub(crate) enum PrimitiveVariantToArrowRowBuilder<'a> {
4144 Float16 ( VariantToPrimitiveArrowRowBuilder < ' a , datatypes:: Float16Type > ) ,
4245 Float32 ( VariantToPrimitiveArrowRowBuilder < ' a , datatypes:: Float32Type > ) ,
4346 Float64 ( VariantToPrimitiveArrowRowBuilder < ' a , datatypes:: Float64Type > ) ,
47+ Boolean ( VariantToBooleanArrowRowBuilder < ' a > ) ,
4448}
4549
4650/// Builder for converting variant values into strongly typed Arrow arrays.
@@ -59,6 +63,7 @@ impl<'a> PrimitiveVariantToArrowRowBuilder<'a> {
5963 pub fn append_null ( & mut self ) -> Result < ( ) > {
6064 use PrimitiveVariantToArrowRowBuilder :: * ;
6165 match self {
66+ Boolean ( b) => b. append_null ( ) ,
6267 Int8 ( b) => b. append_null ( ) ,
6368 Int16 ( b) => b. append_null ( ) ,
6469 Int32 ( b) => b. append_null ( ) ,
@@ -70,6 +75,8 @@ impl<'a> PrimitiveVariantToArrowRowBuilder<'a> {
7075 Float16 ( b) => b. append_null ( ) ,
7176 Float32 ( b) => b. append_null ( ) ,
7277 Float64 ( b) => b. append_null ( ) ,
78+ TimestampMicro ( b) => b. append_null ( ) ,
79+ TimestampNano ( b) => b. append_null ( ) ,
7380 }
7481 }
7582
@@ -87,12 +94,14 @@ impl<'a> PrimitiveVariantToArrowRowBuilder<'a> {
8794 Float16 ( b) => b. append_value ( value) ,
8895 Float32 ( b) => b. append_value ( value) ,
8996 Float64 ( b) => b. append_value ( value) ,
97+ Boolean ( b) => b. append_value ( value) ,
9098 }
9199 }
92100
93101 pub fn finish ( self ) -> Result < ArrayRef > {
94102 use PrimitiveVariantToArrowRowBuilder :: * ;
95103 match self {
104+ Boolean ( b) => b. finish ( ) ,
96105 Int8 ( b) => b. finish ( ) ,
97106 Int16 ( b) => b. finish ( ) ,
98107 Int32 ( b) => b. finish ( ) ,
@@ -104,6 +113,7 @@ impl<'a> PrimitiveVariantToArrowRowBuilder<'a> {
104113 Float16 ( b) => b. finish ( ) ,
105114 Float32 ( b) => b. finish ( ) ,
106115 Float64 ( b) => b. finish ( ) ,
116+ Boolean ( b) => b. finish ( ) ,
107117 }
108118 }
109119}
@@ -146,6 +156,7 @@ pub(crate) fn make_primitive_variant_to_arrow_row_builder<'a>(
146156 use PrimitiveVariantToArrowRowBuilder :: * ;
147157
148158 let builder = match data_type {
159+ DataType :: Boolean => Boolean ( VariantToBooleanArrowRowBuilder :: new ( cast_options, capacity) ) ,
149160 DataType :: Int8 => Int8 ( VariantToPrimitiveArrowRowBuilder :: new (
150161 cast_options,
151162 capacity,
@@ -190,6 +201,7 @@ pub(crate) fn make_primitive_variant_to_arrow_row_builder<'a>(
190201 cast_options,
191202 capacity,
192203 ) ) ,
204+ DataType :: Boolean => Boolean ( VariantToBooleanArrowRowBuilder :: new ( cast_options, capacity) ) ,
193205 _ if data_type. is_primitive ( ) => {
194206 return Err ( ArrowError :: NotYetImplemented ( format ! (
195207 "Primitive data_type {data_type:?} not yet implemented"
@@ -297,6 +309,49 @@ fn get_type_name<T: ArrowPrimitiveType>() -> &'static str {
297309 }
298310}
299311
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+
300355/// Builder for converting variant values to primitive values
301356pub ( crate ) struct VariantToPrimitiveArrowRowBuilder < ' a , T : ArrowPrimitiveType > {
302357 builder : arrow:: array:: PrimitiveBuilder < T > ,
0 commit comments