rahulsmahadev opened a new pull request, #4603: URL: https://github.com/apache/iceberg-python/pull/4603
# Rationale for this change Follow-up to #4359, for files without field IDs: this fixes the first case in [this #4359 review comment](https://github.com/apache/iceberg-python/pull/4359#discussion_r4700853778). The second case there, a dropped column whose name a new column reuses, does not change: the mapping update moves the name to the new column (#4318), and `add_files` and the scan both follow the mapping. Bug: `add_files` resolves the columns of a file without field IDs with a name mapping built from the table's current names. A rename keeps the old name in the table's name mapping (`schema.name-mapping.default`), and the scan reads such files through that mapping, but `add_files` rejects a file that still uses the old name: ```python tbl.add_files(["before.parquet"]) # columns id, name; add_files stores the name mapping with tbl.update_schema() as update: update.rename_column("name", "full_name") # the mapping now has [name, full_name] -> 2 tbl.add_files(["after.parquet"]) # same columns as before.parquet # ValueError: PyArrow table contains more columns: name. Update the schema first (hint, use union_by_name). ``` The spec maps several names to one field ID "to support cases where a field may have different names in different data files". A renamed nested field fails the same way. Fix: `parquet_file_to_data_file` passes the table's name mapping to `_check_pyarrow_schema_compatible`, so the check and the statistics resolve the columns the way the scan does. Without a stored mapping the current names are used as before, and writes are unchanged. A file could now resolve two columns, the old and the new name, to one field ID, so `add_files` rejects files with two columns for one field ID (`Multiple file columns resolve to field ID 2: full_name, name`). This also rejects a file with the same field ID twice, which main added; the scan then read one of the two columns and the value counts added up. Java: Spark's `add_files` procedure creates the name mapping if there is none, and `SparkTableUtil` passes it through `TableMigrationUtil.listPartition` to `ParquetUtil.fileMetrics`, which applies it to files without field IDs; `SchemaUpdate` keeps old names in it (`MappingUtil.update`). Java's importer also fails on duplicate field IDs, but does not check the file against the table schema. `add_files` keeps that check, so a column that maps to no current field, such as the old name of a dropped column, is still rejected. ## Are these changes tested? Yes, in `tests/io/test_pyarrow.py`: `test_add_files_matches_columns_by_table_name_mapping` (top-level and list-of-struct renames, rows and value counts, files with two columns for one field ID, a dropped column's old name, and an incomplete stored mapping) and `test_schema_compatible_with_name_mapping` (the new parameter and its default). Both fail on main (the first with `ValueError: PyArrow table contains more columns: address.element.zip, address.zip, name`, the second with a `TypeError` for the new parameter) and pass with this change. ## Are there any user-facing changes? Yes. `add_files` accepts old column names recorded in the table's mapping and rejects duplicate resolved field IDs. It also rejects files without field IDs when their current table columns are missing from the stored mapping; main added those files, then failed to scan them. 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]
