Skip to content

[Variant] API to construct Shredded Variant Arrays #7895

Closed

Description

@alamb

Is your feature request related to a problem or challenge? Please describe what you are trying to do.

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:

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 variant

I 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:

Additional context

Activity

  1. added
    enhancement
    on Jul 11, 2025
  2. alamb commented on Jul 11, 2025

    @alamb
    ContributorAuthor

    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)

  3. changed the title [-][Variant] API o construct Shredded Variant Arrays[/-] [+][Variant] API to construct Shredded Variant Arrays[/+] on Jul 11, 2025
  4. scovich commented on Jul 11, 2025

    @scovich
    Contributor

    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)

    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.

  5. alamb commented on Jul 11, 2025

    @alamb
    ContributorAuthor

    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)

  6. friendlymatthew commented on Jul 11, 2025

    @friendlymatthew
    Contributor

    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)

    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.

  7. alamb commented on Jul 11, 2025

    @alamb
    ContributorAuthor

    Nice -- I just polished up #7884 and hpefully it can merge shortly

  8. alamb commented on Jul 11, 2025

    @alamb
    ContributorAuthor
  9. alamb commented on Jul 17, 2025

    @alamb
    ContributorAuthor

    Update here

    My suggested next steps are:

    1. Finalize the API in [Variant] Define shredding schema for VariantArrayBuilder #7921
    2. Create a shred_variant kernel that works for existing variant arrays

    We can focus the effort initially on first version of shred_variant where the API is straightfowrard 🤔

  10. alamb commented on Jul 17, 2025

    @alamb
    ContributorAuthor

    Update here

    My suggested next steps are:

    1. Finalize the API in [Variant] Define shredding schema for VariantArrayBuilder #7921
    2. Create a shred_variant kernel that works for existing variant arrays

    We can focus the effort initially on first version of shred_variant where the API is straightfowrard 🤔

  11. friendlymatthew commented on Jul 17, 2025

    @friendlymatthew
    Contributor

    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

  12. alamb commented on Jul 17, 2025

    @alamb
    ContributorAuthor

    Awesome -- thank you @friendlymatthew --

  13. alamb commented on Jul 17, 2025

    @alamb
    ContributorAuthor

    Update here

    My suggested next steps are:

    1. Finalize the API in [Variant] Define shredding schema for VariantArrayBuilder #7921
    2. Create a shred_variant kernel that works for existing variant arrays

    We can focus the effort initially on first version of shred_variant where the API is straightfowrard 🤔

  14. scovich commented on Jul 18, 2025

    @scovich
    Contributor

    We would need this schema:

    STRUCT {
      metadata: BinaryView,
      value: BinaryView,
      typed_value: STRUCT {
        foo: Int64,
        bar: Int32
      }
    }
    

    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:

    STRUCT {
      metadata: BINARY,
      value: BINARY,
      typed_value: STRUCT {
        foo: STRUCT {
          value: BINARY,
          typed_value: Int64,
        },
        bar: STRUCT {
          value: BINARY,
          typed_value: Int32,
        },
      },
    }
    

    (we could debate whether a missing value column 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)

  15. zeroshade commented on Jul 18, 2025

    @zeroshade
    Member

    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

    STRUCT {
      metadata: BINARY,
      value: BINARY,
      typed_value: STRUCT {
        foo: STRUCT {
          value: BINARY,
          typed_value: Int64,
        },
        bar: STRUCT {
          value: BINARY,
          typed_value: Int32,
        },
      },
    }
    

    Will be required given the current design of the canonical extension type, and providing

    STRUCT {
      metadata: BinaryView,
      value: BinaryView,
      typed_value: STRUCT {
        foo: Int64,
        bar: Int32
      }
    }
    

    Would be an error.

  16. alamb commented on Aug 15, 2025

    @alamb
    ContributorAuthor

    I believe the current plan we came up with is to implement shredding as a special case of variant_get, as described here

  17. alamb commented on Sep 21, 2025

    @alamb
    ContributorAuthor

    @scovich implemented shred_variant in #8366 which I think completes this issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

enhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions