Skip to content

Commit 19d892a

Browse files
authored
fix: panic in isnan() when no args are given (#9377)
* fix: panic in isnan() when no args are given * test: add sqllogictest for abs/acos/isnan
1 parent b220f03 commit 19d892a

2 files changed

Lines changed: 27 additions & 6 deletions

File tree

datafusion/functions/src/math/nans.rs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@
1818
//! Math function: `isnan()`.
1919
2020
use arrow::datatypes::DataType;
21-
use datafusion_common::{exec_err, DataFusionError, Result};
21+
use datafusion_common::{exec_err, DataFusionError, Result, plan_datafusion_err};
2222
use datafusion_expr::ColumnarValue;
2323

2424
use arrow::array::{ArrayRef, BooleanArray, Float32Array, Float64Array};
2525
use datafusion_expr::TypeSignature::*;
26-
use datafusion_expr::{ScalarUDFImpl, Signature, Volatility};
26+
use datafusion_expr::{ScalarUDFImpl, Signature, Volatility, utils::generate_signature_error_msg};
2727
use std::any::Any;
2828
use std::sync::Arc;
2929

@@ -57,7 +57,18 @@ impl ScalarUDFImpl for IsNanFunc {
5757
&self.signature
5858
}
5959

60-
fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> {
60+
fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> {
61+
if arg_types.len() != 1 {
62+
return Err(plan_datafusion_err!(
63+
"{}",
64+
generate_signature_error_msg(
65+
self.name(),
66+
self.signature().clone(),
67+
arg_types,
68+
)
69+
));
70+
}
71+
6172
Ok(DataType::Boolean)
6273
}
6374

@@ -68,7 +79,7 @@ impl ScalarUDFImpl for IsNanFunc {
6879
DataType::Float64 => {
6980
Arc::new(make_function_scalar_inputs_return_type!(
7081
&args[0],
71-
"x",
82+
self.name(),
7283
Float64Array,
7384
BooleanArray,
7485
{ f64::is_nan }
@@ -77,13 +88,13 @@ impl ScalarUDFImpl for IsNanFunc {
7788
DataType::Float32 => {
7889
Arc::new(make_function_scalar_inputs_return_type!(
7990
&args[0],
80-
"x",
91+
self.name(),
8192
Float32Array,
8293
BooleanArray,
8394
{ f32::is_nan }
8495
))
8596
}
86-
other => return exec_err!("Unsupported data type {other:?} for function isnan"),
97+
other => return exec_err!("Unsupported data type {other:?} for function {}", self.name()),
8798
};
8899
Ok(ColumnarValue::Array(arr))
89100
}

datafusion/sqllogictest/test_files/scalar.slt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1874,6 +1874,16 @@ SELECT arrow_typeof(1, 1);
18741874
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\)
18751875
SELECT power(1, 2, 3);
18761876

1877+
# The following functions need 1 argument
1878+
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\)
1879+
SELECT abs();
1880+
1881+
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\)
1882+
SELECT acos();
1883+
1884+
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\)
1885+
SELECT isnan();
1886+
18771887
# turn off enable_ident_normalization
18781888
statement ok
18791889
set datafusion.sql_parser.enable_ident_normalization = false;

0 commit comments

Comments
 (0)