Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add random timezone to aggregate fuzz test #13349

Merged
merged 3 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯

// 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};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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()))
}
}