vortex_vector/decimal/
mod.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4//! Definitions and implementations of decimal vector types.
5//!
6//! The types that hold data are [`DVector`] and [`DVectorMut`], which are generic over types `D`
7//! that implement [`NativeDecimalType`].
8//!
9//! [`DecimalVector`] and [`DecimalVectorMut`] are enums that wrap all of the different possible
10//! [`DVector`]s. There are several macros defined in this crate to make working with these
11//! primitive vector types easier.
12//!
13//! # Examples
14//!
15//! ## Creating and building decimal vectors
16//!
17//! ```
18//! use vortex_dtype::{PrecisionScale};
19//! use vortex_vector::decimal::{DVectorMut};
20//! use vortex_vector::VectorMutOps;
21//!
22//! // Create a decimal vector with precision=9, scale=2 (e.g., up to 9999999.99).
23//! let ps = PrecisionScale::<i32>::new(9, 2);
24//! let mut vec = DVectorMut::<i32>::with_capacity(ps, 5);
25//! assert_eq!(vec.len(), 0);
26//! assert!(vec.capacity() >= 5);
27//!
28//! // Values are stored as integers scaled by 10^scale.
29//! // For scale=2: 123.45 is stored as 12345.
30//! vec.try_push(12345).unwrap();  // Represents 123.45.
31//! vec.try_push(9999).unwrap();   // Represents 99.99.
32//! assert_eq!(vec.len(), 2);
33//!
34//! // Values that exceed precision will fail.
35//! let too_large = 10_i32.pow(9);  // Would represent 10000000.00.
36//! assert!(vec.try_push(too_large).is_err());
37//!
38//! // Create from buffers with validation.
39//! use vortex_buffer::BufferMut;
40//! use vortex_mask::MaskMut;
41//! let elements = BufferMut::from_iter([100_i32, 200, 300]);  // 1.00, 2.00, 3.00.
42//! let validity = MaskMut::new_true(3);
43//! let decimal_vec = DVectorMut::<i32>::new(ps, elements, validity);
44//! assert_eq!(decimal_vec.len(), 3);
45//! ```
46//!
47//! ## Working with nulls and validity
48//!
49//! ```
50//! use vortex_buffer::BufferMut;
51//! use vortex_dtype::{PrecisionScale};
52//! use vortex_mask::MaskMut;
53//! use vortex_vector::decimal::DVectorMut;
54//! use vortex_vector::VectorMutOps;
55//!
56//! // Create a decimal vector with nulls.
57//! let ps = PrecisionScale::<i32>::new(5, 2); // Up to 999.99.
58//!
59//! // Create with some null values (validity mask: true = not null, false = null).
60//! let elements = BufferMut::from_iter([1000_i32, 0, 2500, 0]);  // 10.00, null, 25.00, null.
61//! let mut validity = MaskMut::with_capacity(4);
62//! validity.append_n(true, 1);   // index 0: valid
63//! validity.append_n(false, 1);  // index 1: null
64//! validity.append_n(true, 1);   // index 2: valid
65//! validity.append_n(false, 1);  // index 3: null
66//! let mut vec = DVectorMut::new(ps, elements, validity);
67//!
68//! // Check element access with nulls.
69//! assert_eq!(vec.get(0), Some(&1000));  // 10.00.
70//! assert_eq!(vec.get(1), None);         // Null.
71//! assert_eq!(vec.get(2), Some(&2500));  // 25.00.
72//!
73//! // Append null values.
74//! vec.append_nulls(3);
75//! assert_eq!(vec.len(), 7);
76//! ```
77//!
78//! ## Extending and manipulating vectors
79//!
80//! ```
81//! use vortex_dtype::{PrecisionScale};
82//! use vortex_vector::decimal::DVectorMut;
83//! use vortex_vector::VectorMutOps;
84//!
85//! // Create two decimal vectors with scale=3 (3 decimal places).
86//! let ps = PrecisionScale::<i64>::new(10, 3);
87//! let mut vec1 = DVectorMut::<i64>::with_capacity(ps, 10);
88//! vec1.try_push(1234567).unwrap();  // 1234.567.
89//! vec1.try_push(2345678).unwrap();  // 2345.678.
90//!
91//! let mut vec2 = DVectorMut::<i64>::with_capacity(ps, 10);
92//! vec2.try_push(3456789).unwrap();  // 3456.789.
93//! vec2.try_push(4567890).unwrap();  // 4567.890.
94//!
95//! // Extend from an immutable vector.
96//! let immutable = vec2.freeze();
97//! vec1.extend_from_vector(&immutable);
98//! assert_eq!(vec1.len(), 4);
99//!
100//! // Split vector at index 3.
101//! let mut split = vec1.split_off(3);
102//! assert_eq!(vec1.len(), 3);
103//! assert_eq!(split.len(), 1);
104//!
105//! // Reserve capacity for future operations.
106//! vec1.reserve(10);
107//! assert!(vec1.capacity() >= 13);
108//!
109//! // Rejoin the vectors.
110//! vec1.unsplit(split);
111//! assert_eq!(vec1.len(), 4);
112//! ```
113//!
114//! ## Converting between mutable and immutable
115//!
116//! ```
117//! use vortex_dtype::{PrecisionScale};
118//! use vortex_vector::decimal::DVectorMut;
119//! use vortex_vector::{VectorMutOps, VectorOps};
120//!
121//! // Create a mutable decimal vector.
122//! let ps = PrecisionScale::<i128>::new(18, 6);  // High precision with 6 decimal places.
123//! let mut vec_mut = DVectorMut::<i128>::with_capacity(ps, 3);
124//! vec_mut.try_push(1000000).unwrap();    // 1.000000.
125//! vec_mut.try_push(2500000).unwrap();    // 2.500000.
126//! vec_mut.try_push(3333333).unwrap();    // 3.333333.
127//!
128//! // Freeze into an immutable vector.
129//! let vec_immutable = vec_mut.freeze();
130//! assert_eq!(vec_immutable.len(), 3);
131//!
132//! // Access elements from the immutable vector.
133//! assert_eq!(vec_immutable.get(0), Some(&1000000));
134//! assert_eq!(vec_immutable.get(1), Some(&2500000));
135//!
136//! // Can also convert immutable back to mutable using try_into_mut.
137//! // Note: This may fail if the buffer is shared.
138//! // let vec_mut_again = vec_immutable.try_into_mut().unwrap();
139//! // assert_eq!(vec_mut_again.len(), 3);
140//! ```
141
142mod generic;
143pub use generic::DVector;
144
145mod generic_mut;
146pub use generic_mut::DVectorMut;
147
148mod vector;
149pub use vector::DecimalVector;
150
151mod vector_mut;
152pub use vector_mut::DecimalVectorMut;
153
154mod scalar;
155pub use scalar::{DScalar, DecimalScalar};
156
157mod macros;
158
159use vortex_dtype::NativeDecimalType;
160
161use crate::{Vector, VectorMut};
162
163impl From<DecimalVector> for Vector {
164    fn from(v: DecimalVector) -> Self {
165        Self::Decimal(v)
166    }
167}
168
169impl<D: NativeDecimalType> From<DVector<D>> for DecimalVector {
170    fn from(value: DVector<D>) -> Self {
171        D::upcast(value)
172    }
173}
174
175impl<D: NativeDecimalType> From<DVector<D>> for Vector {
176    fn from(v: DVector<D>) -> Self {
177        Self::Decimal(DecimalVector::from(v))
178    }
179}
180
181impl From<DecimalVectorMut> for VectorMut {
182    fn from(v: DecimalVectorMut) -> Self {
183        Self::Decimal(v)
184    }
185}
186
187impl<D: NativeDecimalType> From<DVectorMut<D>> for DecimalVectorMut {
188    fn from(val: DVectorMut<D>) -> Self {
189        D::upcast(val)
190    }
191}
192
193impl<D: NativeDecimalType> From<DVectorMut<D>> for VectorMut {
194    fn from(val: DVectorMut<D>) -> Self {
195        Self::Decimal(DecimalVectorMut::from(val))
196    }
197}