diff --git a/vortex/src/array/primitive/compute/cast.rs b/vortex/src/array/primitive/compute/cast.rs deleted file mode 100644 index 04631ccfa1..0000000000 --- a/vortex/src/array/primitive/compute/cast.rs +++ /dev/null @@ -1,76 +0,0 @@ -// (c) Copyright 2024 Fulcrum Technologies, Inc. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -use crate::array::primitive::PrimitiveArray; -use crate::array::CloneOptionalArray; -use crate::compute::cast::CastPrimitiveFn; -use crate::error::{VortexError, VortexResult}; -use crate::match_each_native_ptype; -use crate::ptype::{NativePType, PType}; - -impl CastPrimitiveFn for PrimitiveArray { - fn cast_primitive(&self, ptype: &PType) -> VortexResult { - if self.ptype() == ptype { - Ok(self.clone()) - } else { - match_each_native_ptype!(ptype, |$T| { - Ok(PrimitiveArray::from_nullable( - cast::<$T>(self)?, - self.validity().clone_optional(), - )) - }) - } - } -} - -fn cast(array: &PrimitiveArray) -> VortexResult> { - array - .typed_data::() - .iter() - // TODO(ngates): allow configurable checked/unchecked casting - .map(|v| { - T::from(*v).ok_or_else(|| { - VortexError::ComputeError(format!("Failed to cast {} to {:?}", v, T::PTYPE).into()) - }) - }) - .collect() -} - -#[cfg(test)] -mod test { - use crate::array::primitive::PrimitiveArray; - use crate::compute; - use crate::ptype::PType; - - #[test] - fn cast_u32_u8() { - let arr = PrimitiveArray::from_vec(vec![0u32, 10, 200]); - let u8arr = compute::cast::cast_primitive(&arr, &PType::U8).unwrap(); - assert_eq!(u8arr.typed_data::(), vec![0u8, 10, 200]); - } - - #[test] - fn cast_u32_f32() { - let arr = PrimitiveArray::from_vec(vec![0u32, 10, 200]); - let u8arr = compute::cast::cast_primitive(&arr, &PType::F32).unwrap(); - assert_eq!(u8arr.typed_data::(), vec![0.0f32, 10., 200.]); - } - - #[test] - fn cast_i32_u32() { - let arr = PrimitiveArray::from_vec(vec![-1i32]); - let u8arr = compute::cast::cast_primitive(&arr, &PType::U32).unwrap(); - assert_eq!(u8arr.typed_data::(), vec![0.0f32, 10., 200.]); - } -} diff --git a/vortex/src/array/primitive/compute/mod.rs b/vortex/src/array/primitive/compute/mod.rs deleted file mode 100644 index 96ef0251b1..0000000000 --- a/vortex/src/array/primitive/compute/mod.rs +++ /dev/null @@ -1,31 +0,0 @@ -// (c) Copyright 2024 Fulcrum Technologies, Inc. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -use crate::array::primitive::PrimitiveArray; -use crate::compute::cast::CastPrimitiveFn; -use crate::compute::patch::PatchFn; -use crate::compute::ArrayCompute; - -mod cast; -mod patch; - -impl ArrayCompute for PrimitiveArray { - fn cast_primitive(&self) -> Option<&dyn CastPrimitiveFn> { - Some(self) - } - - fn patch(&self) -> Option<&dyn PatchFn> { - Some(self) - } -} diff --git a/vortex/src/array/primitive/compute/patch.rs b/vortex/src/array/primitive/compute/patch.rs deleted file mode 100644 index 74a055d5f3..0000000000 --- a/vortex/src/array/primitive/compute/patch.rs +++ /dev/null @@ -1,53 +0,0 @@ -// (c) Copyright 2024 Fulcrum Technologies, Inc. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -use itertools::Itertools; - -use crate::array::downcast::DowncastArrayBuiltin; -use crate::array::primitive::PrimitiveArray; -use crate::array::sparse::{SparseArray, SparseEncoding}; -use crate::array::{Array, ArrayRef, CloneOptionalArray}; -use crate::compute::patch::PatchFn; -use crate::error::{VortexError, VortexResult}; -use crate::{compute, match_each_native_ptype}; - -impl PatchFn for PrimitiveArray { - fn patch(&self, patch: &dyn Array) -> VortexResult { - match patch.encoding().id() { - &SparseEncoding::ID => patch_with_sparse(self, patch.as_sparse()), - // TODO(ngates): support a default implementation based on iter_arrow? - _ => Err(VortexError::MissingKernel( - "patch", - self.encoding().id(), - vec![patch.encoding().id()], - )), - } - } -} - -fn patch_with_sparse(array: &PrimitiveArray, patch: &SparseArray) -> VortexResult { - let patch_indices = patch.resolved_indices(); - match_each_native_ptype!(array.ptype(), |$T| { - let mut values = Vec::from(array.typed_data::<$T>()); - let patch_values = compute::cast::cast_primitive(patch.values(), array.ptype())?; - for (idx, value) in patch_indices.iter().zip_eq(patch_values.typed_data::<$T>().iter()) { - values[*idx] = *value; - } - Ok(PrimitiveArray::from_nullable( - values, - // TODO(ngates): if patch values has null, we need to patch into the validity buffer - array.validity().clone_optional(), - ).boxed()) - }) -}