Repository navigation
Description
Is your feature request related to a problem or challenge? Please describe what you are trying to do.
The variant shredding specification allows for variant values to be "shredded" where part of the overall variant is strongly-typed and part is normal binary variant. Working with shredded variant values requires writers to pull out specific subsets of a variant object that match a target schema ("shredding"). It also requires readers to potentially "unshred" by injecting strongly typed values back into the binary variant they came from.
Partial shredding of object values increases the complexity significantly -- some fields of an object could be shredded out as a struct, while others are not. And so on, recursively.
NOTE: The specification mandates that the variant metadata dictionary must contain all path parts, regardless of whether a given path is shredded or not. So (un)shredding operations do not need to modify the variant metadata dictionary.
Describe the solution you'd like
Ultimately, shredding and unshredding will be a problem for arrow-array and/or arrow-compute to solve (see below). But those higher level operations will need low-level support from Variant and its decoders/builders in order to do their work.
We should start figuring out what that low-level support looks like. A likely starting point would be the ability to insert and remove specific variant values from an existing variant object. These should be cheap byte-shuffling operations that don't waste time introspecting unrelated parts of the variant value buffer. And it needs to be efficient even when doing recursive inserts and removes as part of a partial (un)shredding operation.
At the higher level:
The parquet reader and writer will just use whatever shredding schema they receive from the parquet footer or user, respectively. No special low-level variant support needed there. But a user wishing to write shredded parquet will need a way to convert an Array of binary variant values into an Array of shredded variant values, or a strongly typed Array (e.g. StructArray) into an Array of shredded variant values. And a user wishing to read shredded parquet will will need a way to convert an Array of shredded variant values (with a specific shredding schema) to an Array of binary variant values, or an Array of shredded variant values having a different shredding schema, or a strongly typed Array (e.g. StructArray).
Describe alternatives you've considered
Just starting to think about this, and realizing we should probably start figuring out the low-level building blocks that arrow-array will eventually rely on. Now that we actually have variant builders and decoders, we can probably make progress here.
Additional context
Activity
tustvold commentedon Jun 19, 2025
Just a small point, we probably want to avoid fundamental crates like arrow-array, etc... from needing to depend on variants. These crates are used in contexts that are size sensitive, e.g. WASM, and so keeping them lightweight is important.
I'm not sure what kernel support for variants should look like, but my ideal would be to find a way to minimise the need for explicit support in kernels outside of those specifically for variants. I had envisaged that variants would need to be materialized/coerced to a concrete arrow array before being processed by arrow kernels such as take, cast, eq, etc...
Forcing people to depend on variant functionality, when it isn't actually part of arrow but instead parquet, feels unfortunate
scovich commentedon Jun 19, 2025
This issue is specifically about the low-level (byte slice) support for manipulating individual variant values -- a building block for future higher level columnar operations -- and that logic should almost certainly live in the parquet-variant crate?
Meanwhile -- One way or another, people will need a way to manipulate columns of variant values in and out of more strongly-typed forms. Otherwise I'm not sure how useful variant would be in practice? Where that higher level logic lives seems like a secondary question. Probably some arrow-variant crate that depends on arrow-schema, arrow-array, and parquet-variant?
Not sure how the type system should handle it -- I'm not really familiar arrow's logical type system -- but both binary and shredded variant columns physically take the form of structs. So I'd expect the parquet reader to produce a
StructArraywith the appropriate columns (metadata,value, and/ortyped_value). The problem is what to do with it at that point.One could imagine defining kernels like
variant_extract(that fetch a sub-variant by path, possibly with some form of casting), a variant "reader builder" (similar to JSON builder) that can convert variant values to strongly-typed (**), a "writer builder" that can convert strongly-typed values to variant, etc. All of those would likely need the kinds of byte-level manipulation this issue describes.(**) Problem is, the JSON reader builder has a lot of semantics challenges that people have kind of been expecting variant to solve... so we have some work to do there if we don't want to just lift and shift the problem from json to variant.
alamb commentedon Jun 20, 2025
Here is how I was imagining Variant support looks like:
StructArrays with at least two field:metadata, andvalueof binary (or maybe binary dictionary / RLE) type. This mirrrors the parquet physical representationMetadataon a field (as an extension type)StructArrays with the twometadataandvaluecolumns and the additional shredded columnsparquet-variant)SOme potential new kernels
variant_extractas proposed by @scovich perhaps)variant_extractperhaps to cast to/from variant and a (real)StructArrayI'll try and work up an example / diagram of how this would work over the next few days
friendlymatthew commentedon Jul 1, 2025
take
adriangb commentedon Jul 2, 2025
I've started working on this bit: apache/datafusion#15057
carpecodeum commentedon Jul 2, 2025
@friendlymatthew I have the time to work on this as well. Should we break this issue down into a set of tasks that we can both work on simultaneously?
alamb commentedon Jul 2, 2025
BTW I made some slides to try and show how I think parquet is going to work.
https://docs.google.com/presentation/d/1NN583KuJ3nelIrrH64HAASmbRtO9khhNSO3fw0_Nslc/edit?slide=id.g33d6952b95a_0_321#slide=id.g33d6952b95a_0_321
I would suggest you begin by sketching out what the APIs look like with an example usecase:
So maybe we need a "shred" kernel that takes a StructArray in with a shredding specification and produces a StructArray out with the appropriate fields added 🤔
adriangb commentedon Jul 2, 2025
I think this would be great. I'd also consider a kernel to go directly from json -> shredded if that's significantly cheaper computationally.
One thing to consider is how we're going to define the shredding strategy: first encountered keys, a fixed list of keys, most frequent keys, largest keys by size, ... I think there's a lot of possible strategies!
adriangb commentedon Jul 2, 2025
Btw @alamb I think slides are private
alamb commentedon Jul 3, 2025
Sorry -- fixed
Yes, I agree -- this is a key design API.
I beleive @RyanBlue said they used a "Shredded Schema" specification in iceberg-java on the Parquet call the other day-- maybe we can take a friendly look at what they have done there for API inspiration
[-]Add low level support for shredding and unshredding[/-][+][Variant] Add low level support for shredding and unshredding[/+]alamb commentedon Jul 10, 2025
@friendlymatthew and I spoke a bit about this API today. Here is what I think I heard
Add new kernels (in the
parquet-variant-computecrate that @harshmotw-db is making in #7884), something likeField Access
The first kernel we need is something to extract a field from a variant
Here is a databricks function that does this: https://docs.databricks.com/gcp/en/sql/language-manual/functions/variant_get
Open questions:
Shredding Kernel
Open questions:
ShreddingSpecificationlook like (we could look at the API in iceberg-java) to figure this outvariant_arrayalready has some shredded columnsunshred_variantkernelalamb commentedon Jul 10, 2025
Here is some code archeology in the iceberg crate:
https://github.com/search?q=repo%3Aapache%2Ficeberg%20shred&type=code
Here is the representation of the Shredded Variant:
https://github.com/apache/iceberg/blob/09140e52836048b112c42c9cfe721295bd57048b/core/src/main/java/org/apache/iceberg/variants/ShreddedObject.java#L41
I can't quite figure out how it works, but it looks like you provide a variant shredding function to the writer;
https://github.com/apache/iceberg/blob/09140e52836048b112c42c9cfe721295bd57048b/parquet/src/test/java/org/apache/iceberg/parquet/TestVariantMetrics.java#L460
6 remaining items
scovich commentedon Jul 10, 2025
I'm not sure I follow this argument? If Databricks only supports a subset, that just means it would never send other types of JSON paths. Is there some harm if the underlying variant library supports more?
Or is this an argument that the Databricks-supported subset is "good enough" and there's no need to go fancier?
Anyway tho, this seems like another reason to be cautious about variant paths in general -- different engines will likely have different (maybe even conflicting) expectations and abilities when it comes to pathing.
+1
alamb commentedon Jul 10, 2025
I filed a ticket with an API proposal for variant_get here:
variant_getkernel #7893alamb commentedon Jul 10, 2025
I think this API makes a lot of sense
I think the API I sketeched out on the following issue is consistent with this plan
variant_getkernel #7893If an engine wants to implement their own casting semantics, the will provide
Noneto the API and the result with Variant and then they can do their own casting.If they want to cast as Arrow types they can use the existing kernel
Though you bring up an excellent point -- given Variant has its own type system, supporting casts will require definining cast semantics for casting to Arrow types for all the variant types.
However this seems like a reasonable low level primitive.
scovich commentedon Jul 10, 2025
alamb commentedon Jul 11, 2025
I wrote up an idea of how we might implement variant shredding here in a ticket. The idea would be to create a builder that did the shredding on creation
alamb commentedon Jul 11, 2025
This is an excellent idea and it seems consistent with other implementations. I tried to capture it in
alamb commentedon Jul 11, 2025
I also agree with this @scovich -- however, I am not quite sure what the API would look like yet so I am not sure yet what ticket to file
scovich commentedon Jul 11, 2025
The public API seems simple enough? A shredded variant column would (physically) be a
StructArraywithtyped_valuealongside itsmetadataandvaluefields. I would expect anunshred_variantkernel to take such an input, and produce an output that does not have atyped_valuecolumn any more. The shredding spec requires that themetadatacolumn already contain every needed variant path name, so it's really just a matter of rewriting thevaluecolumn under the hood.The internal API (for low-level variant operations) would leverage a variant builder with some tweaks:
VariantMetadatainstead of aVariantMetadataBuilder, and field insertions fail if the key is not presentThat's what immediately comes to mind; some pathfinding would probably be required to go any further.
alamb commentedon Jul 11, 2025
This makes sense to me. After some sleep and coding, I think it would be a natural follow on when we have
VariantArraythat encapsulates the shreddingVariantArrayandVariantArrayBuilderfor constructing Arrow Arrays of Variants #7905Basically a kernel like
alamb commentedon Jul 12, 2025
FYI @zeroshade I think said the Golang implementation somehow re-shredded on read (VariantArray returned a Variant) - #7905 (comment)
I haven't fully worked out how that would work
alamb commentedon Jul 13, 2025
For anyone else following along, @scovich and I are discussing low level shredded variant access here: #7915 (comment)
alamb commentedon Jul 18, 2025
We are discussing reading shredded variants here;
variant_getkernel for shredded variants #7941We are discussing writing shredded variants here:
alamb commentedon Aug 15, 2025
I believe the proposal is to add this low level support for shredding in
variant_gethere, and we have the basic plan sketched out invariant_get: typed path access (STEP 1) #8150variant_get: untyped path access #8151variant_get: access asSome(DataType::Struct)(nested shredding) #8153variant_get: Access asVARIANT(Unshredding) #8154So let's close this ticket to try and keep the plan organized as possible
variant_get: typed path access (STEP 1) #8150alamb commentedon Sep 19, 2025
I think with the merging of #8366 we should claim this ticket is done -- we have support for shredding / unshredding, though we need to fill out a lot of type support still