Repository navigation
Support row lineage assignment on commit #4102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,16 +22,20 @@ | |
| from urllib.parse import urlparse | ||
|
|
||
| import pyarrow as pa | ||
| import pyarrow.parquet as pq | ||
| import pytest | ||
|
|
||
| from pyiceberg import __version__ | ||
| from pyiceberg.catalog import Catalog | ||
| from pyiceberg.catalog.noop import NoopCatalog | ||
| from pyiceberg.exceptions import ValidationException | ||
| from pyiceberg.io import load_file_io | ||
| from pyiceberg.io.pyarrow import _dataframe_to_data_files | ||
| from pyiceberg.manifest import DataFile, DataFileContent, ManifestContent, ManifestFile | ||
| from pyiceberg.partitioning import PartitionField, PartitionSpec | ||
| from pyiceberg.partitioning import UNPARTITIONED_PARTITION_SPEC, PartitionField, PartitionSpec | ||
| from pyiceberg.schema import Schema | ||
| from pyiceberg.table import Table | ||
| from pyiceberg.table.metadata import new_table_metadata | ||
| from pyiceberg.table.snapshots import ( | ||
| Operation, | ||
| Snapshot, | ||
|
|
@@ -45,6 +49,7 @@ | |
| latest_ancestor_before_timestamp, | ||
| update_snapshot_summaries, | ||
| ) | ||
| from pyiceberg.table.sorting import UNSORTED_SORT_ORDER | ||
| from pyiceberg.transforms import IdentityTransform | ||
| from pyiceberg.typedef import Record | ||
| from pyiceberg.types import ( | ||
|
|
@@ -844,3 +849,101 @@ def test_overwrite_rejects_explicit_delete_without_parent_snapshot( | |
| with empty.transaction() as tx: | ||
| with tx.update_snapshot().overwrite() as overwrite: | ||
| overwrite.delete_data_file(stale_file) | ||
|
|
||
|
|
||
| def test_manifest_list_receives_first_row_id_for_v3(table_v3: Table) -> None: | ||
| """The V3 manifest list writer requires first-row-id, so the producer has to supply it.""" | ||
| from unittest import mock | ||
|
|
||
| from pyiceberg.table.update.snapshot import _FastAppendFiles | ||
|
|
||
| txn = table_v3.transaction() | ||
| append = _FastAppendFiles(operation=Operation.APPEND, transaction=txn, io=table_v3.io) | ||
|
|
||
| with ( | ||
| mock.patch("pyiceberg.table.update.snapshot.write_manifest_list") as writer, | ||
| mock.patch.object(_FastAppendFiles, "_manifests", return_value=[]), | ||
| ): | ||
| append._commit() | ||
|
|
||
| assert writer.call_args.kwargs["format_version"] == 3 | ||
| assert writer.call_args.kwargs["first_row_id"] == table_v3.metadata.next_row_id | ||
|
|
||
|
|
||
| def test_manifest_list_has_no_first_row_id_for_v2(table_v2: Table) -> None: | ||
| from unittest import mock | ||
|
|
||
| from pyiceberg.table.update.snapshot import _FastAppendFiles | ||
|
|
||
| txn = table_v2.transaction() | ||
| append = _FastAppendFiles(operation=Operation.APPEND, transaction=txn, io=table_v2.io) | ||
|
|
||
| with ( | ||
| mock.patch("pyiceberg.table.update.snapshot.write_manifest_list") as writer, | ||
| mock.patch.object(_FastAppendFiles, "_manifests", return_value=[]), | ||
| ): | ||
| append._commit() | ||
|
|
||
| assert writer.call_args.kwargs["format_version"] == 2 | ||
| assert writer.call_args.kwargs["first_row_id"] is None | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def empty_table_v3(tmp_path: Path, arrow_table_simple: pa.Table) -> Table: | ||
| """An empty V3 table; writing V3 metadata files is not supported, so commits stay staged in a transaction.""" | ||
| metadata = new_table_metadata( | ||
| schema=Catalog._convert_schema_if_needed(arrow_table_simple.schema, format_version=3), | ||
| partition_spec=UNPARTITIONED_PARTITION_SPEC, | ||
| sort_order=UNSORTED_SORT_ORDER, | ||
| location=str(tmp_path), | ||
| properties={"format-version": "3"}, | ||
| ).model_copy(update={"next_row_id": 0}) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see that
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. #3551 already covers this: |
||
| return Table( | ||
| identifier=("default", "lineage"), | ||
| metadata=metadata, | ||
| metadata_location=f"{tmp_path}/metadata/00000.metadata.json", | ||
| io=load_file_io(), | ||
| catalog=NoopCatalog("NoopCatalog"), | ||
| ) | ||
|
|
||
|
|
||
| def test_v3_append_assigns_row_lineage(empty_table_v3: Table, arrow_table_simple: pa.Table) -> None: | ||
| txn = empty_table_v3.transaction() | ||
|
|
||
| txn.append(arrow_table_simple) | ||
| first = txn.table_metadata.current_snapshot() | ||
| assert first is not None | ||
| assert (first.first_row_id, first.added_rows) == (0, 3) | ||
| assert txn.table_metadata.next_row_id == 3 | ||
|
|
||
| txn.append(arrow_table_simple) | ||
| second = txn.table_metadata.current_snapshot() | ||
| assert second is not None | ||
| assert (second.first_row_id, second.added_rows) == (3, 3) | ||
| assert txn.table_metadata.next_row_id == 6 | ||
|
|
||
|
|
||
| def test_v3_add_files_assigns_row_lineage(empty_table_v3: Table, arrow_table_simple: pa.Table, tmp_path: Path) -> None: | ||
| file_path = str(tmp_path / "data.parquet") | ||
| pq.write_table(arrow_table_simple, file_path) | ||
| txn = empty_table_v3.transaction() | ||
|
|
||
| txn.add_files([file_path]) | ||
|
|
||
| snapshot = txn.table_metadata.current_snapshot() | ||
| assert snapshot is not None | ||
| assert (snapshot.first_row_id, snapshot.added_rows) == (0, 3) | ||
| assert txn.table_metadata.next_row_id == 3 | ||
|
|
||
|
|
||
| def test_v3_delete_assigns_row_ids_to_rewritten_rows_only(empty_table_v3: Table, arrow_table_simple: pa.Table) -> None: | ||
| txn = empty_table_v3.transaction() | ||
| txn.append(arrow_table_simple) | ||
|
|
||
| txn.delete("bar == 1") | ||
|
|
||
| snapshot = txn.table_metadata.current_snapshot() | ||
| assert snapshot is not None | ||
| # the rewritten data file holds the 2 surviving rows; the old file's DELETED entry uses no row IDs | ||
| assert (snapshot.first_row_id, snapshot.added_rows) == (3, 2) | ||
| assert txn.table_metadata.next_row_id == 5 | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The spec says: "If a commit is retried, the
first-row-idmust be reassigned based on the table's currentnext-row-id". I think_rebuild_snapshot_updatesalready handles this because it calls_commit()again on refreshed metadata, but nothing tests it. How about adding3to theformat_versionparametrize oftest_conflict_append_appendintests/integration/test_writes/test_optimistic_concurrency.py, and asserting that the second snapshot'sfirst_row_idstarts where the first one ended? WDYT?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good call, added.
test_conflict_append_appendnow runs for V3 too, and asserts that the three snapshots have(first_row_id, added_rows)of(0, n),(n, n)and(2n, n), so the retried append starts after the rows of the concurrent one. As you said,_attempt_landed()refreshes the table before_rebuild_snapshot_updates()calls_commit()again, so the retry reads the currentnext-row-id.