vortex_array/arrays/extension/vtable/
mod.rs1mod canonical;
4mod kernel;
5mod operations;
6mod validity;
7
8use std::hash::Hash;
9use std::sync::Arc;
10
11use kernel::PARENT_KERNELS;
12use vortex_error::VortexExpect;
13use vortex_error::VortexResult;
14use vortex_error::vortex_bail;
15use vortex_error::vortex_ensure;
16use vortex_error::vortex_panic;
17use vortex_session::VortexSession;
18
19use crate::ArrayRef;
20use crate::EmptyMetadata;
21use crate::ExecutionCtx;
22use crate::ExecutionResult;
23use crate::Precision;
24use crate::arrays::ExtensionArray;
25use crate::arrays::extension::compute::rules::PARENT_RULES;
26use crate::buffer::BufferHandle;
27use crate::dtype::DType;
28use crate::hash::ArrayEq;
29use crate::hash::ArrayHash;
30use crate::serde::ArrayChildren;
31use crate::stats::StatsSetRef;
32use crate::vtable;
33use crate::vtable::ArrayId;
34use crate::vtable::VTable;
35use crate::vtable::ValidityVTableFromChild;
36
37vtable!(Extension);
38
39impl VTable for Extension {
40 type Array = ExtensionArray;
41
42 type Metadata = EmptyMetadata;
43 type OperationsVTable = Self;
44 type ValidityVTable = ValidityVTableFromChild;
45
46 fn vtable(_array: &Self::Array) -> &Self {
47 &Extension
48 }
49
50 fn id(&self) -> ArrayId {
51 Self::ID
52 }
53
54 fn len(array: &ExtensionArray) -> usize {
55 array.storage_array.len()
56 }
57
58 fn dtype(array: &ExtensionArray) -> &DType {
59 &array.dtype
60 }
61
62 fn stats(array: &ExtensionArray) -> StatsSetRef<'_> {
63 array.stats_set.to_ref(array.as_ref())
64 }
65
66 fn array_hash<H: std::hash::Hasher>(
67 array: &ExtensionArray,
68 state: &mut H,
69 precision: Precision,
70 ) {
71 array.dtype.hash(state);
72 array.storage_array.array_hash(state, precision);
73 }
74
75 fn array_eq(array: &ExtensionArray, other: &ExtensionArray, precision: Precision) -> bool {
76 array.dtype == other.dtype
77 && array
78 .storage_array
79 .array_eq(&other.storage_array, precision)
80 }
81
82 fn nbuffers(_array: &ExtensionArray) -> usize {
83 0
84 }
85
86 fn buffer(_array: &ExtensionArray, idx: usize) -> BufferHandle {
87 vortex_panic!("ExtensionArray buffer index {idx} out of bounds")
88 }
89
90 fn buffer_name(_array: &ExtensionArray, _idx: usize) -> Option<String> {
91 None
92 }
93
94 fn nchildren(_array: &ExtensionArray) -> usize {
95 1
96 }
97
98 fn child(array: &ExtensionArray, idx: usize) -> ArrayRef {
99 match idx {
100 0 => array.storage_array.clone(),
101 _ => vortex_panic!("ExtensionArray child index {idx} out of bounds"),
102 }
103 }
104
105 fn child_name(_array: &ExtensionArray, idx: usize) -> String {
106 match idx {
107 0 => "storage".to_string(),
108 _ => vortex_panic!("ExtensionArray child_name index {idx} out of bounds"),
109 }
110 }
111
112 fn metadata(_array: &ExtensionArray) -> VortexResult<Self::Metadata> {
113 Ok(EmptyMetadata)
114 }
115
116 fn serialize(_metadata: Self::Metadata) -> VortexResult<Option<Vec<u8>>> {
117 Ok(Some(vec![]))
118 }
119
120 fn deserialize(
121 _bytes: &[u8],
122 _dtype: &DType,
123 _len: usize,
124 _buffers: &[BufferHandle],
125 _session: &VortexSession,
126 ) -> VortexResult<Self::Metadata> {
127 Ok(EmptyMetadata)
128 }
129
130 fn build(
131 dtype: &DType,
132 len: usize,
133 _metadata: &Self::Metadata,
134 _buffers: &[BufferHandle],
135 children: &dyn ArrayChildren,
136 ) -> VortexResult<ExtensionArray> {
137 let DType::Extension(ext_dtype) = dtype else {
138 vortex_bail!("Not an extension DType");
139 };
140 if children.len() != 1 {
141 vortex_bail!("Expected 1 child, got {}", children.len());
142 }
143 let storage = children.get(0, ext_dtype.storage_dtype(), len)?;
144 Ok(ExtensionArray::new(ext_dtype.clone(), storage))
145 }
146
147 fn with_children(array: &mut Self::Array, children: Vec<ArrayRef>) -> VortexResult<()> {
148 vortex_ensure!(
149 children.len() == 1,
150 "ExtensionArray expects exactly 1 child (storage), got {}",
151 children.len()
152 );
153 array.storage_array = children
154 .into_iter()
155 .next()
156 .vortex_expect("children length already validated");
157 Ok(())
158 }
159
160 fn execute(array: Arc<Self::Array>, _ctx: &mut ExecutionCtx) -> VortexResult<ExecutionResult> {
161 Ok(ExecutionResult::done_upcast::<Self>(array))
162 }
163
164 fn reduce_parent(
165 array: &Self::Array,
166 parent: &ArrayRef,
167 child_idx: usize,
168 ) -> VortexResult<Option<ArrayRef>> {
169 PARENT_RULES.evaluate(array, parent, child_idx)
170 }
171
172 fn execute_parent(
173 array: &Self::Array,
174 parent: &ArrayRef,
175 child_idx: usize,
176 ctx: &mut ExecutionCtx,
177 ) -> VortexResult<Option<ArrayRef>> {
178 PARENT_KERNELS.execute(array, parent, child_idx, ctx)
179 }
180}
181
182#[derive(Clone, Debug)]
183pub struct Extension;
184
185impl Extension {
186 pub const ID: ArrayId = ArrayId::new_ref("vortex.ext");
187}