datafusion_functions/math/
abs.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 expressions
19
20use std::any::Any;
21use std::sync::Arc;
22
23use arrow::array::{
24    ArrayRef, Decimal32Array, Decimal64Array, Decimal128Array, Decimal256Array,
25    Float16Array, Float32Array, Float64Array, Int8Array, Int16Array, Int32Array,
26    Int64Array,
27};
28use arrow::datatypes::DataType;
29use arrow::error::ArrowError;
30use datafusion_common::{Result, not_impl_err, utils::take_function_args};
31use datafusion_expr::interval_arithmetic::Interval;
32use datafusion_expr::sort_properties::{ExprProperties, SortProperties};
33use datafusion_expr::{
34    ColumnarValue, Documentation, ScalarFunctionArgs, ScalarUDFImpl, Signature,
35    Volatility,
36};
37use datafusion_macros::user_doc;
38use num_traits::sign::Signed;
39
40type MathArrayFunction = fn(&ArrayRef) -> Result<ArrayRef>;
41
42#[macro_export]
43macro_rules! make_abs_function {
44    ($ARRAY_TYPE:ident) => {{
45        |input: &ArrayRef| {
46            let array = downcast_named_arg!(&input, "abs arg", $ARRAY_TYPE);
47            let res: $ARRAY_TYPE = array.unary(|x| x.abs());
48            Ok(Arc::new(res) as ArrayRef)
49        }
50    }};
51}
52
53macro_rules! make_try_abs_function {
54    ($ARRAY_TYPE:ident) => {{
55        |input: &ArrayRef| {
56            let array = downcast_named_arg!(&input, "abs arg", $ARRAY_TYPE);
57            let res: $ARRAY_TYPE = array.try_unary(|x| {
58                x.checked_abs().ok_or_else(|| {
59                    ArrowError::ComputeError(format!(
60                        "{} overflow on abs({})",
61                        stringify!($ARRAY_TYPE),
62                        x
63                    ))
64                })
65            })?;
66            Ok(Arc::new(res) as ArrayRef)
67        }
68    }};
69}
70
71#[macro_export]
72macro_rules! make_wrapping_abs_function {
73    ($ARRAY_TYPE:ident) => {{
74        |input: &ArrayRef| {
75            let array = downcast_named_arg!(&input, "abs arg", $ARRAY_TYPE);
76            let res: $ARRAY_TYPE = array
77                .unary(|x| x.wrapping_abs())
78                .with_data_type(input.data_type().clone());
79            Ok(Arc::new(res) as ArrayRef)
80        }
81    }};
82}
83
84/// Abs SQL function
85/// Return different implementations based on input datatype to reduce branches during execution
86fn create_abs_function(input_data_type: &DataType) -> Result<MathArrayFunction> {
87    match input_data_type {
88        DataType::Float16 => Ok(make_abs_function!(Float16Array)),
89        DataType::Float32 => Ok(make_abs_function!(Float32Array)),
90        DataType::Float64 => Ok(make_abs_function!(Float64Array)),
91
92        // Types that may overflow, such as abs(-128_i8).
93        DataType::Int8 => Ok(make_try_abs_function!(Int8Array)),
94        DataType::Int16 => Ok(make_try_abs_function!(Int16Array)),
95        DataType::Int32 => Ok(make_try_abs_function!(Int32Array)),
96        DataType::Int64 => Ok(make_try_abs_function!(Int64Array)),
97
98        // Types of results are the same as the input.
99        DataType::Null
100        | DataType::UInt8
101        | DataType::UInt16
102        | DataType::UInt32
103        | DataType::UInt64 => Ok(|input: &ArrayRef| Ok(Arc::clone(input))),
104
105        // Decimal types
106        DataType::Decimal32(_, _) => Ok(make_wrapping_abs_function!(Decimal32Array)),
107        DataType::Decimal64(_, _) => Ok(make_wrapping_abs_function!(Decimal64Array)),
108        DataType::Decimal128(_, _) => Ok(make_wrapping_abs_function!(Decimal128Array)),
109        DataType::Decimal256(_, _) => Ok(make_wrapping_abs_function!(Decimal256Array)),
110
111        other => not_impl_err!("Unsupported data type {other:?} for function abs"),
112    }
113}
114#[user_doc(
115    doc_section(label = "Math Functions"),
116    description = "Returns the absolute value of a number.",
117    syntax_example = "abs(numeric_expression)",
118    sql_example = r#"```sql
119> SELECT abs(-5);
120+----------+
121| abs(-5)  |
122+----------+
123| 5        |
124+----------+
125```"#,
126    standard_argument(name = "numeric_expression", prefix = "Numeric")
127)]
128#[derive(Debug, PartialEq, Eq, Hash)]
129pub struct AbsFunc {
130    signature: Signature,
131}
132
133impl Default for AbsFunc {
134    fn default() -> Self {
135        Self::new()
136    }
137}
138
139impl AbsFunc {
140    pub fn new() -> Self {
141        Self {
142            signature: Signature::numeric(1, Volatility::Immutable),
143        }
144    }
145}
146
147impl ScalarUDFImpl for AbsFunc {
148    fn as_any(&self) -> &dyn Any {
149        self
150    }
151
152    fn name(&self) -> &str {
153        "abs"
154    }
155
156    fn signature(&self) -> &Signature {
157        &self.signature
158    }
159
160    fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> {
161        Ok(arg_types[0].clone())
162    }
163
164    fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> {
165        let args = ColumnarValue::values_to_arrays(&args.args)?;
166        let [input] = take_function_args(self.name(), args)?;
167
168        let input_data_type = input.data_type();
169        let abs_fun = create_abs_function(input_data_type)?;
170
171        abs_fun(&input).map(ColumnarValue::Array)
172    }
173
174    fn output_ordering(&self, input: &[ExprProperties]) -> Result<SortProperties> {
175        // Non-decreasing for x ≥ 0 and symmetrically non-increasing for x ≤ 0.
176        let arg = &input[0];
177        let range = &arg.range;
178        let zero_point = Interval::make_zero(&range.lower().data_type())?;
179
180        if range.gt_eq(&zero_point)? == Interval::TRUE {
181            Ok(arg.sort_properties)
182        } else if range.lt_eq(&zero_point)? == Interval::TRUE {
183            Ok(-arg.sort_properties)
184        } else {
185            Ok(SortProperties::Unordered)
186        }
187    }
188
189    fn documentation(&self) -> Option<&Documentation> {
190        self.doc()
191    }
192}