rahulsmahadev opened a new pull request, #4615:
URL: https://github.com/apache/iceberg-python/pull/4615
# 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:
```python
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.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]