@@ -19,8 +19,9 @@ use crate::dtype::FieldNames;
1919/// Type information for a union array.
2020///
2121/// A `UnionVariants` describes the possible alternative types for each row of a union, along with a
22- /// per-variant `u8` type tag. We use the term **variants** (rather than "fields") because a union
23- /// is a sum type: each row chooses exactly one alternative.
22+ /// per-variant `u8` type tag. A union must contain at least one variant. We use the term
23+ /// **variants** (rather than "fields") because a union is a sum type: each row chooses exactly one
24+ /// alternative.
2425///
2526/// By default, tag `i` selects the child at offset `i` (`type_ids = [0, 1, ..., N-1]`). Vortex uses
2627/// unsigned tags and supports up to 256 variants.
@@ -164,22 +165,7 @@ impl UnionVariants {
164165 }
165166}
166167
167- impl Default for UnionVariants {
168- fn default ( ) -> Self {
169- Self :: empty ( )
170- }
171- }
172-
173168impl UnionVariants {
174- /// The variants of the empty union.
175- pub fn empty ( ) -> Self {
176- Self ( Arc :: new ( UnionVariantsInner :: from_fields (
177- FieldNames :: default ( ) ,
178- Arc :: from ( [ ] ) ,
179- Arc :: from ( [ ] ) ,
180- ) ) )
181- }
182-
183169 /// Validate that `names`, `dtypes`, and `type_ids` are mutually consistent.
184170 fn validate_shape ( names : & FieldNames , n_dtypes : usize , type_ids : & [ u8 ] ) -> VortexResult < ( ) > {
185171 vortex_ensure_eq ! (
@@ -196,6 +182,10 @@ impl UnionVariants {
196182 names. len( ) ,
197183 type_ids. len( )
198184 ) ;
185+ vortex_ensure ! (
186+ !names. is_empty( ) ,
187+ "union must have at least one variant (for now)"
188+ ) ;
199189
200190 vortex_ensure ! (
201191 type_ids. iter( ) . all_unique( ) ,
@@ -221,8 +211,9 @@ impl UnionVariants {
221211 ///
222212 /// # Errors
223213 ///
224- /// Returns an error if names, dtypes, or type IDs do not all have the same length, or if there
225- /// are any duplicate names or type IDs, or if there are more than 256 variants.
214+ /// Returns an error if the union has no variants, if names, dtypes, or type IDs do not all have
215+ /// the same length, if there are any duplicate names or type IDs, or if there are more than 256
216+ /// variants.
226217 pub fn try_new ( names : FieldNames , dtypes : Vec < DType > , type_ids : Vec < u8 > ) -> VortexResult < Self > {
227218 Self :: validate_shape ( & names, dtypes. len ( ) , & type_ids) ?;
228219
@@ -238,8 +229,8 @@ impl UnionVariants {
238229 ///
239230 /// # Errors
240231 ///
241- /// `names` and `dtypes` must have the same length, and `names.len()` cannot be more than
242- /// `u8::MAX as usize + 1` (256).
232+ /// The union must have at least one variant, `names` and `dtypes` must have the same length, and
233+ /// `names.len()` cannot be more than ` u8::MAX as usize + 1` (256).
243234 pub fn new ( names : FieldNames , dtypes : Vec < DType > ) -> VortexResult < Self > {
244235 const MAX_VARIANTS : usize = u8:: MAX as usize + 1 ;
245236 vortex_ensure ! (
@@ -265,8 +256,9 @@ impl UnionVariants {
265256 ///
266257 /// # Errors
267258 ///
268- /// Returns an error if names, dtypes, or type IDs do not all have the same length, or if there
269- /// are any duplicate names or type IDs, or if there are more than 256 variants.
259+ /// Returns an error if the union has no variants, if names, dtypes, or type IDs do not all have
260+ /// the same length, if there are any duplicate names or type IDs, or if there are more than 256
261+ /// variants.
270262 pub ( crate ) fn try_from_fields (
271263 names : FieldNames ,
272264 dtypes : Vec < FieldDType > ,
@@ -330,7 +322,7 @@ impl UnionVariants {
330322 )
331323 }
332324
333- /// Get the [`DType`] of a variant by offset.
325+ /// Get the [`DType`] of a variant by offset, or [`None`] if `index` is out of bounds .
334326 pub fn variant_by_index ( & self , index : usize ) -> Option < DType > {
335327 Some (
336328 self . 0
@@ -600,11 +592,19 @@ mod tests {
600592 }
601593
602594 #[ test]
603- fn test_empty ( ) {
604- let v = UnionVariants :: empty ( ) ;
605- assert ! ( v. is_empty( ) ) ;
606- assert_eq ! ( v. len( ) , 0 ) ;
607- assert_eq ! ( v. type_ids( ) , & [ ] as & [ u8 ] ) ;
608- assert ! ( v. is_consecutive( ) ) ;
595+ fn test_empty_rejected ( ) {
596+ let error = UnionVariants :: new ( FieldNames :: default ( ) , vec ! [ ] ) . unwrap_err ( ) ;
597+ assert ! (
598+ error
599+ . to_string( )
600+ . contains( "union must have at least one variant" )
601+ ) ;
602+
603+ let error = UnionVariants :: try_new ( FieldNames :: default ( ) , vec ! [ ] , vec ! [ ] ) . unwrap_err ( ) ;
604+ assert ! (
605+ error
606+ . to_string( )
607+ . contains( "union must have at least one variant" )
608+ ) ;
609609 }
610610}
0 commit comments