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::datatypes::{DataType, Float32Type, Float64Type};
21use datafusion_common::{exec_err, Result};
22use datafusion_expr::{ColumnarValue, ScalarFunctionArgs, TypeSignature};
23
24use arrow::array::{ArrayRef, AsArray, BooleanArray};
25use datafusion_expr::{Documentation, ScalarUDFImpl, Signature, Volatility};
26use datafusion_macros::user_doc;
27use std::any::Any;
28use std::sync::Arc;
29
30#[user_doc(
31    doc_section(label = "Math Functions"),
32    description = "Returns true if a given number is +NaN or -NaN otherwise returns false.",
33    syntax_example = "isnan(numeric_expression)",
34    sql_example = r#"```sql
35> SELECT isnan(1);
36+----------+
37| isnan(1) |
38+----------+
39| false    |
40+----------+
41```"#,
42    standard_argument(name = "numeric_expression", prefix = "Numeric")
43)]
44#[derive(Debug, PartialEq, Eq, Hash)]
45pub struct IsNanFunc {
46    signature: Signature,
47}
48
49impl Default for IsNanFunc {
50    fn default() -> Self {
51        Self::new()
52    }
53}
54
55impl IsNanFunc {
56    pub fn new() -> Self {
57        use DataType::*;
58        Self {
59            signature: Signature::one_of(
60                vec![
61                    TypeSignature::Exact(vec![Float32]),
62                    TypeSignature::Exact(vec![Float64]),
63                ],
64                Volatility::Immutable,
65            ),
66        }
67    }
68}
69
70impl ScalarUDFImpl for IsNanFunc {
71    fn as_any(&self) -> &dyn Any {
72        self
73    }
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 args = ColumnarValue::values_to_arrays(&args.args)?;
88
89        let arr: ArrayRef = match args[0].data_type() {
90            DataType::Float64 => Arc::new(BooleanArray::from_unary(
91                args[0].as_primitive::<Float64Type>(),
92                f64::is_nan,
93            )) as ArrayRef,
94
95            DataType::Float32 => Arc::new(BooleanArray::from_unary(
96                args[0].as_primitive::<Float32Type>(),
97                f32::is_nan,
98            )) as ArrayRef,
99            other => {
100                return exec_err!(
101                    "Unsupported data type {other:?} for function {}",
102                    self.name()
103                )
104            }
105        };
106        Ok(ColumnarValue::Array(arr))
107    }
108
109    fn documentation(&self) -> Option<&Documentation> {
110        self.doc()
111    }
112}