Repository navigation
fix: keep room for an integer's digits when comparing it with a wide decimal - #27124
namanjain24-sudo wants to merge 3 commits into
Conversation
…n-coercion # Conflicts: # datafusion/expr-common/src/type_coercion/binary/tests/comparison.rs
|
I'm picking up #27424 (an integer compared with or added to a
If the integer side produced a
@jayzhan211 @namanjain24-sudo which would you prefer?
Fine with any of them; I mainly don't want to duplicate work that is already in review here. |
|
Option 2 works for me if it suits the maintainers — this PR is small and ready, and #27474 then keeps only the @jayzhan211 you filed #27424 — could you make the call, so #27474 isn't blocked on it? |
…mal's variant An integer compared with or added to a `Decimal32` or `Decimal64` value returned wrong rows rather than an error: a `Decimal32(9, 2)` holding `5.50` compared equal to `5`, and `5.50 + 1` returned `6` as an `Int64`. The helper that turned an integer into a decimal was bound to a single variant and had no arm for an integer too wide for it -- `Int32` or wider for `Decimal32`, `Int64` for `Decimal64` -- so both callers read that `None` as "not a decimal case" and fell through to the integer rules, which coerce the decimal to an integer and drop its fraction. How many digits an integer needs is known exactly, so its precision and scale are now computed independently of any variant, and the common type is the narrowest variant that holds them and is no narrower than the decimal's own. The same rule covers the case where the required precision passed the variant's maximum and was capped while the scale was kept, leaving a type with no room for the integer's digits at all (apache#27095, for which apache#27124 proposes a narrower fix). Where no variant holds the required precision, the capped type stands. A float keeps its previous behaviour. The scale it coerces to is an approximation of its own, and widening on that basis would move ordinary comparisons like `WHERE price = 4.00` onto a wider variant. Everything that asks for a common type follows: an `IN` list, `greatest`/`least`, a `UNION ALL`, an ASOF join key, interval containment and `gcd`/`lcm`. Each of those has a test here, as do a scale of 9 and a scale of -2 on a `Decimal32`. Closes apache#27424. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Which issue does this PR close?
Rationale for this change
Comparing an integer with a decimal that has a large scale fails at execution:
The common type of the two sides is
max(s1, s2) + max(p1 - s1, p2 - s2)digits, 48 here.Decimal128holds 38, and the precision was capped there while the scale stayed at 38, so the result had no room for integer digits at all and the integer side could not be cast into it. PostgreSQL returns the rows.What changes are included in this PR?
When the other side is an integer, the common type is now the narrowest decimal variant that holds the required precision instead of a capped one. How many integer digits an integer needs is known exactly, so a type without room for them fails every value that uses them.
A float keeps the previous behaviour. The scale a float coerces to is an approximation of its own (
Float64becomesDecimal128(30, 15)), and widening on that basis would move ordinary comparisons likeWHERE price = 4.00ontoDecimal256.What is the testing strategy for this PR?
A unit test in
type_coercion/binary/tests/comparison.rspins the coerced types: an integer againstDecimal128(38, 38)and againstDecimal128(38, 30), one that still fitsDecimal128, and a float against the same wide decimal so the unchanged path stays covered. The three queries from the issue are indecimal.slt, with the results checked against PostgreSQL 17.Are there any user-facing changes?
Comparisons of an integer with a wide decimal return rows instead of failing. No API changes.