vortex_array/arrays/null/compute/
cast.rs1use vortex_dtype::DType;
5use vortex_error::VortexResult;
6use vortex_error::vortex_bail;
7
8use crate::ArrayRef;
9use crate::IntoArray;
10use crate::arrays::ConstantArray;
11use crate::arrays::NullArray;
12use crate::arrays::NullVTable;
13use crate::compute::CastReduce;
14use crate::scalar::Scalar;
15
16impl CastReduce for NullVTable {
17 fn cast(array: &NullArray, dtype: &DType) -> VortexResult<Option<ArrayRef>> {
18 if !dtype.is_nullable() {
19 vortex_bail!("Cannot cast Null to {}", dtype);
20 }
21 if dtype == &DType::Null {
22 return Ok(Some(array.to_array()));
23 }
24
25 let scalar = Scalar::null(dtype.clone());
26 Ok(Some(ConstantArray::new(scalar, array.len()).into_array()))
27 }
28}
29
30#[cfg(test)]
31mod tests {
32 use rstest::rstest;
33 use vortex_dtype::DType;
34 use vortex_dtype::Nullability;
35 use vortex_dtype::PType;
36
37 use crate::arrays::NullArray;
38 use crate::builtins::ArrayBuiltins;
39 use crate::compute::conformance::cast::test_cast_conformance;
40
41 #[test]
42 fn test_cast_null_to_null() {
43 let null_array = NullArray::new(5);
44 let result = null_array.to_array().cast(DType::Null).unwrap();
45 assert_eq!(result.len(), 5);
46 assert_eq!(result.dtype(), &DType::Null);
47 }
48
49 #[test]
50 fn test_cast_null_to_nullable_succeeds() {
51 let null_array = NullArray::new(5);
52 let result = null_array
53 .to_array()
54 .cast(DType::Primitive(PType::I32, Nullability::Nullable))
55 .unwrap();
56
57 assert_eq!(result.len(), 5);
59 assert_eq!(
60 result.dtype(),
61 &DType::Primitive(PType::I32, Nullability::Nullable)
62 );
63
64 for i in 0..5 {
66 assert!(result.scalar_at(i).unwrap().is_null());
67 }
68 }
69
70 #[test]
71 fn test_cast_null_to_non_nullable_fails() {
72 let null_array = NullArray::new(5);
73 let result = null_array
74 .to_array()
75 .cast(DType::Primitive(PType::I32, Nullability::NonNullable));
76 assert!(result.is_err());
77 }
78
79 #[rstest]
80 #[case(NullArray::new(5))]
81 #[case(NullArray::new(1))]
82 #[case(NullArray::new(100))]
83 #[case(NullArray::new(0))]
84 fn test_cast_null_conformance(#[case] array: NullArray) {
85 test_cast_conformance(array.as_ref());
86 }
87}