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