scovich commented on code in PR #8345:
URL: https://github.com/apache/arrow-rs/pull/8345#discussion_r2349613663
##########
parquet-variant-compute/src/variant_to_arrow.rs:
##########
@@ -27,87 +26,117 @@ use crate::VariantArrayBuilder;
use std::sync::Arc;
+/// Builder for converting variant values into strongly typed Arrow arrays.
+///
+/// Useful for variant_get kernels that need to extract specific paths from
variant values, possibly
+/// with casting of leaf values to specific types.
+pub(crate) enum VariantToArrowRowBuilder<'a> {
+ // Direct builders (no path extraction)
+ Int8(VariantToPrimitiveArrowRowBuilder<'a, datatypes::Int8Type>),
+ Int16(VariantToPrimitiveArrowRowBuilder<'a, datatypes::Int16Type>),
+ Int32(VariantToPrimitiveArrowRowBuilder<'a, datatypes::Int32Type>),
+ Int64(VariantToPrimitiveArrowRowBuilder<'a, datatypes::Int64Type>),
+ Float16(VariantToPrimitiveArrowRowBuilder<'a, datatypes::Float16Type>),
+ Float32(VariantToPrimitiveArrowRowBuilder<'a, datatypes::Float32Type>),
+ Float64(VariantToPrimitiveArrowRowBuilder<'a, datatypes::Float64Type>),
+ BinaryVariant(VariantToBinaryVariantArrowRowBuilder),
+
+ // Path extraction wrapper - contains a boxed enum for any of the above
+ WithPath(VariantPathRowBuilder<'a>),
+}
+
+impl<'a> VariantToArrowRowBuilder<'a> {
+ pub fn append_null(&mut self) -> Result<()> {
+ use VariantToArrowRowBuilder::*;
+ match self {
+ Int8(b) => b.append_null(),
+ Int16(b) => b.append_null(),
+ Int32(b) => b.append_null(),
+ Int64(b) => b.append_null(),
+ Float16(b) => b.append_null(),
+ Float32(b) => b.append_null(),
+ Float64(b) => b.append_null(),
+ BinaryVariant(b) => b.append_null(),
+ WithPath(path_builder) => path_builder.append_null(),
+ }
+ }
+
+ pub fn append_value(&mut self, value: &Variant<'_, '_>) -> Result<bool> {
+ use VariantToArrowRowBuilder::*;
+ match self {
+ Int8(b) => b.append_value(value),
+ Int16(b) => b.append_value(value),
+ Int32(b) => b.append_value(value),
+ Int64(b) => b.append_value(value),
+ Float16(b) => b.append_value(value),
+ Float32(b) => b.append_value(value),
+ Float64(b) => b.append_value(value),
+ BinaryVariant(b) => b.append_value(value),
+ WithPath(path_builder) => path_builder.append_value(value),
+ }
+ }
+
+ pub fn finish(&mut self) -> Result<ArrayRef> {
+ use VariantToArrowRowBuilder::*;
+ match self {
+ Int8(b) => b.finish(),
+ Int16(b) => b.finish(),
+ Int32(b) => b.finish(),
+ Int64(b) => b.finish(),
+ Float16(b) => b.finish(),
+ Float32(b) => b.finish(),
+ Float64(b) => b.finish(),
+ BinaryVariant(b) => b.finish(),
+ WithPath(path_builder) => path_builder.finish(),
+ }
+ }
+}
+
pub(crate) fn make_variant_to_arrow_row_builder<'a>(
//metadata: &BinaryViewArray,
path: VariantPath<'a>,
- data_type: Option<&'a datatypes::DataType>,
+ data_type: Option<&'a DataType>,
cast_options: &'a CastOptions,
-) -> Result<Box<dyn VariantToArrowRowBuilder + 'a>> {
- use datatypes::{
- Float16Type, Float32Type, Float64Type, Int16Type, Int32Type,
Int64Type, Int8Type,
- };
+) -> Result<VariantToArrowRowBuilder<'a>> {
+ use VariantToArrowRowBuilder::*;
- let builder = match data_type {
+ let mut builder = match data_type {
// If no data type was requested, build an unshredded VariantArray.
- None => VariantToBinaryVariantArrowRowBuilder::new(16).with_path(path),
- Some(datatypes::DataType::Int8) => {
-
VariantToPrimitiveArrowRowBuilder::<Int8Type>::new(cast_options).with_path(path)
- }
- Some(datatypes::DataType::Int16) => {
-
VariantToPrimitiveArrowRowBuilder::<Int16Type>::new(cast_options).with_path(path)
- }
- Some(datatypes::DataType::Int32) => {
-
VariantToPrimitiveArrowRowBuilder::<Int32Type>::new(cast_options).with_path(path)
- }
- Some(datatypes::DataType::Int64) => {
-
VariantToPrimitiveArrowRowBuilder::<Int64Type>::new(cast_options).with_path(path)
- }
- Some(datatypes::DataType::Float16) => {
-
VariantToPrimitiveArrowRowBuilder::<Float16Type>::new(cast_options).with_path(path)
- }
- Some(datatypes::DataType::Float32) => {
-
VariantToPrimitiveArrowRowBuilder::<Float32Type>::new(cast_options).with_path(path)
- }
- Some(datatypes::DataType::Float64) => {
-
VariantToPrimitiveArrowRowBuilder::<Float64Type>::new(cast_options).with_path(path)
- }
+ None => BinaryVariant(VariantToBinaryVariantArrowRowBuilder::new(16)),
+ Some(DataType::Int8) =>
Int8(VariantToPrimitiveArrowRowBuilder::new(cast_options)),
+ Some(DataType::Int16) =>
Int16(VariantToPrimitiveArrowRowBuilder::new(cast_options)),
+ Some(DataType::Int32) =>
Int32(VariantToPrimitiveArrowRowBuilder::new(cast_options)),
+ Some(DataType::Int64) =>
Int64(VariantToPrimitiveArrowRowBuilder::new(cast_options)),
+ Some(DataType::Float16) =>
Float16(VariantToPrimitiveArrowRowBuilder::new(cast_options)),
+ Some(DataType::Float32) =>
Float32(VariantToPrimitiveArrowRowBuilder::new(cast_options)),
+ Some(DataType::Float64) =>
Float64(VariantToPrimitiveArrowRowBuilder::new(cast_options)),
_ => {
return Err(ArrowError::NotYetImplemented(format!(
"variant_get with path={:?} and data_type={:?} not yet
implemented",
path, data_type
)));
}
};
- Ok(builder)
-}
-/// Builder for converting variant values into strongly typed Arrow arrays.
-///
-/// Useful for variant_get kernels that need to extract specific paths from
variant values, possibly
-/// with casting of leaf values to specific types.
-pub(crate) trait VariantToArrowRowBuilder {
- fn append_null(&mut self) -> Result<()>;
-
- fn append_value(&mut self, value: &Variant<'_, '_>) -> Result<bool>;
+ // Wrap with path extraction if needed
+ if !path.is_empty() {
+ builder = WithPath(VariantPathRowBuilder {
+ builder: Box::new(builder),
+ path,
+ })
+ };
- fn finish(&mut self) -> Result<ArrayRef>;
+ Ok(builder)
}
/// A thin wrapper whose only job is to extract a specific path from a variant
value and pass the
/// result to a nested builder.
-struct VariantPathRowBuilder<'a, T: VariantToArrowRowBuilder> {
- builder: T,
+pub(crate) struct VariantPathRowBuilder<'a> {
+ builder: Box<VariantToArrowRowBuilder<'a>>,
Review Comment:
Because `VariantPathRowBuilder` is one of the enum variants of
`VariantToArrowRowBuilder`, and it can't refer to itself
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]