Skip to main content

vortex_array/arrays/shared/
vtable.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use std::hash::Hasher;
5
6use vortex_error::VortexExpect;
7use vortex_error::VortexResult;
8use vortex_error::vortex_panic;
9use vortex_session::VortexSession;
10use vortex_session::registry::CachedId;
11
12use crate::ArrayEq;
13use crate::ArrayHash;
14use crate::ArrayParts;
15use crate::ArrayRef;
16use crate::Canonical;
17use crate::EqMode;
18use crate::ExecutionCtx;
19use crate::ExecutionResult;
20use crate::array::Array;
21use crate::array::ArrayId;
22use crate::array::ArrayView;
23use crate::array::OperationsVTable;
24use crate::array::VTable;
25use crate::array::ValidityVTable;
26use crate::array::with_empty_buffers;
27use crate::arrays::shared::SharedArrayExt;
28use crate::arrays::shared::SharedData;
29use crate::arrays::shared::array::SLOT_NAMES;
30use crate::buffer::BufferHandle;
31use crate::dtype::DType;
32use crate::scalar::Scalar;
33use crate::validity::Validity;
34
35/// A [`Shared`]-encoded Vortex array.
36pub type SharedArray = Array<Shared>;
37
38// TODO(ngates): consider hooking Shared into the iterative execution model. Cache either the
39//  most executed, or after each iteration, and return a shared cache for each execution.
40#[derive(Clone, Debug)]
41pub struct Shared;
42
43impl ArrayHash for SharedData {
44    fn array_hash<H: Hasher>(&self, _state: &mut H, _accuracy: EqMode) {}
45}
46
47impl ArrayEq for SharedData {
48    fn array_eq(&self, _other: &Self, _accuracy: EqMode) -> bool {
49        true
50    }
51}
52
53impl VTable for Shared {
54    type TypedArrayData = SharedData;
55    type OperationsVTable = Self;
56    type ValidityVTable = Self;
57    fn id(&self) -> ArrayId {
58        static ID: CachedId = CachedId::new("vortex.shared");
59        *ID
60    }
61
62    fn validate(
63        &self,
64        _data: &SharedData,
65        dtype: &DType,
66        len: usize,
67        slots: &[Option<ArrayRef>],
68    ) -> VortexResult<()> {
69        let source = slots[0]
70            .as_ref()
71            .vortex_expect("SharedArray source slot must be present");
72        vortex_error::vortex_ensure!(source.dtype() == dtype, "SharedArray dtype mismatch");
73        vortex_error::vortex_ensure!(source.len() == len, "SharedArray len mismatch");
74        Ok(())
75    }
76
77    fn nbuffers(_array: ArrayView<'_, Self>) -> usize {
78        0
79    }
80
81    fn buffer(_array: ArrayView<'_, Self>, _idx: usize) -> BufferHandle {
82        vortex_panic!("SharedArray has no buffers")
83    }
84
85    fn buffer_name(_array: ArrayView<'_, Self>, _idx: usize) -> Option<String> {
86        None
87    }
88
89    fn with_buffers(
90        &self,
91        array: ArrayView<'_, Self>,
92        buffers: &[BufferHandle],
93    ) -> VortexResult<ArrayParts<Self>> {
94        with_empty_buffers(self, array, buffers)
95    }
96
97    fn slot_name(_array: ArrayView<'_, Self>, idx: usize) -> String {
98        SLOT_NAMES[idx].to_string()
99    }
100
101    fn serialize(
102        _array: ArrayView<'_, Self>,
103        _session: &VortexSession,
104    ) -> VortexResult<Option<Vec<u8>>> {
105        vortex_error::vortex_bail!("Shared array is not serializable")
106    }
107
108    fn deserialize(
109        &self,
110        _dtype: &DType,
111        _len: usize,
112        _metadata: &[u8],
113
114        _buffers: &[BufferHandle],
115        _children: &dyn crate::serde::ArrayChildren,
116        _session: &VortexSession,
117    ) -> VortexResult<ArrayParts<Self>> {
118        vortex_error::vortex_bail!("Shared array is not serializable")
119    }
120
121    fn execute(array: Array<Self>, ctx: &mut ExecutionCtx) -> VortexResult<ExecutionResult> {
122        array
123            .get_or_compute(|source| source.clone().execute::<Canonical>(ctx))
124            .map(ExecutionResult::done)
125    }
126}
127impl OperationsVTable<Shared> for Shared {
128    fn scalar_at(
129        array: ArrayView<'_, Shared>,
130        index: usize,
131        ctx: &mut ExecutionCtx,
132    ) -> VortexResult<Scalar> {
133        array.current_array_ref().execute_scalar(index, ctx)
134    }
135}
136
137impl ValidityVTable<Shared> for Shared {
138    fn validity(array: ArrayView<'_, Shared>) -> VortexResult<Validity> {
139        array.current_array_ref().validity()
140    }
141}