Skip to content

Commit b5ec0a0

Browse files
committed
Propagate Union nullability to variants
Signed-off-by: Connor Tsui <[email protected]>
1 parent 204dacc commit b5ec0a0

2 files changed

Lines changed: 38 additions & 9 deletions

File tree

‎vortex-array/src/dtype/dtype_impl.rs‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,9 @@ impl DType {
8282

8383
/// Get a new DType with the given nullability (but otherwise the same as `self`).
8484
///
85-
/// [`DType::Null`] and [`DType::Union`] have intrinsic nullability and are returned unchanged.
86-
/// To change a union's nullability, construct different [`UnionVariants`].
85+
/// [`DType::Null`] has intrinsic nullability and is returned unchanged. A [`DType::Union`] has
86+
/// no top-level nullability of its own — its nullability is derived from its variants — so
87+
/// `nullability` is propagated into every variant instead.
8788
pub fn with_nullability(&self, nullability: Nullability) -> Self {
8889
match self {
8990
Null => Null,
@@ -95,7 +96,7 @@ impl DType {
9596
List(edt, _) => List(Arc::clone(edt), nullability),
9697
FixedSizeList(edt, size, _) => FixedSizeList(Arc::clone(edt), *size, nullability),
9798
Struct(sf, _) => Struct(sf.clone(), nullability),
98-
Union(vs) => Union(vs.clone()),
99+
Union(vs) => Union(vs.with_nullability(nullability)),
99100
Variant(_) => Variant(nullability),
100101
Extension(ext) => Extension(ext.with_nullability(nullability)),
101102
}

‎vortex-array/src/dtype/union.rs‎

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)