Skip to content

perf: Optimize floor and ceil scalar performance#19752

Merged
comphead merged 3 commits intoapache:mainfrom
kumarUjjawal:perf/floor_ceil
Jan 12, 2026
Merged

perf: Optimize floor and ceil scalar performance#19752
comphead merged 3 commits intoapache:mainfrom
kumarUjjawal:perf/floor_ceil

Conversation

@kumarUjjawal
Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Rationale for this change

  • The current floor and ceil implementations always convert scalar inputs to arrays via values_to_arrays(), which introduces unnecessary overhead when processing single values.

What changes are included in this PR?

  • Add scalar fast path for floor and ceil
  • Add criterion benchmark for floor/ceil to measure performance

Are these changes tested?

  • All sqllogictest pass
  • Addec benchmark
Benchmark Array Time Scalar Time Speedup
floor_f64 (1024) ~396 ns ~147 ns 2.7×
ceil_f64 (1024) ~363 ns ~150 ns 2.4×
floor_f64 (4096) ~681 ns ~144 ns 4.7×
ceil_f64 (4096) ~667 ns ~144 ns 4.6×
floor_f64 (8192) ~1,638 ns ~144 ns 11.4×
ceil_f64 (8192) ~1,634 ns ~144 ns 11.3×

Are there any user-facing changes?

No

@github-actions github-actions Bot added the functions Changes to functions implementation label Jan 11, 2026
Comment thread datafusion/functions/src/math/ceil.rs Outdated
ScalarValue::Null => {
return Ok(ColumnarValue::Scalar(ScalarValue::Float64(None)));
}
// Decimal types fall through to array path for overflow validation
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.

For decimals, we could turn the scalars in arrays of size 1, pass through the existing code, then recreate a scalar from the resultant array, to benefit from code reuse and ensure we don't expand the array.

Not the cleanest way to do it but certainly the easiest.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the feedback.

@comphead comphead added this pull request to the merge queue Jan 12, 2026
Merged via the queue into apache:main with commit d484c09 Jan 12, 2026
31 checks passed
Dandandan added a commit to Dandandan/arrow-datafusion that referenced this pull request Jan 12, 2026
Dandandan added a commit to Dandandan/arrow-datafusion that referenced this pull request Jan 12, 2026
de-bgunter pushed a commit to de-bgunter/datafusion that referenced this pull request Mar 24, 2026
## Which issue does this PR close?

<!--
We generally require a GitHub issue to be filed for all bug fixes and
enhancements and this helps us generate change logs for our releases.
You can link an issue to this PR using the GitHub syntax. For example
`Closes apache#123` indicates that this PR will close issue apache#123.
-->

- Part of apache/datafusion-comet#2986

## Rationale for this change


- The current floor and ceil implementations always convert scalar
inputs to arrays via `values_to_arrays()`, which introduces unnecessary
overhead when processing single values.

<!--
Why are you proposing this change? If this is already explained clearly
in the issue then this section is not needed.
Explaining clearly why changes are proposed helps reviewers understand
your changes and offer better suggestions for fixes.
-->

## What changes are included in this PR?

- Add scalar fast path for floor and ceil
- Add criterion benchmark for floor/ceil to measure performance

<!--
There is no need to duplicate the description in the issue here but it
is sometimes worth providing a summary of the individual changes in this
PR.
-->

## Are these changes tested?
 
- All sqllogictest pass
- Addec benchmark

| Benchmark           | Array Time | Scalar Time | Speedup |
|---------------------|------------|-------------|---------|
| floor_f64 (1024)    | ~396 ns    | ~147 ns     | 2.7×    |
| ceil_f64 (1024)     | ~363 ns    | ~150 ns     | 2.4×    |
| floor_f64 (4096)    | ~681 ns    | ~144 ns     | 4.7×    |
| ceil_f64 (4096)     | ~667 ns    | ~144 ns     | 4.6×    |
| floor_f64 (8192)    | ~1,638 ns  | ~144 ns     | 11.4×   |
| ceil_f64 (8192)     | ~1,634 ns  | ~144 ns     | 11.3×   |


<!--
We typically require tests for all PRs in order to:
1. Prevent the code from being accidentally broken by subsequent changes
2. Serve as another way to document the expected behavior of the code

If tests are not included in your PR, please explain why (for example,
are they covered by existing tests)?
-->

## Are there any user-facing changes?

No

<!--
If there are user-facing changes then we may require documentation to be
updated before approving the PR.
-->

<!--
If there are any breaking changes to public APIs, please add the `api
change` label.
-->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

functions Changes to functions implementation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants