Skip to content

Apache DataFusion SQL Query Engine

perf: optimize `trunc` for scalar precision case (10x faster) (#23593)

Andy Grove595e61448fc8
diff --git a/datafusion/functions/Cargo.toml b/datafusion/functions/Cargo.toml
index 58c4d02d9f..7994028b47 100644
--- a/datafusion/functions/Cargo.toml
+++ b/datafusion/functions/Cargo.toml
@@ -301,6 +301,11 @@ harness = false
name = "trunc"
required-features = ["math_expressions"]
+[[bench]]
+harness = false
+name = "trunc_precision"
+required-features = ["math_expressions"]
+
[[bench]]
harness = false
name = "initcap"
diff --git a/datafusion/functions/benches/trunc_precision.rs b/datafusion/functions/benches/trunc_precision.rs
new file mode 100644
index 0000000000..5d75694d6e
--- /dev/null
+++ b/datafusion/functions/benches/trunc_precision.rs
@@ -0,0 +1,91 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+//! Benchmarks the `trunc(value, precision)` array path where `precision` is a
+//! constant (scalar) argument.
+
+use arrow::datatypes::{DataType, Field, Float32Type, Float64Type};
+use arrow::util::bench_util::create_primitive_array;
+use criterion::{Criterion, criterion_group, criterion_main};
+use datafusion_common::ScalarValue;
+use datafusion_common::config::ConfigOptions;
+use datafusion_expr::{ColumnarValue, ScalarFunctionArgs};
+use datafusion_functions::math::trunc;
+use std::hint::black_box;
+use std::sync::Arc;
+
+fn criterion_benchmark(c: &mut Criterion) {
+ let trunc = trunc();
+ let config_options = Arc::new(ConfigOptions::default());
+
+ for size in [1024, 4096, 8192] {
+ let f64_array = Arc::new(create_primitive_array::<Float64Type>(size, 0.2));
+ let f64_args = vec![
+ ColumnarValue::Array(f64_array),
+ ColumnarValue::Scalar(ScalarValue::Int64(Some(3))),
+ ];
+ let arg_fields = vec![
+ Field::new("a", DataType::Float64, true).into(),
+ Field::new("p", DataType::Int64, false).into(),
+ ];
+ let return_field = Field::new("f", DataType::Float64, true).into();
+ c.bench_function(&format!("trunc f64 precision array: {size}"), |b| {
+ b.iter(|| {
+ black_box(
+ trunc
+ .invoke_with_args(ScalarFunctionArgs {
+ args: f64_args.clone(),
+ arg_fields: arg_fields.clone(),
+ number_rows: size,
+ return_field: Arc::clone(&return_field),
+ config_options: Arc::clone(&config_options),
+ })
+ .unwrap(),
+ )
+ })
+ });
+
+ let f32_array = Arc::new(create_primitive_array::<Float32Type>(size, 0.2));
+ let f32_args = vec![
+ ColumnarValue::Array(f32_array),
+ ColumnarValue::Scalar(ScalarValue::Int64(Some(3))),
+ ];
+ let arg_fields = vec![
+ Field::new("a", DataType::Float32, true).into(),
+ Field::new("p", DataType::Int64, false).into(),
+ ];
+ let return_field = Field::new("f", DataType::Float32, true).into();
+ c.bench_function(&format!("trunc f32 precision array: {size}"), |b| {
+ b.iter(|| {
+ black_box(
+ trunc
+ .invoke_with_args(ScalarFunctionArgs {
+ args: f32_args.clone(),
+ arg_fields: arg_fields.clone(),
+ number_rows: size,
+ return_field: Arc::clone(&return_field),
+ config_options: Arc::clone(&config_options),
+ })
+ .unwrap(),
+ )
+ })
+ });
+ }
+}
+
+criterion_group!(benches, criterion_benchmark);
+criterion_main!(benches);
diff --git a/datafusion/functions/src/math/trunc.rs b/datafusion/functions/src/math/trunc.rs
index 7b11e19bdb..1f4fe16fb5 100644
--- a/datafusion/functions/src/math/trunc.rs
+++ b/datafusion/functions/src/math/trunc.rs
@@ -25,8 +25,8 @@ use arrow::datatypes::DataType::{
Decimal32, Decimal64, Decimal128, Decimal256, Float32, Float64,
};
use arrow::datatypes::{
- DataType, Decimal32Type, Decimal64Type, Decimal128Type, Decimal256Type, DecimalType,
- Float32Type, Float64Type, Int64Type,
+ ArrowPrimitiveType, DataType, Decimal32Type, Decimal64Type, Decimal128Type,
+ Decimal256Type, DecimalType, Float32Type, Float64Type, Int64Type,
};
use datafusion_common::ScalarValue::Int64;
use datafusion_common::types::{
@@ -40,7 +40,7 @@ use datafusion_expr::{
};
use datafusion_expr_common::signature::{Coercion, TypeSignature, TypeSignatureClass};
use datafusion_macros::user_doc;
-use num_traits::{One, Zero, pow};
+use num_traits::{Float, NumCast, One, Zero, pow};
#[user_doc(
doc_section(label = "Math Functions"),
@@ -158,6 +158,12 @@ impl ScalarUDFImpl for TruncFunc {
}
};
+ // Whether an explicit precision argument was supplied. The array fast
+ // paths below must only apply to the two-argument form: single-argument
+ // `trunc(x)` uses a different zero handling (mapping `-0.0` to `0.0`)
+ // that must be preserved.
+ let has_precision_arg = args.args.len() == 2;
+
// Scalar fast path using tuple matching for (value, precision)
match (&args.args[0], precision) {
// Null cases
@@ -231,6 +237,25 @@ impl ScalarUDFImpl for TruncFunc {
*lscale,
))),
+ // Array value with a constant (scalar) precision: hoist the power
+ // of ten out of the per-element loop instead of broadcasting the
+ // scalar into a full precision array and recomputing `10^p` for
+ // every element (see `truncate_float_array`).
+ (ColumnarValue::Array(arr), Some(p))
+ if has_precision_arg && arr.data_type() == &Float64 =>
+ {
+ Ok(ColumnarValue::Array(truncate_float_array::<Float64Type>(
+ arr, p,
+ )))
+ }
+ (ColumnarValue::Array(arr), Some(p))
+ if has_precision_arg && arr.data_type() == &Float32 =>
+ {
+ Ok(ColumnarValue::Array(truncate_float_array::<Float32Type>(
+ arr, p,
+ )))
+ }
+
// Array path for everything else
_ => make_scalar_function(trunc, vec![])(&args.args),
}
@@ -375,14 +400,35 @@ fn trunc(args: &[ArrayRef]) -> Result<ArrayRef> {
}
}
-fn compute_truncate32(x: f32, y: i64) -> f32 {
- let factor = 10.0_f32.powi(y as i32);
+/// Truncates `x` using a pre-computed `factor` of `10^precision`. Taking the
+/// factor as an argument lets callers hoist `10^precision` out of a per-element
+/// loop when the precision is constant.
+fn truncate_with_factor<F: Float>(x: F, factor: F) -> F {
(x * factor).trunc() / factor
}
+/// Truncates every element of a float array to `precision` decimal places,
+/// computing the `10^precision` factor once and reusing it for every element.
+fn truncate_float_array<T>(arr: &ArrayRef, precision: i64) -> ArrayRef
+where
+ T: ArrowPrimitiveType,
+ T::Native: Float,
+{
+ let factor = <T::Native as NumCast>::from(10.0_f64)
+ .unwrap()
+ .powi(precision as i32);
+ Arc::new(
+ arr.as_primitive::<T>()
+ .unary::<_, T>(|x| truncate_with_factor(x, factor)),
+ )
+}
+
+fn compute_truncate32(x: f32, y: i64) -> f32 {
+ truncate_with_factor(x, 10.0_f32.powi(y as i32))
+}
+
fn compute_truncate64(x: f64, y: i64) -> f64 {
- let factor = 10.0_f64.powi(y as i32);
- (x * factor).trunc() / factor
+ truncate_with_factor(x, 10.0_f64.powi(y as i32))
}
/// Truncates a decimal value to `truncate_precision` fractional digits.