|
18 | 18 | from unittest.mock import MagicMock |
19 | 19 | from uuid import uuid4 |
20 | 20 |
|
| 21 | +import pyarrow as pa |
21 | 22 | import pytest |
22 | 23 |
|
| 24 | +from pyiceberg.catalog import Catalog |
23 | 25 | from pyiceberg.exceptions import ( |
24 | 26 | NoSuchSnapshotRefError, |
25 | 27 | NotAncestorError, |
@@ -512,3 +514,105 @@ def test_fast_forward_branch__with_operation_chaining__preserves_retention(self, |
512 | 514 | assert feature_updates[1].max_ref_age_ms == 5000 |
513 | 515 | assert feature_updates[1].max_snapshot_age_ms == 6000 |
514 | 516 | assert feature_updates[1].min_snapshots_to_keep == 7 |
| 517 | + |
| 518 | + |
| 519 | +def test_branch_write_preserves_retention(catalog_with_warehouse: Catalog) -> None: |
| 520 | + """Writing to a branch keeps the retention policy it was created with.""" |
| 521 | + catalog_with_warehouse.create_namespace("branch_retention") |
| 522 | + schema = pa.schema([pa.field("id", pa.int64())]) |
| 523 | + tbl = catalog_with_warehouse.create_table("branch_retention.tbl", schema=schema) |
| 524 | + tbl.append(pa.table({"id": [1]}, schema=schema)) |
| 525 | + tbl = catalog_with_warehouse.load_table("branch_retention.tbl") |
| 526 | + |
| 527 | + snapshot_id = tbl.metadata.current_snapshot_id |
| 528 | + assert snapshot_id is not None |
| 529 | + tbl.manage_snapshots().create_branch( |
| 530 | + snapshot_id=snapshot_id, |
| 531 | + branch_name="audit", |
| 532 | + max_ref_age_ms=86400000, |
| 533 | + max_snapshot_age_ms=3600000, |
| 534 | + min_snapshots_to_keep=5, |
| 535 | + ).commit() |
| 536 | + |
| 537 | + for i in range(3): |
| 538 | + tbl = catalog_with_warehouse.load_table("branch_retention.tbl") |
| 539 | + tbl.append(pa.table({"id": [i + 2]}, schema=schema), branch="audit") |
| 540 | + |
| 541 | + ref = catalog_with_warehouse.load_table("branch_retention.tbl").metadata.refs["audit"] |
| 542 | + assert ref.max_ref_age_ms == 86400000 |
| 543 | + assert ref.max_snapshot_age_ms == 3600000 |
| 544 | + assert ref.min_snapshots_to_keep == 5 |
| 545 | + |
| 546 | + |
| 547 | +def test_branch_write_without_retention_stays_unset(catalog_with_warehouse: Catalog) -> None: |
| 548 | + """A branch created without a retention policy does not gain one from a write.""" |
| 549 | + catalog_with_warehouse.create_namespace("branch_no_retention") |
| 550 | + schema = pa.schema([pa.field("id", pa.int64())]) |
| 551 | + tbl = catalog_with_warehouse.create_table("branch_no_retention.tbl", schema=schema) |
| 552 | + tbl.append(pa.table({"id": [1]}, schema=schema)) |
| 553 | + tbl = catalog_with_warehouse.load_table("branch_no_retention.tbl") |
| 554 | + |
| 555 | + snapshot_id = tbl.metadata.current_snapshot_id |
| 556 | + assert snapshot_id is not None |
| 557 | + tbl.manage_snapshots().create_branch(snapshot_id=snapshot_id, branch_name="plain").commit() |
| 558 | + |
| 559 | + tbl = catalog_with_warehouse.load_table("branch_no_retention.tbl") |
| 560 | + tbl.append(pa.table({"id": [2]}, schema=schema), branch="plain") |
| 561 | + |
| 562 | + ref = catalog_with_warehouse.load_table("branch_no_retention.tbl").metadata.refs["plain"] |
| 563 | + assert ref.max_ref_age_ms is None |
| 564 | + assert ref.max_snapshot_age_ms is None |
| 565 | + assert ref.min_snapshots_to_keep is None |
| 566 | + |
| 567 | + |
| 568 | +def test_rollback_preserves_retention(catalog_with_warehouse: Catalog) -> None: |
| 569 | + """Moving a ref keeps its retention policy; rollback and set_current_snapshot move main.""" |
| 570 | + catalog_with_warehouse.create_namespace("rollback_retention") |
| 571 | + schema = pa.schema([pa.field("id", pa.int64())]) |
| 572 | + tbl = catalog_with_warehouse.create_table("rollback_retention.tbl", schema=schema) |
| 573 | + tbl.append(pa.table({"id": [1]}, schema=schema)) |
| 574 | + tbl = catalog_with_warehouse.load_table("rollback_retention.tbl") |
| 575 | + first = tbl.metadata.current_snapshot_id |
| 576 | + assert first is not None |
| 577 | + |
| 578 | + tbl.append(pa.table({"id": [2]}, schema=schema)) |
| 579 | + tbl = catalog_with_warehouse.load_table("rollback_retention.tbl") |
| 580 | + head = tbl.metadata.current_snapshot_id |
| 581 | + assert head is not None |
| 582 | + |
| 583 | + tbl.manage_snapshots().create_branch( |
| 584 | + snapshot_id=head, |
| 585 | + branch_name="main", |
| 586 | + max_ref_age_ms=99999, |
| 587 | + max_snapshot_age_ms=1234, |
| 588 | + min_snapshots_to_keep=7, |
| 589 | + ).commit() |
| 590 | + |
| 591 | + tbl = catalog_with_warehouse.load_table("rollback_retention.tbl") |
| 592 | + tbl.manage_snapshots().rollback_to_snapshot(first).commit() |
| 593 | + |
| 594 | + ref = catalog_with_warehouse.load_table("rollback_retention.tbl").metadata.refs["main"] |
| 595 | + assert ref.snapshot_id == first |
| 596 | + assert ref.max_ref_age_ms == 99999 |
| 597 | + assert ref.max_snapshot_age_ms == 1234 |
| 598 | + assert ref.min_snapshots_to_keep == 7 |
| 599 | + |
| 600 | + |
| 601 | +def test_rollback_without_retention_stays_unset(catalog_with_warehouse: Catalog) -> None: |
| 602 | + """A ref with no retention policy does not gain one from being moved.""" |
| 603 | + catalog_with_warehouse.create_namespace("rollback_plain") |
| 604 | + schema = pa.schema([pa.field("id", pa.int64())]) |
| 605 | + tbl = catalog_with_warehouse.create_table("rollback_plain.tbl", schema=schema) |
| 606 | + tbl.append(pa.table({"id": [1]}, schema=schema)) |
| 607 | + tbl = catalog_with_warehouse.load_table("rollback_plain.tbl") |
| 608 | + first = tbl.metadata.current_snapshot_id |
| 609 | + assert first is not None |
| 610 | + |
| 611 | + tbl.append(pa.table({"id": [2]}, schema=schema)) |
| 612 | + tbl = catalog_with_warehouse.load_table("rollback_plain.tbl") |
| 613 | + tbl.manage_snapshots().rollback_to_snapshot(first).commit() |
| 614 | + |
| 615 | + ref = catalog_with_warehouse.load_table("rollback_plain.tbl").metadata.refs["main"] |
| 616 | + assert ref.max_ref_age_ms is None |
| 617 | + assert ref.max_snapshot_age_ms is None |
| 618 | + assert ref.min_snapshots_to_keep is None |
0 commit comments