Skip to content

[Variant] Unshredded VariantArray producers annotate the value field as nullable (spec requires required) - #10320

Merged
Jefffrey merged 8 commits into
apache:mainfrom
sdf-jkl:unshredded-value-non-nullable-10315
Aug 8, 2026
Merged

Jefffrey merged 8 commits into
apache:mainfrom
sdf-jkl:unshredded-value-non-nullable-10315

Conversation

@sdf-jkl

@sdf-jkl sdf-jkl commented Jul 11, 2026

Copy link
Copy Markdown
Member

Which issue does this PR close?

Rationale for this change

  • Parquet spec required unshredded VariantArray to be non-nullable. Other readers might reject our impl because unshred_variant and variant_get as_type None return nullable unshredded Variant.

What changes are included in this PR?

Are these changes tested?

  • yes, added new unit tests and fixed some old ones

Are there any user-facing changes?

  • Yes, the nullability now conforms the spec

@github-actions github-actions Bot added parquet parquet crate parquet-variant parquet-variant* crates labels Jul 11, 2026
@sdf-jkl sdf-jkl changed the title Unshredded value non nullable 10315 [Variant] Unshredded VariantArray producers annotate the value field as nullable (spec requires required) Jul 11, 2026
@sdf-jkl sdf-jkl changed the title [Variant] Unshredded VariantArray producers annotate the value field as nullable (spec requires required) [Variant] Unshredded VariantArray producers annotate the value field as nullable (spec requires required) Jul 11, 2026
@github-actions github-actions Bot removed the parquet parquet crate label Jul 23, 2026
@sdf-jkl
sdf-jkl marked this pull request as ready for review July 23, 2026 23:18
@sdf-jkl

sdf-jkl commented Jul 23, 2026

Copy link
Copy Markdown
Member Author

@klion26 Please take a look 🙏

@klion26 klion26 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sdf-jkl Thanks for the pr. I dealt with some unexpected emergencies this week. I need some more time to take a look at it; I will response asap.

@klion26 klion26 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sdf-jkl I've left some comments, please take a look

let metadata = VariantMetadata::try_new(metadata_bytes)?;
let mut value_builder = value_builder.builder_ext(&metadata);
row_builder.append_row(&mut value_builder, &metadata, i)?;
if value_col.is_null(i) && typed_value_col.is_none_or(|tv| tv.is_null(i)) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic can be moved into macro handle_unshredded_case and ValueOnlyUnshredVariantBuilder::append_row

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, added here but with a twist. These methods also cover the nested fields, and a missing object field must stay absent instead of becoming Variant::Null (otherwise {"a": 3} would unshred as {"a": 3, "b": null} under a partially shredded schema). So the substitution lives in a sink wrapper (TopLevelRowSink) that only the top level uses.

///
/// # Panics
/// If `value` contains nulls not masked by `nulls`.
pub(crate) fn from_parts_unshredded(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need to panic? The callers of this function, unshred_variant and shredded_get_path(in variant_get) both return Result

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The panic is not new logic in this function, it comes from StructArray::new inside StructArrayBuilder::build, which validates that a non-nullable field has no unmasked nulls. The doc comment just makes that existing behavior visible.

I kept it as a panic rather than a Result because it can only fire on an internal bug, never on bad input data.

}

