|
| 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 | +use arrow::datatypes::{Field, Schema}; |
| 19 | +use arrow::record_batch::RecordBatch; |
| 20 | +use arrow_array::builder::{Int32Builder, StringBuilder}; |
| 21 | +use arrow_schema::DataType; |
| 22 | +use criterion::{black_box, criterion_group, criterion_main, Criterion}; |
| 23 | +use datafusion_common::ScalarValue; |
| 24 | +use datafusion_expr::Operator; |
| 25 | +use datafusion_physical_expr::expressions::{BinaryExpr, CaseExpr}; |
| 26 | +use datafusion_physical_expr_common::expressions::column::Column; |
| 27 | +use datafusion_physical_expr_common::expressions::Literal; |
| 28 | +use datafusion_physical_expr_common::physical_expr::PhysicalExpr; |
| 29 | +use std::sync::Arc; |
| 30 | + |
| 31 | +fn make_col(name: &str, index: usize) -> Arc<dyn PhysicalExpr> { |
| 32 | + Arc::new(Column::new(name, index)) |
| 33 | +} |
| 34 | + |
| 35 | +fn make_lit_i32(n: i32) -> Arc<dyn PhysicalExpr> { |
| 36 | + Arc::new(Literal::new(ScalarValue::Int32(Some(n)))) |
| 37 | +} |
| 38 | + |
| 39 | +fn criterion_benchmark(c: &mut Criterion) { |
| 40 | + // create input data |
| 41 | + let mut c1 = Int32Builder::new(); |
| 42 | + let mut c2 = StringBuilder::new(); |
| 43 | + for i in 0..1000 { |
| 44 | + c1.append_value(i); |
| 45 | + if i % 7 == 0 { |
| 46 | + c2.append_null(); |
| 47 | + } else { |
| 48 | + c2.append_value(&format!("string {i}")); |
| 49 | + } |
| 50 | + } |
| 51 | + let c1 = Arc::new(c1.finish()); |
| 52 | + let c2 = Arc::new(c2.finish()); |
| 53 | + let schema = Schema::new(vec![ |
| 54 | + Field::new("c1", DataType::Int32, true), |
| 55 | + Field::new("c2", DataType::Utf8, true), |
| 56 | + ]); |
| 57 | + let batch = RecordBatch::try_new(Arc::new(schema), vec![c1, c2]).unwrap(); |
| 58 | + |
| 59 | + // use same predicate for all benchmarks |
| 60 | + let predicate = Arc::new(BinaryExpr::new( |
| 61 | + make_col("c1", 0), |
| 62 | + Operator::LtEq, |
| 63 | + make_lit_i32(500), |
| 64 | + )); |
| 65 | + |
| 66 | + // CASE WHEN expr THEN 1 ELSE 0 END |
| 67 | + c.bench_function("case_when: scalar or scalar", |b| { |
| 68 | + let expr = Arc::new( |
| 69 | + CaseExpr::try_new( |
| 70 | + None, |
| 71 | + vec![(predicate.clone(), make_lit_i32(1))], |
| 72 | + Some(make_lit_i32(0)), |
| 73 | + ) |
| 74 | + .unwrap(), |
| 75 | + ); |
| 76 | + b.iter(|| black_box(expr.evaluate(black_box(&batch)).unwrap())) |
| 77 | + }); |
| 78 | + |
| 79 | + // CASE WHEN expr THEN col ELSE null END |
| 80 | + c.bench_function("case_when: column or null", |b| { |
| 81 | + let expr = Arc::new( |
| 82 | + CaseExpr::try_new( |
| 83 | + None, |
| 84 | + vec![(predicate.clone(), make_col("c2", 1))], |
| 85 | + Some(Arc::new(Literal::new(ScalarValue::Utf8(None)))), |
| 86 | + ) |
| 87 | + .unwrap(), |
| 88 | + ); |
| 89 | + b.iter(|| black_box(expr.evaluate(black_box(&batch)).unwrap())) |
| 90 | + }); |
| 91 | +} |
| 92 | + |
| 93 | +criterion_group!(benches, criterion_benchmark); |
| 94 | +criterion_main!(benches); |
0 commit comments