Skip to main content

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