vortex_compute/cast/
null.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use vortex_dtype::DType;
5use vortex_error::VortexResult;
6use vortex_error::vortex_bail;
7use vortex_vector::Scalar;
8use vortex_vector::Vector;
9use vortex_vector::VectorMut;
10use vortex_vector::VectorMutOps;
11use vortex_vector::VectorOps;
12use vortex_vector::null::NullScalar;
13use vortex_vector::null::NullVector;
14
15use crate::cast::Cast;
16
17impl Cast for NullVector {
18    type Output = Vector;
19
20    /// Casts to any nullable target type by creating an all-null vector.
21    fn cast(&self, target_dtype: &DType) -> VortexResult<Vector> {
22        if target_dtype.is_nullable() {
23            // We can create an all-null vector of _any_ type.
24            let mut vec = VectorMut::with_capacity(target_dtype, self.len());
25            vec.append_nulls(self.len());
26            Ok(vec.freeze())
27        } else {
28            vortex_bail!(
29                "Cannot cast NullVector to non-nullable type {}",
30                target_dtype
31            );
32        }
33    }
34}
35
36impl Cast for NullScalar {
37    type Output = Scalar;
38
39    /// Casts to any nullable target type by creating a null scalar.
40    fn cast(&self, target_dtype: &DType) -> VortexResult<Scalar> {
41        if target_dtype.is_nullable() {
42            Ok(Scalar::null(target_dtype))
43        } else {
44            vortex_bail!(
45                "Cannot cast NullScalar to non-nullable type {}",
46                target_dtype
47            );
48        }
49    }
50}