Skip to content

Store shredded Variants as columns and shred Variants on write - #11814

Draft
lwwmanning wants to merge 9 commits into
rk/variant-featuresfrom
wm/variant-layout
Draft

lwwmanning wants to merge 9 commits into
rk/variant-featuresfrom
wm/variant-layout

Conversation

@lwwmanning

Copy link
Copy Markdown
Contributor

This adds a Variant layout to the unstable edition, and lets a writer shred Variant columns. Files written with the stable edition are unchanged. It is stacked on #11505, and #11390 adds JSONBench on top.

Before, a shredded Variant was written as one array per chunk, with its metadata, its binary value and every shredded field in one segment. A scan that extracted one shredded field read and decoded all of it. After, the Variant layout stores a shredded Variant as a struct of its core storage and its shredded child. Each shredded field is then a column with its own encoding and statistics. The reader replaces the Variant in each expression with variant_pack of the columns, so a scan that extracts a shredded path reads only that column. The core storage stays unread only when the writer recorded that the shredded child is complete.

Before, the writer split only a Variant that arrived shredded. After, a writer can take a shredding policy for every Variant field, or one for each field path. InferredShredding infers a schema from a sample of the first chunk. The new variant_shred functions shred by exact type, strictly, or into unions, and variant_get reads the members of a union.

The flat layout also decodes unshredded Variant storage once per split, for every filter conjunct and the projection.

Robert wrote the commits up to "Shred Variants into unions". The two after them move the layout's scalar functions to vortex-functions, where develop now keeps them. They also fix a union shredding test that failed on the original branch.

🤖 Generated with Claude Code

robert3005 and others added 9 commits November 23, 2026 07:42
A shredded Variant was written as one array per chunk: its metadata, its
binary value, and every shredded field in a single segment. A scan that
extracted one shredded field read and deserialized all of it.

- A shredded Variant is stored in a struct layout, as the struct of its
  core storage and its shredded child. Each shredded field becomes a
  column of its own, with its own encoding and statistics.
- The struct reader reads a stored Variant as the pack of its columns.
  It replaces the Variant in each expression with `variant_pack` of the
  columns, and then partitions the expression over the columns as for any
  struct. The plan evaluates `variant_pack` over the plan of the columns.
- A struct layout of a struct is written as `vortex.struct`, as before.
  A struct layout of a Variant is written as `vortex.variant`, which is in
  the unstable edition, with the shredded dtype as its metadata.
- A stream whose first chunk is not shredded is written as before.
- The writer records whether the shredded child is complete, that is
  whether no row left a value for it in the core storage. Only then is
  the core storage left unread.
- The flat layout plans a Variant segment scan under a share, and decodes
  unshredded Variant storage once, so that a split decodes the segment
  once for every filter conjunct and the projection.

Signed-off-by: Robert Kruszewski <github@robertk.io>
Signed-off-by: Will Manning <will@spiraldb.com>
A partially shredded field that the Variant layout reads is a lazy
variant_pack of its stored columns, not a Variant array. The Arrow export
did not see that it is a Variant, and wrote it as a typed leaf of Variant
storage, {metadata, value}, in place of the {value, typed_value} pair
that arrow-rs expects. A DataFusion scan that reads the column raw, as
for a projection that does not fully convert to Vortex expressions, then
failed.

The export now executes such a field to a Variant array first. For the
same reason, a shredded list that is not a ListArray, such as the list
view that a file read gives, now converts to a ListArray first.

Signed-off-by: Robert Kruszewski <github@robertk.io>
Signed-off-by: Will Manning <will@spiraldb.com>
The Variant layout only split a Variant that arrived shredded. A writer
can now shred an unshredded Variant column, and the user decides how.

- `VariantShreddingPolicy` chooses a schema for a Variant stream from
  its first chunk: a struct for an object, a list for an array, and a
  typed leaf. `FixedShredding` shreds by one schema, and `NoShredding`
  writes the stream unshredded. `VariantStrategy` shreds every chunk of
  a stream that is not shredded by the schema of its policy.
- `TableStrategy` and `WriteStrategyBuilder` take a default policy for
  every Variant field, and a policy for each field path that overrides
  it.
- The new `variant_shred` scalar function shreds a Variant by a schema.
  The encoding of the Variant storage does the work, through a kernel
  that it registers, so the layout does not depend on the encoding.
  Parquet Variant storage registers one that uses the Arrow shredder.
- `InferredShredding` infers a schema from a sample of the first chunk.
  The sample size, depth, number of fields and presence it needs are
  options.

Only strings are shredded for now. The Arrow shredder casts a value into
a typed column when the cast succeeds, so that a double shredded into an
integer column, or a number into a boolean column, would lose its value.
A string column takes strings alone, as the Parquet Variant
specification requires. The kernel rejects a schema with other leaves.

Signed-off-by: Robert Kruszewski <github@robertk.io>
Signed-off-by: Will Manning <will@spiraldb.com>
The Arrow shredder casts a value into a typed column whenever the cast
succeeds, so 1.5 and true shred into an integer column as 1. The
shredding specification puts a value into typed_value only if it has
the type of the column.

