|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// SPDX-FileCopyrightText: Copyright the Vortex contributors |
| 3 | + |
| 4 | +//! Shared decimal inputs for splitting and assembly benchmarks. |
| 5 | +
|
| 6 | +use rand::RngExt; |
| 7 | +use rand::SeedableRng; |
| 8 | +use rand::rngs::StdRng; |
| 9 | +use vortex_array::arrays::DecimalArray; |
| 10 | +use vortex_array::dtype::DecimalDType; |
| 11 | +use vortex_array::dtype::DecimalType; |
| 12 | +use vortex_array::dtype::i256; |
| 13 | +use vortex_array::validity::Validity; |
| 14 | +use vortex_buffer::Buffer; |
| 15 | +use vortex_error::vortex_panic; |
| 16 | + |
| 17 | +pub(super) fn cases() -> Vec<(DecimalType, usize)> { |
| 18 | + [DecimalType::I64, DecimalType::I128, DecimalType::I256] |
| 19 | + .into_iter() |
| 20 | + .flat_map(|values_type| [1_024, 8_192].map(|len| (values_type, len))) |
| 21 | + .collect() |
| 22 | +} |
| 23 | + |
| 24 | +pub(super) fn decimal_array( |
| 25 | + values_type: DecimalType, |
| 26 | + len: usize, |
| 27 | + validity: Validity, |
| 28 | +) -> DecimalArray { |
| 29 | + let mut rng = StdRng::seed_from_u64(42); |
| 30 | + |
| 31 | + macro_rules! decimal { |
| 32 | + ($T:ty, $precision:literal) => {{ |
| 33 | + let max = <$T>::pow(10, $precision) - 1; |
| 34 | + let values: Buffer<$T> = (0..len).map(|_| rng.random_range(-max..=max)).collect(); |
| 35 | + DecimalArray::new(values, DecimalDType::new($precision, 2), validity) |
| 36 | + }}; |
| 37 | + } |
| 38 | + |
| 39 | + match values_type { |
| 40 | + DecimalType::I64 => decimal!(i64, 18), |
| 41 | + DecimalType::I128 => decimal!(i128, 38), |
| 42 | + DecimalType::I256 => { |
| 43 | + // Keep the magnitude below 10^76 while exercising all four signed/unsigned words. |
| 44 | + let values: Buffer<i256> = (0..len) |
| 45 | + .map(|_| i256::from_parts(rng.random(), rng.random::<i128>() >> 4)) |
| 46 | + .collect(); |
| 47 | + DecimalArray::new(values, DecimalDType::new(76, 2), validity) |
| 48 | + } |
| 49 | + _ => vortex_panic!("unsupported benchmark storage type: {values_type}"), |
| 50 | + } |
| 51 | +} |
0 commit comments