Jefffrey commented on code in PR #10566:
URL: https://github.com/apache/arrow-rs/pull/10566#discussion_r3725209015
##########
arrow/src/util/bench_util.rs:
##########
@@ -53,6 +60,22 @@ where
.collect()
}
+/// Same as [`create_primitive_array`] but specialized for f16 since it doesn't
+/// implement the required rand traits.
+pub fn create_nullable_f16_array(size: usize, null_density: f32) ->
Float16Array {
Review Comment:
its not ideal to need this separate function, but because of the orphan rule
we cant do
```rust
impl Distribution<f16> for StandardUniform {}
```
we can't implement it on a newtype of `f16` either since
`create_primitive_array()` needs
```rust
T: ArrowPrimitiveType,
StandardUniform: Distribution<T::Native>,
```
the only other way i think is to depend on our own random trait, something
like this that codex suggested:
```rust
#[doc(hidden)]
pub trait RandomNative: Sized {
fn random<R: rand::Rng + ?Sized>(rng: &mut R) -> Self;
}
macro_rules! impl_random_native {
($($t:ty),* $(,)?) => {
$(
impl RandomNative for $t {
fn random<R: rand::Rng + ?Sized>(rng: &mut R) -> Self {
rng.random()
}
}
)*
};
}
impl_random_native!(
i8, i16, i32, i64, i128,
u8, u16, u32, u64, u128,
f32, f64,
);
impl RandomNative for f16 {
fn random<R: rand::Rng + ?Sized>(rng: &mut R) -> Self {
f16::from_f32(rng.random::<f32>())
}
}
pub fn create_primitive_array<T>(
size: usize,
null_density: f32,
) -> PrimitiveArray<T>
where
T: ArrowPrimitiveType,
T::Native: RandomNative,
{
let mut rng = seedable_rng();
(0..size)
.map(|_| {
if rng.random::<f32>() < null_density {
None
} else {
Some(T::Native::random(&mut rng))
}
})
.collect()
}
```
not sure if this is preferable 🤔
- ideally the codex way is more invisible to users assuming they simply call
the function with a concrete type; else if they also put it inside some other
generic code it can cause issues
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]