vortex_array/arrays/struct_/vtable/
mod.rs1use vortex_error::VortexResult;
5use vortex_error::vortex_bail;
6use vortex_error::vortex_panic;
7
8use crate::ArrayRef;
9use crate::ExecutionCtx;
10use crate::ExecutionResult;
11use crate::array::Array;
12use crate::array::ArrayParts;
13use crate::array::ArrayView;
14use crate::array::EmptyArrayData;
15use crate::array::VTable;
16use crate::array::child_to_validity;
17use crate::array::with_empty_buffers;
18use crate::arrays::struct_::array::StructSlots;
19use crate::buffer::BufferHandle;
20use crate::builders::ArrayBuilder;
21use crate::builders::StructBuilder;
22use crate::dtype::DType;
23mod operations;
24mod plugin;
25mod validity;
26
27use vortex_session::registry::CachedId;
28
29use crate::array::ArrayId;
30
31pub type StructArray = Array<Struct>;
33
34impl VTable for Struct {
35 type TypedArrayData = EmptyArrayData;
36
37 type OperationsVTable = Self;
38 type ValidityVTable = Self;
39 fn id(&self) -> ArrayId {
40 static ID: CachedId = CachedId::new("vortex.struct");
41 *ID
42 }
43
44 fn nbuffers(_array: ArrayView<'_, Self>) -> usize {
45 0
46 }
47
48 fn validate(
49 &self,
50 _data: &EmptyArrayData,
51 dtype: &DType,
52 len: usize,
53 slots: &[Option<ArrayRef>],
54 ) -> VortexResult<()> {
55 let DType::Struct(struct_dtype, nullability) = dtype else {
56 vortex_bail!("Expected struct dtype, found {:?}", dtype)
57 };
58
59 let expected_slots = struct_dtype.nfields() + 1;
60 if slots.len() != expected_slots {
61 vortex_bail!(
62 "StructArray has {} slots but expected {}",
63 slots.len(),
64 expected_slots
65 );
66 }
67
68 let validity = child_to_validity(slots[StructSlots::VALIDITY_CHILD].as_ref(), *nullability);
69 if let Some(validity_len) = validity.maybe_len()
70 && validity_len != len
71 {
72 vortex_bail!(
73 "StructArray validity length {} does not match outer length {}",
74 validity_len,
75 len
76 );
77 }
78
79 let field_slots = &slots[StructSlots::FIELDS_OFFSET..];
80 if field_slots.is_empty() {
81 return Ok(());
82 }
83
84 for (idx, (slot, field_dtype)) in field_slots.iter().zip(struct_dtype.fields()).enumerate()
85 {
86 let field = slot
87 .as_ref()
88 .ok_or_else(|| vortex_error::vortex_err!("StructArray missing field slot {idx}"))?;
89 if field.len() != len {
90 vortex_bail!(
91 "StructArray field {idx} has length {} but expected {}",
92 field.len(),
93 len
94 );
95 }
96 if field.dtype() != &field_dtype {
97 vortex_bail!(
98 "StructArray field {idx} has dtype {} but expected {}",
99 field.dtype(),
100 field_dtype
101 );
102 }
103 }
104
105 Ok(())
106 }
107
108 fn buffer(_array: ArrayView<'_, Self>, idx: usize) -> BufferHandle {
109 vortex_panic!("StructArray buffer index {idx} out of bounds")
110 }
111
112 fn buffer_name(_array: ArrayView<'_, Self>, idx: usize) -> Option<String> {
113 vortex_panic!("StructArray buffer_name index {idx} out of bounds")
114 }
115
116 fn with_buffers(
117 &self,
118 array: ArrayView<'_, Self>,
119 buffers: &[BufferHandle],
120 ) -> VortexResult<ArrayParts<Self>> {
121 with_empty_buffers(self, array, buffers)
122 }
123
124 fn slot_name(array: ArrayView<'_, Self>, idx: usize) -> String {
125 if idx == StructSlots::VALIDITY_CHILD {
126 "validity".to_string()
127 } else {
128 array.dtype().as_struct_fields().names()[idx - StructSlots::FIELDS_OFFSET].to_string()
129 }
130 }
131
132 fn execute(array: Array<Self>, _ctx: &mut ExecutionCtx) -> VortexResult<ExecutionResult> {
133 Ok(ExecutionResult::done(array))
134 }
135
136 fn append_to_builder(
137 array: ArrayView<'_, Self>,
138 builder: &mut dyn ArrayBuilder,
139 ctx: &mut ExecutionCtx,
140 ) -> VortexResult<()> {
141 let Some(builder) = builder.as_any_mut().downcast_mut::<StructBuilder>() else {
142 vortex_bail!("append_to_builder for Struct requires a StructBuilder");
143 };
144 builder.append_struct_array(array, ctx)
145 }
146}
147
148#[derive(Clone, Debug)]
149pub struct Struct;