Skip to content

Commit caa9420

Browse files
committed
draft for reaching consensus
1 parent 4a21443 commit caa9420

File tree

3 files changed

+57
-22
lines changed

3 files changed

+57
-22
lines changed

parquet-variant-compute/src/cast_to_variant.rs

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18+
use crate::cast_conversion;
1819
use crate::{VariantArray, VariantArrayBuilder};
1920
use arrow::array::{Array, AsArray};
2021
use 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]
3032
macro_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 => {

parquet-variant-compute/src/lib.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,26 @@ pub use variant_array_builder::{VariantArrayBuilder, VariantArrayVariantBuilder}
4747

4848
pub use from_json::batch_json_string_to_variant;
4949
pub use to_json::batch_variant_to_json_string;
50+
51+
/// Convert the input array to a `VariantArray` row by row, using `method`
52+
/// to downcast the generic array to a specific array type and `cast_fn`
53+
/// to transform each element to a type compatible with Variant
54+
#[macro_export]
55+
macro_rules! cast_conversion {
56+
($t:ty, $method:ident, $cast_fn:expr, $input:expr, builder => $builder:expr) => {{
57+
let array = $input.$method::<$t>();
58+
for i in 0..array.len() {
59+
if array.is_null(i) {
60+
$builder.append_null();
61+
continue;
62+
}
63+
let cast_value = $cast_fn(array.value(i));
64+
$builder.append_variant(Variant::from(cast_value));
65+
}
66+
}};
67+
($t:ty, $method:ident, $cast_fn:expr, $input:expr, index => $index:expr) => {{
68+
let array = $input.$method::<$t>();
69+
let cast_value = $cast_fn(array.value($index));
70+
Variant::from(cast_value)
71+
}};
72+
}

parquet-variant-compute/src/variant_array.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@
1919
2020
use arrow::array::{Array, ArrayData, ArrayRef, AsArray, BinaryViewArray, StructArray};
2121
use arrow::buffer::NullBuffer;
22-
use arrow::datatypes::Int32Type;
22+
use arrow::datatypes::{Int16Type, Int32Type};
2323
use arrow_schema::{ArrowError, DataType};
2424
use parquet_variant::Variant;
2525
use std::any::Any;
2626
use std::sync::Arc;
2727

28+
use crate::cast_conversion;
29+
2830
/// An array of Parquet [`Variant`] values
2931
///
3032
/// A [`VariantArray`] wraps an Arrow [`StructArray`] that stores the underlying
@@ -337,6 +339,9 @@ fn typed_value_to_variant(typed_value: &ArrayRef, index: usize) -> Variant<'_, '
337339
let typed_value = typed_value.as_primitive::<Int32Type>();
338340
Variant::from(typed_value.value(index))
339341
}
342+
DataType::Int16 => {
343+
cast_conversion!(Int16Type, as_primitive, |v| v, typed_value, index => index)
344+
}
340345
// todo other types here (note this is very similar to cast_to_variant.rs)
341346
// so it would be great to figure out how to share this code
342347
_ => {

0 commit comments

Comments
 (0)