Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions datafusion/functions/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ harness = false
name = "gcd"
required-features = ["math_expressions"]

[[bench]]
harness = false
name = "nanvl"
required-features = ["math_expressions"]

[[bench]]
harness = false
name = "uuid"
Expand Down
114 changes: 114 additions & 0 deletions datafusion/functions/benches/nanvl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
// 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.

extern crate criterion;

use arrow::array::{ArrayRef, Float32Array, Float64Array};
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};
use datafusion_functions::math::nanvl;
use std::hint::black_box;
use std::sync::Arc;

fn criterion_benchmark(c: &mut Criterion) {
let nanvl_fn = nanvl();
let config_options = Arc::new(ConfigOptions::default());

// Scalar benchmarks
c.bench_function("nanvl/scalar_f64", |b| {
let args = ScalarFunctionArgs {
args: vec![
ColumnarValue::Scalar(ScalarValue::Float64(Some(f64::NAN))),
ColumnarValue::Scalar(ScalarValue::Float64(Some(1.0))),
],
arg_fields: vec![
Field::new("a", DataType::Float64, true).into(),
Field::new("b", DataType::Float64, true).into(),
],
number_rows: 1,
return_field: Field::new("f", DataType::Float64, true).into(),
config_options: Arc::clone(&config_options),
};

b.iter(|| black_box(nanvl_fn.invoke_with_args(args.clone()).unwrap()))
});

c.bench_function("nanvl/scalar_f32", |b| {
let args = ScalarFunctionArgs {
args: vec![
ColumnarValue::Scalar(ScalarValue::Float32(Some(f32::NAN))),
ColumnarValue::Scalar(ScalarValue::Float32(Some(1.0))),
],
arg_fields: vec![
Field::new("a", DataType::Float32, true).into(),
Field::new("b", DataType::Float32, true).into(),
],
number_rows: 1,
return_field: Field::new("f", DataType::Float32, true).into(),
config_options: Arc::clone(&config_options),
};

b.iter(|| black_box(nanvl_fn.invoke_with_args(args.clone()).unwrap()))
});

// Array benchmarks
for size in [1024, 4096, 8192] {
let a64: ArrayRef = Arc::new(Float64Array::from(vec![f64::NAN; size]));
let b64: ArrayRef = Arc::new(Float64Array::from(vec![1.0; size]));
c.bench_function(&format!("nanvl/array_f64/{size}"), |bench| {
let args = ScalarFunctionArgs {
args: vec![
ColumnarValue::Array(Arc::clone(&a64)),
ColumnarValue::Array(Arc::clone(&b64)),
],
arg_fields: vec![
Field::new("a", DataType::Float64, true).into(),
Field::new("b", DataType::Float64, true).into(),
],
number_rows: size,
return_field: Field::new("f", DataType::Float64, true).into(),
config_options: Arc::clone(&config_options),
};
bench.iter(|| black_box(nanvl_fn.invoke_with_args(args.clone()).unwrap()))
});

let a32: ArrayRef = Arc::new(Float32Array::from(vec![f32::NAN; size]));
let b32: ArrayRef = Arc::new(Float32Array::from(vec![1.0; size]));
c.bench_function(&format!("nanvl/array_f32/{size}"), |bench| {
let args = ScalarFunctionArgs {
args: vec![
ColumnarValue::Array(Arc::clone(&a32)),
ColumnarValue::Array(Arc::clone(&b32)),
],
arg_fields: vec![
Field::new("a", DataType::Float32, true).into(),
Field::new("b", DataType::Float32, true).into(),
],
number_rows: size,
return_field: Field::new("f", DataType::Float32, true).into(),
config_options: Arc::clone(&config_options),
};
bench.iter(|| black_box(nanvl_fn.invoke_with_args(args.clone()).unwrap()))
});
}
}

criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);
53 changes: 51 additions & 2 deletions datafusion/functions/src/math/nanvl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ use crate::utils::make_scalar_function;
use arrow::array::{ArrayRef, AsArray, Float16Array, Float32Array, Float64Array};
use arrow::datatypes::DataType::{Float16, Float32, Float64};
use arrow::datatypes::{DataType, Float16Type, Float32Type, Float64Type};
use datafusion_common::{DataFusionError, Result, exec_err};
use datafusion_common::{
DataFusionError, Result, ScalarValue, exec_err, internal_err,
utils::take_function_args,
};
use datafusion_expr::TypeSignature::Exact;
use datafusion_expr::{
ColumnarValue, Documentation, ScalarFunctionArgs, ScalarUDFImpl, Signature,
Expand Down Expand Up @@ -101,7 +104,53 @@ impl ScalarUDFImpl for NanvlFunc {
}

fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> {
make_scalar_function(nanvl, vec![])(&args.args)
let [x, y] = take_function_args(self.name(), args.args)?;

match (&x, &y) {
(ColumnarValue::Scalar(x), ColumnarValue::Scalar(y)) => {
// NULL propagation
if x.is_null() || y.is_null() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is quite right; from Spark:

>>> spark.sql("select nanvl(null, 1)").show()
+--------------+
|nanvl(NULL, 1)|
+--------------+
|          NULL|
+--------------+

>>> spark.sql("select nanvl(1, null)").show()
+--------------+
|nanvl(1, NULL)|
+--------------+
|           1.0|
+--------------+

return match (x.data_type(), y.data_type()) {
(Float16, Float16) => {
Ok(ColumnarValue::Scalar(ScalarValue::Float16(None)))
}
(Float32, Float32) => {
Ok(ColumnarValue::Scalar(ScalarValue::Float32(None)))
}
(Float64, Float64) => {
Ok(ColumnarValue::Scalar(ScalarValue::Float64(None)))
}
_ => internal_err!(
"Unexpected datatypes for nanvl: {}, {}",
x.data_type(),
y.data_type()
),
};
}

let out = match (x, y) {
(ScalarValue::Float64(Some(xv)), ScalarValue::Float64(Some(yv))) => {
ScalarValue::Float64(Some(if xv.is_nan() { *yv } else { *xv }))
}
(ScalarValue::Float32(Some(xv)), ScalarValue::Float32(Some(yv))) => {
ScalarValue::Float32(Some(if xv.is_nan() { *yv } else { *xv }))
}
(ScalarValue::Float16(Some(xv)), ScalarValue::Float16(Some(yv))) => {
ScalarValue::Float16(Some(if xv.is_nan() { *yv } else { *xv }))
}
_ => {
return internal_err!(
"Unexpected scalar types for nanvl: {}, {}",
x.data_type(),
y.data_type()
);
}
};

Ok(ColumnarValue::Scalar(out))
}
_ => make_scalar_function(nanvl, vec![])(&[x, y]),
}
}

fn documentation(&self) -> Option<&Documentation> {
Expand Down