This is an automated email from the ASF dual-hosted git repository. github-merge-queue[bot] pushed a commit to branch gh-readonly-queue/main/pr-11624-8ecd35b1aa8e7d4ecca79162aefe5ebd846fa174 in repository https://gitbox.apache.org/repos/asf/arrow-rs.git
commit 01a9917658fecd639991e92a5a5810be710b539c Author: Kosta Tarasov <[email protected]> AuthorDate: Wed Oct 21 05:27:32 2026 +0000 Add variant_get benchmarks for per-row metadata (#11624) # Which issue does this PR close? Related to #10358 # Rationale for this change As @cetra3 pointed out in [this comment](https://github.com/apache/arrow-rs/pull/10358#issuecomment-6086383082), the existing object-path `variant_get` benchmark shares one metadata dictionary across all rows and misses the regression with separate per-row dictionaries. This PR adds cases covering `json_to_variant` and `VariantArrayBuilder` output, plus a fixed unshared-metadata fixture, in response to that feedback. # What changes are included in this PR? Adds a `variant_get_object_path_metadata` group with three fixture sources: - `json_to_variant`: measures extraction from JSON conversion output. - `variant_array_builder`: inserts fields in reverse order to exercise unsorted metadata. - `per_row_metadata`: explicitly gives each row its own metadata bytes, preserving this case even if the producers later share dictionaries. Each case extracts `attr.140` as `Int32` from 8,192 rows with 300 fields, alternating between two key sets that both contain the target. Fixture creation is outside the timed loop. The producer cases can therefore reflect future improvements to metadata sharing without including conversion time. # Are these changes tested? All three cases pass a benchmark smoke run. Before timing, they assert the output length and extracted values. The fixed per-row fixture also checks that repeated dictionaries occupy distinct byte ranges. The new cases reproduce the regression when comparing #10358 at `38296248d0` with its base, `1a41738b79`: | Case | Base | PR #10358 | PR time / base time | | --- | ---: | ---: | ---: | | `json_to_variant` | 11.48 ms | 18.44 ms | 1.61× | | `variant_array_builder` | 10.60 ms | 24.28 ms | 2.29× | | `per_row_metadata` | 10.01 ms | 18.60 ms | 1.86× | These are local mean times from paired runs pinned to CPU 4, with 20 samples per case and the same compiler and dependencies. Another benchmark was running concurrently on the host. # Are there any significant user-facing changes? No. --- parquet-variant-compute/benches/variant_kernels.rs | 91 ++++++++++++++++++++++ 1 file changed, 91 insertions(+) diff --git a/parquet-variant-compute/benches/variant_kernels.rs b/parquet-variant-compute/benches/variant_kernels.rs index d556f19170..8354c87984 100644 --- a/parquet-variant-compute/benches/variant_kernels.rs +++ b/parquet-variant-compute/benches/variant_kernels.rs @@ -17,6 +17,7 @@ use arrow::array::{ Array, ArrayRef, BinaryViewArray, BinaryViewBuilder, Int32Array, StringArray, StructArray, + StructArrayAssembler, }; use arrow::buffer::Buffer; use arrow::compute::CastOptions; @@ -299,6 +300,69 @@ pub fn variant_get_unshredded_object_path_bench(c: &mut Criterion) { }); } +pub fn variant_get_object_path_metadata_bench(c: &mut Criterion) { + let size = SHRED_VARIANT_OBJECT_ROWS; + let num_fields = 300; + let field = Arc::new(Field::new("typed_value", DataType::Int32, true)); + let path = VariantPath::from(vec![VariantPathElement::field("attr.140")]); + let options = GetOptions::new_with_path(path).with_as_type(Some(field)); + let mut group = c.benchmark_group("variant_get_object_path_metadata"); + + // Alternate two overlapping key sets, always including the lookup target. + let json_templates = [0, 1].map(|offset| { + Value::Object( + (0..num_fields) + .map(|index| { + if index == num_fields / 2 { + ("attr.140".to_string(), Value::from(140)) + } else { + (format!("attr.{index:03}.{offset}"), Value::from(index)) + } + }) + .collect(), + ) + }); + for case in [ + "json_to_variant", + "variant_array_builder", + "per_row_metadata", + ] { + // Fixture construction is outside the timed loop. The producer cases follow their + // output layouts, including any future dictionary sharing. + let input = match case { + "json_to_variant" => { + let json_strings = json_templates.each_ref().map(Value::to_string); + let json: ArrayRef = Arc::new(StringArray::from_iter_values( + json_strings.iter().cycle().take(size), + )); + ArrayRef::from(json_to_variant(&json).unwrap()) + } + "variant_array_builder" => { + let mut builder = VariantArrayBuilder::new(size); + for json in json_templates.iter().cycle().take(size) { + let mut object = builder.new_object(); + // Reverse insertion order produces an unsorted metadata dictionary. + for (name, value) in json.as_object().unwrap().iter().rev() { + object.insert(name, value.as_i64().unwrap()); + } + object.finish(); + } + ArrayRef::from(builder.build()) + } + _ => ArrayRef::from(create_per_row_metadata_variant_array(&json_templates, size)), + }; + let result = variant_get(&input, options.clone()).unwrap(); + let result = result.as_any().downcast_ref::<Int32Array>().unwrap(); + assert_eq!(result.len(), size); + assert!(result.iter().all(|value| value == Some(140))); + + group.bench_function(case, |b| { + b.iter(|| variant_get(&input, options.clone()).unwrap()) + }); + } + group.finish(); +} + /// Shreds objects whose fields only partially match the requested shredding schema. /// /// Every field that is *not* covered by the schema is copied into the leftover `value` column, @@ -752,6 +816,7 @@ criterion_group!( variant_get_list_index_all_oob_int64_bench, variant_get_shredded_utf8_bench, variant_get_unshredded_object_path_bench, + variant_get_object_path_metadata_bench, shred_variant_partial_object_bench, shred_variant_unmatched_object_bench, variant_array_builder_build_bench, @@ -820,6 +885,32 @@ fn create_shredded_utf8_variant_array(size: usize) -> VariantArray { .expect("created struct should be a valid shredded variant") } +/// Keeps metadata unshared even if `json_to_variant` or `VariantArrayBuilder` starts sharing it. +/// `BinaryViewArray::from_iter_values` copies each row's metadata into its own byte range. +fn create_per_row_metadata_variant_array(json_templates: &[Value], size: usize) -> VariantArray { + let templates = json_templates + .iter() + .map(|json| { + let mut builder = VariantBuilder::new(); + append_json(json, &mut builder).unwrap(); + builder.finish() + }) + .collect::<Vec<_>>(); + let rows = templates.iter().cycle().take(size); + let metadata = BinaryViewArray::from_iter_values(rows.clone().map(|(metadata, _)| metadata)); + let value = BinaryViewArray::from_iter_values(rows.map(|(_, value)| value)); + assert_ne!( + metadata.value(0).as_ptr(), + metadata.value(templates.len()).as_ptr() + ); + let input = StructArrayAssembler::new() + .with_field("metadata", Arc::new(metadata), false) + .with_field("value", Arc::new(value), false) + .build() + .unwrap(); + VariantArray::try_new(&input).unwrap() +} + fn create_unshredded_object_variant_array(size: usize) -> VariantArray { let field_names = (0..300) .map(|index| format!("attr.{index:03}"))
