vortex_array/vtable/
mod.rs

1//! This module contains the VTable definitions for a Vortex Array.
2
3use std::fmt::{Debug, Display, Formatter};
4use std::hash::{Hash, Hasher};
5
6mod compute;
7
8pub use compute::*;
9use vortex_dtype::DType;
10use vortex_error::{VortexExpect, VortexResult, vortex_bail};
11
12use crate::arcref::ArcRef;
13use crate::encoding::EncodingId;
14use crate::serde::ArrayParts;
15use crate::{Array, ArrayContext, ArrayRef, Canonical};
16
17/// A reference to an array VTable, either static or arc'd.
18pub type VTableRef = ArcRef<dyn EncodingVTable>;
19
20/// Dyn-compatible VTable trait for a Vortex array encoding.
21///
22/// This trait provides extension points for arrays to implement various features of Vortex.
23/// It is split into multiple sub-traits to make it easier for consumers to break up the
24/// implementation, as well as to allow for optional implementation of certain features, for example
25/// compute functions.
26pub trait EncodingVTable: 'static + Sync + Send + ComputeVTable {
27    /// Return the ID for this encoding implementation.
28    fn id(&self) -> EncodingId;
29
30    fn decode(
31        &self,
32        parts: &ArrayParts,
33        ctx: &ArrayContext,
34        _dtype: DType,
35        _len: usize,
36    ) -> VortexResult<ArrayRef> {
37        vortex_bail!(
38            "Decoding not supported for encoding {}",
39            ctx.lookup_encoding(parts.encoding_id())
40                .vortex_expect("Encoding already validated")
41                .id()
42        )
43    }
44
45    /// Encode the canonical array into this encoding implementation.
46    ///
47    /// Should error if `like` is encoded with a different encoding.
48    fn encode(
49        &self,
50        input: &Canonical,
51        _like: Option<&dyn Array>,
52    ) -> VortexResult<Option<ArrayRef>> {
53        if self.id() == input.as_ref().encoding() {
54            return Ok(Some(input.as_ref().to_array()));
55        }
56
57        Ok(None)
58    }
59}
60
61impl PartialEq for dyn EncodingVTable + '_ {
62    fn eq(&self, other: &Self) -> bool {
63        self.id() == other.id()
64    }
65}
66
67impl Eq for dyn EncodingVTable + '_ {}
68
69impl Hash for dyn EncodingVTable + '_ {
70    fn hash<H: Hasher>(&self, state: &mut H) {
71        self.id().hash(state)
72    }
73}
74
75impl Debug for dyn EncodingVTable + '_ {
76    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
77        write!(f, "{}", self.id())
78    }
79}
80
81impl Display for dyn EncodingVTable + '_ {
82    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
83        write!(f, "{}", self.id())
84    }
85}