Skip to content

Commit

Permalink
fix: panic in isnan() when no args are given (#9377)
Browse files Browse the repository at this point in the history
* fix: panic in isnan() when no args are given

* test: add sqllogictest for abs/acos/isnan
  • Loading branch information
SteveLauC authored Feb 28, 2024
1 parent b220f03 commit 19d892a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
23 changes: 17 additions & 6 deletions datafusion/functions/src/math/nans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
//! Math function: `isnan()`.
use arrow::datatypes::DataType;
use datafusion_common::{exec_err, DataFusionError, Result};
use datafusion_common::{exec_err, DataFusionError, Result, plan_datafusion_err};
use datafusion_expr::ColumnarValue;

use arrow::array::{ArrayRef, BooleanArray, Float32Array, Float64Array};
use datafusion_expr::TypeSignature::*;
use datafusion_expr::{ScalarUDFImpl, Signature, Volatility};
use datafusion_expr::{ScalarUDFImpl, Signature, Volatility, utils::generate_signature_error_msg};
use std::any::Any;
use std::sync::Arc;

Expand Down Expand Up @@ -57,7 +57,18 @@ impl ScalarUDFImpl for IsNanFunc {
&self.signature
}

fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> {
fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> {
if arg_types.len() != 1 {
return Err(plan_datafusion_err!(
"{}",
generate_signature_error_msg(
self.name(),
self.signature().clone(),
arg_types,
)
));
}

Ok(DataType::Boolean)
}

Expand All @@ -68,7 +79,7 @@ impl ScalarUDFImpl for IsNanFunc {
DataType::Float64 => {
Arc::new(make_function_scalar_inputs_return_type!(
&args[0],
"x",
self.name(),
Float64Array,
BooleanArray,
{ f64::is_nan }
Expand All @@ -77,13 +88,13 @@ impl ScalarUDFImpl for IsNanFunc {
DataType::Float32 => {
Arc::new(make_function_scalar_inputs_return_type!(
&args[0],
"x",
self.name(),
Float32Array,
BooleanArray,
{ f32::is_nan }
))
}
other => return exec_err!("Unsupported data type {other:?} for function isnan"),
other => return exec_err!("Unsupported data type {other:?} for function {}", self.name()),
};
Ok(ColumnarValue::Array(arr))
}
Expand Down
10 changes: 10 additions & 0 deletions datafusion/sqllogictest/test_files/scalar.slt
Original file line number Diff line number Diff line change
Expand Up @@ -1874,6 +1874,16 @@ SELECT arrow_typeof(1, 1);
statement error Error during planning: No function matches the given name and argument types 'power\(Int64, Int64, Int64\)'. You might need to add explicit type casts.\n\tCandidate functions:\n\tpower\(Int64, Int64\)\n\tpower\(Float64, Float64\)
SELECT power(1, 2, 3);

# The following functions need 1 argument
statement error Error during planning: No function matches the given name and argument types 'abs\(\)'. You might need to add explicit type casts.\n\tCandidate functions:\n\tabs\(Any\)
SELECT abs();

statement error Error during planning: No function matches the given name and argument types 'acos\(\)'. You might need to add explicit type casts.\n\tCandidate functions:\n\tacos\(Float64/Float32\)
SELECT acos();

statement error Error during planning: No function matches the given name and argument types 'isnan\(\)'. You might need to add explicit type casts.\n\tCandidate functions:\n\tisnan\(Float32\)\n\tisnan\(Float64\)
SELECT isnan();

# turn off enable_ident_normalization
statement ok
set datafusion.sql_parser.enable_ident_normalization = false;
Expand Down

0 comments on commit 19d892a

Please sign in to comment.