Skip to content

feat: Use fixed-scale Decimals#24542

Merged
ritchie46 merged 18 commits intopola-rs:mainfrom
orlp:decimal-rework
Sep 19, 2025
Merged

feat: Use fixed-scale Decimals#24542
ritchie46 merged 18 commits intopola-rs:mainfrom
orlp:decimal-rework

Conversation

@orlp
Copy link
Copy Markdown
Member

@orlp orlp commented Sep 18, 2025

With this PR decimals now always require an explicit scale, with the exception of Series.str.to_decimal and Python constructors. The scale is now always fixed, even across arithmetic operations. When the inputs to an arithmetic operation have mixed scales the maximum scale is chosen. Precision is respected (and checked) when explicitly specified in casts, or from imported data sources, however any arithmetic operation immediately puts the precision to the maximum allowed (38). Conversions to Decimals round to the nearest value, breaking ties to even.

Unlike other arithmetic in Polars (at least, today), Decimal arithmetic is checked by default, meaning you'll get errors on overflow, division by zero, etc.

Fixes #19784.

Fixes #24481.
Fixes #24387.
Fixes #24350.
Fixes #24345.
Fixes #24190.
Fixes #23896.
Fixes #23063.
Fixes #21947.
Fixes #21684.
Fixes #21537.
Fixes #20575.
Fixes #20556.
Fixes #20464.
Fixes #20013.
Fixes #19959.
Fixes #19756.
Fixes #19630.
Fixes #18662.
Fixes #17046.
Fixes #16690.
Fixes #15953.
Fixes #15154.
Fixes #15153.
Fixes #15037.
Fixes #13987.
Fixes #13572.
Fixes #11593.

@github-actions github-actions Bot added enhancement New feature or an improvement of an existing feature python Related to Python Polars rust Related to Rust Polars labels Sep 18, 2025
@ritchie46
Copy link
Copy Markdown
Member

A few more fixing candidates #24481 #24387 #24190 #22039 #21684 #21660

@codecov
Copy link
Copy Markdown

codecov Bot commented Sep 19, 2025

Codecov Report

❌ Patch coverage is 74.47005% with 277 lines in your changes missing coverage. Please review.
✅ Project coverage is 81.74%. Comparing base (f0fc158) to head (89356e5).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
crates/polars-compute/src/decimal.rs 77.05% 123 Missing ⚠️
...olars-core/src/chunked_array/arithmetic/decimal.rs 68.13% 29 Missing ⚠️
...s/polars-core/src/chunked_array/logical/decimal.rs 77.65% 21 Missing ⚠️
crates/polars-core/src/series/from.rs 32.25% 21 Missing ⚠️
crates/polars-compute/src/cast/decimal_to.rs 30.76% 18 Missing ⚠️
crates/polars-core/src/utils/supertype.rs 42.10% 11 Missing ⚠️
crates/polars-core/src/datatypes/any_value.rs 77.41% 7 Missing ⚠️
py-polars/polars/_utils/construction/series.py 30.00% 4 Missing and 3 partials ⚠️
crates/polars-python/src/series/comparison.rs 50.00% 6 Missing ⚠️
crates/polars-core/src/frame/row/av_buffer.rs 0.00% 4 Missing ⚠️
... and 13 more
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #24542      +/-   ##
==========================================
- Coverage   81.81%   81.74%   -0.07%     
==========================================
  Files        1683     1684       +1     
  Lines      229051   229458     +407     
  Branches     2946     2952       +6     
==========================================
+ Hits       187406   187581     +175     
- Misses      40906    41134     +228     
- Partials      739      743       +4     

☔ View full report in Codecov by Sentry.
📢 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.

@ritchie46 ritchie46 merged commit 88cd1b0 into pola-rs:main Sep 19, 2025
29 checks passed
evertlammerts added a commit to duckdb/duckdb-python that referenced this pull request Oct 4, 2025
See #98 

We have our own Polars IO plugin to create lazy polars dataframes. We
try to push as many predicates down into DuckDB as we can, for which we
try to map Polars expressions (including datatypes) to SQL expressions.
To do this we depend on Polar's `Expr.meta.serialize`.

None of this is very stable. Polars IO source plugins are marked
`@unstable` and `Expr.meta.serialize` says "Serialization is not stable
across Polars versions".

In this case the problems seems to come from Polars requiring an
explicit scale to be set for decimals ([this
pr](pola-rs/polars#24542)). The serialized
format seems to have changed into:
```json
{
  "expr": {
    "Literal": {
      "Scalar": {
        "Decimal": [
          1,
          38, // This now includes the scale
          0
        ]
      }
    }
  },
  "dtype": {
    "Literal": {
      "Decimal": [
        38, // this was already there
        0
      ]
    }
  },
  "options": "Strict"
}

```

Interestingly, even if we explicitly set precision to e.g. 20, the
relevant part of the serialized expression looks as follows:
```json
{
  "expr": {
    "Literal": {
      "Scalar": {
        "Decimal": [
          1,
          38, // Still 38?
          0
        ]
      }
    }
  },
  "dtype": {
    "Literal": {
      "Decimal": [
        20, // This is correct
        0
      ]
    }
  },
  "options": "Strict"
}
```

This PR allows for both a 2 and 3 item list for decimals.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or an improvement of an existing feature python Related to Python Polars rust Related to Rust Polars

Projects

None yet

2 participants