vortex_array/arrays/struct_/vtable/
mod.rs1use itertools::Itertools;
5use kernel::PARENT_KERNELS;
6use vortex_error::VortexExpect;
7use vortex_error::VortexResult;
8use vortex_error::vortex_bail;
9use vortex_error::vortex_panic;
10use vortex_session::VortexSession;
11
12use crate::ArrayRef;
13use crate::ExecutionCtx;
14use crate::ExecutionResult;
15use crate::array::Array;
16use crate::array::ArrayParts;
17use crate::array::ArrayView;
18use crate::array::EmptyArrayData;
19use crate::array::VTable;
20use crate::array::child_to_validity;
21use crate::arrays::struct_::array::FIELDS_OFFSET;
22use crate::arrays::struct_::array::VALIDITY_SLOT;
23use crate::arrays::struct_::array::make_struct_slots;
24use crate::arrays::struct_::compute::rules::PARENT_RULES;
25use crate::buffer::BufferHandle;
26use crate::dtype::DType;
27use crate::serde::ArrayChildren;
28use crate::validity::Validity;
29mod kernel;
30mod operations;
31mod validity;
32
33use vortex_session::registry::CachedId;
34
35use crate::array::ArrayId;
36
37pub type StructArray = Array<Struct>;
39
40impl VTable for Struct {
41 type TypedArrayData = EmptyArrayData;
42
43 type OperationsVTable = Self;
44 type ValidityVTable = Self;
45 fn id(&self) -> ArrayId {
46 static ID: CachedId = CachedId::new("vortex.struct");
47 *ID
48 }
49
50 fn nbuffers(_array: ArrayView<'_, Self>) -> usize {
51 0
52 }
53
54 fn validate(
55 &self,
56 _data: &EmptyArrayData,
57 dtype: &DType,
58 len: usize,
59 slots: &[Option<ArrayRef>],
60 ) -> VortexResult<()> {
61 let DType::Struct(struct_dtype, nullability) = dtype else {
62 vortex_bail!("Expected struct dtype, found {:?}", dtype)
63 };
64
65 let expected_slots = struct_dtype.nfields() + 1;
66 if slots.len() != expected_slots {
67 vortex_bail!(
68 InvalidArgument: "StructArray has {} slots but expected {}",
69 slots.len(),
70 expected_slots
71 );
72 }
73
74 let validity = child_to_validity(slots[VALIDITY_SLOT].as_ref(), *nullability);
75 if let Some(validity_len) = validity.maybe_len()
76 && validity_len != len
77 {
78 vortex_bail!(
79 InvalidArgument: "StructArray validity length {} does not match outer length {}",
80 validity_len,
81 len
82 );
83 }
84
85 let field_slots = &slots[FIELDS_OFFSET..];
86 if field_slots.is_empty() {
87 return Ok(());
88 }
89
90 for (idx, (slot, field_dtype)) in field_slots.iter().zip(struct_dtype.fields()).enumerate()
91 {
92 let field = slot
93 .as_ref()
94 .ok_or_else(|| vortex_error::vortex_err!("StructArray missing field slot {idx}"))?;
95 if field.len() != len {
96 vortex_bail!(
97 InvalidArgument: "StructArray field {idx} has length {} but expected {}",
98 field.len(),
99 len
100 );
101 }
102 if field.dtype() != &field_dtype {
103 vortex_bail!(
104 InvalidArgument: "StructArray field {idx} has dtype {} but expected {}",
105 field.dtype(),
106 field_dtype
107 );
108 }
109 }
110
111 Ok(())
112 }
113
114 fn buffer(_array: ArrayView<'_, Self>, idx: usize) -> BufferHandle {
115 vortex_panic!("StructArray buffer index {idx} out of bounds")
116 }
117
118 fn buffer_name(_array: ArrayView<'_, Self>, idx: usize) -> Option<String> {
119 vortex_panic!("StructArray buffer_name index {idx} out of bounds")
120 }
121
122 fn serialize(
123 _array: ArrayView<'_, Self>,
124 _session: &VortexSession,
125 ) -> VortexResult<Option<Vec<u8>>> {
126 Ok(Some(vec![]))
127 }
128
129 fn deserialize(
130 &self,
131 dtype: &DType,
132 len: usize,
133 metadata: &[u8],
134
135 _buffers: &[BufferHandle],
136 children: &dyn ArrayChildren,
137 _session: &VortexSession,
138 ) -> VortexResult<ArrayParts<Self>> {
139 if !metadata.is_empty() {
140 vortex_bail!(
141 "StructArray expects empty metadata, got {} bytes",
142 metadata.len()
143 );
144 }
145 let DType::Struct(struct_dtype, nullability) = dtype else {
146 vortex_bail!("Expected struct dtype, found {:?}", dtype)
147 };
148
149 let (validity, non_data_children) = if children.len() == struct_dtype.nfields() {
150 (Validity::from(*nullability), 0_usize)
151 } else if children.len() == struct_dtype.nfields() + 1 {
152 let validity = children.get(0, &Validity::DTYPE, len)?;
153 (Validity::Array(validity), 1_usize)
154 } else {
155 vortex_bail!(
156 "Expected {} or {} children, found {}",
157 struct_dtype.nfields(),
158 struct_dtype.nfields() + 1,
159 children.len()
160 );
161 };
162
163 let field_children: Vec<_> = (0..struct_dtype.nfields())
164 .map(|i| {
165 let child_dtype = struct_dtype
166 .field_by_index(i)
167 .vortex_expect("no out of bounds");
168 children.get(non_data_children + i, &child_dtype, len)
169 })
170 .try_collect()?;
171
172 let slots = make_struct_slots(&field_children, &validity, len);
173 Ok(ArrayParts::new(self.clone(), dtype.clone(), len, EmptyArrayData).with_slots(slots))
174 }
175
176 fn slot_name(array: ArrayView<'_, Self>, idx: usize) -> String {
177 if idx == VALIDITY_SLOT {
178 "validity".to_string()
179 } else {
180 array.dtype().as_struct_fields().names()[idx - FIELDS_OFFSET].to_string()
181 }
182 }
183
184 fn execute(array: Array<Self>, _ctx: &mut ExecutionCtx) -> VortexResult<ExecutionResult> {
185 Ok(ExecutionResult::done(array))
186 }
187
188 fn reduce_parent(
189 array: ArrayView<'_, Self>,
190 parent: &ArrayRef,
191 child_idx: usize,
192 ) -> VortexResult<Option<ArrayRef>> {
193 PARENT_RULES.evaluate(array, parent, child_idx)
194 }
195
196 fn execute_parent(
197 array: ArrayView<'_, Self>,
198 parent: &ArrayRef,
199 child_idx: usize,
200 ctx: &mut ExecutionCtx,
201 ) -> VortexResult<Option<ArrayRef>> {
202 PARENT_KERNELS.execute(array, parent, child_idx, ctx)
203 }
204}
205
206#[derive(Clone, Debug)]
207pub struct Struct;