Skip to main content

vortex_array/arrays/runend/compute/
cast.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use vortex_error::VortexResult;
5
6use crate::ArrayRef;
7use crate::ArrayView;
8use crate::IntoArray;
9use crate::arrays::runend::RunEnd;
10use crate::arrays::runend::array::RunEndArrayExt;
11use crate::arrays::runend::array::RunEndArraySlotsExt;
12use crate::builtins::ArrayBuiltins;
13use crate::dtype::DType;
14use crate::scalar_fn::fns::cast::CastReduce;
15impl CastReduce for RunEnd {
16    fn cast(array: ArrayView<'_, Self>, dtype: &DType) -> VortexResult<Option<ArrayRef>> {
17        // Cast the values array to the target type
18        let casted_values = array.values().cast(dtype.clone())?;
19
20        // SAFETY: casting does not affect the ends being valid
21        unsafe {
22            Ok(Some(
23                RunEnd::new_unchecked(
24                    array.ends().clone(),
25                    casted_values,
26                    array.offset(),
27                    array.len(),
28                )
29                .into_array(),
30            ))
31        }
32    }
33}
34
35#[cfg(test)]
36mod tests {
37    use std::sync::LazyLock;
38
39    use rstest::rstest;
40    use vortex_buffer::buffer;
41    use vortex_session::VortexSession;
42
43    use crate::IntoArray;
44    use crate::VortexSessionExecute;
45    use crate::arrays::BoolArray;
46    use crate::arrays::PrimitiveArray;
47    use crate::arrays::runend::RunEnd;
48    use crate::arrays::runend::RunEndArray;
49    use crate::assert_arrays_eq;
50    use crate::builtins::ArrayBuiltins;
51    use crate::compute::conformance::cast::test_cast_conformance;
52    use crate::dtype::DType;
53    use crate::dtype::Nullability;
54    use crate::dtype::PType;
55
56    static SESSION: LazyLock<VortexSession> = LazyLock::new(VortexSession::empty);
57
58    #[test]
59    fn test_cast_runend_i32_to_i64() {
60        let mut ctx = SESSION.create_execution_ctx();
61        let runend = RunEnd::try_new(
62            buffer![3u64, 5, 8, 10].into_array(),
63            buffer![100i32, 200, 100, 300].into_array(),
64            &mut ctx,
65        )
66        .unwrap();
67
68        let casted = runend
69            .into_array()
70            .cast(DType::Primitive(PType::I64, Nullability::NonNullable))
71            .unwrap();
72        assert_eq!(
73            casted.dtype(),
74            &DType::Primitive(PType::I64, Nullability::NonNullable)
75        );
76
77        assert_arrays_eq!(
78            casted,
79            PrimitiveArray::from_iter([100i64, 100, 100, 200, 200, 100, 100, 100, 300, 300]),
80            &mut ctx
81        );
82    }
83
84    #[test]
85    fn test_cast_runend_nullable() {
86        let mut ctx = SESSION.create_execution_ctx();
87        let runend = RunEnd::try_new(
88            buffer![2u64, 4, 7].into_array(),
89            PrimitiveArray::from_option_iter([Some(10i32), None, Some(20)]).into_array(),
90            &mut ctx,
91        )
92        .unwrap();
93
94        let casted = runend
95            .into_array()
96            .cast(DType::Primitive(PType::I64, Nullability::Nullable))
97            .unwrap();
98        assert_eq!(
99            casted.dtype(),
100            &DType::Primitive(PType::I64, Nullability::Nullable)
101        );
102        assert_arrays_eq!(
103            casted,
104            PrimitiveArray::from_option_iter([
105                Some(10i64),
106                Some(10),
107                None,
108                None,
109                Some(20),
110                Some(20),
111                Some(20)
112            ]),
113            &mut ctx
114        );
115    }
116
117    #[test]
118    fn test_cast_runend_with_offset() {
119        let mut ctx = SESSION.create_execution_ctx();
120        // Create a RunEndArray: [100, 100, 100, 200, 200, 300, 300, 300, 300, 300]
121        let runend = RunEnd::try_new(
122            buffer![3u64, 5, 10].into_array(),
123            buffer![100i32, 200, 300].into_array(),
124            &mut ctx,
125        )
126        .unwrap();
127
128        // Slice it to get offset 3, length 5: [200, 200, 300, 300, 300]
129        let sliced = runend.slice(3..8).unwrap();
130
131        // Verify the slice is correct before casting
132        assert_arrays_eq!(
133            sliced,
134            PrimitiveArray::from_iter([200, 200, 300, 300, 300]),
135            &mut ctx
136        );
137
138        // Cast the sliced array
139        let casted = sliced
140            .cast(DType::Primitive(PType::I64, Nullability::NonNullable))
141            .unwrap();
142
143        // Verify the cast preserved the offset
144        assert_arrays_eq!(
145            casted,
146            PrimitiveArray::from_iter([200i64, 200, 300, 300, 300]),
147            &mut ctx
148        );
149    }
150
151    type RunEndBuilder = fn(&mut crate::ExecutionCtx) -> RunEndArray;
152
153    #[rstest]
154    #[case(|ctx: &mut crate::ExecutionCtx| RunEnd::try_new(
155        buffer![3u64, 5, 8].into_array(),
156        buffer![100i32, 200, 300].into_array(),
157        ctx,
158    ).unwrap())]
159    #[case(|ctx: &mut crate::ExecutionCtx| RunEnd::try_new(
160        buffer![1u64, 4, 10].into_array(),
161        buffer![1.5f32, 2.5, 3.5].into_array(),
162        ctx,
163    ).unwrap())]
164    #[case(|ctx: &mut crate::ExecutionCtx| RunEnd::try_new(
165        buffer![2u64, 3, 5].into_array(),
166        PrimitiveArray::from_option_iter([Some(42i32), None, Some(84)]).into_array(),
167        ctx,
168    ).unwrap())]
169    #[case(|ctx: &mut crate::ExecutionCtx| RunEnd::try_new(
170        buffer![10u64].into_array(),
171        buffer![255u8].into_array(),
172        ctx,
173    ).unwrap())]
174    #[case(|ctx: &mut crate::ExecutionCtx| RunEnd::try_new(
175        buffer![2u64, 4, 6, 8, 10].into_array(),
176        BoolArray::from_iter(vec![true, false, true, false, true]).into_array(),
177        ctx,
178    ).unwrap())]
179    fn test_cast_runend_conformance(#[case] build: RunEndBuilder) {
180        let mut ctx = SESSION.create_execution_ctx();
181        let array = build(&mut ctx);
182        test_cast_conformance(&array.into_array(), &mut ctx);
183    }
184}