@@ -353,6 +353,21 @@ impl UnionVariants {
353353 pub fn derived_nullability ( & self ) -> Nullability {
354354 self . variants ( ) . any ( |dtype| dtype. is_nullable ( ) ) . into ( )
355355 }
356+
357+ /// Returns a copy of these variants with `nullability` applied to every variant [`DType`].
358+ ///
359+ /// A union has no top-level nullability of its own; its nullability is derived from its
360+ /// variants (see [`Self::derived_nullability`]). Setting a union's nullability therefore means
361+ /// propagating `nullability` into each variant.
362+ pub fn with_nullability ( & self , nullability : Nullability ) -> Self {
363+ let dtypes: Vec < DType > = self
364+ . variants ( )
365+ . map ( |variant| variant. with_nullability ( nullability) )
366+ . collect ( ) ;
367+
368+ Self :: try_new ( self . names ( ) . clone ( ) , dtypes, self . type_ids ( ) . to_vec ( ) )
369+ . vortex_expect ( "changing variant nullability preserves union validity" )
370+ }
356371}
357372
358373#[ cfg( test) ]
@@ -526,16 +541,29 @@ mod tests {
526541 }
527542
528543 #[ test]
529- fn test_with_nullability_does_not_change_union_variants ( ) {
544+ fn test_with_nullability_propagates_to_variants ( ) {
530545 let nonnullable = DType :: Union ( i32_variants ( ) ) ;
531- assert_eq ! ( nonnullable. as_nullable( ) , nonnullable) ;
532546 assert_eq ! ( nonnullable. nullability( ) , Nullability :: NonNullable ) ;
533547
534- let nullable = DType :: Union (
535- UnionVariants :: new ( [ "value" ] . into ( ) , vec ! [ DType :: Utf8 ( Nullability :: Nullable ) ] ) . unwrap ( ) ,
536- ) ;
537- assert_eq ! ( nullable. as_nonnullable( ) , nullable) ;
548+ // `as_nullable` makes every variant nullable, since a union has no top-level nullability.
549+ let nullable = nonnullable. as_nullable ( ) ;
538550 assert_eq ! ( nullable. nullability( ) , Nullability :: Nullable ) ;
551+ assert_eq ! (
552+ nullable,
553+ DType :: Union (
554+ UnionVariants :: new(
555+ [ "int" , "str" ] . into( ) ,
556+ vec![
557+ DType :: Primitive ( PType :: I32 , Nullability :: Nullable ) ,
558+ DType :: Utf8 ( Nullability :: Nullable ) ,
559+ ] ,
560+ )
561+ . unwrap( )
562+ )
563+ ) ;
564+
565+ // `as_nonnullable` propagates the other direction, round-tripping back to the original.
566+ assert_eq ! ( nullable. as_nonnullable( ) , nonnullable) ;
539567 }
540568
541569 #[ test]
0 commit comments