Skip to main content

vortex_array/arrays/decimal/vtable/
mod.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4//! The [`Decimal`] vtable. Its plugin reads and writes the `vortex.decimal` wire form, including
5//! legacy nodes.
6
7mod kernel;
8mod operations;
9mod plugin;
10mod validity;
11
12use std::hash::Hash;
13use std::hash::Hasher;
14
15use vortex_error::VortexResult;
16use vortex_error::vortex_bail;
17use vortex_error::vortex_ensure;
18use vortex_error::vortex_ensure_eq;
19use vortex_session::VortexSession;
20use vortex_session::registry::CachedId;
21
22use crate::ArrayParts;
23use crate::ArrayRef;
24use crate::EqMode;
25use crate::ExecutionCtx;
26use crate::ExecutionResult;
27use crate::array::Array;
28use crate::array::ArrayId;
29use crate::array::ArrayView;
30use crate::array::VTable;
31use crate::arrays::decimal::DecimalData;
32use crate::arrays::decimal::array::DecimalSlots;
33use crate::arrays::fixed_width::vtable as fixed_width;
34use crate::buffer::BufferHandle;
35use crate::builders::ArrayBuilder;
36use crate::builders::DecimalBuilder;
37use crate::dtype::DType;
38use crate::hash::ArrayEq;
39use crate::hash::ArrayHash;
40use crate::validity::Validity;
41
42/// A [`Decimal`]-encoded Vortex array.
43pub type DecimalArray = Array<Decimal>;
44
45pub(crate) fn initialize(session: &VortexSession) {
46    kernel::initialize(session);
47}
48
49impl ArrayHash for DecimalData {
50    fn array_hash<H: Hasher>(&self, state: &mut H, accuracy: EqMode) {
51        self.values.array_hash(state, accuracy);
52        std::mem::discriminant(&self.values_type()).hash(state);
53    }
54}
55
56impl ArrayEq for DecimalData {
57    fn array_eq(&self, other: &Self, accuracy: EqMode) -> bool {
58        self.values.array_eq(&other.values, accuracy) && self.values_type() == other.values_type()
59    }
60}
61
62impl VTable for Decimal {
63    type TypedArrayData = DecimalData;
64
65    type OperationsVTable = Self;
66    type ValidityVTable = Self;
67
68    fn id(&self) -> ArrayId {
69        static ID: CachedId = CachedId::new("vortex.decimal");
70        *ID
71    }
72
73    fn nbuffers(_array: ArrayView<'_, Self>) -> usize {
74        1
75    }
76
77    fn buffer(array: ArrayView<'_, Self>, idx: usize) -> BufferHandle {
78        fixed_width::buffer("DecimalArray", &array.values, idx)
79    }
80
81    fn buffer_name(_array: ArrayView<'_, Self>, idx: usize) -> Option<String> {
82        fixed_width::buffer_name(idx)
83    }
84
85    fn with_buffers(
86        &self,
87        array: ArrayView<'_, Self>,
88        buffers: &[BufferHandle],
89    ) -> VortexResult<ArrayParts<Self>> {
90        let mut data = array.data().clone();
91        data.values = fixed_width::single_buffer(buffers)?;
92        Ok(ArrayParts::new(
93            self.clone(),
94            array.dtype().clone(),
95            array.len(),
96            data,
97            array.slots().iter().cloned().collect(),
98        ))
99    }
100
101    fn validate(
102        &self,
103        data: &DecimalData,
104        dtype: &DType,
105        len: usize,
106        slots: &[Option<ArrayRef>],
107    ) -> VortexResult<()> {
108        let DType::Decimal(decimal_dtype, nullability) = dtype else {
109            vortex_bail!("Expected decimal dtype, got {dtype}");
110        };
111        vortex_ensure_eq!(
112            data.decimal_dtype,
113            *decimal_dtype,
114            "Decimal values must have the precision and scale of the array",
115        );
116        DecimalData::validate(&data.values, data.values_type())?;
117        vortex_ensure_eq!(
118            data.len(),
119            len,
120            "Decimal values must contain one integer per row",
121        );
122
123        let [validity] = slots else {
124            vortex_bail!("Decimal requires one validity slot, got {}", slots.len());
125        };
126        if let Some(child) = validity {
127            vortex_ensure!(
128                nullability.is_nullable(),
129                "A validity child requires a nullable dtype, got {dtype}",
130            );
131            vortex_ensure_eq!(
132                child.dtype(),
133                &Validity::DTYPE,
134                "Decimal validity must contain non-nullable booleans",
135            );
136            vortex_ensure_eq!(
137                child.len(),
138                len,
139                "Decimal validity must contain one boolean per row",
140            );
141        }
142
143        Ok(())
144    }
145
146    fn slot_name(_array: ArrayView<'_, Self>, idx: usize) -> String {
147        DecimalSlots::NAMES[idx].to_string()
148    }
149
150    fn execute(array: Array<Self>, _ctx: &mut ExecutionCtx) -> VortexResult<ExecutionResult> {
151        Ok(ExecutionResult::done(array))
152    }
153
154    fn append_to_builder(
155        array: ArrayView<'_, Self>,
156        builder: &mut dyn ArrayBuilder,
157        ctx: &mut ExecutionCtx,
158    ) -> VortexResult<()> {
159        let Some(builder) = builder.as_any_mut().downcast_mut::<DecimalBuilder>() else {
160            vortex_bail!("append_to_builder for Decimal requires a DecimalBuilder");
161        };
162        builder.append_decimal_array(array, ctx)
163    }
164}
165
166/// The vtable of [`DecimalArray`].
167#[derive(Clone, Debug)]
168pub struct Decimal;
169
170#[cfg(test)]
171mod tests;