Skip to content
Open
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
85 changes: 73 additions & 12 deletions datafusion/functions/benches/datetime_expressions/date_trunc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
use std::hint::black_box;
use std::sync::Arc;

use arrow::array::{Array, ArrayRef, TimestampNanosecondArray, TimestampSecondArray};
use arrow::array::{
Array, ArrayRef, TimestampMicrosecondArray, TimestampMillisecondArray,
TimestampNanosecondArray, TimestampSecondArray,
};
use arrow::datatypes::Field;
use criterion::{Criterion, criterion_group};
use datafusion_common::ScalarValue;
Expand All @@ -19,6 +22,8 @@ const NUM_ROWS: usize = 1000;
const NANOS_PER_SECOND: i64 = 1_000_000_000;
/// Roughly 30 years, so that values span many months, quarters and years.
const RANGE_SECONDS: i64 = 30 * 365 * 24 * 60 * 60;
/// 400 years, a whole number of days and weeks, in microseconds.
const MICROS_400_YEARS: i64 = 146_097 * 24 * 60 * 60 * 1_000_000;

fn seedable_rng() -> StdRng {
StdRng::seed_from_u64(42)
Expand All @@ -31,16 +36,48 @@ fn second_timestamps() -> TimestampSecondArray {
.collect()
}

fn nanosecond_timestamps() -> TimestampNanosecondArray {
/// Timestamps within [`RANGE_SECONDS`] of the epoch, in units of which there
/// are `per_second` in a second.
fn timestamps(per_second: i64) -> Vec<i64> {
let mut rng = seedable_rng();
(0..NUM_ROWS)
.map(|_| {
let seconds = rng.random_range(-RANGE_SECONDS..RANGE_SECONDS);
Some(seconds * NANOS_PER_SECOND + rng.random_range(0..NANOS_PER_SECOND))
seconds * per_second + rng.random_range(0..per_second)
})
.collect()
}

/// [`timestamps`] in each time unit, in `tz`.
fn timestamps_in_each_unit(tz: Option<&str>) -> [(&'static str, ArrayRef); 4] {
[
(
"nanos",
Arc::new(
TimestampNanosecondArray::from(timestamps(NANOS_PER_SECOND))
.with_timezone_opt(tz),
),
),
(
"micros",
Arc::new(
TimestampMicrosecondArray::from(timestamps(1_000_000))
.with_timezone_opt(tz),
),
),
(
"millis",
Arc::new(
TimestampMillisecondArray::from(timestamps(1_000)).with_timezone_opt(tz),
),
),
(
"seconds",
Arc::new(TimestampSecondArray::from(timestamps(1)).with_timezone_opt(tz)),
),
]
}

fn run_benchmark(c: &mut Criterion, name: &str, granularity: &str, array: ArrayRef) {
let batch_len = array.len();
let precision =
Expand Down Expand Up @@ -83,16 +120,40 @@ fn criterion_benchmark(c: &mut Criterion) {
run_benchmark(c, "date_trunc_minute_1000", "minute", Arc::clone(&seconds));
run_benchmark(c, "date_trunc_month_second_1000", "month", seconds);

// Coarse granularities on an untimezoned array: these need calendar
// arithmetic rather than a plain division.
let nanos: ArrayRef = Arc::new(nanosecond_timestamps());
for granularity in ["week", "month", "quarter", "year"] {
run_benchmark(
c,
&format!("date_trunc_{granularity}_nanos_1000"),
granularity,
Arc::clone(&nanos),
// Each time unit, with and without a timezone. Without one, granularities
// from a week up need calendar arithmetic rather than a plain division;
// with one, those from an hour up are truncated in local time.
for (tz, suffix) in [(None, ""), (Some("America/New_York"), "_tz")] {
for (unit, array) in timestamps_in_each_unit(tz) {
for granularity in
["second", "hour", "day", "week", "month", "quarter", "year"]
{
run_benchmark(
c,
&format!("date_trunc_{granularity}_{unit}{suffix}_1000"),
granularity,
Arc::clone(&array),
);
}
}

// Before the range of a nanosecond timestamp: around 1570
let before_1677: ArrayRef = Arc::new(
TimestampMicrosecondArray::from_iter_values(
timestamps(1_000_000)
.into_iter()
.map(|value| value - MICROS_400_YEARS),
)
.with_timezone_opt(tz),
);
for granularity in ["day", "month", "year"] {
run_benchmark(
c,
&format!("date_trunc_{granularity}_micros_before_1677{suffix}_1000"),
granularity,
Arc::clone(&before_1677),
);
}
}
}

Expand Down
Loading