Skip to content

[Varint] Implement ShreddingState::AllNull variant #8093

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

codephage2020
Copy link
Contributor

@codephage2020 codephage2020 commented Aug 8, 2025

Which issue does this PR close?

What changes are included in this PR?

handles the "all null" case

Are these changes tested?

Yes.

Are there any user-facing changes?

no.

Signed-off-by: codephage2020 <[email protected]>
@codephage2020 codephage2020 marked this pull request as ready for review August 9, 2025 03:10
#[test]
fn all_null_variant_array_construction() {
let metadata = BinaryViewArray::from(vec![b"test" as &[u8]; 3]);
let nulls = NullBuffer::from(vec![false, false, false]); // all null
Copy link
Contributor

Choose a reason for hiding this comment

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

Are we sure that this case (where there is a value field present, but it is all null) should be treated as though there was no value field?

I haven't double checked the spec (probably the Arrow one) but this feels like it may be out of compliance

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As far as I know, the spec's requirement that "both value and typed_value are null" meaning "the value is missing."
We can see from the spec's example:
refer to here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, I got it. I will add a test for the case.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added a comprehensive test that demonstrates when a value field exists in the schema but contains all null values, it correctly remains in the Unshredded state rather than AllNull.

Copy link
Contributor

Choose a reason for hiding this comment

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

From what I understand, the treatment of NULL/NULL depends on context:

  • For a top-level variant value, it's interpreted as Variant::Null
  • For a shredded variant object field, it's interpreted as missing (SQL NULL)

So I guess there are two ways to get SQL NULL -- null mask on the struct(value, typed_value), or if both value and typed_value are themselves NULL?

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah -- the spec requires that the struct ("group") containing value and typed_value columns must be non-nullable:

The group for each named field must use repetition level required. A field's value and typed_value are set to null (missing) to indicate that the field does not exist in the variant. To encode a field that is present with a [variant/JSON, not SQL] null value, the value must contain a Variant null: basic type 0 (primitive) and physical type 0 (null).

So effectively, the NULL/NULL combo becomes the null mask for that nested field. Which is why a top-level NULL/NULL combo is incorrect -- the top-level field already has its own nullability.

Copy link
Contributor

@alamb alamb left a comment

Choose a reason for hiding this comment

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

Thank you @codephage2020 -- this looks great -- I had one corner case question

@codephage2020 codephage2020 force-pushed the feat/shreddingstate-allnull-variant branch from bd43fbf to b5d4c5d Compare August 12, 2025 08:15
Copy link
Contributor

@alamb alamb left a comment

Choose a reason for hiding this comment

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

Thank you @codephage2020 -- I think this looks good to me

FYI @scovich in case you would also like to review

Copy link
Contributor

@scovich scovich left a comment

Choose a reason for hiding this comment

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

Lots of messiness in the shredding spec here... but I think the current code is probably ok-ish, because we only support top-level variant so far?

Main question is whether we should forbid the AllNull case at top level, since it's technically valid only for nested fields?

@@ -187,6 +187,7 @@ impl VariantArray {
typed_value_to_variant(typed_value, index)
}
}
ShreddingState::AllNull { .. } => Variant::Null,
Copy link
Contributor

Choose a reason for hiding this comment

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

This is tricky... see #8122 (comment)

  • For a top-level variant, null/null is illegal (tho returning Variant::Null is arguably a correct way to compensate)
  • For a shredded variant field, null/null means SQL NULL, and returning Variant::Null is arguably incorrect (causes SQL IS NULL operator to return FALSE). But we don't even have a way to return SQL NULL here (it would probably correspond to Option::None?)

Comment on lines +445 to +446
// Should succeed and create an AllNull variant when neither value nor typed_value are present
let variant_array = VariantArray::try_new(Arc::new(array)).unwrap();
Copy link
Contributor

Choose a reason for hiding this comment

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

By a strict reading of the spec, this should actually fail, because this ShreddingState does not represent a shredded object field, but rather represents a top-level variant value:

value typed_value Meaning
null null The value is missing; only valid for shredded object fields

But maybe that's a validation VariantArray::try_new should perform, not ShreddingState::try_new?

Also, it would quickly become annoying if variant_get has to replace a missing or all-null value column with an all-Variant::Null column just to comply with the spec? Maybe that's why there's this additional tidbit?

If a Variant is missing in a context where a value is required, readers must return a Variant null (00): basic type 0 (primitive) and physical type 0 (null). For example, if a Variant is required (like measurement above) and both value and typed_value are null, the returned value must be 00 (Variant null).

#[test]
fn all_null_variant_array_construction() {
let metadata = BinaryViewArray::from(vec![b"test" as &[u8]; 3]);
let nulls = NullBuffer::from(vec![false, false, false]); // all null
Copy link
Contributor

Choose a reason for hiding this comment

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

From what I understand, the treatment of NULL/NULL depends on context:

  • For a top-level variant value, it's interpreted as Variant::Null
  • For a shredded variant object field, it's interpreted as missing (SQL NULL)

So I guess there are two ways to get SQL NULL -- null mask on the struct(value, typed_value), or if both value and typed_value are themselves NULL?

#[test]
fn all_null_variant_array_construction() {
let metadata = BinaryViewArray::from(vec![b"test" as &[u8]; 3]);
let nulls = NullBuffer::from(vec![false, false, false]); // all null
Copy link
Contributor

Choose a reason for hiding this comment

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

Ah -- the spec requires that the struct ("group") containing value and typed_value columns must be non-nullable:

The group for each named field must use repetition level required. A field's value and typed_value are set to null (missing) to indicate that the field does not exist in the variant. To encode a field that is present with a [variant/JSON, not SQL] null value, the value must contain a Variant null: basic type 0 (primitive) and physical type 0 (null).

So effectively, the NULL/NULL combo becomes the null mask for that nested field. Which is why a top-level NULL/NULL combo is incorrect -- the top-level field already has its own nullability.

@github-actions github-actions bot added the parquet-variant parquet-variant* crates label Aug 13, 2025
Signed-off-by: codephage2020 <[email protected]>
@codephage2020
Copy link
Contributor Author

Thanks for the thorough review! I’ll go through all the comments carefully and respond later today.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
parquet-variant parquet-variant* crates
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Variant] Implement ShreddingState::AllNull variant
3 participants