Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
21 changes: 14 additions & 7 deletions datafusion/functions/src/math/iszero.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ use arrow::array::{ArrayRef, AsArray, BooleanArray};
use arrow::datatypes::DataType::{Boolean, Float32, Float64};
use arrow::datatypes::{DataType, Float32Type, Float64Type};

use datafusion_common::{Result, exec_err};
use datafusion_expr::TypeSignature::Exact;
use datafusion_common::types::NativeType;
use datafusion_common::{Result, ScalarValue, exec_err};
use datafusion_expr::{Coercion, TypeSignatureClass};
use datafusion_expr::{
ColumnarValue, Documentation, ScalarFunctionArgs, ScalarUDFImpl, Signature,
Volatility,
Expand Down Expand Up @@ -59,12 +60,14 @@ impl Default for IsZeroFunc {

impl IsZeroFunc {
pub fn new() -> Self {
use DataType::*;
// Accept any numeric type and coerce to float
let float = Coercion::new_implicit(
TypeSignatureClass::Float,
Copy link
Member

Choose a reason for hiding this comment

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

This also includes Float16 but fn iszero() below currently handles only Float32 and Float64.

vec![TypeSignatureClass::Numeric],
NativeType::Float64,
);
Self {
signature: Signature::one_of(
vec![Exact(vec![Float32]), Exact(vec![Float64])],
Volatility::Immutable,
),
signature: Signature::coercible(vec![float], Volatility::Immutable),
}
}
}
Expand All @@ -87,6 +90,10 @@ impl ScalarUDFImpl for IsZeroFunc {
}

fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> {
// Handle NULL input
if args.args[0].data_type().is_null() {
return Ok(ColumnarValue::Scalar(ScalarValue::Boolean(None)));
}
make_scalar_function(iszero, vec![])(&args.args)
}

Expand Down
25 changes: 15 additions & 10 deletions datafusion/functions/src/math/nans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
//! Math function: `isnan()`.

use arrow::datatypes::{DataType, Float32Type, Float64Type};
use datafusion_common::{Result, exec_err};
use datafusion_expr::{ColumnarValue, ScalarFunctionArgs, TypeSignature};
use datafusion_common::types::NativeType;
use datafusion_common::{Result, ScalarValue, exec_err};
use datafusion_expr::{Coercion, ColumnarValue, ScalarFunctionArgs, TypeSignatureClass};

use arrow::array::{ArrayRef, AsArray, BooleanArray};
use datafusion_expr::{Documentation, ScalarUDFImpl, Signature, Volatility};
Expand Down Expand Up @@ -54,15 +55,14 @@ impl Default for IsNanFunc {

impl IsNanFunc {
pub fn new() -> Self {
use DataType::*;
// Accept any numeric type and coerce to float
let float = Coercion::new_implicit(
TypeSignatureClass::Float,
vec![TypeSignatureClass::Numeric],
NativeType::Float64,
);
Self {
signature: Signature::one_of(
vec![
TypeSignature::Exact(vec![Float32]),
TypeSignature::Exact(vec![Float64]),
],
Volatility::Immutable,
),
signature: Signature::coercible(vec![float], Volatility::Immutable),
}
}
}
Expand All @@ -84,6 +84,11 @@ impl ScalarUDFImpl for IsNanFunc {
}

fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> {
// Handle NULL input
if args.args[0].data_type().is_null() {
return Ok(ColumnarValue::Scalar(ScalarValue::Boolean(None)));
}

let args = ColumnarValue::values_to_arrays(&args.args)?;

let arr: ArrayRef = match args[0].data_type() {
Expand Down
35 changes: 29 additions & 6 deletions datafusion/functions/src/math/nanvl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ use crate::utils::make_scalar_function;
use arrow::array::{ArrayRef, AsArray, Float32Array, Float64Array};
use arrow::datatypes::DataType::{Float32, Float64};
use arrow::datatypes::{DataType, Float32Type, Float64Type};
use datafusion_common::{DataFusionError, Result, exec_err};
use datafusion_expr::TypeSignature::Exact;
use datafusion_common::types::NativeType;
use datafusion_common::{DataFusionError, Result, ScalarValue, exec_err};
use datafusion_expr::{Coercion, TypeSignatureClass};
use datafusion_expr::{
ColumnarValue, Documentation, ScalarFunctionArgs, ScalarUDFImpl, Signature,
Volatility,
Expand Down Expand Up @@ -66,10 +67,14 @@ impl Default for NanvlFunc {

impl NanvlFunc {
pub fn new() -> Self {
use DataType::*;
let float = Coercion::new_implicit(
TypeSignatureClass::Float,
vec![TypeSignatureClass::Numeric],
NativeType::Float64,
);
Self {
signature: Signature::one_of(
vec![Exact(vec![Float32, Float32]), Exact(vec![Float64, Float64])],
signature: Signature::coercible(
vec![float.clone(), float],
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we might actually be better off leaving this as exact for now, since the coercible API doesn't have a good way to ensure both arguments are of the same type

But we can include the f16 support, like

SIgnature::one_of(
    vec![Exact(vec![Float16, Float16]), Exact(vec![Float32, Float32]), Exact(vec![Float64, Float64])]
)

Volatility::Immutable,
),
}
Expand Down Expand Up @@ -97,7 +102,25 @@ impl ScalarUDFImpl for NanvlFunc {
}

fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> {
make_scalar_function(nanvl, vec![])(&args.args)
if args.arg_fields.iter().any(|f| f.data_type().is_null()) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure about the handling here. Would like some feedback

return ColumnarValue::Scalar(ScalarValue::Null)
.cast_to(args.return_type(), None);
}

let target_type = args.return_type();
if !matches!(target_type, Float32 | Float64) {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if !matches!(target_type, Float32 | Float64) {
if !matches!(target_type, Float16 | Float32 | Float64) {

return exec_err!(
"Unsupported return type {target_type:?} for function nanvl"
);
}

let casted_args = args
.args
.iter()
.map(|arg| arg.cast_to(target_type, None))
.collect::<Result<Vec<_>>>()?;

make_scalar_function(nanvl, vec![])(&casted_args)
}

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