Skip to content

fix(variant): preserve SQL NULL for missing shredded object fields - #11504

Open
shatrughnaupadhyay wants to merge 7 commits into
apache:mainfrom
shatrughnaupadhyay:fix/11191-missing-shredded-field
Open

shatrughnaupadhyay wants to merge 7 commits into
apache:mainfrom
shatrughnaupadhyay:fix/11191-missing-shredded-field

Conversation

@shatrughnaupadhyay

@shatrughnaupadhyay shatrughnaupadhyay commented Oct 10, 2026 •

Copy link
Copy Markdown

@github-actions github-actions Bot added parquet-variant parquet-variant* crates bug Squash those bugs labels Oct 10, 2026

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

Looks good to me. I checked missing fields after list indexes as well; shredded and unshredded extraction agree for both SQL NULL and encoded Variant null.

@shatrughnaupadhyay
shatrughnaupadhyay marked this pull request as ready for review October 26, 2026 02:50
@shatrughnaupadhyay

Copy link
Copy Markdown
Author

Looks good to me. I checked missing fields after list indexes as well; shredded and unshredded extraction agree for both SQL NULL and encoded Variant null.

Thanks

@sdf-jkl

sdf-jkl commented Oct 27, 2026

Copy link
Copy Markdown
Member

Thanks @shatrughnaupadhyay 🙏

I want to merge #11052 first and see if reusing it's approach here would be easier/less code than having a second implementation for missing object fields.

@sdf-jkl

sdf-jkl commented Nov 13, 2026

Copy link
Copy Markdown
Member

@shatrughnaupadhyay hopefully #12224 passed the merge queue tonight. You will be able to reuse the nulls preservation from there

@sdf-jkl

sdf-jkl commented Nov 13, 2026

Copy link
Copy Markdown
Member

@shatrughnaupadhyay we're good to go 🚀

@sdf-jkl

sdf-jkl commented Nov 20, 2026

Copy link
Copy Markdown
Member

@shatrughnaupadhyay 👀

@shatrughnaupadhyay

Copy link
Copy Markdown
Author

Thanks @sdf-jkl @cakeni I have resolved conflicts, please help with merge.

@sdf-jkl

sdf-jkl commented Nov 23, 2026

Copy link
Copy Markdown
Member

@shatrughnaupadhyay other than the conflicts, could you please enable the CI on your fork and integrate the null preservation introduced in #12224 🙏

@shatrughnaupadhyay

Copy link
Copy Markdown
Author

@shatrughnaupadhyay other than the conflicts, could you please enable the CI on your fork and integrate the null preservation introduced in #12224 🙏

Thanks @sdf-jkl, integrated CI and it is success now https://github.com/shatrughnaupadhyay/arrow-rs/actions/runs/45098316568

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

Thanks again @shatrughnaupadhyay two more suggestions

