Skip to main content

vortex_array/arrays/dict/compute/
slice.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use std::ops::Range;
5
6use vortex_error::VortexExpect;
7use vortex_error::VortexResult;
8
9use crate::ArrayRef;
10use crate::IntoArray;
11use crate::array::ArrayView;
12use crate::arrays::Constant;
13use crate::arrays::ConstantArray;
14use crate::arrays::Dict;
15use crate::arrays::DictArray;
16use crate::arrays::Primitive;
17use crate::arrays::dict::DictArraySlotsExt;
18use crate::arrays::slice::SliceReduce;
19use crate::expr::stats::Precision;
20use crate::expr::stats::Stat;
21use crate::scalar::Scalar;
22use crate::scalar::ScalarValue;
23
24impl SliceReduce for Dict {
25    fn slice(array: ArrayView<'_, Self>, range: Range<usize>) -> VortexResult<Option<ArrayRef>> {
26        if let Some(code) = array.codes().as_opt::<Constant>() {
27            return slice_constant_code(array, code.scalar(), range.len());
28        }
29
30        let sliced_code = if let Some(codes) = array.codes().as_typed::<Primitive>() {
31            let sliced_code = <Primitive as SliceReduce>::slice(codes, range)?
32                .vortex_expect("Primitive SliceReduce should always return Some");
33            // Because we specialize the primitive branch here, we have to make sure to handle the stat inheritance
34            inherit_selection_stats(array.codes(), &sliced_code);
35            sliced_code
36        } else {
37            array.codes().slice(range)?
38        };
39
40        // TODO(joe): if the range is size 1 replace with a constant array
41        if let Some(code) = sliced_code.as_opt::<Constant>() {
42            return slice_constant_code(array, code.scalar(), sliced_code.len());
43        }
44        // SAFETY: slicing the codes preserves invariants.
45        let array =
46            unsafe { DictArray::new_unchecked(sliced_code, array.values().clone()).into_array() };
47
48        Ok(Some(array))
49    }
50}
51
52/// Copies the stats of `source` that hold for any order-preserving selection of its rows, such as a
53/// slice or a filter.
54pub(super) fn inherit_selection_stats(source: &ArrayRef, selected: &ArrayRef) {
55    source.statistics().with_iter(|iter| {
56        selected
57            .statistics()
58            .inherit(iter.filter(|(stat, value)| is_inheritable_selection_stat(*stat, value)));
59    });
60}
61
62fn is_inheritable_selection_stat(stat: Stat, value: &Precision<ScalarValue>) -> bool {
63    matches!(
64        stat,
65        Stat::IsConstant | Stat::IsSorted | Stat::IsStrictSorted
66    ) && value
67        .as_ref()
68        .as_exact()
69        .is_some_and(|value| matches!(value, ScalarValue::Bool(true)))
70}
71
72pub(super) fn slice_constant_code(
73    array: ArrayView<'_, Dict>,
74    code: &Scalar,
75    len: usize,
76) -> VortexResult<Option<ArrayRef>> {
77    let code = code.as_primitive().as_::<usize>();
78    if let Some(code) = code {
79        let values = array.values().slice(code..code + 1)?;
80        // Keep the codes' nullability so the result's dtype matches the original dict's dtype.
81        let codes = ConstantArray::new(
82            Scalar::primitive(0u8, array.codes().dtype().nullability()),
83            len,
84        );
85        Ok(Some(
86            DictArray::new(codes.into_array(), values).into_array(),
87        ))
88    } else {
89        Ok(Some(
90            ConstantArray::new(Scalar::null(array.dtype().clone()), len).into_array(),
91        ))
92    }
93}
94
95#[cfg(test)]
96mod tests {
97    use std::sync::LazyLock;
98
99    use vortex_buffer::buffer;
100    use vortex_error::VortexResult;
101    use vortex_session::VortexSession;
102
103    use crate::IntoArray;
104    use crate::VortexSessionExecute;
105    use crate::arrays::DictArray;
106    use crate::arrays::PrimitiveArray;
107    use crate::arrays::dict::compute::slice::ConstantArray;
108    use crate::assert_arrays_eq;
109    use crate::dtype::DType;
110    use crate::dtype::Nullability::Nullable;
111    use crate::dtype::PType;
112    use crate::scalar::Scalar;
113
114    static SESSION: LazyLock<VortexSession> = LazyLock::new(VortexSession::empty);
115
116    #[test]
117    fn slice_constant_valid_code() -> VortexResult<()> {
118        let mut ctx = SESSION.create_execution_ctx();
119        let dict = DictArray::new(
120            ConstantArray::new(1u8, 5).into_array(),
121            buffer![10i32, 20, 30].into_array(),
122        );
123        let sliced = dict.slice(1..4)?;
124        let expected = PrimitiveArray::from_iter([20i32, 20, 20]).into_array();
125        assert_arrays_eq!(sliced, expected, &mut ctx);
126        Ok(())
127    }
128
129    #[test]
130    fn slice_nullable_constant_valid_code() -> VortexResult<()> {
131        let mut ctx = SESSION.create_execution_ctx();
132        let dict = DictArray::new(
133            ConstantArray::new(Scalar::primitive(1u8, Nullable), 5).into_array(),
134            buffer![10i32, 20, 30].into_array(),
135        );
136        let sliced = dict.slice(1..4)?;
137        let expected = PrimitiveArray::from_option_iter([Some(20i32), Some(20), Some(20)]);
138        assert_arrays_eq!(sliced, expected.into_array(), &mut ctx);
139        Ok(())
140    }
141
142    #[test]
143    fn slice_constant_null_code() -> VortexResult<()> {
144        let mut ctx = SESSION.create_execution_ctx();
145        let dict = DictArray::new(
146            ConstantArray::new(Scalar::null(DType::Primitive(PType::U8, Nullable)), 5).into_array(),
147            buffer![10i32, 20, 30].into_array(),
148        );
149        let sliced = dict.slice(1..4)?;
150        let expected =
151            PrimitiveArray::from_option_iter([Option::<i32>::None, None, None]).into_array();
152        assert_arrays_eq!(sliced, expected, &mut ctx);
153        Ok(())
154    }
155}