This is an automated email from the ASF dual-hosted git repository. github-merge-queue[bot] pushed a commit to branch gh-readonly-queue/main/pr-12256-ab3ae5446f0bbbb8656f576edc479d453ecca38b in repository https://gitbox.apache.org/repos/asf/arrow-rs.git
commit 2fe35e952fe4243d69970f2f69037e2cf4452fb4 Author: Timofey Brunko <[email protected]> AuthorDate: Thu Nov 19 20:19:02 2026 +0000 Fix inherited nullability in Schema::normalize (#12256) # Which issue does this PR close? None — standalone bug fix. # Rationale for this change `Schema::normalize` discards the nullability of expanded parent structs. A non-nullable child of a nullable struct therefore becomes a non-nullable flattened field, even though a null parent makes that child's normalized value null. For example: ```text Before: parent: nullable Struct<child: non-nullable Int32> Previously normalized: parent.child: non-nullable Int32 Expected: parent.child: nullable Int32 ``` This can produce a schema that rejects the corresponding data from `RecordBatch::normalize`. # What changes are included in this PR? Propagate accumulated parent nullability during schema normalization and combine it with each field's own nullability. Fields retained at the depth limit also inherit nullability from expanded ancestors. Nested fields inside retained structs and list element nullability are preserved. # Are these changes tested? Added a regression test covering all combinations of parent, intermediate struct, and child nullability, with scalar and list fields and multiple depth limits. It also checks that nullability does not leak into unrelated sibling fields. Extended the nullable-struct record batch test to verify that `Schema::normalize` produces the expected schema and that this schema accepts the normalized columns. Both regressions fail before the fix and pass afterward. Validation passed: - `cargo test -p arrow-schema -p arrow-array --all-features`. - `cargo test --workspace`. - Targeted schema and record batch regression tests under Miri. - `cargo clippy --workspace --all-targets --all-features -- -D warnings`. - Formatting and spelling checks. # Are there any significant user-facing changes? Flattened fields now correctly become nullable when an expanded ancestor is nullable, including when normalization stops at a depth limit. There are no public API signature changes. # AI assistance Codex helped implement the fix and regression tests, run validation, and draft this PR description. --- arrow-array/src/record_batch.rs | 4 +++ arrow-schema/src/schema.rs | 66 ++++++++++++++++++++++++++++++++--------- 2 files changed, 56 insertions(+), 14 deletions(-) diff --git a/arrow-array/src/record_batch.rs b/arrow-array/src/record_batch.rs index bb51fe4e5a..c830027d44 100644 --- a/arrow-array/src/record_batch.rs +++ b/arrow-array/src/record_batch.rs @@ -1847,5 +1847,9 @@ mod tests { assert!(col.is_valid(0)); assert!(col.is_null(1)); assert!(col.is_valid(2)); + + let normalized_schema = Arc::new(batch.schema().normalize(".", None).unwrap()); + assert_eq!(normalized_schema, normalized.schema()); + RecordBatch::try_new(normalized_schema, normalized.columns().to_vec()).unwrap(); } } diff --git a/arrow-schema/src/schema.rs b/arrow-schema/src/schema.rs index 0e994deb38..a3deb51ea1 100644 --- a/arrow-schema/src/schema.rs +++ b/arrow-schema/src/schema.rs @@ -519,7 +519,7 @@ impl Schema { /// Normalize a [`Schema`] into a flat table. /// /// Nested [`Field`]s will generate names separated by `separator`, up to a depth of `max_level` - /// (unlimited if `None`). + /// (unlimited if `None`). Flattened fields inherit nullability from their expanded ancestors. /// /// e.g. given a [`Schema`]: /// @@ -564,18 +564,19 @@ impl Schema { 0 => usize::MAX, val => val, }; - let mut stack: Vec<(usize, Vec<&str>, &FieldRef)> = self + let mut stack: Vec<(usize, Vec<&str>, &FieldRef, bool)> = self .fields() .iter() .rev() .map(|f| { let name_vec: Vec<&str> = vec![f.name()]; - (0, name_vec, f) + (0, name_vec, f, false) }) .collect(); let mut fields: Vec<FieldRef> = Vec::new(); - while let Some((depth, name, field_ref)) = stack.pop() { + while let Some((depth, name, field_ref, parent_nullable)) = stack.pop() { + let nullable = parent_nullable || field_ref.is_nullable(); match field_ref.data_type() { DataType::Struct(ff) if depth < max_level => { // Need to zip these in reverse to maintain original order @@ -583,15 +584,12 @@ impl Schema { let mut name = name.clone(); name.push(separator); name.push(fff.name()); - stack.push((depth + 1, name, fff)) + stack.push((depth + 1, name, fff, nullable)) } } _ => { - let updated_field = Field::new( - name.concat(), - field_ref.data_type().clone(), - field_ref.is_nullable(), - ); + let updated_field = + Field::new(name.concat(), field_ref.data_type().clone(), nullable); fields.push(Arc::new(updated_field)); } } @@ -908,6 +906,46 @@ mod tests { assert_eq!(schema, expected); } + #[test] + fn normalize_inherits_parent_nullability() { + for parent_nullable in [false, true] { + for middle_nullable in [false, true] { + for child_nullable in [false, true] { + for data_type in [DataType::Int32, DataType::new_list(DataType::Int32, false)] { + let child = Field::new("child", data_type.clone(), child_nullable); + let middle = + Field::new_struct("middle", vec![child.clone()], middle_nullable); + let sibling = Field::new("sibling", DataType::Int32, false); + let schema = Schema::new(vec![ + Field::new_struct("parent", vec![middle], parent_nullable), + sibling.clone(), + ]); + for max_level in [None, Some(0), Some(1), Some(2)] { + let expected_field = if max_level == Some(1) { + Field::new_struct( + "parent.middle", + vec![child.clone()], + parent_nullable || middle_nullable, + ) + } else { + Field::new( + "parent.middle.child", + data_type.clone(), + parent_nullable || middle_nullable || child_nullable, + ) + }; + assert_eq!( + schema.normalize(".", max_level).unwrap(), + Schema::new(vec![expected_field, sibling.clone()]), + "parent_nullable={parent_nullable}, middle_nullable={middle_nullable}, child_nullable={child_nullable}, max_level={max_level:?}, data_type={data_type:?}" + ); + } + } + } + } + } + } + #[test] fn normalize_nested() { let a = Arc::new(Field::new("a", DataType::Utf8, true)); @@ -944,7 +982,7 @@ mod tests { Field::new("!.1.b", DataType::Int64, false), Field::new("!.1.c", DataType::Int64, true), Field::new("!.2.d", DataType::Utf8, true), - Field::new("!.2.e", DataType::Int64, false), + Field::new("!.2.e", DataType::Int64, true), Field::new("!.2.f", DataType::Int64, true), ]); @@ -1000,7 +1038,7 @@ mod tests { // List shouldn't be affected let expected = Schema::new(vec