Skip to content

[Variant] cast_to_variant serializes nested Variant storage as ordinary objects #11942

Done

Description

@sdf-jkl

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

  1. added
    bug
    on Oct 27, 2026
  2. bharadwaj-pendyala commented on Oct 30, 2026

    @bharadwaj-pendyala
    Contributor

    I reproduced this on main (9ffd63d) with your struct example. It prints {"a":{"metadata":"AQAA","value":"FAEAAAA="}}.

    My fix would add a Variant row builder. It wraps the child in VariantArray::try_new, appends null for null rows, and uses try_value(index) otherwise, including for shredded typed_value. Next to make_arrow_to_variant_row_builder, a helper would pick it when the child Field passes has_valid_extension_type::<VariantType>(). Otherwise it uses the existing DataType dispatch. The struct, list-like, map, union and run-end builders already have the child Field, so they can use it. Unannotated structs with metadata/value columns 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?

  3. Mola-maker commented on Nov 12, 2026

    @Mola-maker
    Contributor

    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.

  4. bharadwaj-pendyala commented on Nov 12, 2026

    @bharadwaj-pendyala
    Contributor

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

  5. Mola-maker commented on Nov 12, 2026

    @Mola-maker
    Contributor

    sure,thanks

  6. added a commit that references this issue on Nov 27, 2026
    b325878
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions