|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +//! Compare Spark's float `min` and `max` with DataFusion's; special-value semantics belong in |
| 19 | +//! tests. Both return the same values on the data without NaNs, which is checked before timing. |
| 20 | +//! |
| 21 | +//! The grouped case updates 1024 groups from eight batches. Its loop scatters into the groups, so |
| 22 | +//! it cannot vectorize, and the comparison form and bounds checks in `MinMaxGroupsAccumulator` |
| 23 | +//! decide its speed. |
| 24 | +
|
| 25 | +use arrow::array::{Array, ArrayRef, Float64Array}; |
| 26 | +use arrow::datatypes::{DataType, Field, Schema}; |
| 27 | +use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion}; |
| 28 | +use datafusion::common::ScalarValue; |
| 29 | +use datafusion::functions_aggregate::min_max::{max_udaf, min_udaf}; |
| 30 | +use datafusion::logical_expr::function::AccumulatorArgs; |
| 31 | +use datafusion::logical_expr::{AggregateUDF, EmitTo}; |
| 32 | +use datafusion::physical_expr::expressions::Column; |
| 33 | +use datafusion::physical_expr::PhysicalExpr; |
| 34 | +use datafusion_comet_spark_expr::SparkMinMax; |
| 35 | +use std::hint::black_box; |
| 36 | +use std::sync::Arc; |
| 37 | +use std::time::Duration; |
| 38 | + |
| 39 | +const ROWS: usize = 8192; |
| 40 | +const GROUPS: usize = 1024; |
| 41 | +const BATCHES: usize = 8; |
| 42 | + |
| 43 | +/// Doubles in no particular order, with a NaN in every `nan_every`-th row if it is not zero. |
| 44 | +fn values(nan_every: usize) -> ArrayRef { |
| 45 | + Arc::new(Float64Array::from_iter_values((0..ROWS).map(|i| { |
| 46 | + if nan_every > 0 && i % nan_every == 3 { |
| 47 | + f64::NAN |
| 48 | + } else { |
| 49 | + ((i * 7919) % 10007) as f64 * 0.5 - 2000.0 |
| 50 | + } |
| 51 | + }))) |
| 52 | +} |
| 53 | + |
| 54 | +fn criterion_benchmark(c: &mut Criterion) { |
| 55 | + let schema = Schema::new(vec![Field::new("d", DataType::Float64, true)]); |
| 56 | + let return_field = Arc::new(Field::new("d", DataType::Float64, true)); |
| 57 | + let exprs: [Arc<dyn PhysicalExpr>; 1] = [Arc::new(Column::new("d", 0))]; |
| 58 | + let args = |name| AccumulatorArgs { |
| 59 | + return_field: Arc::clone(&return_field), |
| 60 | + schema: &schema, |
| 61 | + expr_fields: &[], |
| 62 | + ignore_nulls: false, |
| 63 | + order_bys: &[], |
| 64 | + is_reversed: false, |
| 65 | + name, |
| 66 | + is_distinct: false, |
| 67 | + exprs: &exprs, |
| 68 | + }; |
| 69 | + let group_indices: Vec<usize> = (0..ROWS).map(|i| (i * 31) % GROUPS).collect(); |
| 70 | + |
| 71 | + let mut group = c.benchmark_group("min_max"); |
| 72 | + group.sample_size(20); |
| 73 | + group.warm_up_time(Duration::from_millis(250)); |
| 74 | + group.measurement_time(Duration::from_secs(1)); |
| 75 | + for is_max in [true, false] { |
| 76 | + let op = if is_max { "max" } else { "min" }; |
| 77 | + let comet = AggregateUDF::new_from_impl(SparkMinMax::new(is_max)); |
| 78 | + let datafusion = if is_max { max_udaf() } else { min_udaf() }; |
| 79 | + for (data, nan_every) in [("no_nan", 0), ("nan_10pct", 10)] { |
| 80 | + let batch = values(nan_every); |
| 81 | + let ungrouped = |udaf: &AggregateUDF| -> ScalarValue { |
| 82 | + let mut acc = udaf.accumulator(args(op)).unwrap(); |
| 83 | + acc.update_batch(&[Arc::clone(&batch)]).unwrap(); |
| 84 | + acc.evaluate().unwrap() |
| 85 | + }; |
| 86 | + let grouped = |udaf: &AggregateUDF| -> ArrayRef { |
| 87 | + let mut acc = udaf.create_groups_accumulator(args(op)).unwrap(); |
| 88 | + for _ in 0..BATCHES { |
| 89 | + acc.update_batch(&[Arc::clone(&batch)], &group_indices, None, GROUPS) |
| 90 | + .unwrap(); |
| 91 | + } |
| 92 | + acc.evaluate(EmitTo::All).unwrap() |
| 93 | + }; |
| 94 | + if nan_every == 0 { |
| 95 | + assert_eq!(ungrouped(&comet), ungrouped(&datafusion)); |
| 96 | + assert_eq!(grouped(&comet).to_data(), grouped(&datafusion).to_data()); |
| 97 | + } |
| 98 | + for (engine, udaf) in [("comet", &comet), ("datafusion", datafusion.as_ref())] { |
| 99 | + group.bench_function( |
| 100 | + BenchmarkId::new(format!("{op}_ungrouped_{engine}"), data), |
| 101 | + |b| b.iter(|| black_box(ungrouped(udaf))), |
| 102 | + ); |
| 103 | + group.bench_function( |
| 104 | + BenchmarkId::new(format!("{op}_grouped_{engine}"), data), |
| 105 | + |b| b.iter(|| black_box(grouped(udaf))), |
| 106 | + ); |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + group.finish(); |
| 111 | +} |
| 112 | + |
| 113 | +criterion_group!(benches, criterion_benchmark); |
| 114 | +criterion_main!(benches); |
0 commit comments