vortex_compute/cast/
list.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::ScalarOps;
9use vortex_vector::Vector;
10use vortex_vector::VectorOps;
11use vortex_vector::listview::ListViewScalar;
12use vortex_vector::listview::ListViewVector;
13use vortex_vector::vector_matches_dtype;
14
15use crate::cast::Cast;
16use crate::cast::try_cast_scalar_common;
17use crate::cast::try_cast_vector_common;
18
19impl Cast for ListViewVector {
20    type Output = Vector;
21
22    /// Casts to List (identity with same element dtype and compatible nullability).
23    fn cast(&self, target_dtype: &DType) -> VortexResult<Vector> {
24        if let Some(result) = try_cast_vector_common(self, target_dtype)? {
25            return Ok(result);
26        }
27
28        match target_dtype {
29            // Identity cast: same element dtype and compatible nullability.
30            DType::List(element_dtype, n)
31                if vector_matches_dtype(self.elements(), element_dtype)
32                    && (n.is_nullable() || self.validity().all_true()) =>
33            {
34                Ok(self.clone().into())
35            }
36            DType::List(..) => {
37                vortex_bail!(
38                    "Cannot cast ListViewVector to {} (incompatible element type or nullability)",
39                    target_dtype
40                );
41            }
42            _ => {
43                vortex_bail!("Cannot cast ListViewVector to {}", target_dtype);
44            }
45        }
46    }
47}
48
49impl Cast for ListViewScalar {
50    type Output = Scalar;
51
52    /// Casts to List (identity with same element dtype and compatible nullability).
53    fn cast(&self, target_dtype: &DType) -> VortexResult<Scalar> {
54        if let Some(result) = try_cast_scalar_common(self, target_dtype)? {
55            return Ok(result);
56        }
57
58        match target_dtype {
59            // Identity cast: same element dtype and compatible nullability.
60            DType::List(element_dtype, n)
61                if vector_matches_dtype(self.value().elements(), element_dtype)
62                    && (n.is_nullable() || self.is_valid()) =>
63            {
64                Ok(self.clone().into())
65            }
66            DType::List(..) => {
67                vortex_bail!(
68                    "Cannot cast ListViewScalar to {} (incompatible element type or nullability)",
69                    target_dtype
70                );
71            }
72            _ => {
73                vortex_bail!("Cannot cast ListViewScalar to {}", target_dtype);
74            }
75        }
76    }
77}