Comment on lines +1946 to +2083
#[test]
fn test_missing_shredded_object_field_preserves_null_semantics() {
for field_type in [
DataType::Int64,
DataType::Struct(vec![Field::new("unused", DataType::Int64, true)].into()),
] {
let cases = [
(
"a",
vec![
Some(r#"{"a": 7}"#),
Some("{}"),
Some(r#"{"a": null}"#),
Some(r#"{"a": 42}"#),
Some(r#"{"a": {}}"#),
Some(r#"{"a": "fallback"}"#),
None,
],
DataType::Struct(vec![Field::new("a", field_type.clone(), true)].into()),
vec![
Some("7"),
None,
Some("null"),
Some("42"),
Some("{}"),
Some(r#""fallback""#),
None,
],
),
(
"a.b",
vec![
Some(r#"{"a": {"b": 7}}"#),
Some(r#"{"a": {}}"#),
Some(r#"{"a": {"b": null}}"#),
Some(r#"{"a": {"b": 42}}"#),
Some(r#"{"a": {"b": {}}}"#),
Some(r#"{"a": {"b": "fallback"}}"#),
None,
Some("{}"),
Some(r#"{"a": null}"#),
],
DataType::Struct(
vec![Field::new(
"a",
DataType::Struct(
vec![Field::new("b", field_type.clone(), true)].into(),
),
true,
)]
.into(),
),
vec![
Some("7"),
None,
Some("null"),
Some("42"),
Some("{}"),
Some(r#""fallback""#),
None,
None,
None,
],
),
(
"[0].a",
vec![
Some(r#"[{"a": 7}]"#),
Some("[{}]"),
Some(r#"[{"a": null}]"#),
Some(r#"[{"a": 42}]"#),
Some(r#"[{"a": {}}]"#),
Some(r#"[{"a": "fallback"}]"#),
None,
Some("[]"),
Some("[null]"),
],
DataType::List(Arc::new(Field::new(
"item",
DataType::Struct(vec![Field::new("a", field_type.clone(), true)].into()),
true,
))),
vec![
Some("7"),
None,
Some("null"),
Some("42"),
Some("{}"),
Some(r#""fallback""#),
None,
None,
None,
],
),
];
for (path, rows, schema, expected) in cases {
let json: ArrayRef = Arc::new(StringArray::from(rows));
let input = json_to_variant(&json).unwrap();
let requested_variant = Arc::new(input.field("result"));
let shredded = shred_variant(&input, &schema).unwrap();
let expected = StringArray::from(expected);

for array in [ArrayRef::from(input), ArrayRef::from(shredded)] {
for (offset, length) in [(0, array.len()), (1, array.len() - 1)] {
let array = array.slice(offset, length);
let expected = expected.slice(offset, length);
for as_type in [None, Some(requested_variant.clone())] {
for safe in [true, false] {
let options = GetOptions {
path: VariantPath::try_from(path).unwrap(),
as_type: as_type.clone(),
cast_options: CastOptions {
safe,
..Default::default()
},
};
let result = variant_get(&array, options).unwrap();
let variant = VariantArray::try_new(&result).unwrap();
for (row, value) in expected.iter().enumerate() {
assert_eq!(
variant.is_null(row),
value.is_none(),
"{path}, {field_type:?}, safe={safe}, offset={offset}, row={row}"
);
}
let unshredded = crate::unshred_variant(&variant).unwrap();
assert_eq!(
crate::variant_to_json(&ArrayRef::from(unshredded)).unwrap(),
expected,
"{path}, {field_type:?}, safe={safe}, offset={offset}"
);
}
}
}
}
}
}
}

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.

Could the test be smaller? Some of the loops cannot change the result:

  • safe only matters for cast errors, and getting a Variant never casts.
  • as_type: Some(variant) reads the same null mask as None.
  • One schema {a: {b: int64}} covers both the typed and the fallback rows: {"a": {}} lands in typed_value and {"a": 7} lands in value.
  • [0].a: the field step does not depend on whether its input came from an index step, and test_shredded_list_out_of_bounds_is_null already covers the index nulls.

variant_to_json keeps NULL rows as NULL, so comparing the JSON also checks is_null and tells a missing field apart from a Variant null. This version still fails for both paths without the fix:

Suggested change
#[test]
fn test_missing_shredded_object_field_preserves_null_semantics() {
for field_type in [
DataType::Int64,
DataType::Struct(vec![Field::new("unused", DataType::Int64, true)].into()),
] {
let cases = [
(
"a",
vec![
Some(r#"{"a": 7}"#),
Some("{}"),
Some(r#"{"a": null}"#),
Some(r#"{"a": 42}"#),
Some(r#"{"a": {}}"#),
Some(r#"{"a": "fallback"}"#),
None,
],
DataType::Struct(vec![Field::new("a", field_type.clone(), true)].into()),
vec![
Some("7"),
None,
Some("null"),
Some("42"),
Some("{}"),
Some(r#""fallback""#),
None,
],
),
(
"a.b",
vec![
Some(r#"{"a": {"b": 7}}"#),
Some(r#"{"a": {}}"#),
Some(r#"{"a": {"b": null}}"#),
Some(r#"{"a": {"b": 42}}"#),
Some(r#"{"a": {"b": {}}}"#),
Some(r#"{"a": {"b": "fallback"}}"#),
None,
Some("{}"),
Some(r#"{"a": null}"#),
],
DataType::Struct(
vec![Field::new(
"a",
DataType::Struct(
vec![Field::new("b", field_type.clone(), true)].into(),
),
true,
)]
.into(),
),
vec![
Some("7"),
None,
Some("null"),
Some("42"),
Some("{}"),
Some(r#""fallback""#),
None,
None,
None,
],
),
(
"[0].a",
vec![
Some(r#"[{"a": 7}]"#),
Some("[{}]"),
Some(r#"[{"a": null}]"#),
Some(r#"[{"a": 42}]"#),
Some(r#"[{"a": {}}]"#),
Some(r#"[{"a": "fallback"}]"#),
None,
Some("[]"),
Some("[null]"),
],
DataType::List(Arc::new(Field::new(
"item",
DataType::Struct(vec![Field::new("a", field_type.clone(), true)].into()),
true,
))),
vec![
Some("7"),
None,
Some("null"),
Some("42"),
Some("{}"),
Some(r#""fallback""#),
None,
None,
None,
],
),
];
for (path, rows, schema, expected) in cases {
let json: ArrayRef = Arc::new(StringArray::from(rows));
let input = json_to_variant(&json).unwrap();
let requested_variant = Arc::new(input.field("result"));
let shredded = shred_variant(&input, &schema).unwrap();
let expected = StringArray::from(expected);
for array in [ArrayRef::from(input), ArrayRef::from(shredded)] {
for (offset, length) in [(0, array.len()), (1, array.len() - 1)] {
let array = array.slice(offset, length);
let expected = expected.slice(offset, length);
for as_type in [None, Some(requested_variant.clone())] {
for safe in [true, false] {
let options = GetOptions {
path: VariantPath::try_from(path).unwrap(),
as_type: as_type.clone(),
cast_options: CastOptions {
safe,
..Default::default()
},
};
let result = variant_get(&array, options).unwrap();
let variant = VariantArray::try_new(&result).unwrap();
for (row, value) in expected.iter().enumerate() {
assert_eq!(
variant.is_null(row),
value.is_none(),
"{path}, {field_type:?}, safe={safe}, offset={offset}, row={row}"
);
}
let unshredded = crate::unshred_variant(&variant).unwrap();
assert_eq!(
crate::variant_to_json(&ArrayRef::from(unshredded)).unwrap(),
expected,
"{path}, {field_type:?}, safe={safe}, offset={offset}"
);
}
}
}
}
}
}
}
#[test]
fn test_missing_shredded_object_field_preserves_null_semantics() {
let b = DataType::Struct(vec![Field::new("b", DataType::Int64, true)].into());
let schema = DataType::Struct(vec![Field::new("a", b, true)].into());
let rows = [
Some(r#"{"a": {"b": 7}}"#),
Some(r#"{"a": {}}"#),
Some(r#"{"a": 7}"#),
Some(r#"{"a": null}"#),
Some("{}"),
None,
];
let input =
json_to_variant(&(Arc::new(StringArray::from(rows.to_vec())) as ArrayRef)).unwrap();
let shredded = shred_variant(&input, &schema).unwrap();
let cases = [
(
"a",
[
Some(r#"{"b":7}"#),
Some("{}"),
Some("7"),
Some("null"),
None,
None,
],
),
("a.b", [Some("7"), None, None, None, None, None]),
];
for (path, expected) in cases {
let options = GetOptions::new_with_path(VariantPath::try_from(path).unwrap());
let expected = StringArray::from(expected.to_vec());
for array in [
ArrayRef::from(input.clone()),
ArrayRef::from(shredded.clone()),
] {
for offset in [0, 1] {
let array = array.slice(offset, array.len() - offset);
let result = variant_get(&array, options.clone()).unwrap();
let result =
crate::unshred_variant(&VariantArray::try_new(&result).unwrap()).unwrap();
let json = crate::variant_to_json(&ArrayRef::from(result)).unwrap();
assert_eq!(
json,
expected.slice(offset, array.len()),
"{path}, offset={offset}"
);
}
}
}
}

}
accumulated_nulls =
NullBuffer::union(accumulated_nulls.as_ref(), path_nulls.as_ref());
// Typed struct casts retain their existing missing-field semantics.

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.

The gate also skips the mask for number and string outputs, and for those the reason is the fast path: an extra mask makes try_perfect_shredding rebuild the array instead of returning typed_value as-is (removing the gate fails the Arc::ptr_eq check in test_perfect_shredding_three_typed_value_columns).

For struct targets the current behavior is the bug in #11301, so I would not describe it as semantics to keep. How about:

Suggested change
// Typed struct casts retain their existing missing-field semantics.
// Typed outputs skip this: a missing field's null `typed_value` already
// yields NULL we can zero-copy

This branch has not been deployed

No deployments
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] variant_get returns Variant null instead of SQL NULL for missing shredded object fields

4 participants