vortex_array/arrays/widen/vtable/
mod.rs1mod operations;
10
11mod plugin;
12pub use plugin::WidenPlugin;
13
14mod validity;
15
16use vortex_error::VortexResult;
17use vortex_error::vortex_ensure_eq;
18use vortex_error::vortex_err;
19use vortex_error::vortex_panic;
20use vortex_session::registry::CachedId;
21
22use crate::ArrayRef;
23use crate::EmptyArrayData;
24use crate::ExecutionCtx;
25use crate::ExecutionResult;
26use crate::IntoArray;
27use crate::array::Array;
28use crate::array::ArrayId;
29use crate::array::ArrayParts;
30use crate::array::ArrayView;
31use crate::array::VTable;
32use crate::array::ValidityVTableFromChild;
33use crate::array::with_empty_buffers;
34use crate::arrays::Decimal;
35use crate::arrays::DecimalArray;
36use crate::arrays::decimal::widened_buffer;
37use crate::arrays::widen::WidenArraySlotsExt;
38use crate::arrays::widen::WidenSlots;
39use crate::arrays::widen::array::validate_dtypes;
40use crate::buffer::BufferHandle;
41use crate::builtins::ArrayBuiltins;
42use crate::dtype::DType;
43use crate::dtype::DecimalDType;
44use crate::dtype::NativeDecimalType;
45use crate::match_each_decimal_value_type;
46use crate::require_child;
47
48#[derive(Clone, Debug)]
50pub struct Widen;
51
52pub type WidenArray = Array<Widen>;
54
55impl VTable for Widen {
56 type TypedArrayData = EmptyArrayData;
57
58 type OperationsVTable = Self;
59 type ValidityVTable = ValidityVTableFromChild;
60
61 fn id(&self) -> ArrayId {
62 static ID: CachedId = CachedId::new("vortex.widen");
63 *ID
64 }
65
66 fn validate(
67 &self,
68 _data: &EmptyArrayData,
69 dtype: &DType,
70 len: usize,
71 slots: &[Option<ArrayRef>],
72 ) -> VortexResult<()> {
73 vortex_ensure_eq!(slots.len(), WidenSlots::COUNT);
74 let values = slots[WidenSlots::VALUES]
75 .as_ref()
76 .ok_or_else(|| vortex_err!("Widen requires a values child"))?;
77 vortex_ensure_eq!(values.len(), len);
78
79 validate_dtypes(values.dtype(), dtype)
80 }
81
82 fn nbuffers(_array: ArrayView<'_, Self>) -> usize {
83 0
84 }
85
86 fn buffer(_array: ArrayView<'_, Self>, idx: usize) -> BufferHandle {
87 vortex_panic!("Widen buffer index {idx} out of bounds")
88 }
89
90 fn buffer_name(_array: ArrayView<'_, Self>, _idx: usize) -> Option<String> {
91 None
92 }
93
94 fn with_buffers(
95 &self,
96 array: ArrayView<'_, Self>,
97 buffers: &[BufferHandle],
98 ) -> VortexResult<ArrayParts<Self>> {
99 with_empty_buffers(self, array, buffers)
100 }
101
102 fn slot_name(_array: ArrayView<'_, Self>, idx: usize) -> String {
103 WidenSlots::NAMES[idx].to_string()
104 }
105
106 fn execute(array: Array<Self>, _ctx: &mut ExecutionCtx) -> VortexResult<ExecutionResult> {
107 match array.dtype() {
108 DType::Decimal(decimal_dtype, _) => {
109 let decimal_dtype = *decimal_dtype;
110 let array = require_child!(array, array.values(), WidenSlots::VALUES => Decimal);
111 let values = array.values().as_::<Decimal>();
112 let target_type = decimal_dtype.canonical_values_type();
113 let widened = match_each_decimal_value_type!(target_type, |T| {
114 copy_decimal_values::<T>(values, decimal_dtype)?
115 });
116
117 Ok(ExecutionResult::done(widened.into_array()))
118 }
119 DType::Primitive(..) => {
120 let widened = array.values().cast(array.dtype().clone())?;
122
123 Ok(ExecutionResult::done(widened))
124 }
125 dtype => {
126 vortex_panic!("Widen validation requires an integer or decimal dtype, got {dtype}")
127 }
128 }
129 }
130
131 fn reduce(array: ArrayView<'_, Self>) -> VortexResult<Option<ArrayRef>> {
132 if !array.values().is::<Widen>() {
134 return Ok(None);
135 }
136
137 let flattened = WidenArray::try_new(array.values().clone(), array.dtype().clone())?;
138 Ok(Some(flattened.into_array()))
139 }
140}
141
142fn copy_decimal_values<T: NativeDecimalType>(
145 values: ArrayView<'_, Decimal>,
146 decimal_dtype: DecimalDType,
147) -> VortexResult<DecimalArray> {
148 let validity = values.validity()?;
149 let buffer = widened_buffer::<T>(&values);
150
151 DecimalArray::try_new_handle_from_trusted_values(
153 BufferHandle::new_host(buffer.into_byte_buffer()),
154 T::DECIMAL_TYPE,
155 decimal_dtype,
156 validity,
157 )
158}