Skip to content

Use decimal256 for decimal filter literals above 38 digits - #4526

Closed
rahulsmahadev wants to merge 1 commit into
apache:mainfrom
rahulsmahadev:rahul/decimal38-wide-literal
Closed

rahulsmahadev wants to merge 1 commit into
apache:mainfrom
rahulsmahadev:rahul/decimal38-wide-literal

Conversation

@rahulsmahadev

Copy link
Copy Markdown
Contributor

Rationale for this change

Bug: follow-up to #4348, also noted in the review of #4372. A literal past the range of a precision-38 decimal column has 39 or more digits at the column's scale, for example 1 on decimal(38, 38). The filter is valid, but the scan builds the Arrow literal as decimal128, which stops at 38 digits:

# ratio is decimal(38, 38), amount is decimal(38, 18)
tbl.scan(row_filter=LessThan("ratio", 1)).to_arrow()
# ValueError: precision should be between 1 and 38
tbl.scan(row_filter=In("amount", {10**20, -3})).to_arrow()
# ValueError: precision should be between 1 and 38

Other comparisons and NOT IN fail too when they reach the Arrow filter, including when delete() rewrites a file.

Fix: in _literal_arrow_type, use decimal256 for literals that need 39 to 76 digits at the column's scale. PyArrow compares a decimal128 column with a decimal256 scalar or value set by value, also on PyArrow 18. Literals that fit in 38 digits keep their type, and literals that need more than 76 digits still raise.

Java's filters (the Expressions API, Spark pushdown through SparkV2Filters) bind in UnboundPredicate, where integer literals only setScale, and the manifest, metrics and row evaluators (Evaluator, which GenericReader applies to residuals) compare the BigDecimal by value. This PR changes only the Arrow row filter; the existing NULL and scale-binding differences from Java remain.

The alternative is to fold out-of-range decimal literals to always-true/false at bind, as open #4371 does for floats; its always-true result also includes NULL rows. This PR keeps the comparison by value of #4348.

Are these changes tested?

Yes, test_decimal_literal_wider_than_decimal128_to_pyarrow (<, !=, IN, NOT IN with 10**20 on decimal(38, 18)) and test_row_filter_decimal_literal_wider_than_decimal128 (ratio < 1, amount < 10**20 and amount IN (10**20, -3) through a table scan) in tests/io/test_pyarrow.py. Every case fails on main with the ValueError above and passes with this change.

Are there any user-facing changes?

Yes, filters with decimal literals that need 39 to 76 digits at the column's scale return rows instead of raising.

Please add the changelog label.

This pull request and its description were written by Isaac.

A literal past a precision-38 column's range, such as ratio < 1 on
decimal(38, 38), has 39 digits at the column's scale, so the scan raised
"precision should be between 1 and 38" building its decimal128 type.
Use decimal256 above 38 digits; Arrow compares it with the column by value.

Co-authored-by: Isaac <[email protected]>
@rahulsmahadev

Copy link
Copy Markdown
Contributor Author

@kevinjqliu, this is a follow-up to #4348: a filter literal past a decimal(38, s) column's range, such as ratio < 1 on decimal(38, 38), made the scan raise because the Arrow literal type stopped at decimal128. Could you take a look when you have a chance? Thanks!

Comment thread pyiceberg/io/pyarrow.py
Comment on lines +931 to +932
if precision > 38:
return pa.decimal256(precision, iceberg_type.scale)

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.

Isn't this in conflict with the spec: https://iceberg.apache.org/spec/#primitive-types

It explicitly states:

Scale is fixed, precision must be 38 or less

@rahulsmahadev

Copy link
Copy Markdown
Contributor Author

Closing this one. Fair point on the spec: rather than decimal256, out-of-range decimal literals should fold at bind time with the sentinels from #4371.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants