vortex_array/arrays/union/vtable/
mod.rs1use 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
31pub type UnionArray = Array<Union>;
35
36#[derive(Clone, Debug)]
53pub struct Union;
54
55impl VTable for Union {
56 type TypedArrayData = EmptyArrayData;
57 type OperationsVTable = Self;
58 type ValidityVTable = Self;
59
60 fn id(&self) -> ArrayId {
61 static ID: CachedId = CachedId::new("vortex.union");
62 *ID
63 }
64
65 fn validate(
66 &self,
67 _data: &EmptyArrayData,
68 dtype: &DType,
69 len: usize,
70 slots: &[Option<ArrayRef>],
71 ) -> VortexResult<()> {
72 let type_ids = slots
73 .get(UnionSlots::TYPE_IDS)
74 .and_then(Option::as_ref)
75 .ok_or_else(|| vortex_err!("UnionArray is missing its type_ids slot"))?;
76 let variant_arrays = slots
77 .get(UnionSlots::CHILDREN_OFFSET..)
78 .unwrap_or_default()
79 .iter()
80 .enumerate()
81 .map(|(index, slot)| {
82 slot.as_ref()
83 .ok_or_else(|| vortex_err!("UnionArray is missing child {index}"))
84 })
85 .collect::<VortexResult<Vec<_>>>()?;
86
87 validate::validate_union_components(type_ids, &variant_arrays, dtype, len)
88 }
89
90 fn nbuffers(_array: ArrayView<'_, Self>) -> usize {
91 0
92 }
93
94 fn buffer(_array: ArrayView<'_, Self>, idx: usize) -> BufferHandle {
95 vortex_panic!("UnionArray buffer index {idx} out of bounds")
96 }
97
98 fn buffer_name(_array: ArrayView<'_, Self>, idx: usize) -> Option<String> {
99 vortex_panic!("UnionArray buffer_name index {idx} out of bounds")
100 }
101
102 fn with_buffers(
103 &self,
104 array: ArrayView<'_, Self>,
105 buffers: &[BufferHandle],
106 ) -> VortexResult<ArrayParts<Self>> {
107 with_empty_buffers(self, array, buffers)
108 }
109
110 fn slot_name(array: ArrayView<'_, Self>, idx: usize) -> String {
111 if idx == UnionSlots::TYPE_IDS {
112 "type_ids".to_string()
113 } else {
114 array.variants().names()[idx - UnionSlots::CHILDREN_OFFSET].to_string()
115 }
116 }
117
118 fn execute(array: Array<Self>, _ctx: &mut ExecutionCtx) -> VortexResult<ExecutionResult> {
119 Ok(ExecutionResult::done(array))
120 }
121
122 fn append_to_builder(
123 _array: ArrayView<'_, Self>,
124 _builder: &mut dyn ArrayBuilder,
125 _ctx: &mut ExecutionCtx,
126 ) -> VortexResult<()> {
127 vortex_bail!("TODO(connor)[Union]: implement append_to_builder for Union arrays")
128 }
129}