Repository navigation
Description
Is your feature request related to a problem or challenge? Please describe what you are trying to do.
- part of [EPIC] [Parquet] Implement Variant type support in Parquet #6736
- This is a follow up to [Variant] Add low level support for shredding and unshredding #7715
As we begin to contemplate how to read and write shredded variants, we will need some way to construct arrow arrays that contain shredded variants
Physically these will be Arrow StructArrays with two or three fields
- Non shredded: (2 fields)
STRUCT { "metadata": Binary, "value": Binary} - Shredded: (3 fields)
STRUCT { "metadata": Binary, "value": Binary, typed_value: STRUCT { ... } }
More information on to represent Variants as Arrow arrays can be found on the proposal:
- [Format] Add an Arrow Canonical Extension Type for Parquet Variant arrow#46908
- Google Document: https://docs.google.com/document/d/1pw0AWoMQY3SjD7R4LgbPvMjG_xSCtXp3rZHkVp9jpZ4/edit?usp=sharing
Describe the solution you'd like
I would like some way to construct such shredded arrays easily and efficiently in Idomatic Rust style
Describe alternatives you've considered
One an idea from @zeroshade (thank you!) is to create a VariantArrayBuilder that is responsible for building the correct StructArrays from variants, including shredding out any columns. In order to created a shredded output, you would provide the shredded schema up front
For example, (based on the go implemntation and @scovich 's comment here), to create a shredded Arrow array that shreds out columns "foo" and "bar" from any variant objects,
We would need this schema:
STRUCT {
metadata: BinaryView,
value: BinaryView,
typed_value: STRUCT {
foo: Int64,
bar: Int32
}
}
The code would look like this
// Create an arrow Field that describes the desired shredded output schema
let shredded_schema = Field::new_struct(
vec![ "metadata", "value", "typed_value"],
vec![Field::new(DataType::BinaryView), Field::new(DataType::BinaryView), Field:::new_struct(
vec!["foo", "bar"],
vec![Field::new(DataType::Int64), Field::new(DataType::Int32)],
));
// Create a builder for an array (batch) of Variant values
let array_builder = VariantArrayBuilder::new(shredded_schema);
// append a row to the builder
let object= array_builder.new_object();
... add appropriate fields ...
// use like normal ObjectBuilder(??)
object.finish()
// append a second row (has no foo or bar fields)
array_builder.append_value(43);
...
/// Finalze the builder
let variant_array: StructArray = array_builder.build()?;
// variant_array is a shreded variantI think a VariantArrayBuilder will be helpful for usecases other than Variant, and @harshmotw-db has created some version of one here:
Prior Art
Golang implementation:
- https://github.com/apache/arrow-go/blob/main/arrow/extensions/variant_test.go
- https://github.com/apache/arrow-go/blob/main/arrow/extensions/variant.go
- Here are some examples of it being used: https://github.com/apache/arrow-go/blob/b196d3b316d09f63786f021d4f1baa1fdd7620d2/arrow/extensions/variant_test.go#L363-L391
- Spark variant code: https://github.com/apache/spark/tree/master/common/variant/src/main/java/org/apache/spark/types/variant
Additional context
Activity
alamb commentedon Jul 11, 2025
Note one important property of this design is that we could incrementally build up shredded variant output from JSON directly, rather than having to parse the json into a variant and then shred it again (with a copy)
[-][Variant] API o construct Shredded Variant Arrays[/-][+][Variant] API to construct Shredded Variant Arrays[/+]scovich commentedon Jul 11, 2025
Same goes for converting a strongly-typed column as shredded variant, e.g. if the engine/application doing the write uses a fixed schema but writes as variant for compat with other engines/applications -- including past self -- who prefer other schemas.
alamb commentedon Jul 11, 2025
FYI in terms of heading in this direction, I think the first PR / code should be a basic VariantArrayBuilder (without shredding support) to get the basic outline in place. I may try to take a shot at this after merging #7884
(basically pull the variant building stuff into its own module)
friendlymatthew commentedon Jul 11, 2025
Sure that is fine by me. Though I did make some progress with the initial API with shredding support.
But I do agree, we should rip the existing builder code into its own module.
alamb commentedon Jul 11, 2025
Nice -- I just polished up #7884 and hpefully it can merge shortly
alamb commentedon Jul 11, 2025
Here is a PR to add
VariantArrayandVariantArrayBuilderVariantArrayandVariantArrayBuilderfor constructing Arrow Arrays of Variants #7905Add `VariantArray` and `VariantArrayBuilder` for constructing Arrow A…
alamb commentedon Jul 17, 2025
Update here
VariantArrayBuilder#7921Variants directly with the buildersMy suggested next steps are:
VariantArrayBuilder#7921shred_variantkernel that works for existing variant arraysWe can focus the effort initially on first version of
shred_variantwhere the API is straightfowrard 🤔alamb commentedon Jul 17, 2025
Update here
VariantArrayBuilder#7921Variants directly with the buildersMy suggested next steps are:
VariantArrayBuilder#7921shred_variantkernel that works for existing variant arraysWe can focus the effort initially on first version of
shred_variantwhere the API is straightfowrard 🤔friendlymatthew commentedon Jul 17, 2025
Hi @alamb I'm happy to continue on this issue. This week has been pretty hectic but I should be able to devote more time after Friday
alamb commentedon Jul 17, 2025
Awesome -- thank you @friendlymatthew --
alamb commentedon Jul 17, 2025
Update here
VariantArrayBuilder#7921Variants directly with the buildersMy suggested next steps are:
VariantArrayBuilder#7921shred_variantkernel that works for existing variant arraysWe can focus the effort initially on first version of
shred_variantwhere the API is straightfowrard 🤔scovich commentedon Jul 18, 2025
We need to decide whether the shredding schema should match what we will physically write, or if it's a logical schema for convenience? Because the actual physical shredding schema for the above would be:
(we could debate whether a missing
valuecolumn is a request from the user to drop all values that don't shred properly... but that seems like a massive footgun. I'd rather let the perfect shredding case encode all-null and let the parquet writer drop the column if it wants to)zeroshade commentedon Jul 18, 2025
I'm in favor of @scovich's suggestion, and that is what I did for the Go implementation along with my plan for defining the Canonical extension type.
The schema
Will be required given the current design of the canonical extension type, and providing
Would be an error.
alamb commentedon Aug 15, 2025
I believe the current plan we came up with is to implement shredding as a special case of
variant_get, as described herevariant_get: access asSome(DataType::Struct)(nested shredding) #8153alamb commentedon Sep 21, 2025
@scovich implemented
shred_variantin #8366 which I think completes this issue