alamb commented on code in PR #13041: URL: https://github.com/apache/datafusion/pull/13041#discussion_r1813965585
########## test-utils/src/array_gen/primitive.rs: ########## @@ -35,46 +34,66 @@ pub struct PrimitiveArrayGenerator { pub rng: StdRng, } -macro_rules! impl_gen_data { - ($NATIVE_TYPE:ty, $ARROW_TYPE:ident) => { - paste::paste! { - pub fn [< gen_data_ $NATIVE_TYPE >](&mut self) -> ArrayRef { - // table of strings from which to draw - let distinct_primitives: PrimitiveArray<$ARROW_TYPE> = (0..self.num_distinct_primitives) - .map(|_| Some(self.rng.gen::<$NATIVE_TYPE>())) - .collect(); +// TODO: support generating more primitive arrays +impl PrimitiveArrayGenerator { + pub fn gen_data<A>(&mut self) -> ArrayRef + where + A: ArrowPrimitiveType, + Standard: Distribution<<A as ArrowPrimitiveType>::Native>, + { + // table of primitives from which to draw + let distinct_primitives: PrimitiveArray<A> = (0..self.num_distinct_primitives) + .map(|_| { + Some(match A::DATA_TYPE { + DataType::Int8 + | DataType::Int16 + | DataType::Int32 + | DataType::Int64 + | DataType::UInt8 + | DataType::UInt16 + | DataType::UInt32 + | DataType::UInt64 + | DataType::Float32 + | DataType::Float64 + | DataType::Date32 => self.rng.gen::<A::Native>(), - // pick num_strings randomly from the distinct string table - let indicies: UInt32Array = (0..self.num_primitives) - .map(|_| { - if self.rng.gen::<f64>() < self.null_pct { - None - } else if self.num_distinct_primitives > 1 { - let range = 1..(self.num_distinct_primitives as u32); - Some(self.rng.gen_range(range)) - } else { - Some(0) + DataType::Date64 => { + // TODO: constrain this range to valid dates if necessary + let date_value = self.rng.gen_range(i64::MIN..=i64::MAX); + let millis_per_day: i64 = 86_400_000; + let adjusted_value = date_value - (date_value % millis_per_day); + // SAFETY: here we can convert i64 to A::Native safely since we determine that Review Comment: this `unsafe` block is only thing I am worried about. I wonder if you could instead use ```rust let date_value = self.rng.gen_range(i64::MIN..=i64::MAX); let millis_per_day = 86_400_000; let adjusted_value = date_value - (date_value % millis_per_day); ``` And then return adjusted value 🤔 -- 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: github-unsubscr...@datafusion.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org For additional commands, e-mail: github-h...@datafusion.apache.org