Skip to main content

vortex_array/arrays/listview/vtable/
mod.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use std::hash::Hash;
5use std::hash::Hasher;
6use std::sync::Arc;
7
8use prost::Message;
9use vortex_error::VortexExpect;
10use vortex_error::VortexResult;
11use vortex_error::vortex_bail;
12use vortex_error::vortex_ensure;
13use vortex_error::vortex_panic;
14use vortex_session::VortexSession;
15use vortex_session::registry::CachedId;
16
17use crate::ArrayEq;
18use crate::ArrayHash;
19use crate::ArrayParts;
20use crate::ArrayRef;
21use crate::EqMode;
22use crate::ExecutionCtx;
23use crate::ExecutionResult;
24use crate::array::Array;
25use crate::array::ArrayId;
26use crate::array::ArrayView;
27use crate::array::VTable;
28use crate::array::with_empty_buffers;
29use crate::arrays::listview::ListViewArrayExt;
30use crate::arrays::listview::ListViewData;
31use crate::arrays::listview::array::ELEMENTS_SLOT;
32use crate::arrays::listview::array::NUM_SLOTS;
33use crate::arrays::listview::array::OFFSETS_SLOT;
34use crate::arrays::listview::array::SIZES_SLOT;
35use crate::arrays::listview::array::SLOT_NAMES;
36use crate::arrays::listview::compute::rules::PARENT_RULES;
37use crate::buffer::BufferHandle;
38use crate::dtype::DType;
39use crate::dtype::Nullability;
40use crate::dtype::PType;
41use crate::serde::ArrayChildren;
42use crate::validity::Validity;
43mod kernel;
44mod operations;
45mod validity;
46/// A [`ListView`]-encoded Vortex array.
47pub type ListViewArray = Array<ListView>;
48
49pub(crate) fn initialize(session: &VortexSession) {
50    kernel::initialize(session);
51}
52
53#[derive(Clone, Debug)]
54pub struct ListView;
55
56#[derive(Clone, prost::Message)]
57pub struct ListViewMetadata {
58    #[prost(uint64, tag = "1")]
59    elements_len: u64,
60    #[prost(enumeration = "PType", tag = "2")]
61    offset_ptype: i32,
62    #[prost(enumeration = "PType", tag = "3")]
63    size_ptype: i32,
64}
65
66impl ArrayHash for ListViewData {
67    fn array_hash<H: Hasher>(&self, state: &mut H, _accuracy: EqMode) {
68        self.is_zero_copy_to_list().hash(state);
69    }
70}
71
72impl ArrayEq for ListViewData {
73    fn array_eq(&self, other: &Self, _accuracy: EqMode) -> bool {
74        self.is_zero_copy_to_list() == other.is_zero_copy_to_list()
75    }
76}
77
78impl VTable for ListView {
79    type TypedArrayData = ListViewData;
80
81    type OperationsVTable = Self;
82    type ValidityVTable = Self;
83    fn id(&self) -> ArrayId {
84        static ID: CachedId = CachedId::new("vortex.listview");
85        *ID
86    }
87
88    fn nbuffers(_array: ArrayView<'_, Self>) -> usize {
89        0
90    }
91
92    fn buffer(_array: ArrayView<'_, Self>, idx: usize) -> BufferHandle {
93        vortex_panic!("ListViewArray buffer index {idx} out of bounds")
94    }
95
96    fn buffer_name(_array: ArrayView<'_, Self>, idx: usize) -> Option<String> {
97        vortex_panic!("ListViewArray buffer_name index {idx} out of bounds")
98    }
99
100    fn with_buffers(
101        &self,
102        array: ArrayView<'_, Self>,
103        buffers: &[BufferHandle],
104    ) -> VortexResult<ArrayParts<Self>> {
105        with_empty_buffers(self, array, buffers)
106    }
107
108    fn serialize(
109        array: ArrayView<'_, Self>,
110        _session: &VortexSession,
111    ) -> VortexResult<Option<Vec<u8>>> {
112        Ok(Some(
113            ListViewMetadata {
114                elements_len: array.elements().len() as u64,
115                offset_ptype: PType::try_from(array.offsets().dtype())? as i32,
116                size_ptype: PType::try_from(array.sizes().dtype())? as i32,
117            }
118            .encode_to_vec(),
119        ))
120    }
121
122    fn validate(
123        &self,
124        _data: &ListViewData,
125        dtype: &DType,
126        len: usize,
127        slots: &[Option<ArrayRef>],
128    ) -> VortexResult<()> {
129        vortex_ensure!(
130            slots.len() == NUM_SLOTS,
131            "ListViewArray expected {NUM_SLOTS} slots, found {}",
132            slots.len()
133        );
134        let elements = slots[ELEMENTS_SLOT]
135            .as_ref()
136            .vortex_expect("ListViewArray elements slot");
137        let offsets = slots[OFFSETS_SLOT]
138            .as_ref()
139            .vortex_expect("ListViewArray offsets slot");
140        let sizes = slots[SIZES_SLOT]
141            .as_ref()
142            .vortex_expect("ListViewArray sizes slot");
143        vortex_ensure!(
144            offsets.len() == len && sizes.len() == len,
145            "ListViewArray length {} does not match outer length {}",
146            offsets.len(),
147            len
148        );
149
150        let actual_dtype = DType::List(Arc::new(elements.dtype().clone()), dtype.nullability());
151        vortex_ensure!(
152            &actual_dtype == dtype,
153            "ListViewArray dtype {} does not match outer dtype {}",
154            actual_dtype,
155            dtype
156        );
157
158        Ok(())
159    }
160
161    fn deserialize(
162        &self,
163        dtype: &DType,
164        len: usize,
165        metadata: &[u8],
166
167        buffers: &[BufferHandle],
168        children: &dyn ArrayChildren,
169        _session: &VortexSession,
170    ) -> VortexResult<ArrayParts<Self>> {
171        let metadata = ListViewMetadata::decode(metadata)?;
172        vortex_ensure!(
173            buffers.is_empty(),
174            "`ListViewArray::build` expects no buffers"
175        );
176
177        let DType::List(element_dtype, _) = dtype else {
178            vortex_bail!("Expected List dtype, got {:?}", dtype);
179        };
180
181        let validity = if children.len() == 3 {
182            Validity::from(dtype.nullability())
183        } else if children.len() == 4 {
184            let validity = children.get(3, &Validity::DTYPE, len)?;
185            Validity::Array(validity)
186        } else {
187            vortex_bail!(
188                "`ListViewArray::build` expects 3 or 4 children, got {}",
189                children.len()
190            );
191        };
192
193        // Get elements with the correct length from metadata.
194        let elements = children.get(
195            0,
196            element_dtype.as_ref(),
197            usize::try_from(metadata.elements_len)?,
198        )?;
199
200        // Get offsets with proper type from metadata.
201        let offsets = children.get(
202            1,
203            &DType::Primitive(metadata.offset_ptype(), Nullability::NonNullable),
204            len,
205        )?;
206
207        // Get sizes with proper type from metadata.
208        let sizes = children.get(
209            2,
210            &DType::Primitive(metadata.size_ptype(), Nullability::NonNullable),
211            len,
212        )?;
213
214        ListViewData::validate(&elements, &offsets, &sizes, &validity)?;
215        let data = ListViewData::try_new()?;
216        let slots = ListViewData::make_slots(&elements, &offsets, &sizes, &validity, len);
217        Ok(ArrayParts::new(self.clone(), dtype.clone(), len, data).with_slots(slots))
218    }
219
220    fn slot_name(_array: ArrayView<'_, Self>, idx: usize) -> String {
221        SLOT_NAMES[idx].to_string()
222    }
223
224    fn execute(array: Array<Self>, _ctx: &mut ExecutionCtx) -> VortexResult<ExecutionResult> {
225        Ok(ExecutionResult::done(array))
226    }
227
228    fn reduce_parent(
229        array: ArrayView<'_, Self>,
230        parent: &ArrayRef,
231        child_idx: usize,
232    ) -> VortexResult<Option<ArrayRef>> {
233        PARENT_RULES.evaluate(array, parent, child_idx)
234    }
235}