1515// specific language governing permissions and limitations
1616// under the License.
1717
18+ use crate :: cast_conversion;
1819use crate :: { VariantArray , VariantArrayBuilder } ;
1920use arrow:: array:: { Array , AsArray } ;
2021use arrow:: datatypes:: {
@@ -27,6 +28,7 @@ use parquet_variant::Variant;
2728
2829/// Convert the input array of a specific primitive type to a `VariantArray`
2930/// row by row
31+ #[ macro_export]
3032macro_rules! primitive_conversion {
3133 ( $t: ty, $input: expr, $builder: expr) => { {
3234 let array = $input. as_primitive:: <$t>( ) ;
@@ -39,23 +41,28 @@ macro_rules! primitive_conversion {
3941 }
4042 } } ;
4143}
42-
43- /// Convert the input array to a `VariantArray` row by row, using `method`
44- /// to downcast the generic array to a specific array type and `cast_fn`
45- /// to transform each element to a type compatible with Variant
46- macro_rules! cast_conversion {
47- ( $t: ty, $method: ident, $cast_fn: expr, $input: expr, $builder: expr) => { {
48- let array = $input. $method:: <$t>( ) ;
49- for i in 0 ..array. len( ) {
50- if array. is_null( i) {
51- $builder. append_null( ) ;
52- continue ;
53- }
54- let cast_value = $cast_fn( array. value( i) ) ;
55- $builder. append_variant( Variant :: from( cast_value) ) ;
56- }
57- } } ;
58- }
44+ //
45+ // /// Convert the input array to a `VariantArray` row by row, using `method`
46+ // /// to downcast the generic array to a specific array type and `cast_fn`
47+ // /// to transform each element to a type compatible with Variant
48+ // macro_rules! cast_conversion {
49+ // ($t:ty, $method:ident, $cast_fn:expr, $input:expr, builder => $builder:expr) => {{
50+ // let array = $input.$method::<$t>();
51+ // for i in 0..array.len() {
52+ // if array.is_null(i) {
53+ // $builder.append_null();
54+ // continue;
55+ // }
56+ // let cast_value = $cast_fn(array.value(i));
57+ // $builder.append_variant(Variant::from(cast_value));
58+ // }
59+ // }};
60+ // ($t:ty, $method:ident, $cast_fn:expr, $input:expr, index => $index:expr) => {{
61+ // let array = $input.$method::<$t>();
62+ // let cast_value = $cast_fn(array.value($index));
63+ // Variant::from(cast_value)
64+ // }};
65+ // }
5966
6067/// Casts a typed arrow [`Array`] to a [`VariantArray`]. This is useful when you
6168/// need to convert a specific data type
@@ -87,13 +94,13 @@ pub fn cast_to_variant(input: &dyn Array) -> Result<VariantArray, ArrowError> {
8794 // todo: handle other types like Boolean, Strings, Date, Timestamp, etc.
8895 match input_type {
8996 DataType :: Binary => {
90- cast_conversion ! ( BinaryType , as_bytes, |v| v, input, builder) ;
97+ cast_conversion ! ( BinaryType , as_bytes, |v| v, input, builder => builder ) ;
9198 }
9299 DataType :: LargeBinary => {
93- cast_conversion ! ( LargeBinaryType , as_bytes, |v| v, input, builder) ;
100+ cast_conversion ! ( LargeBinaryType , as_bytes, |v| v, input, builder => builder ) ;
94101 }
95102 DataType :: BinaryView => {
96- cast_conversion ! ( BinaryViewType , as_byte_view, |v| v, input, builder) ;
103+ cast_conversion ! ( BinaryViewType , as_byte_view, |v| v, input, builder => builder ) ;
97104 }
98105 DataType :: Int8 => {
99106 primitive_conversion ! ( Int8Type , input, builder) ;
@@ -125,7 +132,7 @@ pub fn cast_to_variant(input: &dyn Array) -> Result<VariantArray, ArrowError> {
125132 as_primitive,
126133 |v: f16| -> f32 { v. into( ) } ,
127134 input,
128- builder
135+ builder => builder
129136 ) ;
130137 }
131138 DataType :: Float32 => {
0 commit comments