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::RULES;
26use crate::buffer::BufferHandle;
27use crate::builders::ArrayBuilder;
28use crate::builders::ExtensionBuilder;
29use crate::dtype::DType;
30
31mod kernel;
32mod operations;
33mod plugin;
34mod validity;
35
36#[derive(Clone, Debug)]
75pub struct Extension;
76
77pub type ExtensionArray = Array<Extension>;
79
80pub(crate) fn initialize(session: &VortexSession) {
81 kernel::initialize(session);
82}
83
84impl VTable for Extension {
85 type TypedArrayData = EmptyArrayData;
86
87 type OperationsVTable = Self;
88 type ValidityVTable = ValidityVTableFromChild;
89
90 fn id(&self) -> ArrayId {
91 static ID: CachedId = CachedId::new("vortex.ext");
92 *ID
93 }
94
95 fn validate(
96 &self,
97 _data: &EmptyArrayData,
98 dtype: &DType,
99 len: usize,
100 slots: &[Option<ArrayRef>],
101 ) -> VortexResult<()> {
102 let storage = slots[ExtensionSlots::STORAGE]
103 .as_ref()
104 .vortex_expect("ExtensionArray storage slot");
105 vortex_ensure_eq!(
106 storage.len(),
107 len,
108 "ExtensionArray length does not match outer length",
109 );
110
111 let ext_dtype = dtype
112 .as_extension_opt()
113 .ok_or_else(|| vortex_err!("not an extension dtype"))?;
114
115 vortex_ensure_eq!(storage.dtype(), ext_dtype.storage_dtype());
116
117 Ok(())
118 }
119
120 fn nbuffers(_array: ArrayView<'_, Self>) -> usize {
121 0
122 }
123
124 fn buffer(_array: ArrayView<'_, Self>, idx: usize) -> BufferHandle {
125 vortex_panic!("ExtensionArray buffer index {idx} out of bounds")
126 }
127
128 fn buffer_name(_array: ArrayView<'_, Self>, _idx: usize) -> Option<String> {
129 None
130 }
131
132 fn with_buffers(
133 &self,
134 array: ArrayView<'_, Self>,
135 buffers: &[BufferHandle],
136 ) -> VortexResult<ArrayParts<Self>> {
137 with_empty_buffers(self, array, buffers)
138 }
139
140 fn slot_name(_array: ArrayView<'_, Self>, idx: usize) -> String {
141 ExtensionSlots::NAMES[idx].to_string()
142 }
143
144 fn execute(array: Array<Self>, _ctx: &mut ExecutionCtx) -> VortexResult<ExecutionResult> {
145 Ok(ExecutionResult::done(array))
146 }
147
148 fn append_to_builder(
149 array: ArrayView<'_, Self>,
150 builder: &mut dyn ArrayBuilder,
151 ctx: &mut ExecutionCtx,
152 ) -> VortexResult<()> {
153 let Some(builder) = builder.as_any_mut().downcast_mut::<ExtensionBuilder>() else {
154 vortex_bail!("append_to_builder for Extension requires an ExtensionBuilder");
155 };
156 builder.append_extension_array(array, ctx)
157 }
158
159 fn reduce(array: ArrayView<'_, Self>) -> VortexResult<Option<ArrayRef>> {
160 RULES.evaluate(array)
161 }
162}