rahulsmahadev opened a new pull request, #4540:
URL: https://github.com/apache/iceberg-python/pull/4540
# Rationale for this change
Bug: `SqlCatalog.register_table` inserts and commits the table row, then
calls `load_table`, which reads the metadata file in `_convert_orm_to_iceberg`.
When the file is missing or is not table metadata, `register_table` raises, but
the row stays. On main, with a fresh catalog:
```python
from pyiceberg.catalog.sql import SqlCatalog
catalog = SqlCatalog("default", uri="sqlite:///catalog.db",
warehouse="file:///tmp/warehouse")
catalog.create_namespace("db")
catalog.register_table("db.events",
"file:///tmp/metadata/00001-typo.metadata.json")
# FileNotFoundError
catalog.list_tables("db") # [('db', 'events')]
catalog.table_exists("db.events") # FileNotFoundError
catalog.register_table("db.events",
"file:///tmp/metadata/00001-abc.metadata.json")
# TableAlreadyExistsError: Table db.events already exists
```
The name stays taken until `drop_table` removes the row. `InMemoryCatalog`
extends `SqlCatalog`, so it has the same problem.
Fix: check the name, read the metadata, then insert the row. Java's REST
handler and Spark `register_table` procedure pass an existing metadata-file
location (from a table write or rewrite/copy) through
`BaseMetastoreCatalog.registerTable` to the JDBC insert; this PR changes the
SQL registration step to use the same read-before-insert order. Unlike the
`tableExists` call in Java's `registerTable`, the name check is a primary-key
lookup that does not load the existing table and also matches view rows, so an
existing name still raises `TableAlreadyExistsError` before the file is read,
as on main. Concurrent inserts still translate `IntegrityError` to
`TableAlreadyExistsError`. `register_table` returns the metadata read before
the insert instead of reloading it afterward, so no read can fail after the row
is committed.
The Glue and Hive catalogs also read the file before they create the table.
## Are these changes tested?
Yes. Two new tests fail on main and pass with this change:
- `test_register_table_with_unreadable_metadata` in
`tests/catalog/test_catalog_behaviors.py` tries to register a missing file and
a `{}` file, and checks that the table is not listed, `table_exists` returns
`False` and the name can then be registered with a valid file. It runs on the
in-memory and both SQL catalog fixtures, fails on main at the missing-file
`table_exists` check with `FileNotFoundError` and passes with this change. It
also checks that an existing name with a missing file still raises
`TableAlreadyExistsError`.
- `test_register_table_on_v0_schema` in `tests/catalog/test_sql.py`
registers a missing file and then a valid one on the legacy schema; on main,
`list_tables` still shows the table after the missing file.
Two guards in `tests/catalog/test_sql.py` also pass on main, and keep what
the name check and the insert must preserve:
- `test_register_table_rejects_view_and_unreadable_table_rows`: registering
the name of a view row, or of a table whose metadata file was removed, with a
missing file raises `TableAlreadyExistsError` and leaves both rows unchanged.
It fails if the name check uses `table_exists`.
- `test_register_table_concurrent_integrity_error`: a registration that
loses the race for the name raises `TableAlreadyExistsError`, and the winner
stays loadable. It fails if the insert stops translating `IntegrityError`.
## Are there any user-facing changes?
Yes, when `register_table` cannot read the metadata, `SqlCatalog` and
`InMemoryCatalog` no longer keep the table. Rows that earlier versions left
behind stay until `drop_table` removes 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]