Repository navigation
Description
I just realized something -- when dealing with shredded variants, this value method will do a lot of work to unshred and encode the whole thing (see e.g. #7915 (comment)). And that work is not memoized anywhere unless the caller is able to do so. For efficiency reasons we should strongly consider some kind of direct pathing support in VariantArray itself. Otherwise, it would be far too easy for a caller to accidentally do quadratic work when repeatedly calling value+get_path pairs for different paths.
Originally posted by @scovich in #7919 (comment)
Just to understand this -- is this what you're suggesting?
- We add a new
get_pathmethod inVariantArraythat does this:- For each row, look up the type of the variant and perform the pathing without decoding. So, for example, if it's a
VariantObjectand there's aVariantPathElement::Fieldpath, it would get the offset for the given field (not sure how yet) and advance thevalueslice by that much. - We would then create a new
VariantArraywith the metadata directly copied over and the value copied starting from the advanced slice. - For shredded variants, if the path ends up on a shredded value, what would be the expected behavior? I'm guessing that the shredded fields will be represented as an Array of the concrete type (an
Int32Arrayfor example) and not aVariantArray. Will we wrap these in aVariantArrayand send it back? This is one case where having the path + cast in the same operation would help.
- For each row, look up the type of the variant and perform the pathing without decoding. So, for example, if it's a
- This
variant_getwould then simply re-useVariantArray::get_pathand perform the appropriate cast.
For efficiency reasons we should strongly consider some kind of direct pathing support in VariantArray itself. Otherwise, it would be far too easy for a caller to accidentally do quadratic work when repeatedly calling value+get_path pairs for different paths.
I think @scovich is saying that the variant_get kernel (on VariantArray should have a special case that knows how to look for a shredded sub field -- and if for example it is asking for a and the the typed_value.a column exists, variant_get could simply return that a column (already as an arrow array, no actual Variant manipulation required)
- I filed [Variant] Support
variant_getkernel for shredded variants #7941 to track this
Activity
[-]Optimize `variant_get` kernel for shredded variants[/-][+][Variant] Optimize `variant_get` kernel for shredded variants[/+]scovich commentedon Jul 17, 2025
Consider the case where we want to project out just one leaf field of a very wide and deep variant value. I think two kinds of things can go wrong if pathing is a two-step process of calling
VariantArray::valuefollowed byVariant::get_path:Variant::ShreddedVariantObjectinstances that the subsequent pathing call will just ignore. And if we end up callingVariantArray::valuemultiple times, we repeat the unnecessary work each time.Variant::ShreddedVariantObjectfor the path of interest. Which is itself potentially quite wide and deep and thus a lot of work.Int32Array(I think that's what @alamb was referring to), then it's definitely nice to avoid a round trip variant encoding at all.VariantArraywith the samemetadatacolumn, a missingvaluecolumn, and an appropriately filteredtyped_value. Again, this would not require any actual variant encoding work, just rearranging physical columns within theVariantArrayitself.alamb commentedon Jul 17, 2025
That is an interesting point -- maybe we could somehow defer the construction of the shredded
Variant(whatever form that takes) until actual access.We would have to play around with it I think
[-][Variant] Optimize `variant_get` kernel for shredded variants[/-][+][Variant] Support `variant_get` kernel for shredded variants[/+]alamb commentedon Jul 18, 2025
Here is a practical suggestion on how to make progress on variant shredding -- we start working out how it would work for a simple example
I have written some tests here that manually construct a shredded VariantArray and then call
variant_geton it.Perhaps someone can use that as inspiration as a first step to implement this feature
alamb commentedon Jul 18, 2025
@scovich and I were discussing other options here #7915 (comment):
@scovich : #7915 (comment)
An unshredding operation requires two kinds of variant "building":
VariantMetadataalready contains all their field names, so the builder only needs to look up their field ids. The spec requires the ability to unshred using a read-only metadata dictionary, so why go copying it?VariantBuilder#7914 seems to favor). We need a builder to keep track of the updated offsets as we interleave existing variant value bytes with newly-unshredded value bytes.Imagine a partially shredded variant column
v:a,mandxlive in thetyped_valuecolumn as a perfectly shredded structxis itself a shredded struct with its own fieldsiandjiis a variant column (also using the same top-level metadata)jis a partially shredded structb,n, andylive in thevaluecolumn as a variant objectWhen unshredding, we need to turn
a,b, andxfrom strongly typed values into variant objects -- the latter recursively so -- which requires a variant builder. The recursive unshredding ofjalso requires a builder of its own.Once we have all the field values in variant form, we need to create a new variant object for
v. We can copy the bytes ofb,nandyas a contiguous block if we want -- relying on field id sorting to give the required orderinga,b,m,n,x,y-- but we do have to copy those bytes in order to inject the newly created variant value bytes fora,m, andx. Plus, it's entirely possible other newly-unshredded sibling values before and aftervwould have required copying its value bytes even ifvwere completely unshredded.alamb commentedon Jul 18, 2025
@alamb in #7915 (comment)
I see -- you are imagining unshredding the varaint phiscally (aka forming the bytes for a new unshredded variant
valueusing aVariantBuilder) to access the shredded fields.What if we made a view into the variant so we didn't have to copy anything to read the Variant? If we could get a
Variantthat accessed the shredded fields, physically unshredding becomes an exercise in creating a newbuilderand callingappend_value:Hypothetical
unshredkernelShredded Variants as "Views"
The question then is how to implement a view. Accessing primitive types as a Variant is straightforward
The tricky bit is accessing partially shredded objects. Maybe we could add the shredded fields to
VariantObject, something likeThen we can update all the accessor methods, etc.
An alternate mechanism that might be more explicit would a new explicit enumeration in
VariantlikeWe could
Boxthe shredded fields to keep the size of Variant down if neededHows does this handle the example?
I think the view approach could handle this example. For example
vis aVariant::Object(..)withshredded_fieldsv's fields in thevalue_columncontainb,nandyv::shredded_fieldscontains theVariants fora,m, andxalamb commentedon Jul 18, 2025
@friendlymatthew in #7915 (comment)
Hi, how do we plan on storing
typed_values? Do we plan on encoding it as aVariantand storing the binary representation of it? Or since it is supposed to be strongly typed, we want to map to specificDataTypes, which would require something like #7921I've been playing around with the unshredding logic and the most naive version would look something like this. I don't mean this to be the actual implementation, but I am curious how we plan on representing
typed_value.naive reconstruct logic
If we strongly type
typed_valueto whatever the variant type is, the primitive variants work nicely, since we'd be storing aPrimitiveArray<T>and we can easily index into it.For complex variants like objects/lists, I'm having a hard time figuring out how to strongly type these values. Maybe for these cases we should just store the binary encoded version of a Variant? (metadata, value).
alamb commentedon Jul 18, 2025
There is a bunch more back and forth on the thread as well that might be interesting
[Variant] Add `variant_get` and Shredded `VariantArray` (apache#8021)
alamb commentedon Aug 21, 2025
label_issue.pyautomatically added labels {'parquet'} from #7965