Skip to content

Commit c9a5496

Browse files
committed
perf: Check sqrt inputs in a vectorizable loop
#22308 made `sqrt` return an error for negative inputs by switching every unary math function from `unary` to `try_unary` with a per-value validator. `try_unary` can return early on any value, so its loop is not vectorized, and for nullable input it visits valid indices one at a time. That made `sqrt`, which otherwise compiles to vector square-root instructions, several times slower. Add `unary_with_input_check`, which applies the function and checks every value in one branch-free loop, then looks for an error to report only if some value failed the check. `sqrt`'s validator now returns an error message instead of a `Result`, so the check never builds an error. The other unary math functions go back to using `unary`. The error is now reported as an execution error, rather than wrapped in an Arrow compute error.
1 parent d8a5898 commit c9a5496

3 files changed

Lines changed: 92 additions & 42 deletions

File tree

‎datafusion/functions/src/macros.rs‎

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,8 @@ macro_rules! downcast_arg {
209209
/// $OUTPUT_ORDERING: the output ordering calculation method of the function
210210
/// $STRICT: whether the function returns NULL when any argument is NULL
211211
/// $GET_DOC: the function to get the documentation of the UDF
212+
/// $INPUT_ERROR (optional): a function that returns an error message for an
213+
/// argument value outside the function's domain, or `None` for a valid value
212214
macro_rules! make_math_unary_udf {
213215
($UDF:ident, $NAME:ident, $UNARY_FUNC:ident, $OUTPUT_ORDERING:expr, $EVALUATE_BOUNDS:expr, $STRICT:expr, $GET_DOC:expr) => {
214216
make_math_unary_udf!(
@@ -219,10 +221,10 @@ macro_rules! make_math_unary_udf {
219221
$EVALUATE_BOUNDS,
220222
$STRICT,
221223
$GET_DOC,
222-
None::<fn(f64) -> Result<()>>
224+
None::<fn(f64) -> Option<&'static str>>
223225
);
224226
};
225-
($UDF:ident, $NAME:ident, $UNARY_FUNC:ident, $OUTPUT_ORDERING:expr, $EVALUATE_BOUNDS:expr, $STRICT:expr, $GET_DOC:expr, $VALIDATOR:expr) => {
227+
($UDF:ident, $NAME:ident, $UNARY_FUNC:ident, $OUTPUT_ORDERING:expr, $EVALUATE_BOUNDS:expr, $STRICT:expr, $GET_DOC:expr, $INPUT_ERROR:expr) => {
226228
$crate::make_udf_function!($NAME::$UDF, $NAME);
227229

228230
mod $NAME {
@@ -231,7 +233,6 @@ macro_rules! make_math_unary_udf {
231233

232234
use arrow::array::{ArrayRef, AsArray};
233235
use arrow::datatypes::{DataType, Float32Type, Float64Type};
234-
use arrow::error::ArrowError;
235236
use datafusion_common::{Result, exec_err};
236237
use datafusion_expr::interval_arithmetic::Interval;
237238
use datafusion_expr::sort_properties::{ExprProperties, SortProperties};
@@ -297,36 +298,32 @@ macro_rules! make_math_unary_udf {
297298
let args = ColumnarValue::values_to_arrays(&args.args)?;
298299
let arr: ArrayRef = match args[0].data_type() {
299300
DataType::Float64 => {
300-
let values = args[0]
301-
.as_primitive::<Float64Type>()
302-
.try_unary::<_, Float64Type, _>(
303-
|x: f64| -> std::result::Result<f64, ArrowError> {
304-
if let Some(validate) = $VALIDATOR {
305-
validate(x).map_err(|error| {
306-
ArrowError::ComputeError(error.to_string())
307-
})?;
308-
}
309-
310-
Ok(f64::$UNARY_FUNC(x))
311-
},
312-
)?;
313-
Arc::new(values) as ArrayRef
301+
let array = args[0].as_primitive::<Float64Type>();
302+
let result = match $INPUT_ERROR {
303+
Some(input_error) => {
304+
$crate::math::common::unary_with_input_check(
305+
array,
306+
f64::$UNARY_FUNC,
307+
input_error,
308+
)?
309+
}
310+
None => array.unary::<_, Float64Type>(f64::$UNARY_FUNC),
311+
};
312+
Arc::new(result) as ArrayRef
314313
}
315314
DataType::Float32 => {
316-
let values = args[0]
317-
.as_primitive::<Float32Type>()
318-
.try_unary::<_, Float32Type, _>(
319-
|x: f32| -> std::result::Result<f32, ArrowError> {
320-
if let Some(validate) = $VALIDATOR {
321-
validate(x as f64).map_err(|error| {
322-
ArrowError::ComputeError(error.to_string())
323-
})?;
324-
}
325-
326-
Ok(f32::$UNARY_FUNC(x))
327-
},
328-
)?;
329-
Arc::new(values) as ArrayRef
315+
let array = args[0].as_primitive::<Float32Type>();
316+
let result = match $INPUT_ERROR {
317+
Some(input_error) => {
318+
$crate::math::common::unary_with_input_check(
319+
array,
320+
f32::$UNARY_FUNC,
321+
|x: f32| input_error(x as f64),
322+
)?
323+
}
324+
None => array.unary::<_, Float32Type>(f32::$UNARY_FUNC),
325+
};
326+
Arc::new(result) as ArrayRef
330327
}
331328
other => {
332329
return exec_err!(

‎datafusion/functions/src/math/common.rs‎

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18-
use arrow::array::ArrowNativeTypeOp;
18+
use arrow::array::{Array, ArrowNativeTypeOp, ArrowPrimitiveType, PrimitiveArray};
1919
use arrow::error::ArrowError;
20+
use datafusion_common::{Result, exec_err};
2021
use num_traits::{CheckedMul, CheckedNeg, Signed};
2122
use std::fmt::Display;
2223
use std::mem::swap;
@@ -150,10 +151,48 @@ pub(crate) fn lcm_signed_int(x: i64, y: i64) -> Result<i64, ArrowError> {
150151
})
151152
}
152153

154+
/// Applies `op` to every value in `array`, like `unary`, but returns an error
155+
/// if `input_error` returns a message for any non-null value.
156+
///
157+
/// Use this for functions that return an error for some argument values, such
158+
/// as `sqrt`, which returns an error for negative numbers. `try_unary` can also
159+
/// return errors, but it can return early on any value, which keeps the
160+
/// compiler from vectorizing its loop. That makes cheap functions like `sqrt`
161+
/// several times slower.
162+
///
163+
/// Instead, `input_error` is called on every value, including those in null
164+
/// slots, in the same loop as `op`; only if some value fails is the array
165+
/// searched again for an error to report. `input_error` should therefore be a
166+
/// cheap check, such as a comparison.
167+
pub(crate) fn unary_with_input_check<T: ArrowPrimitiveType>(
168+
array: &PrimitiveArray<T>,
169+
op: impl Fn(T::Native) -> T::Native,
170+
input_error: impl Fn(T::Native) -> Option<&'static str>,
171+
) -> Result<PrimitiveArray<T>> {
172+
let mut any_invalid = false;
173+
let values: Vec<T::Native> = array
174+
.values()
175+
.iter()
176+
.map(|&x| {
177+
any_invalid |= input_error(x).is_some();
178+
op(x)
179+
})
180+
.collect();
181+
182+
// The check above also ran on null slots, which can hold any value, so the
183+
// failure may be spurious. Re-check just the non-null values.
184+
if any_invalid && let Some(message) = array.iter().flatten().find_map(input_error) {
185+
return exec_err!("{message}");
186+
}
187+
188+
Ok(PrimitiveArray::new(values.into(), array.nulls().cloned()))
189+
}
190+
153191
#[cfg(test)]
154192
mod tests {
155193
use super::*;
156-
use arrow_buffer::i256;
194+
use arrow::array::Float64Array;
195+
use arrow_buffer::{NullBuffer, i256};
157196

158197
const GCD_COMMON_TEST_CASES: [(i64, i64, i64); 18] = [
159198
// Basic cases
@@ -317,4 +356,21 @@ mod tests {
317356
);
318357
}
319358
}
359+
360+
#[test]
361+
fn test_unary_with_input_check() {
362+
let input_error = |x: f64| (x < 0.0).then_some("negative input");
363+
364+
// -1.0 is in a null slot, so it is not an error.
365+
let array = Float64Array::new(
366+
vec![4.0, -1.0, 9.0].into(),
367+
Some(NullBuffer::from(vec![true, false, true])),
368+
);
369+
let result = unary_with_input_check(&array, f64::sqrt, input_error).unwrap();
370+
assert_eq!(result, Float64Array::from(vec![Some(2.0), None, Some(3.0)]));
371+
372+
let array = Float64Array::from(vec![Some(4.0), None, Some(-1.0)]);
373+
let error = unary_with_input_check(&array, f64::sqrt, input_error).unwrap_err();
374+
assert_eq!(error.strip_backtrace(), "Execution error: negative input");
375+
}
320376
}

‎datafusion/functions/src/math/mod.rs‎

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
//! "math" DataFusion functions
1919
2020
use crate::math::monotonicity::*;
21-
use datafusion_common::{Result, exec_err};
2221
use datafusion_expr::ScalarUDF;
2322
use std::sync::Arc;
2423

@@ -44,12 +43,10 @@ pub mod round;
4443
pub mod signum;
4544
pub mod trunc;
4645

47-
fn validate_sqrt_input(value: f64) -> Result<()> {
48-
if value < 0.0 {
49-
exec_err!("cannot take square root of a negative number")
50-
} else {
51-
Ok(())
52-
}
46+
/// `f64::sqrt` returns NaN for negative numbers; like PostgreSQL, `sqrt`
47+
/// returns an error instead.
48+
fn sqrt_input_error(value: f64) -> Option<&'static str> {
49+
(value < 0.0).then_some("cannot take square root of a negative number")
5350
}
5451

5552
// Create UDFs
@@ -238,7 +235,7 @@ make_math_unary_udf!(
238235
super::bounds::sqrt_bounds,
239236
true,
240237
super::get_sqrt_doc,
241-
Some(super::validate_sqrt_input)
238+
Some(super::sqrt_input_error)
242239
);
243240
make_math_unary_udf!(
244241
TanFunc,
@@ -264,7 +261,7 @@ make_udf_function!(trunc::TruncFunc, trunc);
264261
mod strict_tests {
265262
use super::*;
266263
use arrow::datatypes::Field;
267-
use datafusion_common::ScalarValue;
264+
use datafusion_common::{Result, ScalarValue};
268265
use datafusion_expr::{
269266
ColumnarValue, ReturnFieldArgs, ScalarFunctionArgs, ScalarUDF,
270267
};

0 commit comments

Comments
 (0)