Skip to main content

vortex_array/arrays/union/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_err;
7use vortex_error::vortex_panic;
8use vortex_session::registry::CachedId;
9
10use crate::ArrayRef;
11use crate::ExecutionCtx;
12use crate::ExecutionResult;
13use crate::array::Array;
14use crate::array::ArrayId;
15use crate::array::ArrayParts;
16use crate::array::ArrayView;
17use crate::array::EmptyArrayData;
18use crate::array::VTable;
19use crate::array::with_empty_buffers;
20use crate::arrays::union::UnionArrayExt;
21use crate::arrays::union::UnionSlots;
22use crate::buffer::BufferHandle;
23use crate::builders::ArrayBuilder;
24use crate::dtype::DType;
25
26mod operations;
27mod plugin;
28mod validate;
29mod validity;
30
31/// A canonical [`Union`]-encoded array.
32///
33/// This is similar to Arrow's "spase" union type.
34pub type UnionArray = Array<Union>;
35
36/// The canonical Union encoding.
37///
38/// This is similar to Arrow's "spase" union type.
39#[derive(Clone, Debug)]
40pub struct Union;
41
42impl VTable for Union {
43    type TypedArrayData = EmptyArrayData;
44    type OperationsVTable = Self;
45    type ValidityVTable = Self;
46
47    fn id(&self) -> ArrayId {
48        static ID: CachedId = CachedId::new("vortex.union");
49        *ID
50    }
51
52    fn validate(
53        &self,
54        _data: &EmptyArrayData,
55        dtype: &DType,
56        len: usize,
57        slots: &[Option<ArrayRef>],
58    ) -> VortexResult<()> {
59        let type_ids = slots
60            .get(UnionSlots::TYPE_IDS)
61            .and_then(Option::as_ref)
62            .ok_or_else(|| vortex_err!("UnionArray is missing its type_ids slot"))?;
63        let variant_arrays = slots
64            .get(UnionSlots::CHILDREN_OFFSET..)
65            .unwrap_or_default()
66            .iter()
67            .enumerate()
68            .map(|(index, slot)| {
69                slot.as_ref()
70                    .ok_or_else(|| vortex_err!("UnionArray is missing child {index}"))
71            })
72            .collect::<VortexResult<Vec<_>>>()?;
73
74        validate::validate_union_components(type_ids, &variant_arrays, dtype, len)
75    }
76
77    fn nbuffers(_array: ArrayView<'_, Self>) -> usize {
78        0
79    }
80
81    fn buffer(_array: ArrayView<'_, Self>, idx: usize) -> BufferHandle {
82        vortex_panic!("UnionArray buffer index {idx} out of bounds")
83    }
84
85    fn buffer_name(_array: ArrayView<'_, Self>, idx: usize) -> Option<String> {
86        vortex_panic!("UnionArray buffer_name index {idx} out of bounds")
87    }
88
89    fn with_buffers(
90        &self,
91        array: ArrayView<'_, Self>,
92        buffers: &[BufferHandle],
93    ) -> VortexResult<ArrayParts<Self>> {
94        with_empty_buffers(self, array, buffers)
95    }
96
97    fn slot_name(array: ArrayView<'_, Self>, idx: usize) -> String {
98        if idx == UnionSlots::TYPE_IDS {
99            "type_ids".to_string()
100        } else {
101            array.variants().names()[idx - UnionSlots::CHILDREN_OFFSET].to_string()
102        }
103    }
104
105    fn execute(array: Array<Self>, _ctx: &mut ExecutionCtx) -> VortexResult<ExecutionResult> {
106        Ok(ExecutionResult::done(array))
107    }
108
109    fn append_to_builder(
110        _array: ArrayView<'_, Self>,
111        _builder: &mut dyn ArrayBuilder,
112        _ctx: &mut ExecutionCtx,
113    ) -> VortexResult<()> {
114        vortex_bail!("TODO(connor)[Union]: implement append_to_builder for Union arrays")
115    }
116}