rokadepiyush49-rgb commented on issue #11284:
URL: https://github.com/apache/arrow-rs/issues/11284#issuecomment-7106428283

   Here's what I found:
   
   - **Spec:** unions have no null bitmap, so nulls go in a child 
([link](https://github.com/apache/arrow/blob/95c2fb75626e411ce20111b2139d517675084d6e/docs/source/format/Columnar.rst#L873-L875)).
 It doesn't say which child, or what to do if none is nullable.
   - **Other implementations:** C++, Go and nanoarrow always use the first 
child and ignore nullability 
([C++](https://github.com/apache/arrow/blob/95c2fb75626e411ce20111b2139d517675084d6e/cpp/src/arrow/array/util.cc#L520-L546),
 
[Go](https://github.com/apache/arrow-go/blob/f1ab6442059e31d1b59cb9daad35f3422b66093b/arrow/array/util.go#L565-L588),
 
[nanoarrow](https://github.com/apache/arrow-nanoarrow/blob/a392a6276b027669797654ff7d5a866a90ba46e3/src/nanoarrow/common/inline_array.h#L237-L269)).
 Java doesn't allow setting a null on a union at all 
([link](https://github.com/apache/arrow-java/blob/14fef469519763c63b62f51cc6ee040b7fd43fe0/vector/src/main/codegen/templates/UnionVector.java#L940-L949)).
   - **Mailing list:** the first-child habit comes from the [2020 
thread](https://lists.apache.org/thread/przo99rtpv4rp66g1h4gn0zyxdq56m27) that 
removed the union bitmap. Nullability wasn't discussed there. A [July 2026 
thread](https://lists.apache.org/thread/osgpy5b12bhyb2vpxd8brtv9zzc4cq9s) 
raised it but didn't settle it for unions.
   - **arrow-rs:** #10992 already decided kernels should error when no child 
can hold a null.
   
   Places in arrow-rs that add nulls to a union:
   
   | Where | Today | Follows #10992? |
   |---|---|---|
   | `ArrayData::new_null` | always first child | No |
   | `take` / `nullif` | first child that can hold a null, otherwise an error | 
Yes |
   | `MutableArrayData::try_extend_nulls` | first nullable child, otherwise an 
error | Yes |
   | variant to union cast | `Null` child, otherwise first child | No |
   | Avro reader | `null` branch, otherwise first branch | No |
   | `UnionBuilder` | marks all children non-nullable | No (#1637) |
   
   <details><summary>Reproduction on current main</summary>
   
   ```rust
   let fields = UnionFields::try_new(
       vec![0, 1],
       vec![
           Field::new("a", DataType::Int32, false), // non-nullable
           Field::new("b", DataType::Int32, true),  // nullable
       ],
   )
   .unwrap();
   let array = new_null_array(&DataType::Union(fields, UnionMode::Sparse), 3);
   let union = array.as_union();
   
   assert_eq!(&union.type_ids()[..], &[0, 0, 0]); // every slot points at `a`, 
not `b`
   assert_eq!(union.child(0).null_count(), 3); // nulls in a non-nullable child
   ```
   
   Dense mode gives the same result.
   </details>
   
   **Proposal:** make `new_null` pick the first child that can hold a null, 
using the same check as `take`, and panic if there isn't one. It returns 
`Self`, so it can't return an error.
   
   **Questions:**
   1. `UnionBuilder` makes every child non-nullable (#1637), so this panic 
would hit any union built with it. Should #1637 be fixed first?
   2. In sparse mode, `new_null` also fills the *other* children with nulls. 
`take` uses dummy values there instead, but `new_null` has no existing values 
to copy. Should this PR also fix that, or keep it separate?
   
   I'd handle the variant cast and Avro reader as separate follow-ups.
   
   (I used an AI assistant to help find sources, write the reproduction and 
draft this comment.)
   


-- 
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