|
| 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 Comet's float `sort_array` and `array_remove` with DataFusion's `array_sort` and |
| 19 | +//! `array_remove_all` on doubles without zeros or NaNs, where both return the same arrays, which |
| 20 | +//! is checked before timing; special-value semantics belong in tests. |
| 21 | +
|
| 22 | +use arrow::array::{Array, ArrayRef, Float64Array, ListArray}; |
| 23 | +use arrow::buffer::{NullBuffer, OffsetBuffer}; |
| 24 | +use arrow::datatypes::{DataType, Field}; |
| 25 | +use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion}; |
| 26 | +use datafusion::common::config::ConfigOptions; |
| 27 | +use datafusion::common::ScalarValue; |
| 28 | +use datafusion::functions_nested::remove::array_remove_all_udf; |
| 29 | +use datafusion::functions_nested::sort::array_sort_udf; |
| 30 | +use datafusion::logical_expr::{ColumnarValue, ScalarFunctionArgs, ScalarUDF}; |
| 31 | +use datafusion_comet_spark_expr::{SparkArrayRemove, SparkSortArray}; |
| 32 | +use std::hint::black_box; |
| 33 | +use std::sync::Arc; |
| 34 | +use std::time::Duration; |
| 35 | + |
| 36 | +const ROWS: usize = 8192; |
| 37 | + |
| 38 | +/// The value of element `i`. Values repeat so that `array_remove` finds something to remove. |
| 39 | +fn value(i: usize) -> f64 { |
| 40 | + ((i * 7919) % 101) as f64 * 0.5 + 1.0 |
| 41 | +} |
| 42 | + |
| 43 | +/// `ROWS` lists of `len` doubles, with every tenth list and every seventh element null if |
| 44 | +/// `nulls`. |
| 45 | +fn lists(len: usize, nulls: bool) -> ArrayRef { |
| 46 | + let values = if nulls { |
| 47 | + Float64Array::from_iter((0..ROWS * len).map(|i| (i % 7 != 0).then(|| value(i)))) |
| 48 | + } else { |
| 49 | + Float64Array::from_iter_values((0..ROWS * len).map(value)) |
| 50 | + }; |
| 51 | + Arc::new(ListArray::new( |
| 52 | + Arc::new(Field::new_list_field(DataType::Float64, true)), |
| 53 | + OffsetBuffer::from_lengths(std::iter::repeat_n(len, ROWS)), |
| 54 | + Arc::new(values), |
| 55 | + nulls.then(|| NullBuffer::from_iter((0..ROWS).map(|row| row % 10 != 0))), |
| 56 | + )) |
| 57 | +} |
| 58 | + |
| 59 | +fn args(args: Vec<ColumnarValue>, list: &ArrayRef) -> ScalarFunctionArgs { |
| 60 | + ScalarFunctionArgs { |
| 61 | + arg_fields: args |
| 62 | + .iter() |
| 63 | + .map(|arg| Arc::new(Field::new("arg", arg.data_type(), true))) |
| 64 | + .collect(), |
| 65 | + args, |
| 66 | + number_rows: ROWS, |
| 67 | + return_field: Arc::new(Field::new("result", list.data_type().clone(), true)), |
| 68 | + config_options: Arc::new(ConfigOptions::default()), |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +fn criterion_benchmark(c: &mut Criterion) { |
| 73 | + let mut group = c.benchmark_group("float_arrays"); |
| 74 | + group.sample_size(20); |
| 75 | + group.warm_up_time(Duration::from_millis(250)); |
| 76 | + group.measurement_time(Duration::from_secs(1)); |
| 77 | + let comet_sort = ScalarUDF::new_from_impl(SparkSortArray::default()); |
| 78 | + let comet_remove = ScalarUDF::new_from_impl(SparkArrayRemove::default()); |
| 79 | + let (datafusion_sort, datafusion_remove) = (array_sort_udf(), array_remove_all_udf()); |
| 80 | + for len in [8, 50] { |
| 81 | + for nulls in [false, true] { |
| 82 | + let list = lists(len, nulls); |
| 83 | + let data = format!("len={len}_null={nulls}"); |
| 84 | + let column = ColumnarValue::Array(Arc::clone(&list)); |
| 85 | + let scalar = |value: ScalarValue| ColumnarValue::Scalar(value); |
| 86 | + let comet_sort_args = args( |
| 87 | + vec![ |
| 88 | + column.clone(), |
| 89 | + scalar(ScalarValue::Boolean(Some(true))), |
| 90 | + scalar(ScalarValue::Boolean(Some(false))), |
| 91 | + ], |
| 92 | + &list, |
| 93 | + ); |
| 94 | + let datafusion_sort_args = args( |
| 95 | + vec![ |
| 96 | + column.clone(), |
| 97 | + scalar(ScalarValue::from("ASC")), |
| 98 | + scalar(ScalarValue::from("NULLS FIRST")), |
| 99 | + ], |
| 100 | + &list, |
| 101 | + ); |
| 102 | + let remove_args = args( |
| 103 | + vec![column.clone(), scalar(ScalarValue::Float64(Some(26.0)))], |
| 104 | + &list, |
| 105 | + ); |
| 106 | + // A value per row: the row's second element. |
| 107 | + let row_values = |
| 108 | + Float64Array::from_iter_values((0..ROWS).map(|row| value(row * len + 1))); |
| 109 | + let remove_column_args = args( |
| 110 | + vec![column, ColumnarValue::Array(Arc::new(row_values))], |
| 111 | + &list, |
| 112 | + ); |
| 113 | + let cases = [ |
| 114 | + ( |
| 115 | + "sort_array", |
| 116 | + &comet_sort, |
| 117 | + &datafusion_sort, |
| 118 | + &comet_sort_args, |
| 119 | + &datafusion_sort_args, |
| 120 | + ), |
| 121 | + ( |
| 122 | + "array_remove", |
| 123 | + &comet_remove, |
| 124 | + &datafusion_remove, |
| 125 | + &remove_args, |
| 126 | + &remove_args, |
| 127 | + ), |
| 128 | + ( |
| 129 | + "array_remove_column", |
| 130 | + &comet_remove, |
| 131 | + &datafusion_remove, |
| 132 | + &remove_column_args, |
| 133 | + &remove_column_args, |
| 134 | + ), |
| 135 | + ]; |
| 136 | + for (op, comet, datafusion, comet_args, datafusion_args) in cases { |
| 137 | + let evaluate = |udf: &ScalarUDF, args: &ScalarFunctionArgs| { |
| 138 | + udf.invoke_with_args(args.clone()) |
| 139 | + .unwrap() |
| 140 | + .into_array(ROWS) |
| 141 | + .unwrap() |
| 142 | + }; |
| 143 | + assert_eq!( |
| 144 | + evaluate(comet, comet_args).to_data(), |
| 145 | + evaluate(datafusion, datafusion_args).to_data(), |
| 146 | + "{op} {data}" |
| 147 | + ); |
| 148 | + for (engine, udf, udf_args) in [ |
| 149 | + ("comet", comet, comet_args), |
| 150 | + ("datafusion", datafusion.as_ref(), datafusion_args), |
| 151 | + ] { |
| 152 | + group.bench_function(BenchmarkId::new(format!("{op}_{engine}"), &data), |b| { |
| 153 | + b.iter(|| { |
| 154 | + black_box(udf.invoke_with_args(black_box(udf_args.clone())).unwrap()) |
| 155 | + }) |
| 156 | + }); |
| 157 | + } |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | + group.finish(); |
| 162 | +} |
| 163 | + |
| 164 | +criterion_group!(benches, criterion_benchmark); |
| 165 | +criterion_main!(benches); |
0 commit comments