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;
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_array.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.array_hash(state, precision);
69    }
70
71    fn array_eq(array: &ExtensionArray, other: &ExtensionArray, precision: Precision) -> bool {
72        array.dtype == other.dtype
73            && array
74                .storage_array
75                .array_eq(&other.storage_array, precision)
76    }
77
78    fn nbuffers(_array: &ExtensionArray) -> usize {
79        0
80    }
81
82    fn buffer(_array: &ExtensionArray, idx: usize) -> BufferHandle {
83        vortex_panic!("ExtensionArray buffer index {idx} out of bounds")
84    }
85
86    fn buffer_name(_array: &ExtensionArray, _idx: usize) -> Option<String> {
87        None
88    }
89
90    fn nchildren(_array: &ExtensionArray) -> usize {
91        1
92    }
93
94    fn child(array: &ExtensionArray, idx: usize) -> ArrayRef {
95        match idx {
96            0 => array.storage_array.clone(),
97            _ => vortex_panic!("ExtensionArray child index {idx} out of bounds"),
98        }
99    }
100
101    fn child_name(_array: &ExtensionArray, idx: usize) -> String {
102        match idx {
103            0 => "storage".to_string(),
104            _ => vortex_panic!("ExtensionArray child_name index {idx} out of bounds"),
105        }
106    }
107
108    fn metadata(_array: &ExtensionArray) -> VortexResult<Self::Metadata> {
109        Ok(EmptyMetadata)
110    }
111
112    fn serialize(_metadata: Self::Metadata) -> VortexResult<Option<Vec<u8>>> {
113        Ok(Some(vec![]))
114    }
115
116    fn deserialize(
117        _bytes: &[u8],
118        _dtype: &DType,
119        _len: usize,
120        _buffers: &[BufferHandle],
121        _session: &VortexSession,
122    ) -> VortexResult<Self::Metadata> {
123        Ok(EmptyMetadata)
124    }
125
126    fn build(
127        dtype: &DType,
128        len: usize,
129        _metadata: &Self::Metadata,
130        _buffers: &[BufferHandle],
131        children: &dyn ArrayChildren,
132    ) -> VortexResult<ExtensionArray> {
133        let DType::Extension(ext_dtype) = dtype else {
134            vortex_bail!("Not an extension DType");
135        };
136        if children.len() != 1 {
137            vortex_bail!("Expected 1 child, got {}", children.len());
138        }
139        let storage = children.get(0, ext_dtype.storage_dtype(), len)?;
140        Ok(ExtensionArray::new(ext_dtype.clone(), storage))
141    }
142
143    fn with_children(array: &mut Self::Array, children: Vec<ArrayRef>) -> VortexResult<()> {
144        vortex_ensure!(
145            children.len() == 1,
146            "ExtensionArray expects exactly 1 child (storage), got {}",
147            children.len()
148        );
149        array.storage_array = children
150            .into_iter()
151            .next()
152            .vortex_expect("children length already validated");
153        Ok(())
154    }
155
156    fn execute(array: &Self::Array, _ctx: &mut ExecutionCtx) -> VortexResult<ExecutionStep> {
157        Ok(ExecutionStep::Done(array.clone().into_array()))
158    }
159
160    fn reduce_parent(
161        array: &Self::Array,
162        parent: &ArrayRef,
163        child_idx: usize,
164    ) -> VortexResult<Option<ArrayRef>> {
165        PARENT_RULES.evaluate(array, parent, child_idx)
166    }
167
168    fn execute_parent(
169        array: &Self::Array,
170        parent: &ArrayRef,
171        child_idx: usize,
172        ctx: &mut ExecutionCtx,
173    ) -> VortexResult<Option<ArrayRef>> {
174        PARENT_KERNELS.execute(array, parent, child_idx, ctx)
175    }
176}
177
178#[derive(Debug)]
179pub struct ExtensionVTable;
180
181impl ExtensionVTable {
182    pub const ID: ArrayId = ArrayId::new_ref("vortex.ext");
183}