Repository navigation
Support upgrading tables to format version 3 - #3623
moomindani wants to merge 4 commits into
Conversation
4f3e899 to
44631e0
Compare
|
Note on the red CI: the Upgrading to v3 produces a So this stays a draft until #3551 merges, at which point I'll rebase and mark it ready for review. @rambleraptor — no rush, just flagging that this one is queued behind #3551. |
e51c0e9 to
68d80d3
Compare
|
Not stale — this is a draft, blocked on #3551. Restating why the one red check is expected rather than a defect, since it has come up before: upgrading to v3 produces a |
|
This pull request has been marked as stale due to 30 days of inactivity. It will be closed in 1 week if no further activity occurs. If you think that's incorrect or this pull request requires a review, please simply write any comment. If closed, you can revive the PR at any time and @mention a reviewer or discuss it on the dev@iceberg.apache.org list. Thank you for your contributions. |
|
Not stale, still a draft blocked on #3551. |
|
This pull request has been marked as stale due to 30 days of inactivity. It will be closed in 1 week if no further activity occurs. If you think that's incorrect or this pull request requires a review, please simply write any comment. If closed, you can revive the PR at any time and @mention a reviewer or discuss it on the dev@iceberg.apache.org list. Thank you for your contributions. |
Allow upgrade_table_version(3) and initialize next-row-id to 0 on upgrade, per the spec's Row Lineage for Upgraded Tables. Persisting the upgraded metadata still requires V3 metadata serialization (apache#3551). Closes apache#3622
Support upgrading via the format-version table property in set_properties, matching TableMetadata.replaceProperties in Java: the property triggers UpgradeFormatVersionUpdate instead of being stored. Add tests for parity with TestTableMetadata and TestRowLineageMetadata: direct v1-to-v3 upgrade, same-version no-op, and property-based upgrade.
Reject non-integer format-version values in set_properties with a clear error instead of truncating floats or failing with a confusing cast error, and verify in the integration test that the upgraded metadata persists and reloads from the catalog.
68d80d3 to
a60c232
Compare
Fokko
left a comment
There was a problem hiding this comment.
Thanks @moomindani for working on this, and for keeping it rebased while it waits on #3551 👍 The upgrade itself looks right to me: initializing next-row-id to 0 and leaving the existing snapshots untouched is what the spec asks for in Row Lineage for Upgraded Tables, and it lines up with Java. I gave it a spin with #3551's change applied: the upgrade and the read of the existing data work, but the next append fails with ValueError: Cannot write manifest for table version: 3, so we'd let users upgrade a table that PyIceberg can't write to anymore. I left a question on that, plus a request to split the set_properties change into a separate PR, since it's a different concern. Happy to take another look once #3551 is in.
| The alter table builder. | ||
| """ | ||
| if format_version not in {1, 2}: | ||
| if not 1 <= format_version <= SUPPORTED_TABLE_FORMAT_VERSION: |
There was a problem hiding this comment.
Before we open this up, should we make sure we can actually write to a v3 table? With #3551 applied locally, upgrade_table_version(3) followed by tbl.append(df) raises ValueError: Cannot write manifest for table version: 3. Users would be able to upgrade a table that PyIceberg then can't append to. I think we should at least document this, or gate it until v3 manifest writes (#3624) land. @kevinjqliu @rambleraptor WDYT?
|
|
||
| INITIAL_ROW_ID = 0 | ||
| """The initial value of `next-row-id` for newly upgraded V3 tables.""" |
There was a problem hiding this comment.
INITIAL_ROW_ID = 0 is already defined a few lines up, and update/__init__.py already imports it from there. Can we drop the redefinition?
| INITIAL_ROW_ID = 0 | |
| """The initial value of `next-row-id` for newly upgraded V3 tables.""" |
|
|
||
| updated_metadata = base_metadata.model_copy(update={"format_version": update.format_version}) | ||
| updates: dict[str, Any] = {"format_version": update.format_version} | ||
| if update.format_version >= 3 and getattr(base_metadata, "next_row_id", None) is None: |
There was a problem hiding this comment.
We only get here when upgrading, and the highest supported version is 3, so base_metadata is always V1/V2 and doesn't have next_row_id. I think the getattr can go, mirroring Java where nextRowId stays at INITIAL_ROW_ID until v3:
| if update.format_version >= 3 and getattr(base_metadata, "next_row_id", None) is None: | |
| if update.format_version >= 3: |
| updates = dict(properties or kwargs) | ||
| if (new_format_version := updates.pop(TableProperties.FORMAT_VERSION, None)) is not None: | ||
| if isinstance(new_format_version, bool) or not str(new_format_version).isdigit(): | ||
| raise ValueError(f"Invalid format-version: {new_format_version}") | ||
| self.upgrade_table_version(format_version=cast(TableVersion, int(new_format_version))) | ||
| if updates: | ||
| return self._apply((SetPropertiesUpdate(updates=updates),)) | ||
| return self |
There was a problem hiding this comment.
This matches Java's replaceProperties, but it's a separate concern, it isn't mentioned in the description, and it changes set_properties behavior for v1/v2 tables too. Could we split it into its own PR? Then this one stays minimal. If we keep it, we could reuse property_as_int from pyiceberg.utils.properties instead of the bool/isdigit handling, and let upgrade_table_version reject unsupported versions.
| # the upgraded metadata must persist and reload from the catalog | ||
| reloaded = catalog.load_table("default.test_table_version") | ||
| assert reloaded.format_version == 3 | ||
| assert reloaded.metadata.next_row_id == 0 |
There was a problem hiding this comment.
default.test_table_version is empty, so this doesn't show that existing data is still readable after the upgrade. How about also creating a v2 table with some rows, upgrading it to v3, and then asserting that both PyIceberg and Spark read the same rows? The Spark read checks that Java accepts the metadata we wrote.
| ) | ||
|
|
||
|
|
||
| def test_set_properties_invalid_format_version_rejected(table_v2: Table) -> None: |
There was a problem hiding this comment.
Could we move this next to the other set_properties tests? Appending it at the end of the file is what's causing the merge conflict with main.
| assert validated.next_row_id == 0 | ||
|
|
||
|
|
||
| def test_update_metadata_upgrade_same_version_is_noop(table_v2: Table) -> None: |
There was a problem hiding this comment.
This already passes on main, so it doesn't test the change here. Can we remove it? Less is more 👍
|
|
||
|
|
||
| def test_update_metadata_upgrade_to_v3(table_v2: Table) -> None: | ||
| from pyiceberg.table.metadata import TableMetadataV3 |
There was a problem hiding this comment.
Nit: TableMetadataV3 can be imported at the top of the file, next to TableMetadataV2, instead of inside the test. Same for the next test.
Closes #3622 (part of #1551)
Rationale for this change
Allow upgrading existing tables to format version 3:
upgrade_table_version(3)is no longer rejected, andnext-row-idis initialized to 0 on upgrade, following the spec's "Row Lineage for Upgraded Tables" (existing snapshots are left unmodified).Draft until #3551 merges: persisting the upgraded metadata requires V3 metadata serialization, which #3551 enables. Until then the updated integration test (
test_upgrade_table_version) fails at the metadata write. I'll rebase and mark this ready for review once #3551 lands.Are these changes tested?
Yes. A new unit test (
test_update_metadata_upgrade_to_v3) covers the upgrade producingTableMetadataV3withnext-row-id=0including re-validation, and thetest_upgrade_table_versionintegration test is updated to upgrade to v3 (currently blocked on #3551 as noted above).Are there any user-facing changes?
Yes: v2 tables can be upgraded to v3 via
upgrade_table_version(3)once V3 metadata serialization (#3551) is available.This pull request and its description were written by Claude Fable 5.