#[test]
fn test_variant_get_missing_path_as_variant_annotates_value_non_nullable() {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to add this test

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR changes two production call sites, not just unshred_variant. The missing-path branch of shredded_get_path used to build its all-NULL output with a nullable value field and now uses from_parts_unshredded. This test pins that second call site, without it only the unshred_variant paths would be covered.

/// Returns true if every null in `value` is masked by a parent null, i.e. the column may be
/// annotated non-nullable.
fn value_nulls_are_masked(value: &ArrayRef, parent_nulls: Option<&NullBuffer>) -> bool {
match value.logical_nulls() {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Is there any reason that logical_nulls is better than nulls here?
  2. The match arms None and null_count() == 0 seem can be merged into ArrayRef::null_count() == 0?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Applied here

}

#[test]
fn test_unshred_already_unshredded_reannotates_nullable_value() {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 Do we need to add some tests for decimal/timestamp/list/map? currently they'are all covered by the macro handle_unshredded_case but they're differnt enum of UnshredVariantRowBuilder
2 Do we need to add a test that returns null (not Variant::Null)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Thanks, added here
  2. That case is covered by test_unshred_with_nulls_annotates_value_non_nullable: row 1 is a top-level NULL row and the test asserts unshredded.is_null(1) on the output.

@sdf-jkl

sdf-jkl commented Aug 3, 2026

Copy link
Copy Markdown
Member Author

Thanks @klion26 I addressed everything. Please take another look.

@klion26 klion26 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@Jefffrey
Jefffrey merged commit 4da9d64 into apache:main Aug 8, 2026
19 checks passed
@Jefffrey

Jefffrey commented Aug 8, 2026

Copy link
Copy Markdown
Contributor

thanks @sdf-jkl & @klion26

@sdf-jkl
sdf-jkl deleted the unshredded-value-non-nullable-10315 branch August 8, 2026 03:56
MassivePizza pushed a commit to massive-com/arrow-rs that referenced this pull request Aug 19, 2026
…eld as nullable (spec requires `required`) (apache#10320)

# Which issue does this PR close?

<!--
We generally require a GitHub issue to be filed for all bug fixes and
enhancements and this helps us generate change logs for our releases.
You can link an issue to this PR using the GitHub syntax.
-->

- Closes apache#10315.

- let's wait till apache#10318 ships

# Rationale for this change

- Parquet spec required unshredded `VariantArray` to be non-nullable.
Other readers might reject our impl because `unshred_variant` and
`variant_get as_type None` return nullable unshredded Variant.
<!--
Why are you proposing this change? If this is already explained clearly
in the issue then this section is not needed.
Explaining clearly why changes are proposed helps reviewers understand
your changes and offer better suggestions for fixes.
-->

# What changes are included in this PR?

- builds on apache#10318
- Add changes to `unshred_variant` and `variant_get` to return
non-nullable unshredded Variant
<!--
There is no need to duplicate the description in the issue here but it
is sometimes worth providing a summary of the individual changes in this
PR.
-->

# Are these changes tested?

- yes, added new unit tests and fixed some old ones
<!--
We typically require tests for all PRs in order to:
1. Prevent the code from being accidentally broken by subsequent changes
2. Serve as another way to document the expected behavior of the code

If tests are not included in your PR, please explain why (for example,
are they covered by existing tests)?

If this PR claims a performance improvement, please include evidence
such as benchmark results.
-->

# Are there any user-facing changes?

- Yes, the nullability now conforms the spec
<!--
If there are user-facing changes then we may require documentation to be
updated before approving the PR.

If there are any breaking changes to public APIs, please call them out.
-->

---------

Co-authored-by: Jeffrey Vo <[email protected]>
Surajsuthar pushed a commit to Surajsuthar/arrow-rs that referenced this pull request Nov 14, 2026
# Which issue does this PR close?

No issue. This cleans up a wrapper added in apache#10320.

# Rationale for this change

`unshred_variant` wraps its top-level builder in `TopLevelRowSink`,
whose only job is to turn `append_null` into `Variant::Null`, because a
physical NULL cannot go in the non-nullable output `value` column. That
takes about 30 lines of forwarding code to override one method.

The method it overrides, `VariantValueArrayBuilderExt::append_null`, has
no other callers in the workspace. `shred_variant` only uses the ext to
start an object, and writes its nulls through the inherent
`VariantValueArrayBuilder::append_null`. So the behaviour can live on
the builder itself.

# What changes are included in this PR?

- `VariantValueArrayBuilderExt::append_null` now appends
`Variant::Null`.
- `TopLevelRowSink` is removed, and `unshred_variant` passes the ext
directly to the row builders.

`unshred_variant` output does not change. Missing top-level rows still
become `Variant::Null`, and nested builders keep their own `append_null`
behaviour: list elements become `Variant::Null` and missing object
fields are omitted.

# Are these changes tested?

Covered by existing tests, which fail if the ext writes a physical NULL:
`test_unshred_missing_top_level_value_becomes_variant_null`,
`test_unshred_missing_row_for_decimal_timestamp_object_list`,
`test_unshred_value_only_with_unmasked_nulls_materializes_variant_null`,
and `variant_integration` case 129.

# Are there any significant user-facing changes?

`VariantValueArrayBuilder::builder_ext(..).append_null()` now writes
`Variant::Null` instead of a physical NULL. Callers building the `value`
column of a shredded variant should call the inherent
`VariantValueArrayBuilder::append_null`, which is unchanged. No
signatures change.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Opus 5.5 <[email protected]>
cetra3 pushed a commit to pydantic/arrow-rs that referenced this pull request Nov 18, 2026
# Which issue does this PR close?

- Closes apache#11938.

# 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
apache#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](https://claude.com/claude-code)

---------

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

Labels

bug Squash those bugs parquet-variant parquet-variant* crates

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Variant] Unshredded VariantArray producers annotate the value field as nullable (spec requires required)

3 participants