kosiew commented on code in PR #23350: URL: https://github.com/apache/datafusion/pull/23350#discussion_r3549214068
########## datafusion/functions/benches/date_part.rs: ########## @@ -0,0 +1,345 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +use std::hint::black_box; +use std::sync::Arc; + +use arrow::array::types::{IntervalDayTime, IntervalMonthDayNano}; +use arrow::array::{ + Array, ArrayRef, Date32Array, Date64Array, DurationNanosecondArray, + IntervalDayTimeArray, IntervalMonthDayNanoArray, IntervalYearMonthArray, + Time32MillisecondArray, Time32SecondArray, Time64MicrosecondArray, + Time64NanosecondArray, TimestampMicrosecondArray, TimestampMillisecondArray, + TimestampNanosecondArray, TimestampSecondArray, +}; +use arrow::datatypes::{DataType, Field}; +use criterion::{Criterion, criterion_group, criterion_main}; +use datafusion_common::ScalarValue; +use datafusion_common::config::ConfigOptions; +use datafusion_expr::{ColumnarValue, ScalarFunctionArgs, ScalarUDF}; +use datafusion_functions::datetime::date_part; +use rand::Rng; +use rand::rngs::ThreadRng; + +const BATCH_SIZE: usize = 1000; +const TS_BOUND: i64 = 2_006_463_600; +const SEC_DAY: i64 = 86_400; + +fn generate_timestamp_ns_array(rng: &mut ThreadRng) -> TimestampNanosecondArray { + TimestampNanosecondArray::from( + (0..BATCH_SIZE) + .map(|_| rng.random_range(0..TS_BOUND * 1_000_000_000)) + .collect::<Vec<_>>(), + ) +} + +fn generate_timestamp_us_array(rng: &mut ThreadRng) -> TimestampMicrosecondArray { + TimestampMicrosecondArray::from( + (0..BATCH_SIZE) + .map(|_| rng.random_range(0..TS_BOUND * 1_000_000)) + .collect::<Vec<_>>(), + ) +} + +fn generate_timestamp_ms_array(rng: &mut ThreadRng) -> TimestampMillisecondArray { + TimestampMillisecondArray::from( + (0..BATCH_SIZE) + .map(|_| rng.random_range(0..TS_BOUND * 1_000)) + .collect::<Vec<_>>(), + ) +} + +fn generate_timestamp_s_array(rng: &mut ThreadRng) -> TimestampSecondArray { + TimestampSecondArray::from( + (0..BATCH_SIZE) + .map(|_| rng.random_range(0..TS_BOUND)) + .collect::<Vec<_>>(), + ) +} + +fn generate_date32_array(rng: &mut ThreadRng) -> Date32Array { + Date32Array::from( + (0..BATCH_SIZE) + .map(|_| rng.random_range(0..30_000)) + .collect::<Vec<_>>(), + ) +} + +fn generate_date64_array(rng: &mut ThreadRng) -> Date64Array { + Date64Array::from( + (0..BATCH_SIZE) + .map(|_| rng.random_range(0i64..30_000)) + .collect::<Vec<_>>(), + ) +} + +fn generate_time32_second_array(rng: &mut ThreadRng) -> Time32SecondArray { + Time32SecondArray::from( + (0..BATCH_SIZE) + .map(|_| rng.random_range(0..SEC_DAY as i32)) + .collect::<Vec<_>>(), + ) +} + +fn generate_time32_millisecond_array(rng: &mut ThreadRng) -> Time32MillisecondArray { + Time32MillisecondArray::from( + (0..BATCH_SIZE) + .map(|_| rng.random_range(0..(SEC_DAY * 1_000) as i32)) + .collect::<Vec<_>>(), + ) +} + +fn generate_time64_microsecond_array(rng: &mut ThreadRng) -> Time64MicrosecondArray { + Time64MicrosecondArray::from( + (0..BATCH_SIZE) + .map(|_| rng.random_range(0..SEC_DAY * 1_000_000)) + .collect::<Vec<_>>(), + ) +} + +fn generate_time64_nanosecond_array(rng: &mut ThreadRng) -> Time64NanosecondArray { + Time64NanosecondArray::from( + (0..BATCH_SIZE) + .map(|_| rng.random_range(0..SEC_DAY * 1_000_000_000)) + .collect::<Vec<_>>(), + ) +} + +fn generate_interval_year_month_array(rng: &mut ThreadRng) -> IntervalYearMonthArray { + IntervalYearMonthArray::from( + (0..BATCH_SIZE) + .map(|_| rng.random_range(0..1_200)) + .collect::<Vec<_>>(), + ) +} + +fn generate_interval_day_time_array(rng: &mut ThreadRng) -> IntervalDayTimeArray { + IntervalDayTimeArray::from( + (0..BATCH_SIZE) + .map(|_| IntervalDayTime { + days: rng.random_range(0..365), + milliseconds: rng.random_range(0..(SEC_DAY * 1_000) as i32), + }) + .collect::<Vec<_>>(), + ) +} + +fn generate_interval_mdn_array(rng: &mut ThreadRng) -> IntervalMonthDayNanoArray { + IntervalMonthDayNanoArray::from( + (0..BATCH_SIZE) + .map(|_| IntervalMonthDayNano { + months: rng.random_range(0..120), + days: rng.random_range(0..365), + nanoseconds: rng.random_range(0..SEC_DAY * 1_000_000_000), + }) + .collect::<Vec<_>>(), + ) +} + +fn generate_duration_nanosecond_array(rng: &mut ThreadRng) -> DurationNanosecondArray { + DurationNanosecondArray::from( + (0..BATCH_SIZE) + .map(|_| rng.random_range(0..TS_BOUND * 1_000_000_000)) + .collect::<Vec<_>>(), + ) +} + +fn bench_date_part( + c: &mut Criterion, + udf: &Arc<ScalarUDF>, + bench_name: &str, + part: &str, + array: ArrayRef, + return_type: DataType, +) { + let batch_len = array.len(); + let part_cv = ColumnarValue::Scalar(ScalarValue::Utf8(Some(part.to_string()))); + let array_cv = ColumnarValue::Array(array); + let return_field = Arc::new(Field::new("date_part", return_type, true)); + let arg_fields = vec![ + Field::new("a", part_cv.data_type(), true).into(), + Field::new("b", array_cv.data_type(), true).into(), + ]; + let config_options = Arc::new(ConfigOptions::default()); + + c.bench_function(bench_name, |b| { + b.iter(|| { + black_box( + udf.invoke_with_args(ScalarFunctionArgs { + args: vec![part_cv.clone(), array_cv.clone()], + arg_fields: arg_fields.clone(), + number_rows: batch_len, + return_field: Arc::clone(&return_field), + config_options: Arc::clone(&config_options), + }) + .expect("date_part should work on valid values"), + ) + }) + }); +} + +fn criterion_benchmark(c: &mut Criterion) { + let mut rng = rand::rng(); Review Comment: Small benchmark hygiene suggestion: the data is regenerated from `ThreadRng` on each run. That can make Criterion comparisons a bit noisier when looking into regressions. Using a seeded `StdRng` would keep the benchmark data stable across runs. ########## datafusion/functions/benches/date_part.rs: ########## @@ -0,0 +1,345 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +use std::hint::black_box; +use std::sync::Arc; + +use arrow::array::types::{IntervalDayTime, IntervalMonthDayNano}; +use arrow::array::{ + Array, ArrayRef, Date32Array, Date64Array, DurationNanosecondArray, + IntervalDayTimeArray, IntervalMonthDayNanoArray, IntervalYearMonthArray, + Time32MillisecondArray, Time32SecondArray, Time64MicrosecondArray, + Time64NanosecondArray, TimestampMicrosecondArray, TimestampMillisecondArray, + TimestampNanosecondArray, TimestampSecondArray, +}; +use arrow::datatypes::{DataType, Field}; +use criterion::{Criterion, criterion_group, criterion_main}; +use datafusion_common::ScalarValue; +use datafusion_common::config::ConfigOptions; +use datafusion_expr::{ColumnarValue, ScalarFunctionArgs, ScalarUDF}; +use datafusion_functions::datetime::date_part; +use rand::Rng; +use rand::rngs::ThreadRng; + +const BATCH_SIZE: usize = 1000; +const TS_BOUND: i64 = 2_006_463_600; +const SEC_DAY: i64 = 86_400; + +fn generate_timestamp_ns_array(rng: &mut ThreadRng) -> TimestampNanosecondArray { + TimestampNanosecondArray::from( + (0..BATCH_SIZE) + .map(|_| rng.random_range(0..TS_BOUND * 1_000_000_000)) + .collect::<Vec<_>>(), + ) +} + +fn generate_timestamp_us_array(rng: &mut ThreadRng) -> TimestampMicrosecondArray { + TimestampMicrosecondArray::from( + (0..BATCH_SIZE) + .map(|_| rng.random_range(0..TS_BOUND * 1_000_000)) + .collect::<Vec<_>>(), + ) +} + +fn generate_timestamp_ms_array(rng: &mut ThreadRng) -> TimestampMillisecondArray { + TimestampMillisecondArray::from( + (0..BATCH_SIZE) + .map(|_| rng.random_range(0..TS_BOUND * 1_000)) + .collect::<Vec<_>>(), + ) +} + +fn generate_timestamp_s_array(rng: &mut ThreadRng) -> TimestampSecondArray { + TimestampSecondArray::from( + (0..BATCH_SIZE) + .map(|_| rng.random_range(0..TS_BOUND)) + .collect::<Vec<_>>(), + ) +} + +fn generate_date32_array(rng: &mut ThreadRng) -> Date32Array { + Date32Array::from( + (0..BATCH_SIZE) + .map(|_| rng.random_range(0..30_000)) + .collect::<Vec<_>>(), + ) +} + +fn generate_date64_array(rng: &mut ThreadRng) -> Date64Array { Review Comment: Nice addition. One small thought: `Date64Array` values are milliseconds since epoch, but this generator uses `0..30_000`, so every value lands within the first 30 seconds of 1970-01-01. That still exercises the Date64 branch, but the calendar-part benchmarks like `year`, `month`, `week`, and `day` may be less representative than `Date32`. Could be worth generating something like `days * SEC_DAY * 1_000`, or another millisecond date range, so Date64 measures more realistic date extraction work. -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
