Skip to content

Read the metadata before inserting the row in SqlCatalog.register_table - #4540

Open
rahulsmahadev wants to merge 1 commit into
apache:mainfrom
rahulsmahadev:rahul/sql-register-table-read-first
Open

rahulsmahadev wants to merge 1 commit into
apache:mainfrom
rahulsmahadev:rahul/sql-register-table-read-first

Conversation

@rahulsmahadev

Copy link
Copy Markdown
Contributor

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:

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.

register_table committed the table row before load_table read the metadata file,
so a missing or invalid file left the table registered: list_tables showed it,
table_exists raised and the name could not be registered again. Check the name,
read the metadata, then insert the row, as Java's registerTable does.

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

Copy link
Copy Markdown
Contributor Author

@rchowell, this changes SqlCatalog.register_table, which #3263 last touched: it now reads the metadata before inserting the row, as Java's registerTable and the Glue and Hive catalogs do, so a bad metadata location no longer leaves a row behind. Could you take a look when you have a chance? Thanks!

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.

1 participant