Skip to content

Various ScalarValue numeric method fixes & refactors (especially decimal) - #23631

Merged
alamb merged 10 commits into
apache:mainfrom
Jefffrey:fix-scalar-val-num-ops
Jul 26, 2026
Merged

Various ScalarValue numeric method fixes & refactors (especially decimal)#23631
alamb merged 10 commits into
apache:mainfrom
Jefffrey:fix-scalar-val-num-ops

Conversation

@Jefffrey

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

N/A

Rationale for this change

I noticed there were some subtle errors with how decimals were handled in scalar values, and also opportunity to remove power calls in favour of precomputed constant tables. Also filling out some other missing support.

What changes are included in this PR?

I recommend looking at the commits as they are self contained with detailed messages for each.

Are these changes tested?

Yes

Are there any user-facing changes?

No

Since we already have access to these constant lookup tables, its more
efficient to index into them rather than calculating via `checked_pow()`
at runtime.
There's a side-effect of some tests failing so I removed them. I think
this is fine since it doesn't make sense to have a `ScalarValue` method
where we request a value of a certain type but can get a different value
type out.
Seems like this was just a simple omission
For one/negative one, if the scale equals the precision then all values
are to the right of the decimal point meaning we actually cannot
represent a value of 1, so this could have caused some subtle errors.
Similar for ten, we need at least 2 digits left of the decimal point,
that is (p - s) >= 2, to represent a ten value.
Using macros here to generate these tables instead of manually
enumerating the values into the code itself; for i256 its a bit
complicated since it doesn't have native support so can't use direct
multiplication as it doesn't have const support.
Used Codex in this case, but added a unit test to ensure the constant
values generated are correct.
I noticed we didn't have support for `Float16`, `Decimal32` and
`Decimal64` here; instead of just adding them here, I figured it would
be easier to plumb in the `ScalarValue` code to reduce duplication and
ensure there's less opportunities for subtle omissions like this.
@github-actionsgithub-actionsBot added optimizer Optimizer rules common Related to common crate labels Jul 16, 2026
@codecov-commenter

codecov-commenter commented Jul 16, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 83.47826% with 19 lines in your changes missing coverage. Please review.
✅ Project coverage is 80.74%. Comparing base (ba8796e) to head (d2407bd).

Files with missing linesPatch %Lines
datafusion/common/src/scalar/mod.rs80.80%19 Missing ⚠️
Additional details and impacted files
@@ Coverage Diff @@## main #23631 +/- ##
==========================================
+ Coverage 80.73% 80.74% +0.01% 
==========================================
Files 1089 1090 +1 Lines 368948 368937 -11 Branches 368948 368937 -11 ==========================================
+ Hits 297857 297889 +32 + Misses 53334 53289 -45 - Partials 17757 17759 +2 

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@Jefffrey

Copy link
Copy Markdown
ContributorAuthor

msrv failure related to

@Jefffrey
Jefffrey marked this pull request as ready for review July 16, 2026 11:24
//
// This is mainly a shortcut for not needing to manually list out each value
// anyway.
let mut values = [i256::ONE; _];

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.

I think this is basically const mul? I also ran into that recently elsewhere, so figured I'll make a PR - apache/arrow-rs#10363

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

this would be very helpful indeed 🙏

Comment threaddatafusion/optimizer/src/simplify_expressions/utils.rs

@AdamGSAdamGS left a comment

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.

I think a bunch of this code is mine so I figured I'll review it 😅
It all makes sense, just two small comments

jayhan94 pushed a commit to jayhan94/datafusion that referenced this pull request Jul 23, 2026
see previous bump
- apache#18403
honestly my main motivation was i was using a feature from `1.89.0`
apparently for apache#23631 and
realized we hadn't bumped this in a while
if we prefer to be more conservative with msrv bumps (i.e. only bump if
a dependency requires it or we see a really useful feature/lib function
in newer versions) then i'm fine with leaving it and altering my PR
above, but in the past we usually kept up to date with bumping our msrv
@alamb
alamb added this pull request to the merge queueJul 26, 2026
@alamb

Copy link
Copy Markdown
Contributor

Thanks @AdamGS and @Jefffrey

Merged via the queue into apache:main with commit 50ae076Jul 26, 2026
40 checks passed
@Jefffrey
Jefffrey deleted the fix-scalar-val-num-ops branch July 26, 2026 23:17
kosiew pushed a commit to kosiew/datafusion that referenced this pull request Aug 12, 2026
see previous bump
- apache#18403
honestly my main motivation was i was using a feature from `1.89.0`
apparently for apache#23631 and
realized we hadn't bumped this in a while
if we prefer to be more conservative with msrv bumps (i.e. only bump if
a dependency requires it or we see a really useful feature/lib function
in newer versions) then i'm fine with leaving it and altering my PR
above, but in the past we usually kept up to date with bumping our msrv
kosiew pushed a commit to kosiew/datafusion that referenced this pull request Aug 12, 2026
…cimal) (apache#23631)
## 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
`Closesapache#123` indicates that this PR will close issue apache#123.
-->
N/A
## Rationale for this change
<!--
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.
-->
I noticed there were some subtle errors with how decimals were handled
in scalar values, and also opportunity to remove power calls in favour
of precomputed constant tables. Also filling out some other missing
support.
## What changes are included in this PR?
<!--
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.
-->
I recommend looking at the commits as they are self contained with
detailed messages for each.
## Are these changes tested?
<!--
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)?
-->
Yes
## Are there any user-facing changes?
<!--
If there are user-facing changes then we may require documentation to be
updated before approving the PR.
-->
No
<!--
If there are any breaking changes to public APIs, please add the `api
change` label.
-->
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

commonRelated to common crateoptimizerOptimizer rules

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

@Jefffrey@codecov-commenter@alamb@AdamGS