Skip to main content

vortex_array/arrays/map/vtable/
mod.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use vortex_error::VortexResult;
5use vortex_error::vortex_bail;
6use vortex_error::vortex_ensure_eq;
7use vortex_error::vortex_panic;
8use vortex_session::VortexSession;
9use vortex_session::registry::CachedId;
10
11use crate::ArrayParts;
12use crate::ArrayRef;
13use crate::ExecutionCtx;
14use crate::ExecutionResult;
15use crate::array::Array;
16use crate::array::ArrayId;
17use crate::array::ArrayView;
18use crate::array::VTable;
19use crate::array::ValidityVTableFromChild;
20use crate::array::with_empty_buffers;
21use crate::arrays::map::MapData;
22use crate::arrays::map::MapSlots;
23use crate::arrays::map::MapSlotsView;
24use crate::arrays::map::array::validate_entries;
25use crate::buffer::BufferHandle;
26use crate::builders::ArrayBuilder;
27use crate::dtype::DType;
28use crate::match_each_map_builder;
29
30mod kernel;
31mod operations;
32mod plugin;
33mod validity;
34
35/// A [`Map`]-encoded Vortex array.
36pub type MapArray = Array<Map>;
37
38pub(crate) fn initialize(session: &VortexSession) {
39    kernel::initialize(session);
40}
41
42/// The canonical encoding for [`DType::Map`].
43///
44/// A map array has one `ListView<Struct<key, value>>` child. Its outer dtype retains map-specific
45/// metadata such as the `keys_sorted` assertion.
46#[derive(Clone, Debug, Default)]
47pub struct Map;
48
49impl VTable for Map {
50    type TypedArrayData = MapData;
51
52    type OperationsVTable = Self;
53    type ValidityVTable = ValidityVTableFromChild;
54
55    fn id(&self) -> ArrayId {
56        static ID: CachedId = CachedId::new("vortex.map");
57        *ID
58    }
59
60    fn validate(
61        &self,
62        _data: &MapData,
63        dtype: &DType,
64        len: usize,
65        slots: &[Option<ArrayRef>],
66    ) -> VortexResult<()> {
67        vortex_ensure_eq!(slots.len(), MapSlots::COUNT);
68
69        let DType::Map(map_dtype, nullability) = dtype else {
70            vortex_bail!("Expected map dtype, got {dtype}");
71        };
72        let slots = MapSlotsView::from_slots(slots);
73        validate_entries(map_dtype, *nullability, len, slots.entries)
74    }
75
76    fn nbuffers(_array: ArrayView<'_, Self>) -> usize {
77        0
78    }
79
80    fn buffer(_array: ArrayView<'_, Self>, idx: usize) -> BufferHandle {
81        vortex_panic!("MapArray buffer index {idx} out of bounds")
82    }
83
84    fn buffer_name(_array: ArrayView<'_, Self>, _idx: usize) -> Option<String> {
85        None
86    }
87
88    fn with_buffers(
89        &self,
90        array: ArrayView<'_, Self>,
91        buffers: &[BufferHandle],
92    ) -> VortexResult<ArrayParts<Self>> {
93        with_empty_buffers(self, array, buffers)
94    }
95
96    fn slot_name(_array: ArrayView<'_, Self>, idx: usize) -> String {
97        MapSlots::NAMES[idx].to_string()
98    }
99
100    fn execute(array: Array<Self>, _ctx: &mut ExecutionCtx) -> VortexResult<ExecutionResult> {
101        Ok(ExecutionResult::done(array))
102    }
103
104    fn append_to_builder(
105        array: ArrayView<'_, Self>,
106        builder: &mut dyn ArrayBuilder,
107        ctx: &mut ExecutionCtx,
108    ) -> VortexResult<()> {
109        match match_each_map_builder!(&mut *builder, |b| b.append_map_array(array, ctx)) {
110            Some(result) => result,
111            None => vortex_bail!(
112                "cannot append a Map array of dtype {} to a {} builder",
113                array.dtype(),
114                builder.dtype()
115            ),
116        }
117    }
118}