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 let actual_dtype = DType::Extension(ext_dtype.clone());
117 vortex_ensure_eq!(
118 &actual_dtype,
119 dtype,
120 "ExtensionArray dtype does not match outer dtype",
121 );
122
123 Ok(())
124 }
125
126 fn nbuffers(_array: ArrayView<'_, Self>) -> usize {
127 0
128 }
129
130 fn buffer(_array: ArrayView<'_, Self>, idx: usize) -> BufferHandle {
131 vortex_panic!("ExtensionArray buffer index {idx} out of bounds")
132 }
133
134 fn buffer_name(_array: ArrayView<'_, Self>, _idx: usize) -> Option<String> {
135 None
136 }
137
138 fn with_buffers(
139 &self,
140 array: ArrayView<'_, Self>,
141 buffers: &[BufferHandle],
142 ) -> VortexResult<ArrayParts<Self>> {
143 with_empty_buffers(self, array, buffers)
144 }
145
146 fn serialize(
147 _array: ArrayView<'_, Self>,
148 _session: &VortexSession,
149 ) -> VortexResult<Option<Vec<u8>>> {
150 Ok(Some(vec![]))
151 }
152
153 fn deserialize(
154 &self,
155 dtype: &DType,
156 len: usize,
157 metadata: &[u8],
158
159 _buffers: &[BufferHandle],
160 children: &dyn ArrayChildren,
161 _session: &VortexSession,
162 ) -> VortexResult<ArrayParts<Self>> {
163 if !metadata.is_empty() {
164 vortex_bail!(
165 "ExtensionArray expects empty metadata, got {} bytes",
166 metadata.len()
167 );
168 }
169 let DType::Extension(ext_dtype) = dtype else {
170 vortex_bail!("Not an extension DType");
171 };
172 if children.len() != 1 {
173 vortex_bail!("Expected 1 child, got {}", children.len());
174 }
175 let storage = children.get(0, ext_dtype.storage_dtype(), len)?;
176 Ok(ArrayParts::new(
177 self.clone(),
178 dtype.clone(),
179 len,
180 EmptyArrayData,
181 ExtensionSlots { storage }.into_slots(),
182 ))
183 }
184
185 fn slot_name(_array: ArrayView<'_, Self>, idx: usize) -> String {
186 ExtensionSlots::NAMES[idx].to_string()
187 }
188
189 fn execute(array: Array<Self>, _ctx: &mut ExecutionCtx) -> VortexResult<ExecutionResult> {
190 Ok(ExecutionResult::done(array))
191 }
192
193 fn append_to_builder(
194 array: ArrayView<'_, Self>,
195 builder: &mut dyn ArrayBuilder,
196 ctx: &mut ExecutionCtx,
197 ) -> VortexResult<()> {
198 let Some(builder) = builder.as_any_mut().downcast_mut::<ExtensionBuilder>() else {
199 vortex_bail!("append_to_builder for Extension requires an ExtensionBuilder");
200 };
201 builder.append_extension_array(&array.into_owned(), ctx)
202 }
203
204 fn reduce(array: ArrayView<'_, Self>) -> VortexResult<Option<ArrayRef>> {
205 RULES.evaluate(array)
206 }
207
208 fn reduce_parent(
209 array: ArrayView<'_, Self>,
210 parent: &ArrayRef,
211 child_idx: usize,
212 ) -> VortexResult<Option<ArrayRef>> {
213 PARENT_RULES.evaluate(array, parent, child_idx)
214 }
215}