Skip to content

[Variant] Validate storage fields in VariantType - #12245

Merged
alamb merged 8 commits into
apache:mainfrom
sdf-jkl:variant-type-validate-11938
Nov 17, 2026
Merged

alamb merged 8 commits into
apache:mainfrom
sdf-jkl:variant-type-validate-11938

Conversation

@sdf-jkl

@sdf-jkl sdf-jkl commented Nov 13, 2026 •

Copy link
Copy Markdown
Member

Which issue does this PR close?

Rationale for this change

VariantType accepted any Struct as storage, so try_new, validate and Field::try_canonical_extension_type treated structs like Struct("unrelated": Int32) as Variant. Extension wrappers such as DataFusion's DFVariant (apache/datafusion#26822) should be able to delegate this check to the Arrow type instead of building a VariantArray.

What changes are included in this PR?

VariantType::supports_data_type now requires a struct with a binary metadata field (or a dictionary or run-end encoded array of binary) and at least one of a binary value field and a typed_value field.

These are the top-level checks VariantArray::try_new already applied, and try_new now calls this check instead of its own, so everything VariantArray reads still passes. That includes shredded storage without value, and the Parquet reader, which attaches VariantType to every VARIANT column. try_new runs it before canonicalizing shredded types, so a non-Variant struct reports the storage error rather than an illegal shredded type. Nullability is deliberately not checked, to keep reads lenient: arrow-rs wrote unshredded value as nullable before #10320, and those files must still read. typed_value types and nested shredded groups stay with VariantArray. Each failed check reports its own error, reusing the wording of VariantArray's former per-field messages with VariantType in place of VariantArray. The storage layout is now documented on VariantType, and VariantArray::try_new links to it.

Are these changes tested?

Yes. New unit tests in variant.rs cover accepted layouts (each binary type, dictionary and run-end encoded metadata, shredded, and typed_value without value) and rejected ones, asserting the error message for each. VariantArray::try_new's per-field error tests duplicated these cases, so a single test replaces them, checking that the storage error is reported before canonicalization's illegal shredded type error. The other parquet-variant-compute and parquet Variant tests, including variant_integration, pass unchanged.

Are there any significant user-facing changes?

Storage types that are not Variant storage now fail VariantType::try_new and validate, so Field::with_extension_type(VariantType) panics on them and try_with_extension_type returns an error. Code that tagged placeholder structs such as Struct(Fields::empty()) needs a real storage type. VariantArray::try_new now reports invalid storage with VariantType's errors. They keep the old per-field wording but name VariantType, a non-struct input reports VariantType only supports StructArray, got ..., and a struct with neither value nor typed_value now says either field is accepted.

AI usage disclosure

Implementation and tests prepared with Claude Code.

🤖 Generated with Claude Code

Require a binary `metadata` field and a binary `value` and/or
`typed_value` field, matching the top-level checks in
`VariantArray::try_new`.

Closes apache#11938.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@boring-cyborg boring-cyborg Bot added the arrow-core Core crates related to representing arrow data label Nov 13, 2026
sdf-jkl and others added 2 commits November 13, 2026 17:03
`VariantArray::try_new` now validates its input with
`VariantType::supports_data_type` before canonicalizing, replacing its own
struct and `metadata` checks and `validate_metadata_array`. Invalid storage
reports the `VariantType data type mismatch` error.

Also clarify the test comment on missing `value` (allowed by the Arrow spec)
and the nullability doc, and test that `FixedSizeBinary` metadata is rejected.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@boring-cyborg boring-cyborg Bot added the parquet-variant parquet-variant* crates label Nov 13, 2026
sdf-jkl and others added 3 commits November 13, 2026 17:37
`VariantArray::try_new` delegates storage validation to `VariantType`, whose
tests already cover these layouts. Keep one test that checks the storage
error wins over canonicalization's illegal shredded type error, and drop a
`metadata` case that hit the same branch as `FixedSizeBinary`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@sdf-jkl

sdf-jkl commented Nov 13, 2026

Copy link
Copy Markdown
Member Author

@alamb you're a Variant accomplice -- PTAL 🙏

@alamb

alamb commented Nov 14, 2026

Copy link
Copy Markdown
Contributor

@alamb you're a Variant accomplice

😎

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

Makes sense to me -- thank you @sdf-jkl

/// of using this extension type.
///
/// The storage type must be a struct with a binary `metadata` field and at
/// least one of a binary `value` field or a `typed_value` field. Nullability

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.

is the mention of nullability really necessary here?

err.unwrap_err().to_string(),
"Invalid argument error: Invalid VariantArray: StructArray must contain a 'metadata' field"
err.to_string(),
"Invalid argument error: VariantType data type mismatch, expected Struct with binary \"metadata\" and binary \"value\" and/or \"typed_value\" fields, found Struct(\"metadata\": non-null BinaryView, \"value\": UInt8)"

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.

nit is I think the old message was more specific and less verbose

It would be nice if we could retain it

@Rich-T-kid Rich-T-kid added the bug Squash those bugs label Nov 16, 2026
sdf-jkl and others added 2 commits November 16, 2026 17:17
Move the top-level field requirements from `VariantArray::try_new` to
`VariantType` and link to them, and drop the nullability note.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Return one message per failed check instead of a single generic mismatch,
reusing the wording of the former `VariantArray` checks.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@sdf-jkl

sdf-jkl commented Nov 16, 2026

Copy link
Copy Markdown
Member Author

Thanks @alamb 🙏

Shouldn't have rushed to get this checked on Friday afternoon

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

thanks @sdf-jkl -- this looks great now

)),
value(DataType::BinaryView),
]),
"VariantType 'metadata' field must be Binary, LargeBinary, BinaryView, or a Dictionary or RunEndEncoded array of one of those types, got Dictionary(Int8, Utf8)",

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.

❤️ love it

@alamb
alamb added this pull request to the merge queue Nov 17, 2026
Merged via the queue into apache:main with commit e28432b Nov 17, 2026
22 checks passed
sdf-jkl added a commit to datafusion-contrib/datafusion-variant that referenced this pull request Nov 30, 2026
Variant storage validation (apache/arrow-rs#12245) and the shredded
list index result the tests expect both first shipped in 60.2.0.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
sdf-jkl added a commit to datafusion-contrib/datafusion-variant that referenced this pull request Nov 30, 2026
* Document Variant storage validation and remove TODO

arrow-schema 60.2.0 validates the Variant storage schema
(apache/arrow-rs#11938), so the extension type check no longer accepts
any struct.

Co-Authored-By: Claude Fable 5.2 <[email protected]>

* Require arrow and parquet 60.2

Variant storage validation (apache/arrow-rs#12245) and the shredded
list index result the tests expect both first shipped in 60.2.0.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Claude Fable 5.2 <[email protected]>
Co-authored-by: sdf-jkl <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

arrow-core Core crates related to representing arrow data bug Squash those bugs parquet-variant parquet-variant* crates

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Variant] Validate storage fields in VariantType

3 participants