vinaykpud opened a new issue, #12544:
URL: https://github.com/apache/arrow-rs/issues/12544

   ## Describe the bug
   
   `FlightDataEncoderBuilder` changes union fields while preparing its wire 
schema:
   it drops custom field metadata and changes `nullable = true` to `false`.
   
   For a nullable union child of a struct, this causes a valid batch containing
   a selected null to fail the Flight round trip:
   
   ```text
   Found unmasked nulls for non-nullable StructArray field "value"
   ```
   
   The same batch round-trips through an ordinary Arrow IPC stream. Both
   `DictionaryHandling::Hydrate` and `DictionaryHandling::Resend` are affected,
   even when the input contains no dictionaries.
   
   ## To Reproduce
   
   Reproduces on the arrow-rs `main` branch.
   
   Create a standalone Cargo project with this `Cargo.toml`:
   
   ```toml
   [package]
   name = "flight-union-repro"
   version = "0.1.0"
   edition = "2024"
   
   [dependencies]
   arrow-array = { git = "https://github.com/apache/arrow-rs";, branch = "main" }
   arrow-buffer = { git = "https://github.com/apache/arrow-rs";, branch = "main" 
}
   arrow-flight = { git = "https://github.com/apache/arrow-rs";, branch = "main" 
}
   arrow-schema = { git = "https://github.com/apache/arrow-rs";, branch = "main" 
}
   futures = "0.3"
   ```
   
   Put this in `src/main.rs`, then run `cargo run`:
   
   ```rust
   use std::sync::Arc;
   
   use arrow_array::{ArrayRef, BooleanArray, RecordBatch, StructArray, 
UnionArray};
   use arrow_buffer::ScalarBuffer;
   use arrow_flight::decode::FlightRecordBatchStream;
   use arrow_flight::encode::{DictionaryHandling, FlightDataEncoderBuilder};
   use arrow_schema::{DataType, Field, Schema, UnionFields};
   use futures::TryStreamExt;
   
   fn main() {
       futures::executor::block_on(async {
           let values: ArrayRef = Arc::new(
               UnionArray::try_new(
                   UnionFields::try_new([0], [Field::new("bool", 
DataType::Boolean, true)]).unwrap(),
                   ScalarBuffer::from(vec![0_i8, 0]),
                   Some(ScalarBuffer::from(vec![0_i32, 1])),
                   vec![Arc::new(BooleanArray::from(vec![Some(true), None]))],
               )
               .unwrap(),
           );
           let child = Field::new("value", values.data_type().clone(), true);
           let objects: ArrayRef = 
Arc::new(StructArray::new(vec![child].into(), vec![values], None));
           let schema = Arc::new(Schema::new(vec![Field::new(
               "object",
               objects.data_type().clone(),
               true,
           )]));
           let batch = RecordBatch::try_new(schema, vec![objects]).unwrap();
           let encoded = FlightDataEncoderBuilder::new()
               .with_dictionary_handling(DictionaryHandling::Resend)
               .with_schema(batch.schema())
               .build(futures::stream::iter(vec![Ok(batch.clone())]));
           let decoded = FlightRecordBatchStream::new_from_flight_data(encoded)
               .try_collect::<Vec<_>>()
               .await
               .expect("Flight should preserve the nullable union child");
           assert_eq!(decoded[0].schema(), batch.schema());
           assert_eq!(decoded[0], batch);
       });
   }
   ```
   
   ## Expected behavior
   
   The Flight round trip preserves the batch's schema, field metadata, and 
values,
   including a logical null represented by a null in the selected union child.
   
   ## Additional context
   
   ### Actual behavior
   
   The example fails decoding the record batch because its union child field has
   become non-nullable.
   
   Separate round-trip checks also reproduce:
   
   - Metadata loss on a non-nullable union field containing no null values.
   - Nullability loss on a top-level union field.
   - Nullability loss on a list's union item field. Hydrate mode rejects the 
null
     items; Resend mode decodes a list with a different item field type.
   
   ### Root cause and suggested fix
   
   In the union arm of
   
[`prepare_field_for_flight`](https://github.com/apache/arrow-rs/blob/50b25a98ff329266089722d156aedfb373b9abed/arrow-flight/src/encode.rs#L522-L534),
   the field is rebuilt with `Field::new_union`. That constructor sets 
nullability
   to false and does not carry the original field metadata. The list and struct
   arms explicitly preserve those properties.
   
   Preserving both properties after rebuilding the union field fixes the
   reproductions:
   
   ```rust
   Field::new_union(field.name(), type_ids, new_fields, *mode)
       .with_nullable(field.is_nullable())
       .with_metadata(field.metadata().clone())
   ```
   
   The regression suite has four Flight cases, each checked with both dictionary
   handling modes, plus an ordinary IPC control over all four input batches.
   Before the patch: **1 passed, 4 failed**. With these two lines:
   **5 passed, 0 failed**.
   
   ### Related issues
   
   [Issue #10291](https://github.com/apache/arrow-rs/issues/10291), fixed by
   [PR #10298](https://github.com/apache/arrow-rs/pull/10298), concerns the same
   helper rebuilding `LargeList` as `List`. It addresses a different match arm.
   
   ### AI assistance
   
   Codex helped investigate the bug and prepare the reproducer and report.
   
   ## Willingness to contribute
   
   I plan to contribute a fix for this bug independently.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to