vortex_array/arrays/
dict_test.rs1#![expect(clippy::unwrap_used)]
5
6use rand::RngExt;
7use rand::SeedableRng;
8use rand::distr::Alphanumeric;
9use rand::distr::Distribution;
10use rand::distr::StandardUniform;
11use rand::prelude::IndexedRandom;
12use rand::prelude::StdRng;
13use vortex_buffer::Buffer;
14use vortex_error::VortexExpect;
15use vortex_error::VortexResult;
16
17use crate::ArrayRef;
18use crate::IntoArray;
19use crate::arrays::ChunkedArray;
20use crate::arrays::DictArray;
21use crate::arrays::PrimitiveArray;
22use crate::dtype::NativePType;
23use crate::validity::Validity;
24
25pub fn gen_primitive_for_dict<T: NativePType>(len: usize, unique_count: usize) -> PrimitiveArray
26where
27 StandardUniform: Distribution<T>,
28{
29 let mut rng = StdRng::seed_from_u64(0);
30 let values = (0..unique_count)
31 .map(|_| rng.random::<T>())
32 .collect::<Vec<T>>();
33 let data = (0..len)
34 .map(|_| *values.choose(&mut rng).unwrap())
35 .collect::<Buffer<_>>();
36 PrimitiveArray::new(data, Validity::NonNullable)
37}
38
39pub fn gen_primitive_dict<V: NativePType, C: NativePType>(
40 len: usize,
41 unique_count: usize,
42) -> VortexResult<DictArray>
43where
44 StandardUniform: Distribution<V>,
45{
46 let mut rng = StdRng::seed_from_u64(0);
47 let values = (0..unique_count)
48 .map(|_| rng.random::<V>())
49 .collect::<PrimitiveArray>();
50
51 let codes = (0..len)
52 .map(|_| C::from(rng.random_range(0..unique_count)).unwrap())
53 .collect::<PrimitiveArray>();
54
55 DictArray::try_new(codes.into_array(), values.into_array())
56}
57
58pub fn gen_varbin_words(len: usize, unique_count: usize) -> Vec<String> {
59 gen_varbin_words_of_len(len, unique_count, 16)
60}
61
62pub fn gen_varbin_words_of_len(len: usize, unique_count: usize, word_len: usize) -> Vec<String> {
70 let rng = &mut StdRng::seed_from_u64(0);
71 let dict: Vec<String> = (0..unique_count)
72 .map(|_| {
73 rng.sample_iter(&Alphanumeric)
74 .take(word_len)
75 .map(char::from)
76 .collect()
77 })
78 .collect();
79
80 (0..len)
81 .map(|_| dict.choose(rng).unwrap().clone())
82 .collect()
83}
84
85pub fn gen_dict_primitive_chunks<T: NativePType, O: NativePType>(
86 len: usize,
87 unique_values: usize,
88 chunk_count: usize,
89) -> ArrayRef
90where
91 StandardUniform: Distribution<T>,
92{
93 (0..chunk_count)
94 .map(|_| {
95 gen_primitive_dict::<T, O>(len, unique_values)
96 .vortex_expect("operation should succeed in test")
97 .into_array()
98 })
99 .collect::<ChunkedArray>()
100 .into_array()
101}