-
Notifications
You must be signed in to change notification settings - Fork 340
perf: optimize spark_ceil (3x faster)
#4926
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
andygrove
merged 8 commits into
apache:main
from
andygrove:auto-opt/spark_ceil-datafusion-comet-20260714-125829
Jul 24, 2026
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d6e27c7
perf: optimize spark_floor in datafusion-comet-spark-expr
andygrove 60ce5c0
Merge remote-tracking branch 'apache/main' into auto-opt/spark_floor-…
andygrove 5cf901c
refactor: unify make_decimal_scalar/array signatures on impl Fn
andygrove 6acb445
chore: Codegen fallback explain for plan (#4891)
comphead 643b4c9
perf: optimize spark_cast_int_to_int in datafusion-comet-spark-expr (…
andygrove cd7a0ba
perf: optimize spark_ceil in datafusion-comet-spark-expr
andygrove 345e770
perf: share dispatch_pow10 with floor; add wide-path decimal test
andygrove d532780
Merge branch 'main' into auto-opt/spark_ceil-datafusion-comet-2026071…
andygrove File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| // 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::{ArrayRef, Decimal128Array, Float64Array}; | ||
| use arrow::datatypes::DataType; | ||
| use criterion::{criterion_group, criterion_main, Criterion}; | ||
| use datafusion::physical_plan::ColumnarValue; | ||
| use datafusion_comet_spark_expr::spark_floor; | ||
| use std::hint::black_box; | ||
| use std::sync::Arc; | ||
|
|
||
| const ROWS: usize = 8192; | ||
|
|
||
| /// Unscaled decimal values that fit in 64 bits, which is the common case, with every 10th row | ||
| /// null. | ||
| fn decimal_array(precision: u8, scale: i8) -> ArrayRef { | ||
| let values: Vec<Option<i128>> = (0..ROWS) | ||
| .map(|i| { | ||
| if i % 10 == 0 { | ||
| None | ||
| } else { | ||
| let v = (i as i128) * 987_654_321 % 100_000_000_000_000_000; | ||
| Some(if i % 2 == 0 { v } else { -v }) | ||
| } | ||
| }) | ||
| .collect(); | ||
| Arc::new( | ||
| Decimal128Array::from(values) | ||
| .with_precision_and_scale(precision, scale) | ||
| .unwrap(), | ||
| ) | ||
| } | ||
|
|
||
| /// Unscaled decimal values too wide for 64 bits, exercising the 128-bit fallback. | ||
| fn wide_decimal_array(precision: u8, scale: i8) -> ArrayRef { | ||
| let values: Vec<Option<i128>> = (0..ROWS) | ||
| .map(|i| { | ||
| if i % 10 == 0 { | ||
| None | ||
| } else { | ||
| let v = (i as i128 + 1) * 10_000_000_000_000_000_000_000_000_000; | ||
| Some(if i % 2 == 0 { v } else { -v }) | ||
| } | ||
| }) | ||
| .collect(); | ||
| Arc::new( | ||
| Decimal128Array::from(values) | ||
| .with_precision_and_scale(precision, scale) | ||
| .unwrap(), | ||
| ) | ||
| } | ||
|
|
||
| fn float_array() -> ArrayRef { | ||
| Arc::new(Float64Array::from( | ||
| (0..ROWS) | ||
| .map(|i| i as f64 * 1.5 - 4096.0) | ||
| .collect::<Vec<_>>(), | ||
| )) | ||
| } | ||
|
|
||
| fn criterion_benchmark(c: &mut Criterion) { | ||
| let dec_2 = decimal_array(38, 2); | ||
| c.bench_function("spark_floor: decimal128(38,2)", |b| { | ||
| let args = vec![ColumnarValue::Array(Arc::clone(&dec_2))]; | ||
| b.iter(|| black_box(spark_floor(black_box(&args), &DataType::Decimal128(37, 0)).unwrap())) | ||
| }); | ||
|
|
||
| let dec_18 = decimal_array(38, 18); | ||
| c.bench_function("spark_floor: decimal128(38,18)", |b| { | ||
| let args = vec![ColumnarValue::Array(Arc::clone(&dec_18))]; | ||
| b.iter(|| black_box(spark_floor(black_box(&args), &DataType::Decimal128(21, 0)).unwrap())) | ||
| }); | ||
|
|
||
| let wide = wide_decimal_array(38, 6); | ||
| c.bench_function("spark_floor: decimal128(38,6) wide values", |b| { | ||
| let args = vec![ColumnarValue::Array(Arc::clone(&wide))]; | ||
| b.iter(|| black_box(spark_floor(black_box(&args), &DataType::Decimal128(33, 0)).unwrap())) | ||
| }); | ||
|
|
||
| let floats = float_array(); | ||
| c.bench_function("spark_floor: float64", |b| { | ||
| let args = vec![ColumnarValue::Array(Arc::clone(&floats))]; | ||
| b.iter(|| black_box(spark_floor(black_box(&args), &DataType::Float64).unwrap())) | ||
| }); | ||
| } | ||
|
|
||
| criterion_group!(benches, criterion_benchmark); | ||
| criterion_main!(benches); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ceil.rs:97-254tests only small unscaled values (max12999), which all take the new i64 fast path. The entire point of this PR is the branch atceil.rs:85, and the fallback arm (_ => div_ceil(x, div)atceil.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 exceedi64::MAX(positive and negative), asserting the ceil result. Example: unscaled20_000_000_000_000_000_000(> i64::MAX) at scale 6 targetingDecimal128(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.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added
test_ceil_decimal128_wide_arraywithDecimal128(38, 6)unscaled values abovei64::MAX(positive, negative, exact multiple), so the wide fallback arm and the negative-remainder branch are covered.