|
4 | 4 | use vortex_buffer::ByteBufferMut; |
5 | 5 | use vortex_buffer::buffer; |
6 | 6 | use vortex_error::VortexResult; |
| 7 | +use vortex_error::vortex_err; |
7 | 8 | use vortex_mask::Mask; |
8 | 9 | use vortex_session::registry::ReadContext; |
9 | 10 |
|
10 | 11 | use crate::ArrayContext; |
| 12 | +use crate::Canonical; |
| 13 | +use crate::CanonicalValidity; |
11 | 14 | use crate::IntoArray; |
12 | 15 | use crate::VortexSessionExecute; |
13 | 16 | use crate::array_session; |
@@ -339,6 +342,121 @@ fn constant_union_builds_nested_union_placeholders() -> VortexResult<()> { |
339 | 342 | Ok(()) |
340 | 343 | } |
341 | 344 |
|
| 345 | +#[test] |
| 346 | +fn constant_union_builds_deeply_nested_union_placeholders() -> VortexResult<()> { |
| 347 | + let nested_dtype = DType::Union( |
| 348 | + UnionVariants::try_new( |
| 349 | + ["value"].into(), |
| 350 | + vec![DType::Primitive(PType::I64, Nullability::NonNullable)], |
| 351 | + vec![3], |
| 352 | + )?, |
| 353 | + Nullability::NonNullable, |
| 354 | + ); |
| 355 | + let wrapper_dtype = |
| 356 | + DType::struct_([("nested", nested_dtype.clone())], Nullability::NonNullable); |
| 357 | + let outer_variants = UnionVariants::try_new( |
| 358 | + ["number", "wrapper"].into(), |
| 359 | + vec![ |
| 360 | + DType::Primitive(PType::I32, Nullability::NonNullable), |
| 361 | + wrapper_dtype, |
| 362 | + ], |
| 363 | + vec![5, 9], |
| 364 | + )?; |
| 365 | + let selected_number = |
| 366 | + Scalar::union(outer_variants, 5, 42_i32.into(), Nullability::NonNullable)?; |
| 367 | + let mut ctx = array_session().create_execution_ctx(); |
| 368 | + let array = ConstantArray::new(selected_number, 2) |
| 369 | + .into_array() |
| 370 | + .execute::<UnionArray>(&mut ctx)?; |
| 371 | + let wrapper = array |
| 372 | + .child_by_name("wrapper")? |
| 373 | + .execute_scalar(0, &mut ctx)?; |
| 374 | + |
| 375 | + assert_eq!( |
| 376 | + wrapper.as_struct().field("nested"), |
| 377 | + Some(Scalar::default_value(&nested_dtype)) |
| 378 | + ); |
| 379 | + |
| 380 | + Ok(()) |
| 381 | +} |
| 382 | + |
| 383 | +#[test] |
| 384 | +fn constant_union_rejects_uninhabited_placeholders_without_panicking() -> VortexResult<()> { |
| 385 | + let uninhabited = DType::Union( |
| 386 | + UnionVariants::new(Default::default(), vec![])?, |
| 387 | + Nullability::NonNullable, |
| 388 | + ); |
| 389 | + let outer_variants = UnionVariants::try_new( |
| 390 | + ["number", "uninhabited"].into(), |
| 391 | + vec![ |
| 392 | + DType::Primitive(PType::I32, Nullability::NonNullable), |
| 393 | + uninhabited, |
| 394 | + ], |
| 395 | + vec![5, 9], |
| 396 | + )?; |
| 397 | + let selected_number = |
| 398 | + Scalar::union(outer_variants, 5, 42_i32.into(), Nullability::NonNullable)?; |
| 399 | + let mut ctx = array_session().create_execution_ctx(); |
| 400 | + |
| 401 | + assert!( |
| 402 | + ConstantArray::new(selected_number.clone(), 1) |
| 403 | + .into_array() |
| 404 | + .execute::<UnionArray>(&mut ctx) |
| 405 | + .is_err() |
| 406 | + ); |
| 407 | + assert_eq!( |
| 408 | + ConstantArray::new(selected_number, 0) |
| 409 | + .into_array() |
| 410 | + .execute::<UnionArray>(&mut ctx)? |
| 411 | + .len(), |
| 412 | + 0 |
| 413 | + ); |
| 414 | + |
| 415 | + Ok(()) |
| 416 | +} |
| 417 | + |
| 418 | +#[test] |
| 419 | +fn empty_union_supports_variant_children() -> VortexResult<()> { |
| 420 | + let variants = UnionVariants::try_new( |
| 421 | + ["dynamic"].into(), |
| 422 | + vec![DType::Variant(Nullability::NonNullable)], |
| 423 | + vec![5], |
| 424 | + )?; |
| 425 | + let array = Canonical::empty(&DType::Union(variants, Nullability::NonNullable)).into_union(); |
| 426 | + |
| 427 | + assert_eq!(array.len(), 0); |
| 428 | + assert_eq!( |
| 429 | + array.child(0).map(|child| child.dtype()), |
| 430 | + Some(&DType::Variant(Nullability::NonNullable)) |
| 431 | + ); |
| 432 | + |
| 433 | + Ok(()) |
| 434 | +} |
| 435 | + |
| 436 | +#[test] |
| 437 | +fn canonical_validity_canonicalizes_union_type_ids() -> VortexResult<()> { |
| 438 | + let array = UnionArray::try_new( |
| 439 | + ConstantArray::new(Scalar::primitive(5_u8, Nullability::Nullable), 2).into_array(), |
| 440 | + variants()?, |
| 441 | + vec![ |
| 442 | + PrimitiveArray::from_iter([10_i32, 20]).into_array(), |
| 443 | + BoolArray::from_iter([false, true]).into_array(), |
| 444 | + ], |
| 445 | + )?; |
| 446 | + let mut ctx = array_session().create_execution_ctx(); |
| 447 | + let Canonical::Union(array) = array.into_array().execute::<CanonicalValidity>(&mut ctx)?.0 |
| 448 | + else { |
| 449 | + return Err(vortex_err!( |
| 450 | + "UnionArray must remain canonical Union storage" |
| 451 | + )); |
| 452 | + }; |
| 453 | + |
| 454 | + assert!(array.type_ids().is::<crate::arrays::Primitive>()); |
| 455 | + assert_eq!(array.dtype().nullability(), Nullability::Nullable); |
| 456 | + |
| 457 | + Ok(()) |
| 458 | +} |
| 459 | + |
342 | 460 | #[test] |
343 | 461 | fn chunked_union_packs_components() -> VortexResult<()> { |
344 | 462 | let first = union_array()?.into_array().slice(0..1)?; |
|
0 commit comments