Skip to content

Commit

Permalink
feat: Add random timezone to aggregate fuzz test (#13349)
Browse files Browse the repository at this point in the history
* add random tz

* fixes

* add comment
  • Loading branch information
jonathanc-n authored Nov 13, 2024
1 parent 3a7dde3 commit 99cdbcc
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
2 changes: 2 additions & 0 deletions datafusion/core/tests/fuzz_cases/aggregate_fuzz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ fn baseline_config() -> DatasetGeneratorConfig {
ColumnDescr::new("time32_ms", DataType::Time32(TimeUnit::Millisecond)),
ColumnDescr::new("time64_us", DataType::Time64(TimeUnit::Microsecond)),
ColumnDescr::new("time64_ns", DataType::Time64(TimeUnit::Nanosecond)),
// `None` is passed in here however when generating the array, it will generate
// random timezones.
ColumnDescr::new("timestamp_s", DataType::Timestamp(TimeUnit::Second, None)),
ColumnDescr::new(
"timestamp_ms",
Expand Down
1 change: 1 addition & 0 deletions test-utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ workspace = true

[dependencies]
arrow = { workspace = true }
chrono-tz = { version = "0.10.0", default-features = false }
datafusion-common = { workspace = true, default-features = true }
env_logger = { workspace = true }
paste = "1.0.15"
Expand Down
32 changes: 29 additions & 3 deletions test-utils/src/array_gen/primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@

use arrow::array::{ArrayRef, ArrowPrimitiveType, PrimitiveArray, UInt32Array};
use arrow::datatypes::DataType;
use rand::rngs::StdRng;
use rand::Rng;
use chrono_tz::{Tz, TZ_VARIANTS};
use rand::{rngs::StdRng, seq::SliceRandom, thread_rng, Rng};
use std::sync::Arc;

use super::random_data::RandomNativeData;

Expand All @@ -40,8 +41,16 @@ impl PrimitiveArrayGenerator {
where
A: ArrowPrimitiveType + RandomNativeData,
{
let data_type = match A::DATA_TYPE {
DataType::Timestamp(unit, _) => {
let timezone = Self::generate_timezone();
DataType::Timestamp(unit, timezone)
}
other => other,
};

// table of primitives from which to draw
let distinct_primitives: PrimitiveArray<A> = match A::DATA_TYPE {
let distinct_primitives: PrimitiveArray<A> = match data_type {
DataType::Int8
| DataType::Int16
| DataType::Int32
Expand Down Expand Up @@ -86,4 +95,21 @@ impl PrimitiveArrayGenerator {
let options = None;
arrow::compute::take(&distinct_primitives, &indicies, options).unwrap()
}

// Generates a random timezone or returns `None`.
///
/// Returns:
/// - `Some(Arc<String>)` containing the timezone name.
/// - `None` if no timezone is selected.
fn generate_timezone() -> Option<Arc<str>> {
let mut rng = thread_rng();

// Allows for timezones + None
let mut timezone_options: Vec<Option<&Tz>> = vec![None];
timezone_options.extend(TZ_VARIANTS.iter().map(Some));

let selected_option = timezone_options.choose(&mut rng).cloned().flatten(); // random timezone/None

selected_option.map(|tz| Arc::from(tz.name()))
}
}

0 comments on commit 99cdbcc

Please sign in to comment.