Shred unshredded storage by reading the Variant encoding in place. A
value moves into a typed column only if its type is the column type.
Integers of all widths are one class and move into any integer column
that they fit. Every other value stays in the residual value byte for
byte. The residual of a partly shredded object keeps the field ids and
the metadata of the row.

Typed leaves can now be strings, binaries, booleans, signed integers,
floats and doubles. Inference also shreds booleans, integers as 64-bit
integers, and doubles.

Split the shred module into exact shredding, inference and tests.

Signed-off-by: Robert Kruszewski <github@robertk.io>
Signed-off-by: Will Manning <will@spiraldb.com>
A shredding policy could only name a schema, and every value that did
not fit the schema stayed in the core storage, as Parquet shredding
requires. A writer that expects its data to fit had no way to say so,
and a value of an unexpected type went into the residual without notice.

A policy now chooses a shredding function, which owns both the schema
and what happens to a value that does not fit it:

- `vortex.variant_shred` keeps it in the core storage, as before.
  `FixedShredding` and `InferredShredding` use it.
- `vortex.variant_shred.strict` fails, and the error names the row and
  the path of the value. A Variant null fits every path, as a missing
  value. `StrictShredding` uses it.

The Parquet storage implements both functions in its in-place shredder.

Claude-Session: https://claude.ai/code/session_018SWbEvCNMBbsFaCQeCSw6h
Signed-off-by: Robert Kruszewski <github@robertk.io>
Signed-off-by: Will Manning <will@spiraldb.com>
A shredded Variant can now hold a union at any position of its shredded
tree. variant_get walks a field through the struct members of a union,
converts each member to a requested dtype, and wraps the members as
Variants for an untyped request. A row that the union does not hold reads
the core storage.

Over a complete variant_pack, variant_get reads a union from the shredded
child alone, so a scan does not read the core storage. The writer records
a shredded union as complete when the core storage holds no value in the
rows that the union does not hold.

Signed-off-by: Robert Kruszewski <github@robertk.io>
Claude-Session: https://claude.ai/code/session_018SWbEvCNMBbsFaCQeCSw6h
Signed-off-by: Will Manning <will@spiraldb.com>
A field whose values have several types cannot be shredded into one
typed column, so its values stay in the residual binary value. A union
schema lets each type have a column of its own.

VariantShredUnion shreds by a schema that can hold unions at any
position, with leaves, structs, lists and unions as members. A value
goes to the first member that holds all of it, and the residual keeps a
value that no member holds. A member holds the whole value, so it has no
residual of its own. Parquet storage has no union, so the result is a
canonical Variant, and the Arrow export folds each union back into
Variant bytes written with the metadata of the row. The Parquet and
strict shreddings reject a union schema.

UnionShredding is the writer policy for it.

Claude-Session: https://claude.ai/code/session_018SWbEvCNMBbsFaCQeCSw6h
Signed-off-by: Robert Kruszewski <github@robertk.io>
Signed-off-by: Will Manning <will@spiraldb.com>
Develop moved the scalar functions out of vortex-array into
vortex-functions (#10762). The Variant layout was written before that
move, so this ports it: `variant_shred` and its strict and union forms
are registered by vortex-functions, the `variant_shred` expression is in
`vortex_functions::expr`, and `decode_for_repeated_extraction` moves next
to `variant_get`, because vortex-array cannot name `VariantGet`.

It also drops a second copy of `element_metadata` in the Parquet Variant
array, which the rebase onto #11505 left behind.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Will Manning <will@spiraldb.com>
The test failed on its own branch. A field that keeps values no member
fits canonicalizes to a nested Variant whose shredded child is the
union, so the test now looks for the union there. Reading the field as
a type converts each value as Arrow's Variant conversion does, so the
Boolean `true` reads as the integer 1, and the integer 1 as `true`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Will Manning <will@spiraldb.com>
@robert3005

robert3005 commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

We should figure out how to do this purely in the partitioner - not sure we need an explicit variant layout. Or the variant layout is just 2 chldren - unshredded and shredded

@lwwmanning
lwwmanning marked this pull request as ready for review November 23, 2026 15:09
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-23T15:16:38.386312Z a87e9c1 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@lwwmanning
lwwmanning marked this pull request as draft November 23, 2026 15:10

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a87e9c103b

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines 398 to +402
if let Some(list_array) = shredded.as_opt::<List>() {
let elements = parquet_shredded_field_from_logical(list_array.elements().clone(), ctx)?;
let elements = list_array.elements().clone();
let metadata = metadata
.map(|metadata| element_metadata(metadata, list_array.offsets(), elements.len(), ctx))
.transpose()?;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Normalize sliced lists before assigning element metadata

When exporting a union nested in a shredded list after the Variant has been sliced, a canonical ListArray can retain the original full element buffer and non-zero offsets (vortex-array/src/arrays/list/compute/slice.rs). This branch skips list_from_list_view, assigns metadata across elements.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_union rejects 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 👍 / 👎.

Comment on lines +255 to +260
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)?;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve union field names when preparing a null core

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 typed_value = None synthesizes metadata with an empty field-name dictionary, but fold_union subsequently needs the names of any object-valued union member to encode its fields and rejects names absent from this dictionary. Thus exporting a file-read Variant whose union contains struct members can fail solely because compression made its empty residual core constant. Build the synthesized metadata using names collected from shredded, or otherwise retain the original metadata before folding the union.

Useful? React with 👍 / 👎.

Comment on lines +128 to +130
let shredded = pb::DType::try_from(&self.shredded_dtype)
.map(|shredded| shredded.encode_to_vec())
.unwrap_or_default();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Propagate shredded dtype serialization failures

If an already-shredded Variant uses an extension dtype whose metadata serialization fails, pb::DType::try_from returns an error here but unwrap_or_default silently records an empty dtype in the layout metadata. The file write then succeeds, while reopening it necessarily fails when VariantStorage::deserialize attempts to decode those empty bytes. Validate and serialize the shredded dtype while constructing the layout so the write fails with the original error instead of producing an unreadable file.

Useful? React with 👍 / 👎.

Comment on lines +122 to +127
for name in fields.names().iter() {
let field = fields.field(name.as_ref()).ok_or_else(missing)?;
append(
&mut ObjectFieldBuilder::new(name.as_ref(), &mut object),
&field,
)?;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Omit missing fields when folding union objects

A struct union member is allowed to match an object containing only a subset of the member's schema fields (ObjectMember::fits in shred/union.rs), and the missing fields are represented as nulls in the member array. This loop nevertheless serializes every schema field, so an input such as {"a":1} matched by a member schema containing a and b is folded as an object with b: null; normally the row metadata does not contain b, causing encode to reject the export, and otherwise the reconstructed Variant has gained a field. Since an explicit Variant-null field does not fit a union member, null struct fields here represent absence and should be skipped while rebuilding the object.

Useful? React with 👍 / 👎.

@robert3005 robert3005 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The layout approach is sound. It reuses the struct reader, partitioning and zone maps, and the complete flag lets the reader skip the core column safely. The main problems are in the writer.

CI

  • Generated sources: the PR adds vortex.variant to the unstable edition, but vortex/editions/unstable/unstable2026.10.0.toml still has layouts = []. Run cargo run -p xtask -- generate-editions and commit the result.
  • Rust (docs): rustdoc::redundant_explicit_links at vortex-layout/src/layouts/struct_/mod.rs:62. Use [`variant`].

1. The writer accepts only one shredding per stream (vortex-layout/src/layouts/variant/writer.rs:221, :317, :327)
The first chunk selects the policy and the shredded dtype, and split bails on any chunk that differs:

  • The first chunk is shredded and a later chunk is not.
  • Two chunks are pre-shredded with different schemas.
  • The first chunk is unshredded and a later chunk is shredded (variant_shred.rs:299).

from_session turns on use_variant_layout for the unstable edition even when no policy is set. So a writer that copies Parquet files whose row groups are shredded differently now fails. Unshred and re-shred a chunk that does not match, or fall back when chunks differ, and add a test with mixed chunks.

2. Strict shredding stores a Variant null as a missing value (encodings/parquet-variant/src/shred/exact.rs:352)
The comment says this is intended, but it contradicts "Shredding never changes what a Variant holds" (shred/mod.rs:10):

  • {"k": null} reads back as {}.
  • A root null becomes value=null and typed_value=null. The Parquet shredding spec allows that state only for object fields.
  • A list element null becomes a missing element. The spec does not allow that state.

strict_shredding_keeps_values_that_fit compares only typed variant_get results, where null and missing look the same. Keep a Variant null in the residual value, as Residual mode does, or document the loss and do not apply it to roots and list elements. Add a round-trip test that compares untyped values.

Questions

  • Inference samples only the first chunk. VariantStrategy sits above repartitioning, so it sees the batches as the caller sent them (table.rs:374, infer.rs:97). Small or unusual first batches give a poor schema for the whole file.
  • Metadata copies. Each partially shredded field becomes a nested Variant whose core stores metadata again, so 64 fields give 65 metadata columns (array.rs:283, :308). Please measure the file size against the stable layout before the format is frozen.
  • Three ways to decode once. These are decode in segment_scan/execute.rs:49, SharePlan, and decode_for_repeated_extraction in the generic EvalStage. In FlatReader::filter_evaluation the decode also runs before the sparse-mask path. Can it be one mechanism, with no Variant knowledge in vortex-scan?
  • One complete flag covers the whole column. One residual value in one chunk turns pushdown off for the whole file. A per-chunk or per-zone flag would keep pushdown for the rest.
  • New dependency. vortex-parquet-variant now depends on vortex-layout at runtime, only for InferredShredding. Can VariantShreddingPolicy move to a lower crate?
  • Wrong doc. The doc says that the unstable edition gates reads (struct_/mod.rs:195, :208), but it gates only writes.

Nit: vortex_ensure!(parts.dtype.is_variant() == is_variant, ...) at struct_/mod.rs:235 should be vortex_ensure_eq!.

This branch has not been deployed

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

Labels

changelog/feature A new feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants