vortex_array/arrays/chunked/compute/
cast.rs

1use vortex_dtype::DType;
2use vortex_error::VortexResult;
3
4use crate::arrays::{ChunkedArray, ChunkedVTable};
5use crate::compute::{CastKernel, CastKernelAdapter, cast};
6use crate::{ArrayRef, IntoArray, register_kernel};
7
8impl CastKernel for ChunkedVTable {
9    fn cast(&self, array: &ChunkedArray, dtype: &DType) -> VortexResult<ArrayRef> {
10        let mut cast_chunks = Vec::new();
11        for chunk in array.chunks() {
12            cast_chunks.push(cast(chunk, dtype)?);
13        }
14
15        Ok(ChunkedArray::new_unchecked(cast_chunks, dtype.clone()).into_array())
16    }
17}
18
19register_kernel!(CastKernelAdapter(ChunkedVTable).lift());
20
21#[cfg(test)]
22mod test {
23    use vortex_buffer::buffer;
24    use vortex_dtype::{DType, Nullability, PType};
25
26    use crate::IntoArray;
27    use crate::arrays::chunked::ChunkedArray;
28    use crate::canonical::ToCanonical;
29    use crate::compute::cast;
30
31    #[test]
32    fn test_cast_chunked() {
33        let arr0 = buffer![0u32, 1].into_array();
34        let arr1 = buffer![2u32, 3].into_array();
35
36        let chunked = ChunkedArray::try_new(
37            vec![arr0, arr1],
38            DType::Primitive(PType::U32, Nullability::NonNullable),
39        )
40        .unwrap()
41        .into_array();
42
43        // Two levels of chunking, just to be fancy.
44        let root = ChunkedArray::try_new(
45            vec![chunked],
46            DType::Primitive(PType::U32, Nullability::NonNullable),
47        )
48        .unwrap()
49        .into_array();
50
51        assert_eq!(
52            cast(
53                &root,
54                &DType::Primitive(PType::U64, Nullability::NonNullable)
55            )
56            .unwrap()
57            .to_primitive()
58            .unwrap()
59            .as_slice::<u64>(),
60            &[0u64, 1, 2, 3],
61        );
62    }
63}