Create a random [ArrayRef] from a [DataType] with a length, null density and true density (for [BooleanArray]). # Arguments `field` - The field containing the data type for which to create a random array `size` - The number of elements in the generated array `null_density` - The approximate fraction of null values in the resulting array (0.0 to 1.0) `true_density` - The approximate fraction of t
(
field: &Field,
size: usize,
mut null_density: f32,
true_density: f32,
)
| 64 | /// * `true_density` - The approximate fraction of true values in boolean arrays (0.0 to 1.0) |
| 65 | /// |
| 66 | pub fn create_random_array( |
| 67 | field: &Field, |
| 68 | size: usize, |
| 69 | mut null_density: f32, |
| 70 | true_density: f32, |
| 71 | ) -> Result<ArrayRef> { |
| 72 | // Override nullability in case of not nested and not dictionary |
| 73 | // For nested we don't want to override as we want to keep the nullability for the children |
| 74 | // For dictionary it handle the nullability internally |
| 75 | if !field.data_type().is_nested() && !matches!(field.data_type(), Dictionary(_, _)) { |
| 76 | // Override null density with 0.0 if the array is non-nullable |
| 77 | null_density = match field.is_nullable() { |
| 78 | true => null_density, |
| 79 | false => 0.0, |
| 80 | }; |
| 81 | } |
| 82 | |
| 83 | use DataType::*; |
| 84 | let array = match field.data_type() { |
| 85 | Null => Arc::new(NullArray::new(size)) as ArrayRef, |
| 86 | Boolean => Arc::new(create_boolean_array(size, null_density, true_density)), |
| 87 | Int8 => Arc::new(create_primitive_array::<Int8Type>(size, null_density)), |
| 88 | Int16 => Arc::new(create_primitive_array::<Int16Type>(size, null_density)), |
| 89 | Int32 => Arc::new(create_primitive_array::<Int32Type>(size, null_density)), |
| 90 | Int64 => Arc::new(create_primitive_array::<Int64Type>(size, null_density)), |
| 91 | UInt8 => Arc::new(create_primitive_array::<UInt8Type>(size, null_density)), |
| 92 | UInt16 => Arc::new(create_primitive_array::<UInt16Type>(size, null_density)), |
| 93 | UInt32 => Arc::new(create_primitive_array::<UInt32Type>(size, null_density)), |
| 94 | UInt64 => Arc::new(create_primitive_array::<UInt64Type>(size, null_density)), |
| 95 | Float16 => Arc::new(create_primitive_array::<Float16Type>(size, null_density)), |
| 96 | Float32 => Arc::new(create_primitive_array::<Float32Type>(size, null_density)), |
| 97 | Float64 => Arc::new(create_primitive_array::<Float64Type>(size, null_density)), |
| 98 | Timestamp(unit, tz) => match unit { |
| 99 | TimeUnit::Second => Arc::new( |
| 100 | create_random_temporal_array::<TimestampSecondType>(size, null_density) |
| 101 | .with_timezone_opt(tz.clone()), |
| 102 | ) as ArrayRef, |
| 103 | TimeUnit::Millisecond => Arc::new( |
| 104 | create_random_temporal_array::<TimestampMillisecondType>(size, null_density) |
| 105 | .with_timezone_opt(tz.clone()), |
| 106 | ), |
| 107 | TimeUnit::Microsecond => Arc::new( |
| 108 | create_random_temporal_array::<TimestampMicrosecondType>(size, null_density) |
| 109 | .with_timezone_opt(tz.clone()), |
| 110 | ), |
| 111 | TimeUnit::Nanosecond => Arc::new( |
| 112 | create_random_temporal_array::<TimestampNanosecondType>(size, null_density) |
| 113 | .with_timezone_opt(tz.clone()), |
| 114 | ), |
| 115 | }, |
| 116 | Date32 => Arc::new(create_random_temporal_array::<Date32Type>( |
| 117 | size, |
| 118 | null_density, |
| 119 | )), |
| 120 | Date64 => Arc::new(create_random_temporal_array::<Date64Type>( |
| 121 | size, |
| 122 | null_density, |
| 123 | )), |