|
| 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 | +
|
| 20 | +use arrow::array::Decimal128Array; |
| 21 | +use arrow::array::Decimal256Array; |
| 22 | +use arrow::array::Int16Array; |
| 23 | +use arrow::array::Int32Array; |
| 24 | +use arrow::array::Int64Array; |
| 25 | +use arrow::array::Int8Array; |
| 26 | +use arrow::datatypes::DataType; |
| 27 | +use datafusion_common::not_impl_err; |
| 28 | +use datafusion_common::plan_datafusion_err; |
| 29 | +use datafusion_common::{internal_err, Result, DataFusionError}; |
| 30 | +use datafusion_expr::utils; |
| 31 | +use datafusion_expr::ColumnarValue; |
| 32 | + |
| 33 | +use datafusion_expr::{ScalarUDFImpl, Signature, Volatility}; |
| 34 | +use std::any::Any; |
| 35 | +use std::sync::Arc; |
| 36 | +use arrow::array::{ArrayRef, Float32Array, Float64Array}; |
| 37 | +use arrow::error::ArrowError; |
| 38 | + |
| 39 | +type MathArrayFunction = fn(&Vec<ArrayRef>) -> Result<ArrayRef>; |
| 40 | + |
| 41 | +macro_rules! make_abs_function { |
| 42 | + ($ARRAY_TYPE:ident) => {{ |
| 43 | + |args: &Vec<ArrayRef>| { |
| 44 | + let array = downcast_arg!(&args[0], "abs arg", $ARRAY_TYPE); |
| 45 | + let res: $ARRAY_TYPE = array.unary(|x| x.abs()); |
| 46 | + Ok(Arc::new(res) as ArrayRef) |
| 47 | + } |
| 48 | + }}; |
| 49 | +} |
| 50 | + |
| 51 | +macro_rules! make_try_abs_function { |
| 52 | + ($ARRAY_TYPE:ident) => {{ |
| 53 | + |args: &Vec<ArrayRef>| { |
| 54 | + let array = downcast_arg!(&args[0], "abs arg", $ARRAY_TYPE); |
| 55 | + let res: $ARRAY_TYPE = array.try_unary(|x| { |
| 56 | + x.checked_abs().ok_or_else(|| { |
| 57 | + ArrowError::ComputeError(format!( |
| 58 | + "{} overflow on abs({})", |
| 59 | + stringify!($ARRAY_TYPE), |
| 60 | + x |
| 61 | + )) |
| 62 | + }) |
| 63 | + })?; |
| 64 | + Ok(Arc::new(res) as ArrayRef) |
| 65 | + } |
| 66 | + }}; |
| 67 | +} |
| 68 | + |
| 69 | +macro_rules! make_decimal_abs_function { |
| 70 | + ($ARRAY_TYPE:ident) => {{ |
| 71 | + |args: &Vec<ArrayRef>| { |
| 72 | + let array = downcast_arg!(&args[0], "abs arg", $ARRAY_TYPE); |
| 73 | + let res: $ARRAY_TYPE = array |
| 74 | + .unary(|x| x.wrapping_abs()) |
| 75 | + .with_data_type(args[0].data_type().clone()); |
| 76 | + Ok(Arc::new(res) as ArrayRef) |
| 77 | + } |
| 78 | + }}; |
| 79 | +} |
| 80 | + |
| 81 | +/// Abs SQL function |
| 82 | +/// Return different implementations based on input datatype to reduce branches during execution |
| 83 | +fn create_abs_function( |
| 84 | + input_data_type: &DataType, |
| 85 | +) -> Result<MathArrayFunction> { |
| 86 | + match input_data_type { |
| 87 | + DataType::Float32 => Ok(make_abs_function!(Float32Array)), |
| 88 | + DataType::Float64 => Ok(make_abs_function!(Float64Array)), |
| 89 | + |
| 90 | + // Types that may overflow, such as abs(-128_i8). |
| 91 | + DataType::Int8 => Ok(make_try_abs_function!(Int8Array)), |
| 92 | + DataType::Int16 => Ok(make_try_abs_function!(Int16Array)), |
| 93 | + DataType::Int32 => Ok(make_try_abs_function!(Int32Array)), |
| 94 | + DataType::Int64 => Ok(make_try_abs_function!(Int64Array)), |
| 95 | + |
| 96 | + // Types of results are the same as the input. |
| 97 | + DataType::Null |
| 98 | + | DataType::UInt8 |
| 99 | + | DataType::UInt16 |
| 100 | + | DataType::UInt32 |
| 101 | + | DataType::UInt64 => Ok(|args: &Vec<ArrayRef>| Ok(args[0].clone())), |
| 102 | + |
| 103 | + // Decimal types |
| 104 | + DataType::Decimal128(_, _) => Ok(make_decimal_abs_function!(Decimal128Array)), |
| 105 | + DataType::Decimal256(_, _) => Ok(make_decimal_abs_function!(Decimal256Array)), |
| 106 | + |
| 107 | + other => not_impl_err!("Unsupported data type {other:?} for function abs"), |
| 108 | + } |
| 109 | +} |
| 110 | +#[derive(Debug)] |
| 111 | +pub(super) struct AbsFunc { |
| 112 | + signature: Signature, |
| 113 | +} |
| 114 | + |
| 115 | +impl AbsFunc { |
| 116 | + pub fn new() -> Self { |
| 117 | + Self { |
| 118 | + signature: Signature::any(1, Volatility::Immutable) |
| 119 | + } |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +impl ScalarUDFImpl for AbsFunc { |
| 124 | + fn as_any(&self) -> &dyn Any { |
| 125 | + self |
| 126 | + } |
| 127 | + fn name(&self) -> &str { |
| 128 | + "abs" |
| 129 | + } |
| 130 | + |
| 131 | + fn signature(&self) -> &Signature { |
| 132 | + &self.signature |
| 133 | + } |
| 134 | + |
| 135 | + fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> { |
| 136 | + if arg_types.len() != 1 { |
| 137 | + return Err(plan_datafusion_err!( |
| 138 | + "{}", |
| 139 | + utils::generate_signature_error_msg( |
| 140 | + self.name(), |
| 141 | + self.signature().clone(), |
| 142 | + arg_types, |
| 143 | + ) |
| 144 | + )); |
| 145 | + } |
| 146 | + match arg_types[0] { |
| 147 | + DataType::Float32 => Ok(DataType::Float32), |
| 148 | + DataType::Float64 => Ok(DataType::Float64), |
| 149 | + DataType::Int8 => Ok(DataType::Int8), |
| 150 | + DataType::Int16 => Ok(DataType::Int16), |
| 151 | + DataType::Int32 => Ok(DataType::Int32), |
| 152 | + DataType::Int64 => Ok(DataType::Int64), |
| 153 | + DataType::Null => Ok(DataType::Null), |
| 154 | + DataType::UInt8 => Ok(DataType::UInt8), |
| 155 | + DataType::UInt16 => Ok(DataType::UInt16), |
| 156 | + DataType::UInt32 => Ok(DataType::UInt32), |
| 157 | + DataType::UInt64 => Ok(DataType::UInt64), |
| 158 | + DataType::Decimal128(precision, scale) => Ok(DataType::Decimal128(precision, scale)), |
| 159 | + DataType::Decimal256(precision, scale) => Ok(DataType::Decimal256(precision, scale)), |
| 160 | + _ => not_impl_err!("Unsupported data type {} for function abs", arg_types[0].to_string()), |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + fn invoke(&self, args: &[ColumnarValue]) -> Result<ColumnarValue> { |
| 165 | + let args = ColumnarValue::values_to_arrays(args)?; |
| 166 | + |
| 167 | + if args.len() != 1 { |
| 168 | + return internal_err!("abs function requires 1 argument, got {}", args.len()); |
| 169 | + } |
| 170 | + |
| 171 | + let input_data_type = args[0].data_type(); |
| 172 | + let abs_fun = create_abs_function(input_data_type)?; |
| 173 | + |
| 174 | + let arr = abs_fun(&args)?; |
| 175 | + Ok(ColumnarValue::Array(arr)) |
| 176 | + } |
| 177 | +} |
0 commit comments