vortex_array/arrays/variant/vtable/
mod.rs1mod operations;
5mod validity;
6
7use std::hash::Hasher;
8use std::sync::Arc;
9
10use vortex_error::VortexExpect;
11use vortex_error::VortexResult;
12use vortex_error::vortex_ensure;
13use vortex_error::vortex_panic;
14
15use crate::ArrayEq;
16use crate::ArrayHash;
17use crate::ArrayRef;
18use crate::EmptyMetadata;
19use crate::ExecutionCtx;
20use crate::ExecutionResult;
21use crate::Precision;
22use crate::arrays::VariantArray;
23use crate::buffer::BufferHandle;
24use crate::dtype::DType;
25use crate::serde::ArrayChildren;
26use crate::stats::StatsSetRef;
27use crate::vtable;
28use crate::vtable::Array;
29use crate::vtable::ArrayId;
30use crate::vtable::VTable;
31
32vtable!(Variant);
33
34#[derive(Clone, Debug)]
35pub struct Variant;
36
37impl Variant {
38 pub const ID: ArrayId = ArrayId::new_ref("vortex.variant");
39}
40
41impl VTable for Variant {
42 type Array = VariantArray;
43
44 type Metadata = EmptyMetadata;
45
46 type OperationsVTable = Self;
47
48 type ValidityVTable = Self;
49
50 fn vtable(_array: &Self::Array) -> &Self {
51 &Variant
52 }
53
54 fn id(&self) -> ArrayId {
55 Self::ID
56 }
57
58 fn len(array: &Self::Array) -> usize {
59 array.child.len()
60 }
61
62 fn dtype(array: &Self::Array) -> &DType {
63 array.child.dtype()
64 }
65
66 fn stats(array: &Self::Array) -> StatsSetRef<'_> {
67 array.child.statistics()
68 }
69
70 fn array_hash<H: Hasher>(array: &Self::Array, state: &mut H, precision: Precision) {
71 array.child.array_hash(state, precision);
72 }
73
74 fn array_eq(array: &Self::Array, other: &Self::Array, precision: Precision) -> bool {
75 array.child.array_eq(&other.child, precision)
76 }
77
78 fn nbuffers(_array: &Self::Array) -> usize {
79 0
80 }
81
82 fn buffer(_array: &Self::Array, idx: usize) -> BufferHandle {
83 vortex_panic!("VariantArray buffer index {idx} out of bounds")
84 }
85
86 fn buffer_name(_array: &Self::Array, _idx: usize) -> Option<String> {
87 None
88 }
89
90 fn nchildren(_array: &Self::Array) -> usize {
91 1
92 }
93
94 fn child(array: &Self::Array, idx: usize) -> ArrayRef {
95 match idx {
96 0 => array.child.clone(),
97 _ => vortex_panic!("VariantArray child index {idx} out of bounds"),
98 }
99 }
100
101 fn child_name(_array: &Self::Array, idx: usize) -> String {
102 match idx {
103 0 => "child".to_string(),
104 _ => vortex_panic!("VariantArray child_name index {idx} out of bounds"),
105 }
106 }
107
108 fn metadata(_array: &Self::Array) -> 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: &vortex_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<Self::Array> {
133 vortex_ensure!(matches!(dtype, DType::Variant(_)), "Expected Variant DType");
134 vortex_ensure!(
135 children.len() == 1,
136 "Expected 1 child, got {}",
137 children.len()
138 );
139 let child = children.get(0, dtype, len)?;
141 Ok(VariantArray::new(child))
142 }
143
144 fn with_children(array: &mut Self::Array, children: Vec<ArrayRef>) -> VortexResult<()> {
145 vortex_ensure!(
146 children.len() == 1,
147 "VariantArray expects exactly 1 child, got {}",
148 children.len()
149 );
150 array.child = children
151 .into_iter()
152 .next()
153 .vortex_expect("VariantArray must have 1 child");
154 Ok(())
155 }
156
157 fn execute(array: Arc<Array<Self>>, _ctx: &mut ExecutionCtx) -> VortexResult<ExecutionResult> {
158 Ok(ExecutionResult::done(array))
159 }
160}