Repository navigation
Description
Describe the bug
cast_to_variant does not honor Variant extension annotations on nested input fields. When an ordinary Arrow struct contains a Variant child, the kernel converts the child's physical metadata and value fields into object members instead of incorporating the existing Variant value.
For example, a struct with a Variant-valued field a containing 1 becomes {"a":{"metadata":"AQAA","value":"FAEAAAA="}} instead of {"a":1}.
Found while implementing Spark-compatible to_variant_object in datafusion-variant. This concerns constructing a Variant from an ordinary Arrow container containing Variant children; an already encoded Variant object has a different representation.
To Reproduce
Reproduced with arrow, parquet-variant, and parquet-variant-compute version 60.0.0. The nested field retains its Variant extension annotation, so this reproducer does not depend on DataFusion or metadata loss in SQL constructors.
use std::sync::Arc;
use arrow::array::{ArrayRef, StructArray};
use parquet_variant::Variant;
use parquet_variant_compute::{VariantArrayBuilder, cast_to_variant, variant_to_json};
fn main() {
let mut builder = VariantArrayBuilder::new(1);
builder.append_variant(Variant::Int32(1));
let child = builder.build();
// An ordinary struct with an explicitly annotated Variant child.
let field = Arc::new(child.field("a"));
let input = StructArray::new(vec![field].into(), vec![ArrayRef::from(child)], None);
let output = ArrayRef::from(cast_to_variant(&input).unwrap());
let json = variant_to_json(&output).unwrap();
println!("{}", json.value(0));
assert_eq!(json.value(0), r#"{"a":1}"#);
}The assertion fails with:
actual: {"a":{"metadata":"AQAA","value":"FAEAAAA="}}
expected: {"a":1}
The same failure is reproducible for arrays. Replacing the field and input construction above with:
let field = Arc::new(child.field("item"));
let input = arrow::array::ListArray::new(
field,
arrow::buffer::OffsetBuffer::new(vec![0_i32, 1].into()),
ArrayRef::from(child),
None,
);produces [{"metadata":"AQAA","value":"FAEAAAA="}] instead of [1] (also reproduced on 60.0.0).
Additional probes on 60.0.0 reproduce the same problem with explicitly annotated children in Map values, LargeList, FixedSizeList, ListView, LargeListView, dense/sparse Union, and RunEndEncoded values. Map values become storage objects; the other list layouts match the result above; union and run-end encoded values become storage objects instead of the logical scalar 1.
Expected behavior
Recognize nested fields annotated with the Variant extension type and incorporate their logical values when recursively converting containers or decoding union/run-end encoded values. Unannotated structs with fields named metadata and value should continue to be treated as ordinary objects.
Additional context
Source inspection of main at 5746b443f7 shows that the struct conversion builder passes each child's DataType and array to the recursive builder, omitting the child's field metadata. The nested annotation is already available in the enclosing struct's fields.
Spark 4.2 explicitly tests casting an array of existing Variant objects and expects their logical contents to be preserved. Its recursive conversion implementation handles Variant children explicitly through appendVariant for arrays, maps, and structs. These Spark references were inspected, not executed locally.
Related: #8319's discussion identified the ambiguity between Variant storage and ordinary structs. #9519 concerns the opposite direction, extracting into structs containing Variant fields. #11937 concerns preserving UUID field metadata in the same casting API.
Activity
bharadwaj-pendyala commentedon Oct 30, 2026
I reproduced this on main (9ffd63d) with your struct example. It prints
{"a":{"metadata":"AQAA","value":"FAEAAAA="}}.My fix would add a
Variantrow builder. It wraps the child inVariantArray::try_new, appends null for null rows, and usestry_value(index)otherwise, including for shreddedtyped_value. Next tomake_arrow_to_variant_row_builder, a helper would pick it when the childFieldpasseshas_valid_extension_type::<VariantType>(). Otherwise it uses the existingDataTypedispatch. The struct, list-like, map, union and run-end builders already have the childField, so they can use it. Unannotated structs withmetadata/valuecolumns stay ordinary objects.Dictionary values don't carry a field, so they'd stay as they are. The public
cast_to_variant(&dyn Array)signature doesn't change either. A top-level annotated input is the field-aware entry point from #11937, and I'd keep that out of this PR.Am I missing a case here?
Mola-maker commentedon Nov 12, 2026
Hi @bharadwaj-pendyala, thanks for reproducing this and sketching the fix. Are you still planning to open a PR for it? If not, I'd like to take this on. Let me know either way.
bharadwaj-pendyala commentedon Nov 12, 2026
@Mola-maker go ahead, it's yours. I haven't started on code, so there's nothing to hand over beyond the sketch above. Use it if it helps.
Mola-maker commentedon Nov 12, 2026
sure,thanks
fix(parquet-variant-compute): preserve nested Variant values in cast_…