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