Skip to main content

vortex_array/arrays/extension/vtable/
mod.rs

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