Skip to main content

datafusion_functions/math/
nans.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18//! Math function: `isnan()`.
19
20use arrow::array::{ArrayRef, AsArray, BooleanArray};
21use arrow::datatypes::DataType::{
22    Decimal32, Decimal64, Decimal128, Decimal256, Float16, Float32, Float64, Int8, Int16,
23    Int32, Int64, Null, UInt8, UInt16, UInt32, UInt64,
24};
25use arrow::datatypes::{
26    DataType, Decimal32Type, Decimal64Type, Decimal128Type, Decimal256Type, Float16Type,
27    Float32Type, Float64Type, Int8Type, Int16Type, Int32Type, Int64Type, UInt8Type,
28    UInt16Type, UInt32Type, UInt64Type,
29};
30use datafusion_common::{Result, ScalarValue, exec_err, utils::take_function_args};
31use datafusion_expr::{
32    Coercion, ColumnarValue, Documentation, ScalarFunctionArgs, ScalarUDFImpl, Signature,
33    TypeSignatureClass, Volatility,
34};
35use datafusion_macros::user_doc;
36use std::sync::Arc;
37
38#[user_doc(
39    doc_section(label = "Math Functions"),
40    description = "Returns true if a given number is +NaN or -NaN otherwise returns false.",
41    syntax_example = "isnan(numeric_expression)",
42    sql_example = r#"```sql
43> SELECT isnan(1);
44+----------+
45| isnan(1) |
46+----------+
47| false    |
48+----------+
49```"#,
50    standard_argument(name = "numeric_expression", prefix = "Numeric")
51)]
52#[derive(Debug, PartialEq, Eq, Hash)]
53pub struct IsNanFunc {
54    signature: Signature,
55}
56
57impl Default for IsNanFunc {
58    fn default() -> Self {
59        Self::new()
60    }
61}
62
63impl IsNanFunc {
64    pub fn new() -> Self {
65        // Accept any numeric type (ints, uints, floats, decimals) without implicit casts.
66        let numeric = Coercion::new_exact(TypeSignatureClass::Numeric);
67        Self {
68            signature: Signature::coercible(vec![numeric], Volatility::Immutable),
69        }
70    }
71}
72
73impl ScalarUDFImpl for IsNanFunc {
74    fn name(&self) -> &str {
75        "isnan"
76    }
77
78    fn signature(&self) -> &Signature {
79        &self.signature
80    }
81
82    fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> {
83        Ok(DataType::Boolean)
84    }
85
86    fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> {
87        let [arg] = take_function_args(self.name(), args.args)?;
88
89        match arg {
90            ColumnarValue::Scalar(scalar) => {
91                if scalar.is_null() {
92                    return Ok(ColumnarValue::Scalar(ScalarValue::Boolean(None)));
93                }
94
95                let result = match scalar {
96                    ScalarValue::Float64(Some(v)) => Some(v.is_nan()),
97                    ScalarValue::Float32(Some(v)) => Some(v.is_nan()),
98                    ScalarValue::Float16(Some(v)) => Some(v.is_nan()),
99
100                    // Non-float numeric inputs are never NaN
101                    ScalarValue::Int8(_)
102                    | ScalarValue::Int16(_)
103                    | ScalarValue::Int32(_)
104                    | ScalarValue::Int64(_)
105                    | ScalarValue::UInt8(_)
106                    | ScalarValue::UInt16(_)
107                    | ScalarValue::UInt32(_)
108                    | ScalarValue::UInt64(_)
109                    | ScalarValue::Decimal32(_, _, _)
110                    | ScalarValue::Decimal64(_, _, _)
111                    | ScalarValue::Decimal128(_, _, _)
112                    | ScalarValue::Decimal256(_, _, _) => Some(false),
113
114                    other => {
115                        return exec_err!(
116                            "Unsupported data type {other:?} for function {}",
117                            self.name()
118                        );
119                    }
120                };
121
122                Ok(ColumnarValue::Scalar(ScalarValue::Boolean(result)))
123            }
124            ColumnarValue::Array(array) => {
125                // NOTE: BooleanArray::from_unary preserves nulls.
126                let arr: ArrayRef = match array.data_type() {
127                    Null => Arc::new(BooleanArray::new_null(array.len())) as ArrayRef,
128
129                    Float64 => Arc::new(BooleanArray::from_unary(
130                        array.as_primitive::<Float64Type>(),
131                        f64::is_nan,
132                    )) as ArrayRef,
133                    Float32 => Arc::new(BooleanArray::from_unary(
134                        array.as_primitive::<Float32Type>(),
135                        f32::is_nan,
136                    )) as ArrayRef,
137                    Float16 => Arc::new(BooleanArray::from_unary(
138                        array.as_primitive::<Float16Type>(),
139                        |x| x.is_nan(),
140                    )) as ArrayRef,
141
142                    // Non-float numeric arrays are never NaN
143                    Decimal32(_, _) => Arc::new(BooleanArray::from_unary(
144                        array.as_primitive::<Decimal32Type>(),
145                        |_| false,
146                    )) as ArrayRef,
147                    Decimal64(_, _) => Arc::new(BooleanArray::from_unary(
148                        array.as_primitive::<Decimal64Type>(),
149                        |_| false,
150                    )) as ArrayRef,
151                    Decimal128(_, _) => Arc::new(BooleanArray::from_unary(
152                        array.as_primitive::<Decimal128Type>(),
153                        |_| false,
154                    )) as ArrayRef,
155                    Decimal256(_, _) => Arc::new(BooleanArray::from_unary(
156                        array.as_primitive::<Decimal256Type>(),
157                        |_| false,
158                    )) as ArrayRef,
159
160                    Int8 => Arc::new(BooleanArray::from_unary(
161                        array.as_primitive::<Int8Type>(),
162                        |_| false,
163                    )) as ArrayRef,
164                    Int16 => Arc::new(BooleanArray::from_unary(
165                        array.as_primitive::<Int16Type>(),
166                        |_| false,
167                    )) as ArrayRef,
168                    Int32 => Arc::new(BooleanArray::from_unary(
169                        array.as_primitive::<Int32Type>(),
170                        |_| false,
171                    )) as ArrayRef,
172                    Int64 => Arc::new(BooleanArray::from_unary(
173                        array.as_primitive::<Int64Type>(),
174                        |_| false,
175                    )) as ArrayRef,
176                    UInt8 => Arc::new(BooleanArray::from_unary(
177                        array.as_primitive::<UInt8Type>(),
178                        |_| false,
179                    )) as ArrayRef,
180                    UInt16 => Arc::new(BooleanArray::from_unary(
181                        array.as_primitive::<UInt16Type>(),
182                        |_| false,
183                    )) as ArrayRef,
184                    UInt32 => Arc::new(BooleanArray::from_unary(
185                        array.as_primitive::<UInt32Type>(),
186                        |_| false,
187                    )) as ArrayRef,
188                    UInt64 => Arc::new(BooleanArray::from_unary(
189                        array.as_primitive::<UInt64Type>(),
190                        |_| false,
191                    )) as ArrayRef,
192
193                    other => {
194                        return exec_err!(
195                            "Unsupported data type {other:?} for function {}",
196                            self.name()
197                        );
198                    }
199                };
200
201                Ok(ColumnarValue::Array(arr))
202            }
203        }
204    }
205
206    fn documentation(&self) -> Option<&Documentation> {
207        self.doc()
208    }
209}