Skip to content

Accept INT32 and INT64 decimals for wider decimal columns in add_files - #4615

Open
rahulsmahadev wants to merge 1 commit into
apache:mainfrom
rahulsmahadev:rahul/add-files-int-stored-decimal
Open

rahulsmahadev wants to merge 1 commit into
apache:mainfrom
rahulsmahadev:rahul/add-files-int-stored-decimal

Conversation

@rahulsmahadev

Copy link
Copy Markdown
Contributor

Rationale for this change

Bug: add_files accepts a file whose decimal(18, 2) column maps to a decimal(38, 2) table column, since widening the precision is a valid promotion, and then fails while collecting the column's bounds:

tbl = catalog.create_table("default.payments", schema=Schema(NestedField(1, "amount", DecimalType(38, 2))))
pq.write_table(
    pa.table({"amount": pa.array([Decimal("1.25"), Decimal("-3.50")], pa.decimal128(18, 2))}),
    "payments.parquet",
    store_decimal_as_integer=True,
)
tbl.add_files(["payments.parquet"])
# ValueError: Unexpected physical type INT64 for decimal(38, 2), expected FIXED_LEN_BYTE_ARRAY for column 'amount'

StatsAggregator compares the file's physical type with the one PyIceberg writes for the table's precision. But a file keeps the storage of its own precision: the spec stores decimal(P, S) as int32 for P <= 9 and int64 for P <= 18, and PyIceberg, Iceberg Java, Spark, DuckDB and Polars write it that way. So an INT32 or INT64 decimal fails once the table column's precision is above 18, for example after widening it. #2515 (for #2057) only allowed the opposite case, FIXED_LEN_BYTE_ARRAY for a small precision.

Fix: allow INT32, INT64 and FIXED_LEN_BYTE_ARRAY for any decimal, as the comment above the check already says (kevinjqliu asked for it on #2515). data_file_statistics_from_parquet_metadata already decodes all three, and decimal bounds do not depend on the precision (minimal two's complement bytes). The FIXED_LEN_BYTE_ARRAY exception now only applies to decimals; other types never reached it from add_files, because the schema check rejects them first. The TODO to reuse the promotion logic stays: a decimal's storage does not follow from its type even without a promotion (#2057).

Java: Spark's add_files procedure gets the metrics from ParquetUtil.fileMetrics (through SparkTableUtil and TableMigrationUtil.listPartition), which uses the file's own schema, and ParquetConversions.convertValue reads decimal statistics stored as INT32, INT64, FIXED_LEN_BYTE_ARRAY or BINARY at any precision. This PR fixes the physical type check in front of PyIceberg's version of that conversion. One difference remains:

  • A decimal stored as BINARY (BYTE_ARRAY) still raises when its bounds are collected (the default metrics mode). Its bounds could be read like FIXED_LEN_BYTE_ARRAY ones, but pyarrow cannot write such a file, so a test needs a checked-in Parquet file; that can follow separately.

Are these changes tested?

Yes, in tests/io/test_pyarrow.py: test_add_files_int_stored_decimal_into_wider_decimal (INT32 into decimal(20, 2) and INT64 into decimal(38, 2): bounds, pruning and rows) fails on main with ValueError: Unexpected physical type INT32 for decimal(20, 2), expected FIXED_LEN_BYTE_ARRAY. The new cases in test_stats_aggregator_conditionally_allowed_types_pass also fail on main with Unexpected physical type, and the LongType() case in test_stats_aggregator_physical_type_does_not_match_expected_raise_error fails because main does not raise. The BYTE_ARRAY case passes on main too; it checks that the new branch still rejects a storage the decoder cannot read. All pass with this change.

Are there any user-facing changes?

Yes, add_files accepts these files instead of raising.

Please add the changelog label.

This pull request and its description were written by Isaac.

StatsAggregator compared a decimal column's Parquet physical type with the one PyIceberg writes for the
table's precision, so add_files rejected a decimal(18, 2) file stored as INT64 for a decimal(38, 2) column.
Allow INT32, INT64 and FIXED_LEN_BYTE_ARRAY for any decimal, as the comment above the check says; the
statistics of all three are already decoded with the column's scale.

Co-authored-by: Isaac <no-reply@databricks.com>

@Fokko Fokko 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.

Good catch @rahulsmahadev! Verified that Java's ParquetConversions.convertValue accepts INT32, INT64 and FIXED_LEN_BYTE_ARRAY decimals at any precision, so this aligns add_files with Spark. Thanks for the tests covering the new pass cases and the still-rejected BYTE_ARRAY case. 👍

This branch has not been deployed

No deployments
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