Skip to content

perf(variant): copy leftover object fields by field id when shredding - #12311

Draft
cetra3 wants to merge 5 commits into
apache:mainfrom
pydantic:variant-views-4-shred
Draft

cetra3 wants to merge 5 commits into
apache:mainfrom
pydantic:variant-views-4-shred

Conversation

@cetra3

@cetra3 cetra3 commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

This PR is stacked on #12309 and #12310. Its own change is the commit "perf(variant): copy leftover object fields by field id when shredding". It is a draft until they merge.

Rationale for this change

When shred_variant and unshred_variant copy an object field that is not shredded, they decode it into a Variant, look its name up in the metadata dictionary to get the field id back, and encode the value again. The output uses the same metadata dictionary as the input, so the field id and the encoded bytes are already correct.

What changes are included in this PR?

  • ObjectBuilder::insert_encoded adds a field by its field id and its encoded bytes.
  • VariantView::bytes returns the exact encoding of a value.
  • shred_variant and unshred_variant copy leftover object fields through them. They match field names as bytes against a sorted list of the shredded field names.

Are these changes tested?

The existing shredding tests cover the changed code. VariantView::bytes has tests for every primitive type, and for the fields of an object and the elements of a list, whose encodings must tile the value region of their parent.

Benchmarks (local run, this PR against #12310, x86_64 Linux):

benchmark #12310 this PR change
shred_variant_partial_object_8k_rows 10.90 ms 9.42 ms −14%
shred_variant_unmatched_object_8k_rows 14.43 ms 11.36 ms −21%
unshred_variant_list 5.44 ms 5.59 ms +3%
unshred_variant_list_of_objects 11.26 ms 11.46 ms +2%
unshred_variant_nested_fallback 74.62 ms 75.06 ms +1%
unshred_variant_object 2.93 ms 3.05 ms +4%
unshred_variant_partial_object 24.13 ms 21.81 ms −10%
unshred_variant_partial_object_unsorted_metadata 107.97 ms 62.79 ms −42%
unshred_variant_primitive 263.33 µs 260.74 µs −1%
variant_get_unshredded_object_path_262k_rows 14.98 ms 13.61 ms −9%
variant_get_list_index_unshredded 308.11 µs 283.84 µs −8%
variant_get_utf8_from_unshredded_string 214.52 µs 233.12 µs +9%
variant_get_binary_from_string 207.79 µs 226.29 µs +9%

Slower inputs: unshred_variant_object is 4% slower and unshred_variant_list is 3% slower. variant_get_utf8_from_unshredded_string and variant_get_binary_from_string are 9% slower in this run, although this PR does not change variant_get; these two benchmarks have moved by 10–15% with unrelated changes on this machine.

Are there any user-facing changes?

New public API in parquet-variant: ObjectBuilder::insert_encoded and VariantView::bytes.

On malformed variant bytes, shred_variant and the leftover copy in unshred_variant return an error. Before, these paths could panic.

🤖 Generated with Claude Code

@alamb

alamb commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

run benchmark variant_kernels

@adriangbot

Copy link
Copy Markdown

🤖 Benchmark starting (GKE) | trigger

Target: variant_kernels

Sharding factor: 1 (1 workers).

Comparing variant-views-4-shred (4d0fdfd) to 093ef04 (merge-base) diff

Run configuration
run benchmark variant_kernels
shards: 1

Results will be posted when all workers finish.


File an issue against this benchmark runner

@alamb alamb 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.

Looks good an dmakes sense to me -- thank yoU @cetra3

Note that I think we could apply a similar optimization (copy the original bytes rather than decode/reencode in some other places too (maybe as a follow on PR)

fn append_row(
&self,
builder: &mut impl VariantBuilderExt,
metadata: &VariantMetadata,
index: usize,
) -> Result<()> {
if self.value.is_null(index) {
builder.append_null();
} else {
let value_bytes = binary_array_value(self.value.as_ref(), index).ok_or_else(|| {
ArrowError::InvalidArgumentError(
"value field must be a binary-like array".to_string(),
)
})?;
let variant = Variant::try_new_with_metadata(metadata.clone(), value_bytes)?;
builder.append_value(variant);
}

And

macro_rules! handle_unshredded_case {
($self:expr, $builder:expr, $metadata:expr, $index:expr, $partial_shredding:expr) => {{
let value = $self.value.as_ref().filter(|v| v.is_valid($index));
let value = value
.map(|v| {
let bytes = binary_array_value(v.as_ref(), $index).ok_or_else(|| {
ArrowError::InvalidArgumentError(format!(
"value field must be a binary-like array, instead got {}",
v.data_type(),
))
})?;
Variant::try_new_with_metadata($metadata.clone(), bytes)
})
.transpose()?;
// If typed_value is null, handle unshredded case and return early
if $self.typed_value.is_null($index) {
match value {
Some(value) => $builder.append_value(value),
None => $builder.append_null(),
}
return Ok(());

..
} = self;
seen.fill(false);
let object = obj.view();

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.

It is somewhat confusing that there is both obj (the view) and object in this sciopt
it might be easier to read either to redefine obj or use obj_view or something more distinguishing

Comment on lines +157 to +159
/// The caller must ensure that `field_id`, and every field id inside `value`, belong to the
/// metadata dictionary that this builder writes against. Copying a field out of an object that
/// uses that same dictionary, as shredding does, satisfies this.

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.

Suggested change
/// The caller must ensure that `field_id`, and every field id inside `value`, belong to the
/// metadata dictionary that this builder writes against. Copying a field out of an object that
/// uses that same dictionary, as shredding does, satisfies this.
/// The caller must ensure that `field_id`, and every field id inside `value`, belong to the
/// builder's metadata dictionary. For example, during shredding, when copying fields to the
/// output (residual) variant that are not shredded

)));
}
object_builder.insert_bytes(field_name, field_value);
object_builder.insert_encoded(object.field_id(i)?, object.field(i)?.bytes()?)?;

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.

I think this is one key optimization -- just copy the original variant bytes rather than re-walking/re-encoding a parsed one 👍

Variant::try_new_with_metadata_and_shallow_validation(metadata, self.value)
}

/// The exact encoding of this value, without any bytes that follow it.

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 "without any bytes that follow it" is somewhat strange phrasing to me

It might be clearer if it just says "returns just the bytes that encode this variant value"

@adriangbot

Copy link
Copy Markdown

🤖 Benchmark completed (GKE) | trigger

Target: variant_kernels

Comparing variant-views-4-shred (4d0fdfd) to 093ef04 (merge-base) diff

Run configuration
run benchmark variant_kernels
shards: 1
Details
group                                                                                             base                                    changed
-----                                                                                             ----                                    -------
batch_json_string_to_variant duplicate_key_100k (100014 bytes per document, 10 rows)              1.00    141.6±0.12µs        ? ?/sec     1.00    141.6±0.10µs        ? ?/sec
batch_json_string_to_variant escaped_strings (58 bytes per document, 8000 rows)                   1.00      6.8±0.08ms        ? ?/sec     1.01      6.9±0.06ms        ? ?/sec
batch_json_string_to_variant fixed_point_decimals (65 bytes per document, 8000 rows)              1.01      6.6±0.02ms        ? ?/sec     1.00      6.6±0.04ms        ? ?/sec
batch_json_string_to_variant int32 (16 bytes per document, 8000 rows)                             1.00      2.9±0.02ms        ? ?/sec     1.01      2.9±0.04ms        ? ?/sec
batch_json_string_to_variant json_list 8k string                                                  1.01     20.0±0.24ms        ? ?/sec     1.00     19.9±0.22ms        ? ?/sec
batch_json_string_to_variant large_array_1024 (4011 bytes per document, 261 rows)                 1.00      9.3±0.21ms        ? ?/sec     1.00      9.2±0.15ms        ? ?/sec
batch_json_string_to_variant large_string_8k (8214 bytes per document, 127 rows)                  1.00    262.2±0.80µs        ? ?/sec     1.01    264.9±1.01µs        ? ?/sec
batch_json_string_to_variant object - 1 depth(100 fields) random_json(2436 bytes per document)    1.00    149.7±0.40ms        ? ?/sec     1.00    149.8±0.55ms        ? ?/sec
batch_json_string_to_variant object - 1 depth(200 fields) random_json(4871 bytes per document)    1.00    293.5±0.91ms        ? ?/sec     1.00    294.5±0.82ms        ? ?/sec
batch_json_string_to_variant random_json(2633 bytes per document)                                 1.00    222.5±0.86ms        ? ?/sec     1.00    222.9±0.86ms        ? ?/sec
batch_json_string_to_variant repeated_struct 8k string                                            1.00      6.4±0.02ms        ? ?/sec     1.01      6.5±0.03ms        ? ?/sec
shred_variant_partial_object_8k_rows                                                              1.56     21.9±0.28ms        ? ?/sec     1.00     14.0±0.04ms        ? ?/sec
shred_variant_unmatched_object_8k_rows                                                            1.63     27.4±0.49ms        ? ?/sec     1.00     16.8±0.04ms        ? ?/sec
unshred_variant_list                                                                              1.03      9.8±0.04ms        ? ?/sec     1.00      9.6±0.06ms        ? ?/sec
unshred_variant_list_of_objects                                                                   1.02     22.9±0.15ms        ? ?/sec     1.00     22.5±0.56ms        ? ?/sec
unshred_variant_nested_fallback                                                                   1.01    152.4±0.93ms        ? ?/sec     1.00    150.7±1.10ms        ? ?/sec
unshred_variant_object                                                                            1.02      5.5±0.03ms        ? ?/sec     1.00      5.3±0.14ms        ? ?/sec
unshred_variant_partial_object                                                                    1.24     44.4±0.14ms        ? ?/sec     1.00     35.8±0.12ms        ? ?/sec
unshred_variant_partial_object_unsorted_metadata                                                  2.12    198.3±1.68ms        ? ?/sec     1.00     93.6±0.21ms        ? ?/sec
unshred_variant_primitive                                                                         1.02    433.5±4.88µs        ? ?/sec     1.00   425.9±32.15µs        ? ?/sec
variant_array_builder_build_262k_small_values                                                     1.06  1134.5±153.17µs        ? ?/sec    1.00   1065.8±9.07µs        ? ?/sec
variant_get_binary_from_string                                                                    1.30    471.0±1.43µs        ? ?/sec     1.00    363.3±2.31µs        ? ?/sec
variant_get_binary_from_unshredded_binary                                                         1.30    458.6±2.81µs        ? ?/sec     1.00    353.8±7.76µs        ? ?/sec
variant_get_decimal_double_to_decimal128(20,3)_non_finite_8192_safe                               1.29    496.7±1.89µs        ? ?/sec     1.00    385.9±9.41µs        ? ?/sec
variant_get_decimal_double_to_decimal128(20,3)_valid_8192_safe                                    1.28    515.8±1.64µs        ? ?/sec     1.00    402.8±9.09µs        ? ?/sec
variant_get_decimal_double_to_decimal128(20,3)_valid_8192_strict                                  1.34    540.2±1.45µs        ? ?/sec     1.00    402.8±9.24µs        ? ?/sec
variant_get_decimal_double_to_decimal256(40,-2)_scale_down_8192_safe                              1.07    963.7±5.41µs        ? ?/sec     1.00    897.7±4.05µs        ? ?/sec
variant_get_decimal_double_to_decimal64(3,0)_precision_overflow_8192_safe                         1.28    495.6±2.38µs        ? ?/sec     1.00    386.4±5.95µs        ? ?/sec
variant_get_decimal_float_to_decimal32(7,2)_valid_8192_safe                                       1.31    459.2±1.92µs        ? ?/sec     1.00    350.7±6.18µs        ? ?/sec
variant_get_decimal_float_to_decimal32(7,2)_valid_8192_strict                                     1.43    499.8±1.67µs        ? ?/sec     1.00    350.6±6.18µs        ? ?/sec
variant_get_decimal_float_to_decimal64(3,0)_precision_overflow_8192_safe                          1.25    501.2±1.70µs        ? ?/sec     1.00    401.1±4.77µs        ? ?/sec
variant_get_decimal_int32_to_decimal32(8,0)_precision_overflow_8192_safe                          1.28    470.6±1.61µs        ? ?/sec     1.00    366.5±4.73µs        ? ?/sec
variant_get_decimal_int32_to_decimal32(9,0)_valid_8192_safe                                       1.31    459.6±1.55µs        ? ?/sec     1.00    351.8±6.71µs        ? ?/sec
variant_get_decimal_int32_to_decimal32(9,0)_valid_8192_strict                                     1.42    498.6±2.11µs        ? ?/sec     1.00    351.7±6.67µs        ? ?/sec
variant_get_decimal_int64_to_decimal128(38,2)_scale_up_8192_safe                                  1.24    491.7±2.01µs        ? ?/sec     1.00    396.5±4.48µs        ? ?/sec
variant_get_decimal_int64_to_decimal128(38,2)_scale_up_8192_strict                                1.36    541.7±4.62µs        ? ?/sec     1.00    397.1±4.50µs        ? ?/sec
variant_get_decimal_int64_to_decimal256(76,-1)_scale_down_8192_safe                               1.18    504.2±2.26µs        ? ?/sec     1.00    425.6±6.23µs        ? ?/sec
variant_get_decimal_int64_to_decimal32(9,-1)_scale_before_narrowing_8192_safe                     1.30    472.2±1.67µs        ? ?/sec     1.00    362.8±4.06µs        ? ?/sec
variant_get_decimal_int64_to_decimal32(9,-20)_all_zero_8192_safe                                  1.30    456.0±1.78µs        ? ?/sec     1.00    349.6±4.84µs        ? ?/sec
variant_get_decimal_short_string_to_decimal256(40,-2)_scale_down_8192_safe                        1.23    570.6±1.94µs        ? ?/sec     1.00    465.1±4.16µs        ? ?/sec
variant_get_decimal_short_string_to_decimal256(76,2)_wide_8192_safe                               1.12    950.7±2.79µs        ? ?/sec     1.00    845.2±2.37µs        ? ?/sec
variant_get_decimal_short_string_to_decimal32(7,2)_valid_8192_safe                                1.30    539.9±2.43µs        ? ?/sec     1.00    416.6±2.51µs        ? ?/sec
variant_get_decimal_short_string_to_decimal32(7,2)_valid_8192_strict                              1.41    589.3±7.63µs        ? ?/sec     1.00    416.7±2.44µs        ? ?/sec
variant_get_decimal_string_to_decimal128(38,3)_long_8192_safe                                     1.09   1103.2±9.91µs        ? ?/sec     1.00   1009.3±5.16µs        ? ?/sec
variant_get_decimal_string_to_decimal128(38,3)_long_8192_strict                                   1.16  1173.4±12.52µs        ? ?/sec     1.00   1009.1±5.18µs        ? ?/sec
variant_get_list_index_all_oob_int64_64_rows                                                      1.00    855.9±7.80ns        ? ?/sec     1.01   868.0±12.92ns        ? ?/sec
variant_get_list_index_shredded                                                                   1.00     47.1±1.59µs        ? ?/sec     1.00     47.1±1.61µs        ? ?/sec
variant_get_list_index_unshredded                                                                 1.36   961.5±17.52µs        ? ?/sec     1.00    707.1±2.76µs        ? ?/sec
variant_get_object_path_metadata/json_to_variant                                                  1.42      2.7±0.02ms        ? ?/sec     1.00  1885.8±29.16µs        ? ?/sec
variant_get_object_path_metadata/per_row_metadata                                                 1.40      2.5±0.02ms        ? ?/sec     1.00  1797.8±14.31µs        ? ?/sec
variant_get_object_path_metadata/variant_array_builder                                            1.45      2.8±0.03ms        ? ?/sec     1.00  1903.9±51.65µs        ? ?/sec
variant_get_primitive                                                                             1.02    559.3±9.01ns        ? ?/sec     1.00    548.2±6.03ns        ? ?/sec
variant_get_shredded_utf8                                                                         1.00    719.6±8.38ns        ? ?/sec     1.00    720.3±4.68ns        ? ?/sec
variant_get_unshredded_object_path_262k_rows                                                      1.00     35.0±0.26ms        ? ?/sec     1.00     34.8±0.31ms        ? ?/sec
variant_get_utf8_from_date                                                                        1.29    564.4±5.13µs        ? ?/sec     1.00    437.8±7.30µs        ? ?/sec
variant_get_utf8_from_decimal                                                                     1.27    466.5±9.31µs        ? ?/sec     1.00   365.9±10.78µs        ? ?/sec
variant_get_utf8_from_float                                                                       1.32    433.9±1.35µs        ? ?/sec     1.00    327.9±3.39µs        ? ?/sec
variant_get_utf8_from_int                                                                         1.32    435.1±1.41µs        ? ?/sec     1.00    328.7±3.95µs        ? ?/sec
variant_get_utf8_from_list                                                                        1.00   597.5±13.07µs        ? ?/sec     1.16   692.3±11.58µs        ? ?/sec
variant_get_utf8_from_map_in_list                                                                 1.00   613.4±14.63µs        ? ?/sec     1.16    713.1±8.19µs        ? ?/sec
variant_get_utf8_from_time                                                                        1.19    776.4±2.12µs        ? ?/sec     1.00    652.3±1.91µs        ? ?/sec
variant_get_utf8_from_timestamp_with_timezone                                                     1.31    566.3±3.38µs        ? ?/sec     1.00    431.2±4.94µs        ? ?/sec
variant_get_utf8_from_timestamp_without_timezone                                                  1.31    566.4±3.37µs        ? ?/sec     1.00    431.1±4.96µs        ? ?/sec
variant_get_utf8_from_unshredded_string                                                           1.26    482.6±3.29µs        ? ?/sec     1.00    382.4±5.80µs        ? ?/sec
variant_to_json_dictionary_metadata                                                               1.12      3.1±0.02ms        ? ?/sec     1.00      2.7±0.01ms        ? ?/sec
variant_to_json_list                                                                              1.01     35.4±0.38ms        ? ?/sec     1.00     35.1±0.76ms        ? ?/sec
variant_to_json_object                                                                            1.12      3.0±0.02ms        ? ?/sec     1.00      2.7±0.01ms        ? ?/sec
variant_to_json_primitive                                                                         1.03    732.8±6.07µs        ? ?/sec     1.00    712.3±8.35µs        ? ?/sec
variant_to_json_ree_metadata                                                                      1.10      3.2±0.01ms        ? ?/sec     1.00      2.9±0.01ms        ? ?/sec
variant_to_json_shredded_primitive                                                                1.00    434.4±6.15µs        ? ?/sec     1.00    434.9±4.88µs        ? ?/sec
variant_to_json_wide_object                                                                       1.18    178.9±0.87ms        ? ?/sec     1.00    152.2±0.64ms        ? ?/sec

Per-runner information
variant_kernels — shard 1/1

Node: gk3-benchmark-cluster-nap-fytca8go-9faf2da9-wvqf

Instance: c4a-highmem-16 (12 vCPU / 65 GiB)

uname:

Linux bench-c7041911538-5527-g7bwr 6.12.105+ #1 SMP Sat Sep 19 07:55:25 UTC 2026 aarch64 GNU/Linux

BENCH_COMMAND:

cargo bench --features=arrow,async,test_common,experimental,object_store --bench variant_kernels
CPU Details (lscpu)
Architecture:                            aarch64
CPU op-mode(s):                          64-bit
Byte Order:                              Little Endian
CPU(s):                                  16
On-line CPU(s) list:                     0-15
Vendor ID:                               ARM
Model name:                              Neoverse-V2
Model:                                   1
Thread(s) per core:                      1
Core(s) per cluster:                     16
Socket(s):                               -
Cluster(s):                              1
Stepping:                                r0p1
BogoMIPS:                                2000.00
Flags:                                   fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp cpuid asimdrdm jscvt fcma lrcpc dcpop sha3 sm3 sm4 asimddp sha512 sve asimdfhm dit uscat ilrcpc flagm sb paca pacg dcpodp sve2 sveaes svepmull svebitperm svesha3 svesm4 flagm2 frint svei8mm svebf16 i8mm bf16 dgh rng bti
L1d cache:                               1 MiB (16 instances)
L1i cache:                               1 MiB (16 instances)
L2 cache:                                32 MiB (16 instances)
L3 cache:                                80 MiB (1 instance)
NUMA node(s):                            1
NUMA node0 CPU(s):                       0-15
Vulnerability Gather data sampling:      Not affected
Vulnerability Indirect target selection: Not affected
Vulnerability Itlb multihit:             Not affected
Vulnerability L1tf:                      Not affected
Vulnerability Mds:                       Not affected
Vulnerability Meltdown:                  Not affected
Vulnerability Mmio stale data:           Not affected
Vulnerability Reg file data sampling:    Not affected
Vulnerability Retbleed:                  Not affected
Vulnerability Spec rstack overflow:      Not affected
Vulnerability Spec store bypass:         Mitigation; Speculative Store Bypass disabled via prctl
Vulnerability Spectre v1:                Mitigation; __user pointer sanitization
Vulnerability Spectre v2:                Mitigation; CSV2, BHB
Vulnerability Srbds:                     Not affected
Vulnerability Tsa:                       Not affected
Vulnerability Tsx async abort:           Not affected
Vulnerability Vmscape:                   Not affected
Resource Usage

base (merge-base)

Metric Value
Wall time 870.2s
Peak memory 450.4 MiB
Avg memory 275.6 MiB
CPU user 867.5s
CPU sys 0.2s
Peak spill 0 B

branch

Metric Value
Wall time 865.2s
Peak memory 450.3 MiB
Avg memory 277.9 MiB
CPU user 858.8s
CPU sys 0.2s
Peak spill 0 B

File an issue against this benchmark runner

cetra3 and others added 2 commits November 24, 2026 13:56
Add `VariantView`, `VariantMetadataView`, `VariantObjectView` and
`VariantListView`. Building a view reads one header. Each accessor reads
and bounds-checks only the bytes it needs, and compares field names as
bytes.

`VariantMetadata`, `VariantObject` and `VariantList` are now built by
promoting a view, and their accessors delegate to it.
`ObjectBuilder::finish` sorts fields with the new
`MetadataBuilder::cmp_field_names`.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
`try_binary_search_range_by` now returns the comparison's error and
`None` when nothing matches, and replaces `try_binary_search`. The view
types read the header fields through accessors, so the fields stay
private.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@cetra3
cetra3 force-pushed the variant-views-4-shred branch from 4d0fdfd to d32659d Compare November 24, 2026 03:38
cetra3 and others added 3 commits November 24, 2026 14:15
`VariantView::to_variant`, `VariantMetadataView::into_metadata`,
`VariantObjectView::into_object` and `VariantListView::into_list` can
fail, so they become `try_to_variant`, `try_into_metadata`,
`try_into_object` and `try_into_list`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`variant_get` reads each row of an unshredded column through
`VariantView::get_path`. A row reads only the headers and field names
along the path, and decodes only the value that it finds.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
`shred_variant` and `unshred_variant` copy each leftover object field by
its field id and encoded bytes, through the new
`ObjectBuilder::insert_encoded` and `VariantView::bytes`. A leftover
field is no longer decoded into a `Variant` and encoded again.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@cetra3
cetra3 force-pushed the variant-views-4-shred branch from d32659d to 76ad2d5 Compare November 24, 2026 03:46

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

parquet-variant parquet-variant* crates

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants