vortex_array/arrays/extension/vtable/
mod.rs1use vortex_error::VortexExpect;
5use vortex_error::VortexResult;
6use vortex_error::vortex_bail;
7use vortex_error::vortex_ensure_eq;
8use vortex_error::vortex_err;
9use vortex_error::vortex_panic;
10use vortex_session::VortexSession;
11use vortex_session::registry::CachedId;
12
13use crate::ArrayRef;
14use crate::EmptyArrayData;
15use crate::ExecutionCtx;
16use crate::ExecutionResult;
17use crate::array::Array;
18use crate::array::ArrayId;
19use crate::array::ArrayParts;
20use crate::array::ArrayView;
21use crate::array::VTable;
22use crate::array::ValidityVTableFromChild;
23use crate::array::with_empty_buffers;
24use crate::arrays::extension::array::ExtensionSlots;
25use crate::arrays::extension::compute::rules::PARENT_RULES;
26use crate::arrays::extension::compute::rules::RULES;
27use crate::buffer::BufferHandle;
28use crate::builders::ArrayBuilder;
29use crate::builders::ExtensionBuilder;
30use crate::dtype::DType;
31use crate::serde::ArrayChildren;
32
33mod kernel;
34mod operations;
35mod validity;
36
37#[derive(Clone, Debug)]
76pub struct Extension;
77
78pub type ExtensionArray = Array<Extension>;
80
81pub(crate) fn initialize(session: &VortexSession) {
82 kernel::initialize(session);
83}
84
85impl VTable for Extension {
86 type TypedArrayData = EmptyArrayData;
87
88 type OperationsVTable = Self;
89 type ValidityVTable = ValidityVTableFromChild;
90
91 fn id(&self) -> ArrayId {
92 static ID: CachedId = CachedId::new("vortex.ext");
93 *ID
94 }
95
96 fn validate(
97 &self,
98 _data: &EmptyArrayData,
99 dtype: &DType,
100 len: usize,
101 slots: &[Option<ArrayRef>],
102 ) -> VortexResult<()> {
103 let storage = slots[ExtensionSlots::STORAGE]
104 .as_ref()
105 .vortex_expect("ExtensionArray storage slot");
106 vortex_ensure_eq!(
107 storage.len(),
108 len,
109 "ExtensionArray length does not match outer length",
110 );
111
112 let ext_dtype = dtype
113 .as_extension_opt()
114 .ok_or_else(|| vortex_err!("not an extension dtype"))?;
115
116 vortex_ensure_eq!(storage.dtype(), ext_dtype.storage_dtype());
117
118 Ok(())
119 }
120
121 fn nbuffers(_array: ArrayView<'_, Self>) -> usize {
122 0
123 }
124
125 fn buffer(_array: ArrayView<'_, Self>, idx: usize) -> BufferHandle {
126 vortex_panic!("ExtensionArray buffer index {idx} out of bounds")
127 }
128
129 fn buffer_name(_array: ArrayView<'_, Self>, _idx: usize) -> Option<String> {
130 None
131 }
132
133 fn with_buffers(
134 &self,
135 array: ArrayView<'_, Self>,
136 buffers: &[BufferHandle],
137 ) -> VortexResult<ArrayParts<Self>> {
138 with_empty_buffers(self, array, buffers)
139 }
140
141 fn serialize(
142 _array: ArrayView<'_, Self>,
143 _session: &VortexSession,
144 ) -> VortexResult<Option<Vec<u8>>> {
145 Ok(Some(vec![]))
146 }
147
148 fn deserialize(
149 &self,
150 dtype: &DType,
151 len: usize,
152 metadata: &[u8],
153
154 _buffers: &[BufferHandle],
155 children: &dyn ArrayChildren,
156 _session: &VortexSession,
157 ) -> VortexResult<ArrayParts<Self>> {
158 if !metadata.is_empty() {
159 vortex_bail!(
160 "ExtensionArray expects empty metadata, got {} bytes",
161 metadata.len()
162 );
163 }
164 let DType::Extension(ext_dtype) = dtype else {
165 vortex_bail!("Not an extension DType");
166 };
167 if children.len() != 1 {
168 vortex_bail!("Expected 1 child, got {}", children.len());
169 }
170 let storage = children.get(0, ext_dtype.storage_dtype(), len)?;
171 Ok(ArrayParts::new(
172 self.clone(),
173 dtype.clone(),
174 len,
175 EmptyArrayData,
176 ExtensionSlots { storage }.into_slots(),
177 ))
178 }
179
180 fn slot_name(_array: ArrayView<'_, Self>, idx: usize) -> String {
181 ExtensionSlots::NAMES[idx].to_string()
182 }
183
184 fn execute(array: Array<Self>, _ctx: &mut ExecutionCtx) -> VortexResult<ExecutionResult> {
185 Ok(ExecutionResult::done(array))
186 }
187
188 fn append_to_builder(
189 array: ArrayView<'_, Self>,
190 builder: &mut dyn ArrayBuilder,
191 ctx: &mut ExecutionCtx,
192 ) -> VortexResult<()> {
193 let Some(builder) = builder.as_any_mut().downcast_mut::<ExtensionBuilder>() else {
194 vortex_bail!("append_to_builder for Extension requires an ExtensionBuilder");
195 };
196 builder.append_extension_array(&array.into_owned(), ctx)
197 }
198
199 fn reduce(array: ArrayView<'_, Self>) -> VortexResult<Option<ArrayRef>> {
200 RULES.evaluate(array)
201 }
202
203 fn reduce_parent(
204 array: ArrayView<'_, Self>,
205 parent: &ArrayRef,
206 child_idx: usize,
207 ) -> VortexResult<Option<ArrayRef>> {
208 PARENT_RULES.evaluate(array, parent, child_idx)
209 }
210}