|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// SPDX-FileCopyrightText: Copyright the Vortex contributors |
| 3 | + |
| 4 | +use vortex_dtype::{DType, NativePType}; |
| 5 | +use vortex_error::{VortexResult, vortex_bail}; |
| 6 | +use vortex_vector::null::NullVector; |
| 7 | +use vortex_vector::primitive::{PVector, PrimitiveVector}; |
| 8 | +use vortex_vector::{Vector, VectorOps, match_each_pvector}; |
| 9 | + |
| 10 | +use crate::cast::Cast; |
| 11 | + |
| 12 | +impl Cast for PrimitiveVector { |
| 13 | + fn cast(&self, dtype: &DType) -> VortexResult<Vector> { |
| 14 | + match_each_pvector!(self, |v| { Cast::cast(v, dtype) }) |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +impl<T: NativePType> Cast for PVector<T> { |
| 19 | + fn cast(&self, dtype: &DType) -> VortexResult<Vector> { |
| 20 | + match dtype { |
| 21 | + // Can cast an all-null PVector to NullVector. |
| 22 | + DType::Null if self.validity().all_false() => Ok(NullVector::new(self.len()).into()), |
| 23 | + // We're already the correct PType, and we have compatible nullability. |
| 24 | + DType::Primitive(target_ptype, n) |
| 25 | + if *target_ptype == T::PTYPE && (n.is_nullable() || self.validity().all_true()) => |
| 26 | + { |
| 27 | + Ok(self.clone().into()) |
| 28 | + } |
| 29 | + // We're not the correct PType, but we do have compatible nullability. |
| 30 | + DType::Primitive(target_ptype, n) if n.is_nullable() || self.validity().all_true() => { |
| 31 | + vortex_bail!( |
| 32 | + "Casting from PType {} to PType {} not yet implemented", |
| 33 | + T::PTYPE, |
| 34 | + target_ptype |
| 35 | + ); |
| 36 | + } |
| 37 | + DType::Extension(ext_dtype) => self.cast(ext_dtype.storage_dtype()), |
| 38 | + _ => { |
| 39 | + vortex_bail!("Cannot cast {:?} to dtype {}", self, dtype); |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | +} |
0 commit comments