use arrow::array::{RecordBatch, builder::Int32Builder};
use arrow::datatypes::{DataType, Field, Schema};
use criterion::{Criterion, criterion_group, criterion_main};
use datafusion_physical_expr::expressions::{Column, IsNotNullExpr, IsNullExpr};
use datafusion_physical_expr_common::physical_expr::PhysicalExpr;
use std::hint::black_box;
use std::sync::Arc;
fn criterion_benchmark(c: &mut Criterion) {
let mut c1 = Int32Builder::new();
let mut c2 = Int32Builder::new();
let mut c3 = Int32Builder::new();
for i in 0..1000 {
c1.append_null();
c2.append_value(i);
if i % 7 == 0 {
c3.append_null();
} else {
c3.append_value(i);
}
}
let c1 = Arc::new(c1.finish());
let c2 = Arc::new(c2.finish());
let c3 = Arc::new(c3.finish());
let schema = Schema::new(vec![
Field::new("c1", DataType::Int32, true),
Field::new("c2", DataType::Int32, false),
Field::new("c3", DataType::Int32, true),
]);
let batch = RecordBatch::try_new(Arc::new(schema), vec![c1, c2, c3]).unwrap();
c.bench_function("is_null: column is all nulls", |b| {
let expr = is_null("c1", 0);
b.iter(|| black_box(expr.evaluate(black_box(&batch)).unwrap()))
});
c.bench_function("is_null: column is never null", |b| {
let expr = is_null("c2", 1);
b.iter(|| black_box(expr.evaluate(black_box(&batch)).unwrap()))
});
c.bench_function("is_null: column is mix of values and nulls", |b| {
let expr = is_null("c3", 2);
b.iter(|| black_box(expr.evaluate(black_box(&batch)).unwrap()))
});
c.bench_function("is_not_null: column is all nulls", |b| {
let expr = is_not_null("c1", 0);
b.iter(|| black_box(expr.evaluate(black_box(&batch)).unwrap()))
});
c.bench_function("is_not_null: column is never null", |b| {
let expr = is_not_null("c2", 1);
b.iter(|| black_box(expr.evaluate(black_box(&batch)).unwrap()))
});
c.bench_function("is_not_null: column is mix of values and nulls", |b| {
let expr = is_not_null("c3", 2);
b.iter(|| black_box(expr.evaluate(black_box(&batch)).unwrap()))
});
}
fn is_null(name: &str, index: usize) -> Arc<dyn PhysicalExpr> {
Arc::new(IsNullExpr::new(Arc::new(Column::new(name, index))))
}
fn is_not_null(name: &str, index: usize) -> Arc<dyn PhysicalExpr> {
Arc::new(IsNotNullExpr::new(Arc::new(Column::new(name, index))))
}
criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);