Skip to content

Commit bcdbfa9

Browse files
committed
check history when other commit appended
1 parent 9596da4 commit bcdbfa9

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

pyiceberg/catalog/bigquery_metastore.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,22 @@ def _check_bigquery_commit_status(self, table_ref: TableReference, new_metadata_
510510
if bq_table.external_catalog_table_options and bq_table.external_catalog_table_options.parameters
511511
else {}
512512
)
513-
return "SUCCESS" if parameters.get(METADATA_LOCATION_PROP) == new_metadata_location else "FAILURE"
513+
current_metadata_location = parameters.get(METADATA_LOCATION_PROP)
514+
if current_metadata_location == new_metadata_location:
515+
return "SUCCESS"
516+
517+
if not current_metadata_location:
518+
return "FAILURE"
519+
520+
io = self._load_file_io(location=current_metadata_location)
521+
current_metadata = FromInputFile.table_metadata(io.new_input(current_metadata_location))
522+
523+
previous_metadata_locations = {log.metadata_file for log in current_metadata.metadata_log}
524+
previous_metadata_location = parameters.get(PREVIOUS_METADATA_LOCATION_PROP)
525+
if previous_metadata_location:
526+
previous_metadata_locations.add(previous_metadata_location)
527+
528+
return "SUCCESS" if new_metadata_location in previous_metadata_locations else "FAILURE"
514529
except NotFound:
515530
return "FAILURE"
516531
except Exception:

tests/catalog/test_bigquery_metastore.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,3 +277,29 @@ def test_commit_table_raises_unknown_when_commit_status_is_unknown(mocker: MockF
277277

278278
with pytest.raises(CommitStateUnknownException):
279279
catalog.commit_table(table, requirements=(), updates=())
280+
281+
282+
def test_check_bigquery_commit_status_returns_success_when_metadata_in_history(mocker: MockFixture) -> None:
283+
client_mock = MagicMock()
284+
bq_table = MagicMock()
285+
bq_table.external_catalog_table_options = MagicMock(
286+
parameters={"metadata_location": "gs://bucket/db/table/metadata/00002.metadata.json"}
287+
)
288+
client_mock.get_table.return_value = bq_table
289+
mocker.patch("pyiceberg.catalog.bigquery_metastore.Client", return_value=client_mock)
290+
mocker.patch.dict(os.environ, values={"PYICEBERG_LEGACY_CURRENT_SNAPSHOT_ID": "True"})
291+
292+
catalog = BigQueryMetastoreCatalog("test_catalog", **{"gcp.bigquery.project-id": "my-project"})
293+
io_mock = MagicMock()
294+
catalog._load_file_io = MagicMock(return_value=io_mock) # type: ignore[method-assign]
295+
296+
current_metadata = MagicMock()
297+
current_metadata.metadata_log = [MagicMock(metadata_file="gs://bucket/db/table/metadata/00001.metadata.json")]
298+
mocker.patch("pyiceberg.catalog.bigquery_metastore.FromInputFile.table_metadata", return_value=current_metadata)
299+
300+
status = catalog._check_bigquery_commit_status(
301+
TableReference(dataset_ref=DatasetReference(project="my-project", dataset_id="my-dataset"), table_id="my-table"),
302+
"gs://bucket/db/table/metadata/00001.metadata.json",
303+
)
304+
305+
assert status == "SUCCESS"

0 commit comments

Comments
 (0)