15
15
// specific language governing permissions and limitations
16
16
// under the License.
17
17
18
+ use crate :: cast_conversion;
18
19
use crate :: { VariantArray , VariantArrayBuilder } ;
19
20
use arrow:: array:: { Array , AsArray } ;
20
21
use arrow:: datatypes:: {
@@ -27,6 +28,7 @@ use parquet_variant::Variant;
27
28
28
29
/// Convert the input array of a specific primitive type to a `VariantArray`
29
30
/// row by row
31
+ #[ macro_export]
30
32
macro_rules! primitive_conversion {
31
33
( $t: ty, $input: expr, $builder: expr) => { {
32
34
let array = $input. as_primitive:: <$t>( ) ;
@@ -39,23 +41,28 @@ macro_rules! primitive_conversion {
39
41
}
40
42
} } ;
41
43
}
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
+ // }
59
66
60
67
/// Casts a typed arrow [`Array`] to a [`VariantArray`]. This is useful when you
61
68
/// need to convert a specific data type
@@ -87,13 +94,13 @@ pub fn cast_to_variant(input: &dyn Array) -> Result<VariantArray, ArrowError> {
87
94
// todo: handle other types like Boolean, Strings, Date, Timestamp, etc.
88
95
match input_type {
89
96
DataType :: Binary => {
90
- cast_conversion ! ( BinaryType , as_bytes, |v| v, input, builder) ;
97
+ cast_conversion ! ( BinaryType , as_bytes, |v| v, input, builder => builder ) ;
91
98
}
92
99
DataType :: LargeBinary => {
93
- cast_conversion ! ( LargeBinaryType , as_bytes, |v| v, input, builder) ;
100
+ cast_conversion ! ( LargeBinaryType , as_bytes, |v| v, input, builder => builder ) ;
94
101
}
95
102
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 ) ;
97
104
}
98
105
DataType :: Int8 => {
99
106
primitive_conversion ! ( Int8Type , input, builder) ;
@@ -125,7 +132,7 @@ pub fn cast_to_variant(input: &dyn Array) -> Result<VariantArray, ArrowError> {
125
132
as_primitive,
126
133
|v: f16| -> f32 { v. into( ) } ,
127
134
input,
128
- builder
135
+ builder => builder
129
136
) ;
130
137
}
131
138
DataType :: Float32 => {
0 commit comments