-
Notifications
You must be signed in to change notification settings - Fork 1.1k
[Variant] Fix variant_get to return List<T> instead of List<Struct>
#9631
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -107,7 +107,7 @@ fn make_typed_variant_to_arrow_row_builder<'a>( | |
| | DataType::LargeListView(_) | ||
| | DataType::FixedSizeList(..)) => { | ||
| let builder = | ||
| ArrayVariantToArrowRowBuilder::try_new(data_type, cast_options, capacity)?; | ||
| ArrayVariantToArrowRowBuilder::try_new(data_type, cast_options, capacity, false)?; | ||
| Ok(Array(builder)) | ||
| } | ||
| data_type => { | ||
|
|
@@ -587,10 +587,17 @@ impl<'a> StructVariantToArrowRowBuilder<'a> { | |
| } | ||
|
|
||
| impl<'a> ArrayVariantToArrowRowBuilder<'a> { | ||
| /// Creates a new list builder for the given data type. | ||
| /// | ||
| /// # Arguments | ||
| /// * `shredded` - If true, element builders produce shredded structs with `value`/`typed_value` | ||
| /// fields (for [`crate::shred_variant()`]). If false, element builders produce strongly typed | ||
| /// arrays directly (for [`crate::variant_get()`]). | ||
| pub(crate) fn try_new( | ||
| data_type: &'a DataType, | ||
| cast_options: &'a CastOptions, | ||
| capacity: usize, | ||
| shredded: bool, | ||
| ) -> Result<Self> { | ||
| use ArrayVariantToArrowRowBuilder::*; | ||
|
|
||
|
|
@@ -602,6 +609,7 @@ impl<'a> ArrayVariantToArrowRowBuilder<'a> { | |
| $field.data_type(), | ||
| cast_options, | ||
| capacity, | ||
| shredded, | ||
| )?) | ||
| }; | ||
| } | ||
|
|
@@ -893,13 +901,45 @@ impl<'a> VariantToUuidArrowRowBuilder<'a> { | |
| } | ||
| } | ||
|
|
||
| /// Element builder for list variants, supporting both typed (for [`crate::variant_get()`]) | ||
| /// and shredded (for [`crate::shred_variant()`]) output modes. | ||
| enum ListElementBuilder<'a> { | ||
| /// Produces the target array type directly. | ||
| Typed(Box<VariantToArrowRowBuilder<'a>>), | ||
| /// Produces a shredded struct with `value` and `typed_value` fields. | ||
| Shredded(Box<VariantToShreddedVariantRowBuilder<'a>>), | ||
|
Comment on lines
+908
to
+910
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any particular reason these need to be boxed? They're fully strongly typed, no?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. They need to be boxed because of a recursive type cycle:
It would be infinitely sized without |
||
| } | ||
|
|
||
| impl<'a> ListElementBuilder<'a> { | ||
| fn append_value(&mut self, value: Variant<'_, '_>) -> Result<bool> { | ||
| match self { | ||
| Self::Typed(b) => b.append_value(value), | ||
| Self::Shredded(b) => b.append_value(value), | ||
| } | ||
| } | ||
|
|
||
| fn finish(self) -> Result<ArrayRef> { | ||
| match self { | ||
| Self::Typed(b) => b.finish(), | ||
| Self::Shredded(b) => { | ||
| let (value, typed_value, nulls) = b.finish()?; | ||
| Ok(ArrayRef::from(ShreddedVariantFieldArray::from_parts( | ||
| Some(value), | ||
| Some(typed_value), | ||
| nulls, | ||
| ))) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| pub(crate) struct VariantToListArrowRowBuilder<'a, O, const IS_VIEW: bool> | ||
| where | ||
| O: OffsetSizeTrait + ArrowNativeTypeOp, | ||
| { | ||
| field: FieldRef, | ||
| offsets: Vec<O>, | ||
| element_builder: Box<VariantToShreddedVariantRowBuilder<'a>>, | ||
| element_builder: ListElementBuilder<'a>, | ||
| nulls: NullBufferBuilder, | ||
| current_offset: O, | ||
| cast_options: &'a CastOptions<'a>, | ||
|
|
@@ -914,6 +954,7 @@ where | |
| element_data_type: &'a DataType, | ||
| cast_options: &'a CastOptions, | ||
| capacity: usize, | ||
| shredded: bool, | ||
| ) -> Result<Self> { | ||
| if capacity >= isize::MAX as usize { | ||
| return Err(ArrowError::ComputeError( | ||
|
|
@@ -922,16 +963,24 @@ where | |
| } | ||
| let mut offsets = Vec::with_capacity(capacity + 1); | ||
| offsets.push(O::ZERO); | ||
| let element_builder = make_variant_to_shredded_variant_arrow_row_builder( | ||
| element_data_type, | ||
| cast_options, | ||
| capacity, | ||
| NullValue::ArrayElement, | ||
| )?; | ||
| let element_builder = if shredded { | ||
| let builder = make_variant_to_shredded_variant_arrow_row_builder( | ||
| element_data_type, | ||
| cast_options, | ||
| capacity, | ||
| NullValue::ArrayElement, | ||
| )?; | ||
| ListElementBuilder::Shredded(Box::new(builder)) | ||
| } else { | ||
| let builder = | ||
| make_typed_variant_to_arrow_row_builder(element_data_type, cast_options, capacity)?; | ||
| ListElementBuilder::Typed(Box::new(builder)) | ||
| }; | ||
|
|
||
| Ok(Self { | ||
| field, | ||
| offsets, | ||
| element_builder: Box::new(element_builder), | ||
| element_builder, | ||
| nulls: NullBufferBuilder::new(capacity), | ||
| current_offset: O::ZERO, | ||
| cast_options, | ||
|
|
@@ -966,9 +1015,7 @@ where | |
| } | ||
|
|
||
| fn finish(mut self) -> Result<ArrayRef> { | ||
| let (value, typed_value, nulls) = self.element_builder.finish()?; | ||
| let element_array = | ||
| ShreddedVariantFieldArray::from_parts(Some(value), Some(typed_value), nulls); | ||
| let element_array: ArrayRef = self.element_builder.finish()?; | ||
| let field = Arc::new( | ||
| self.field | ||
| .as_ref() | ||
|
|
@@ -987,15 +1034,15 @@ where | |
| field, | ||
| ScalarBuffer::from(self.offsets), | ||
| ScalarBuffer::from(sizes), | ||
| ArrayRef::from(element_array), | ||
| element_array, | ||
| self.nulls.finish(), | ||
| ); | ||
| Ok(Arc::new(list_view_array)) | ||
| } else { | ||
| let list_array = GenericListArray::<O>::new( | ||
| field, | ||
| OffsetBuffer::<O>::new(ScalarBuffer::from(self.offsets)), | ||
| ArrayRef::from(element_array), | ||
| element_array, | ||
| self.nulls.finish(), | ||
| ); | ||
| Ok(Arc::new(list_array)) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like this
shreddedparam allows quite a bit of code reuse. Should we look at other shred-vs-get pairs to see if similar consolidation is possible? Struct, in particular, is likely to be complex?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From the current impl, the struct builders aren't as straightforward to consolidate as the list builders were. Created an issue #9633 to keep track of the refactor