|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | +from pathlib import Path |
| 18 | + |
| 19 | +import pyarrow as pa |
| 20 | +import pytest |
| 21 | + |
| 22 | +from pyiceberg.catalog import Catalog |
| 23 | +from pyiceberg.catalog.memory import InMemoryCatalog |
| 24 | +from pyiceberg.manifest import ManifestContent |
| 25 | +from pyiceberg.table import Table |
| 26 | +from pyiceberg.table.snapshots import Operation |
| 27 | + |
| 28 | + |
| 29 | +@pytest.fixture |
| 30 | +def catalog(tmp_path: Path) -> Catalog: |
| 31 | + catalog = InMemoryCatalog("test.rewrite_manifests", warehouse=f"file://{tmp_path}") |
| 32 | + catalog.create_namespace("default") |
| 33 | + return catalog |
| 34 | + |
| 35 | + |
| 36 | +def _arrow_table(offset: int = 0) -> pa.Table: |
| 37 | + return pa.table({"id": pa.array([offset + 1, offset + 2, offset + 3], type=pa.int64())}) |
| 38 | + |
| 39 | + |
| 40 | +def _create_table_with_appends(catalog: Catalog, appends: int = 3) -> Table: |
| 41 | + table = catalog.create_table("default.test_rewrite", schema=pa.schema([pa.field("id", pa.int64())])) |
| 42 | + for i in range(appends): |
| 43 | + table.append(_arrow_table(offset=i * 3)) |
| 44 | + return table |
| 45 | + |
| 46 | + |
| 47 | +def _data_manifests(table: Table) -> list: |
| 48 | + snapshot = table.current_snapshot() |
| 49 | + assert snapshot is not None |
| 50 | + return [m for m in snapshot.manifests(table.io) if m.content == ManifestContent.DATA] |
| 51 | + |
| 52 | + |
| 53 | +def test_rewrite_manifests_merges_data_manifests(catalog: Catalog) -> None: |
| 54 | + table = _create_table_with_appends(catalog, appends=3) |
| 55 | + assert len(_data_manifests(table)) == 3 |
| 56 | + rows_before = table.scan().to_arrow().sort_by("id") |
| 57 | + |
| 58 | + table.maintenance.rewrite_manifests().commit() |
| 59 | + |
| 60 | + table = catalog.load_table("default.test_rewrite") |
| 61 | + manifests = _data_manifests(table) |
| 62 | + assert len(manifests) == 1 |
| 63 | + # entries are rewritten as EXISTING |
| 64 | + assert manifests[0].existing_files_count == 3 |
| 65 | + assert manifests[0].added_files_count == 0 |
| 66 | + |
| 67 | + # data is unchanged |
| 68 | + assert table.scan().to_arrow().sort_by("id") == rows_before |
| 69 | + |
| 70 | + snapshot = table.current_snapshot() |
| 71 | + assert snapshot is not None |
| 72 | + assert snapshot.summary is not None |
| 73 | + assert snapshot.summary.operation == Operation.REPLACE |
| 74 | + assert snapshot.summary["manifests-created"] == "1" |
| 75 | + assert snapshot.summary["manifests-replaced"] == "3" |
| 76 | + assert snapshot.summary["entries-processed"] == "3" |
| 77 | + # totals carry over unchanged |
| 78 | + assert snapshot.summary["total-data-files"] == "3" |
| 79 | + assert snapshot.summary["total-records"] == "9" |
| 80 | + |
| 81 | + |
| 82 | +def test_rewrite_manifests_preserves_sequence_numbers(catalog: Catalog) -> None: |
| 83 | + table = _create_table_with_appends(catalog, appends=3) |
| 84 | + entries_before = { |
| 85 | + entry.data_file.file_path: entry.sequence_number |
| 86 | + for manifest in _data_manifests(table) |
| 87 | + for entry in manifest.fetch_manifest_entry(table.io, discard_deleted=True) |
| 88 | + } |
| 89 | + |
| 90 | + table.maintenance.rewrite_manifests().commit() |
| 91 | + |
| 92 | + table = catalog.load_table("default.test_rewrite") |
| 93 | + manifests = _data_manifests(table) |
| 94 | + entries_after = { |
| 95 | + entry.data_file.file_path: entry.sequence_number |
| 96 | + for manifest in manifests |
| 97 | + for entry in manifest.fetch_manifest_entry(table.io, discard_deleted=True) |
| 98 | + } |
| 99 | + assert entries_after == entries_before |
| 100 | + # the merged manifest keeps the min sequence number of its entries |
| 101 | + assert manifests[0].min_sequence_number == min(entries_before.values()) |
| 102 | + |
| 103 | + |
| 104 | +def test_rewrite_manifests_single_manifest_is_noop_kept(catalog: Catalog) -> None: |
| 105 | + table = _create_table_with_appends(catalog, appends=1) |
| 106 | + manifest_path_before = _data_manifests(table)[0].manifest_path |
| 107 | + |
| 108 | + table.maintenance.rewrite_manifests().commit() |
| 109 | + |
| 110 | + table = catalog.load_table("default.test_rewrite") |
| 111 | + manifests = _data_manifests(table) |
| 112 | + assert len(manifests) == 1 |
| 113 | + # a single manifest is kept as-is, not rewritten |
| 114 | + assert manifests[0].manifest_path == manifest_path_before |
| 115 | + |
| 116 | + snapshot = table.current_snapshot() |
| 117 | + assert snapshot is not None |
| 118 | + assert snapshot.summary is not None |
| 119 | + assert snapshot.summary["manifests-created"] == "0" |
| 120 | + assert snapshot.summary["manifests-replaced"] == "0" |
| 121 | + |
| 122 | + |
| 123 | +def test_rewrite_manifests_respects_target_size(catalog: Catalog) -> None: |
| 124 | + table = _create_table_with_appends(catalog, appends=4) |
| 125 | + manifest_length = _data_manifests(table)[0].manifest_length |
| 126 | + |
| 127 | + # allow roughly two source manifests per group |
| 128 | + with table.transaction() as tx: |
| 129 | + tx.set_properties({"commit.manifest.target-size-bytes": str(manifest_length * 2)}) |
| 130 | + |
| 131 | + table = catalog.load_table("default.test_rewrite") |
| 132 | + table.maintenance.rewrite_manifests().commit() |
| 133 | + |
| 134 | + table = catalog.load_table("default.test_rewrite") |
| 135 | + manifests = _data_manifests(table) |
| 136 | + assert len(manifests) == 2 |
| 137 | + assert all(m.existing_files_count == 2 for m in manifests) |
| 138 | + |
| 139 | + |
| 140 | +def test_rewrites_needed(catalog: Catalog) -> None: |
| 141 | + table = _create_table_with_appends(catalog, appends=1) |
| 142 | + assert table.maintenance.rewrite_manifests().rewrites_needed() is False |
| 143 | + |
| 144 | + table.append(_arrow_table(offset=3)) |
| 145 | + table = catalog.load_table("default.test_rewrite") |
| 146 | + assert table.maintenance.rewrite_manifests().rewrites_needed() is True |
0 commit comments