Create a Decimal128Array / Decimal256Array with random values.
(&mut self)
| 41 | impl DecimalArrayGenerator { |
| 42 | /// Create a Decimal128Array / Decimal256Array with random values. |
| 43 | pub fn gen_data<D>(&mut self) -> ArrayRef |
| 44 | where |
| 45 | D: DecimalType + RandomNativeData, |
| 46 | { |
| 47 | // table of decimals from which to draw |
| 48 | let distinct_decimals: PrimitiveArray<D> = { |
| 49 | let mut decimal_builder = |
| 50 | PrimitiveBuilder::<D>::with_capacity(self.num_distinct_decimals); |
| 51 | for _ in 0..self.num_distinct_decimals { |
| 52 | decimal_builder |
| 53 | .append_option(Some(D::generate_random_native_data(&mut self.rng))); |
| 54 | } |
| 55 | |
| 56 | decimal_builder |
| 57 | .finish() |
| 58 | .with_precision_and_scale(self.precision, self.scale) |
| 59 | .unwrap() |
| 60 | }; |
| 61 | |
| 62 | // pick num_decimals randomly from the distinct decimal table |
| 63 | let indices: UInt32Array = (0..self.num_decimals) |
| 64 | .map(|_| { |
| 65 | if self.rng.random::<f64>() < self.null_pct { |
| 66 | None |
| 67 | } else if self.num_distinct_decimals > 1 { |
| 68 | let range = 1..(self.num_distinct_decimals as u32); |
| 69 | Some(self.rng.random_range(range)) |
| 70 | } else { |
| 71 | Some(0) |
| 72 | } |
| 73 | }) |
| 74 | .collect(); |
| 75 | |
| 76 | let options = None; |
| 77 | arrow::compute::take(&distinct_decimals, &indices, options).unwrap() |
| 78 | } |
| 79 | } |