Repository navigation
Description
Describe the bug
The Variant spec has two distinct requirements on the value column of a variant group:
-
Presence: the group must contain a field named
value(tracked by Variant: report that arrow-rs produces "bad" shredded variant #10306, which covers omitting the column entirely). -
Annotation: per VariantEncoding.md:
The
valuefield must be annotated asrequiredfor unshredded Variant values, oroptionalif parts of the value are shredded as typed Parquet columns.
This issue tracks a violation of the second requirement: VariantArray::from_parts hardcodes the value field as nullable, so kernels that produce unshredded output through it annotate value as nullable/optional even though the data never contains an unmasked null there:
| Producer | Output | value annotation |
Spec |
|---|---|---|---|
VariantArrayBuilder::build |
unshredded | non-nullable | ✅ |
json_to_variant / cast_to_variant (via builder) |
unshredded | non-nullable | ✅ |
unshred_variant (via from_parts) |
unshredded | nullable | ❌ must be required |
variant_get with as_type: None (VariantToBinaryVariantArrowRowBuilder::finish, via from_parts) |
unshredded | nullable | ❌ must be required |
shred_variant |
shredded | nullable | ✅ (optional is legal when typed_value exists) |
Notably, VariantValueArrayBuilder::append_null already documents the rule ("It is only valid to call this method when building the value field of a shredded variant column...") — the callers respect it at the data level (value-nulls only under struct-null rows, i.e. masked), but from_parts then discards that guarantee at the schema level.
Consequences:
- Writing
unshred_variantoutput to Parquet producesoptional group v (VARIANT) { required binary metadata; optional binary value; }, which is out of spec for unshredded variants — the annotation-level sibling of Variant: report that arrow-rs produces "bad" shredded variant #10306. - Arrow C++'s
VariantExtensionType::IsSupportedStorageTypeexplicitly rejects nullable-valuestorage for unshredded variants (nullable_valueis one of the invalid cases incpp/src/parquet/arrow/variant_test.cc), so C++ cannot attach the variant extension type to such data. VariantArray::eqcompares the innerStructArrayincluding data types, so ashred_variant→unshred_variantround trip yields an array that is not equal to theVariantArrayBuilder-produced original purely because of the annotation.
To Reproduce
use arrow_schema::DataType;
use parquet_variant::Variant;
use parquet_variant_compute::{VariantArrayBuilder, shred_variant, unshred_variant};
let mut builder = VariantArrayBuilder::new(1);
builder.append_variant(Variant::from(42i64));
let original = builder.build();
// spec-correct: the builder annotates `value` as non-nullable
let (idx, field) = original.inner().fields().find("value").unwrap();
assert!(!field.is_nullable());
// round trip through shredding
let shredded = shred_variant(&original, &DataType::Int64).unwrap();
let unshredded = unshred_variant(&shredded).unwrap();
// out of spec: the output is unshredded but `value` is annotated nullable
let (idx, field) = unshredded.inner().fields().find("value").unwrap();
assert!(field.is_nullable());Expected behavior
Unshredded VariantArrays produced by unshred_variant and variant_get (binary-variant output path) should annotate the value field as non-nullable, matching VariantArrayBuilder and the spec.
Note a blanket fix in from_parts (e.g. nullable = typed_value.is_none()) is not correct: variant_get's internal NotShredded intermediates are unshredded arrays whose value column legitimately contains unmasked nulls (rows whose data lived in typed_value before the path step, later surfaced as Variant::Null by try_value), and StructArray::new rejects unmasked nulls under a non-nullable field. Options:
- Caller-directed (preferred): the two affected producers build the
valuefield non-nullable; their value-nulls are always masked by the parent null buffer, which Arrow permits. - Data-driven in
from_parts: annotate non-nullable whentyped_value.is_none()and all value-nulls are covered by the parent null buffer. - Normalize annotations at the Parquet write boundary only.
Additional context
- Presence-level sibling issue: Variant: report that arrow-rs produces "bad" shredded variant #10306
- Spec discussion around
valuerequirements: GH-590: Allow VARIANT value to be omitted parquet-format#591 - Arrow C++ variant storage validation (rejects nullable
value):cpp/src/arrow/extension/parquet_variant.cc(IsSupportedStorageType); shredding support tracked in GH-45937: [C++][Parquet] Variant shredding arrow#45948
Activity
[Variant] Unshredded `VariantArray` producers annotate the `value` fi…
[Variant] Unshredded `VariantArray` producers annotate the `value` fi…