Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions native/spark-expr/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,7 @@ harness = false
[[bench]]
name = "to_json"
harness = false

[[bench]]
name = "ceil"
harness = false
65 changes: 65 additions & 0 deletions native/spark-expr/benches/ceil.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// 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.

use arrow::array::Decimal128Array;
use arrow::datatypes::DataType;
use criterion::{criterion_group, criterion_main, Criterion};
use datafusion::physical_plan::ColumnarValue;
use datafusion_comet_spark_expr::spark_ceil;
use std::hint::black_box;
use std::sync::Arc;

const NUM_ROWS: i128 = 8192;

fn decimal_args(precision: u8, scale: i8, spread: i128) -> Vec<ColumnarValue> {
let values: Vec<i128> = (0..NUM_ROWS)
.map(|i| (i - NUM_ROWS / 2) * spread + i % 7)
.collect();
let array = Decimal128Array::from(values)
.with_precision_and_scale(precision, scale)
.unwrap();
vec![ColumnarValue::Array(Arc::new(array))]
}

fn criterion_benchmark(c: &mut Criterion) {
// decimal(18, 4): unscaled values fit comfortably in 64 bits
let narrow = decimal_args(18, 4, 1_000_003);
// decimal(38, 6): unscaled values exceed the 64-bit range
let wide = decimal_args(38, 6, 1_000_000_000_000_000_003);

let mut group = c.benchmark_group("ceil");
group.bench_function("ceil_decimal_18_4", |b| {
b.iter(|| {
black_box(spark_ceil(
black_box(&narrow),
black_box(&DataType::Decimal128(18, 0)),
))
})
});
group.bench_function("ceil_decimal_38_6", |b| {
b.iter(|| {
black_box(spark_ceil(
black_box(&wide),
black_box(&DataType::Decimal128(38, 0)),
))
})
});
group.finish();
}

criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);
15 changes: 14 additions & 1 deletion native/spark-expr/src/math_funcs/ceil.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,20 @@ pub fn spark_ceil(
#[inline]
fn decimal_ceil_f(scale: &i8) -> impl Fn(i128) -> i128 {
let div = 10_i128.pow_wrapping(*scale as u32);
move |x: i128| div_ceil(x, div)
// 128-bit division is a compiler intrinsic call, while 64-bit division maps to a single
// hardware instruction. Decimals with precision <= 18 always fit in 64 bits, so take that
// path whenever both the value and the divisor do, and fall back to 128-bit otherwise.
let div_i64 = i64::try_from(div).ok();
move |x: i128| match (div_i64, i64::try_from(x)) {
(Some(d), Ok(x)) => {
let quotient = x / d;
// `d` is positive, so a positive remainder means the truncated quotient is below
// the true value and has to be rounded up.
let adjusted = if x % d > 0 { quotient + 1 } else { quotient };
adjusted as i128
}
_ => div_ceil(x, div),
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ceil.rs:97-254 tests only small unscaled values (max 12999), which all take the new i64 fast path. The entire point of this PR is the branch at ceil.rs:85, and the fallback arm (_ => div_ceil(x, div) at ceil.rs:93) has zero direct coverage. A regression that broke only the wide path would pass CI.

Suggested change: add an array test with a Decimal128(38, s) input whose unscaled values exceed i64::MAX (positive and negative), asserting the ceil result. Example: unscaled 20_000_000_000_000_000_000 (> i64::MAX) at scale 6 targeting Decimal128(38, 0), plus a negative sibling and an exact multiple, so both the fast and fallback arms plus the negative-remainder branch are covered in one suite.

#[cfg(test)]
Expand Down
2 changes: 1 addition & 1 deletion native/spark-expr/src/math_funcs/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pub(crate) fn make_decimal_array(
array: &ArrayRef,
precision: u8,
scale: i8,
f: &dyn Fn(i128) -> i128,
f: impl Fn(i128) -> i128,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

utils.rs:60 now takes f: impl Fn(i128) -> i128, but the array call site at ceil.rs:51 still passes &f. This compiles because &F: Fn via the blanket impl, so F monomorphizes to &closure and dispatch is static, but it is now a stray reference that obscures the intent of the change and adds a pointer indirection the PR was trying to remove.

Suggested change: pass the closure by value at ceil.rs:51: make_decimal_array(array, precision, scale, f).

) -> Result<ColumnarValue, DataFusionError> {
let array = array.as_primitive::<Decimal128Type>();
let result: Decimal128Array = arrow::compute::kernels::arity::unary(array, f);
Expand Down
Loading