Repository navigation
Store shredded Variants as columns and shred Variants on write #11814
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
77c75b7
3f08376
215dcf7
3322e2b
2d11775
c2b9cbb
30bd74f
290f187
a87e9c1
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 |
|---|---|---|
|
|
@@ -61,6 +61,8 @@ use crate::ParquetVariantArrayExt; | |
| use crate::ParquetVariantArraySlotsExt; | ||
| use crate::array::parquet_typed_value_from_logical_shredded; | ||
| use crate::builder::StorageBuilder; | ||
| use crate::fold::fold_union; | ||
| use crate::shred::has_union; | ||
|
|
||
| /// Arrow canonical extension name for Parquet Variant storage. | ||
| const PARQUET_VARIANT_ARROW_EXTENSION_NAME: &str = "arrow.parquet.variant"; | ||
|
|
@@ -172,6 +174,17 @@ pub(crate) fn prepare_variant_for_export( | |
| let array = array.execute_until::<ParquetVariantExportable>(ctx)?; | ||
|
|
||
| if let Some(variant) = array.as_opt::<Variant>() { | ||
| if let Some(shredded) = variant | ||
| .shredded() | ||
| .filter(|shredded| has_union(shredded.dtype())) | ||
| { | ||
| vortex_ensure!( | ||
| typed_value.is_none(), | ||
| "multiple levels of Variant shredding are unsupported during Arrow export" | ||
| ); | ||
| return prepare_union_for_export(variant.core_storage().clone(), shredded.clone(), ctx); | ||
| } | ||
|
|
||
| let typed_value = match variant.shredded() { | ||
| Some(shredded) => { | ||
| vortex_ensure!( | ||
|
|
@@ -180,6 +193,7 @@ pub(crate) fn prepare_variant_for_export( | |
| ); | ||
| Some(parquet_typed_value_from_logical_shredded( | ||
| shredded.clone(), | ||
| None, | ||
| ctx, | ||
| )?) | ||
| } | ||
|
|
@@ -229,6 +243,34 @@ pub(crate) fn prepare_variant_for_export( | |
| vortex_bail!("cannot export Variant without ParquetVariant core storage") | ||
| } | ||
|
|
||
| /// Prepares the Variant of `core` storage with a `shredded` child that holds a union. | ||
| /// | ||
| /// Parquet has no union, so each union is folded into the residual value of its node, with the | ||
| /// metadata of each row; see [`fold_union`]. The core storage is prepared first for its metadata. | ||
| fn prepare_union_for_export( | ||
| core: ArrayRef, | ||
| shredded: ArrayRef, | ||
| ctx: &mut ExecutionCtx, | ||
| ) -> VortexResult<ArrayRef> { | ||
| let core = prepare_variant_for_export(core, None, ctx)?; | ||
| let storage = core.as_::<ParquetVariant>(); | ||
| let metadata = storage.metadata().clone(); | ||
|
|
||
| if shredded.dtype().is_union() { | ||
| let value = fold_union(shredded, storage.value().cloned(), &metadata, ctx)?; | ||
|
Comment on lines
+255
to
+260
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.
When every value fits a union shredding, the residual core can be stored and read back as a constant Variant-null array. Preparing that core with Useful? React with 👍 / 👎. |
||
| return ParquetVariant::try_new( | ||
| ParquetVariantArrayExt::parquet_variant_validity(&storage), | ||
| metadata, | ||
| Some(value), | ||
| None, | ||
| ) | ||
| .map(IntoArray::into_array); | ||
| } | ||
|
|
||
| let typed_value = parquet_typed_value_from_logical_shredded(shredded, Some(&metadata), ctx)?; | ||
| attach_typed_value(core, Some(typed_value)) | ||
| } | ||
|
|
||
| /// Prepares every chunk of the `ChunkedArray` `array`, then concatenates their storage into one | ||
| /// Parquet Variant array. | ||
| /// | ||
|
|
@@ -527,11 +569,16 @@ mod tests { | |
| use vortex_array::arrays::ChunkedArray; | ||
| use vortex_array::arrays::ConstantArray; | ||
| use vortex_array::arrays::ListArray; | ||
| use vortex_array::arrays::ListViewArray; | ||
| use vortex_array::arrays::MaskedArray; | ||
| use vortex_array::arrays::PrimitiveArray; | ||
| use vortex_array::arrays::Struct; | ||
| use vortex_array::arrays::StructArray as VortexStructArray; | ||
| use vortex_array::arrays::VarBinViewArray; | ||
| use vortex_array::arrays::Variant; | ||
| use vortex_array::arrays::VariantArray; | ||
| use vortex_array::arrays::struct_::StructArrayExt; | ||
| use vortex_array::arrays::variant::VariantArraySlotsExt; | ||
| use vortex_array::assert_arrays_eq; | ||
| use vortex_array::dtype::DType; | ||
| use vortex_array::dtype::FieldNames; | ||
|
|
@@ -544,6 +591,8 @@ mod tests { | |
| use vortex_buffer::buffer; | ||
| use vortex_error::VortexResult; | ||
| use vortex_error::vortex_bail; | ||
| use vortex_functions::variant_pack::VariantPack; | ||
| use vortex_functions::variant_pack::VariantPackOptions; | ||
| use vortex_session::VortexSession; | ||
|
|
||
| use super::PARQUET_VARIANT_ARROW_EXTENSION_NAME; | ||
|
|
@@ -751,6 +800,77 @@ mod tests { | |
| Ok(()) | ||
| } | ||
|
|
||
| /// A partially shredded field read from a file is a lazy pack of its stored columns. It must | ||
| /// export as a `{value, typed_value}` field, as the canonical Variant does, and not as a | ||
| /// typed leaf of Variant storage. | ||
| #[rstest] | ||
| fn test_export_lazy_nested_residual(session: VortexSession) -> VortexResult<()> { | ||
| let core = | ||
| ConstantArray::new(Scalar::null(DType::Variant(Nullability::Nullable)), 2).into_array(); | ||
| let nested = shredded_with_nested_residual()?; | ||
| let nested = nested.as_::<Struct>(); | ||
| let a = nested.unmasked_field_by_name("a")?.as_::<Variant>(); | ||
| let a_shredded = VortexStructArray::try_new( | ||
| FieldNames::from_iter(["y"]), | ||
| vec![PrimitiveArray::from_option_iter([Some(5i32), None]).into_array()], | ||
| 2, | ||
| Validity::NonNullable, | ||
| )? | ||
| .into_array(); | ||
|
|
||
| let shredded = |a: ArrayRef| -> VortexResult<ArrayRef> { | ||
| Ok(VortexStructArray::try_new( | ||
| FieldNames::from_iter(["a"]), | ||
| vec![a], | ||
| 2, | ||
| Validity::NonNullable, | ||
| )? | ||
| .into_array()) | ||
| }; | ||
| let canonical = VariantArray::try_new(a.core_storage().clone(), Some(a_shredded.clone()))?; | ||
| let lazy = VariantPack::try_new( | ||
| a.core_storage().clone(), | ||
| a_shredded, | ||
| VariantPackOptions::new(false), | ||
| )?; | ||
| let expected = | ||
| VariantArray::try_new(core.clone(), Some(shredded(canonical.into_array())?))?; | ||
| let actual = VariantArray::try_new(core, Some(shredded(lazy.into_array())?))?; | ||
|
|
||
| let expected = export_variant(&session, expected.into_array())?; | ||
| let actual = export_variant(&session, actual.into_array())?; | ||
|
|
||
| assert_struct_arrays_eq(actual.inner(), expected.inner()); | ||
| Ok(()) | ||
| } | ||
|
|
||
| /// A shredded list read from a file is a list view. Its elements must export as | ||
| /// `{value, typed_value}` fields, as those of a list do. | ||
| #[rstest] | ||
| fn test_export_list_view_shredding(session: VortexSession) -> VortexResult<()> { | ||
| let mut ctx = session.create_execution_ctx(); | ||
| let list = ListArray::try_new( | ||
| PrimitiveArray::from_iter([1i32, 2, 3]).into_array(), | ||
| PrimitiveArray::from_iter([0u32, 2, 3]).into_array(), | ||
| Validity::NonNullable, | ||
| &mut ctx, | ||
| )? | ||
| .into_array(); | ||
| let list_view = list | ||
| .clone() | ||
| .execute::<ListViewArray>(&mut ctx)? | ||
| .into_array(); | ||
|
|
||
| let expected = VariantArray::try_new(variant_null_core(2), Some(list))?; | ||
| let actual = VariantArray::try_new(variant_null_core(2), Some(list_view))?; | ||
|
|
||
| let expected = export_variant(&session, expected.into_array())?; | ||
| let actual = export_variant(&session, actual.into_array())?; | ||
|
|
||
| assert_struct_arrays_eq(actual.inner(), expected.inner()); | ||
| Ok(()) | ||
| } | ||
|
|
||
| #[rstest] | ||
| fn test_export_rejects_nested_shredding_under_mask(session: VortexSession) -> VortexResult<()> { | ||
| let mut ctx = session.create_execution_ctx(); | ||
|
|
||
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.
When exporting a union nested in a shredded list after the Variant has been sliced, a canonical
ListArraycan retain the original full element buffer and non-zero offsets (vortex-array/src/arrays/list/compute/slice.rs). This branch skipslist_from_list_view, assigns metadata acrosselements.len(), and then folds every element, including the unreachable prefix and suffix, using the first retained row's metadata. If a discarded union element is an object containing a field absent from that row's metadata,fold_unionrejects the export; other discarded elements can likewise be encoded against the wrong metadata. Reset the offsets and slice the elements before deriving element metadata and rebuilding the typed value.Useful? React with 👍 / 👎.