|
| 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 | +# pylint:disable=redefined-outer-name |
| 18 | + |
| 19 | +import pyarrow as pa |
| 20 | +import pytest |
| 21 | + |
| 22 | +from pyiceberg.catalog import Catalog |
| 23 | +from pyiceberg.expressions import EqualTo |
| 24 | +from pyiceberg.partitioning import PartitionField, PartitionSpec |
| 25 | +from pyiceberg.schema import Schema |
| 26 | +from pyiceberg.table import TableProperties |
| 27 | +from pyiceberg.transforms import IdentityTransform |
| 28 | +from pyiceberg.types import LongType, NestedField |
| 29 | +from utils import _create_table |
| 30 | + |
| 31 | +_SCHEMA = Schema( |
| 32 | + NestedField(field_id=1, name="id", field_type=LongType(), required=False), |
| 33 | + NestedField(field_id=2, name="partition_col", field_type=LongType(), required=False), |
| 34 | +) |
| 35 | + |
| 36 | +_PARTITION_SPEC = PartitionSpec(PartitionField(source_id=2, field_id=1001, transform=IdentityTransform(), name="partition_col")) |
| 37 | + |
| 38 | +_DATA_P1 = pa.table({"id": [1, 2, 3], "partition_col": [1, 1, 1]}) |
| 39 | +_DATA_P2 = pa.table({"id": [4, 5, 6], "partition_col": [2, 2, 2]}) |
| 40 | +_DATA_P1_NEW = pa.table({"id": [10, 11, 12], "partition_col": [1, 1, 1]}) |
| 41 | + |
| 42 | + |
| 43 | +@pytest.mark.integration |
| 44 | +@pytest.mark.parametrize("format_version", [1, 2]) |
| 45 | +def test_overwrite_with_merging_bounds_manifest_count(session_catalog: Catalog, format_version: int) -> None: |
| 46 | + """After a partial overwrite with manifest merging enabled, the manifest count should |
| 47 | + not exceed the count before the overwrite, even when many manifests were accumulated.""" |
| 48 | + identifier = f"default.test_overwrite_merges_manifests_v{format_version}" |
| 49 | + |
| 50 | + # Build up 6 manifests via fast append (merging disabled) |
| 51 | + tbl = _create_table( |
| 52 | + session_catalog, |
| 53 | + identifier, |
| 54 | + { |
| 55 | + "format-version": str(format_version), |
| 56 | + TableProperties.MANIFEST_MERGE_ENABLED: "false", |
| 57 | + }, |
| 58 | + partition_spec=_PARTITION_SPEC, |
| 59 | + schema=_SCHEMA, |
| 60 | + ) |
| 61 | + |
| 62 | + for _ in range(3): |
| 63 | + tbl.append(_DATA_P1) |
| 64 | + tbl.append(_DATA_P2) |
| 65 | + |
| 66 | + assert len(tbl.inspect.manifests()) == 6 |
| 67 | + |
| 68 | + # Enable merging before the overwrite |
| 69 | + with tbl.transaction() as tx: |
| 70 | + tx.set_properties( |
| 71 | + { |
| 72 | + TableProperties.MANIFEST_MERGE_ENABLED: "true", |
| 73 | + TableProperties.MANIFEST_MIN_MERGE_COUNT: "2", |
| 74 | + } |
| 75 | + ) |
| 76 | + |
| 77 | + # Overwrite partition_col=1 only: the 3 partition_col=2 manifests are preserved |
| 78 | + # and merged together with the new manifest by _MergeAppendFiles._process_manifests |
| 79 | + tbl.overwrite(_DATA_P1_NEW, EqualTo("partition_col", 1)) |
| 80 | + |
| 81 | + assert len(tbl.inspect.manifests()) < 6 |
| 82 | + |
| 83 | + # Data correctness: partition_col=2 has 3 × 3 = 9 rows; partition_col=1 replaced with 3 new rows |
| 84 | + result = tbl.scan().to_arrow() |
| 85 | + assert len(result) == 12 |
| 86 | + assert sorted(result.column("id").to_pylist()) == [4, 4, 4, 5, 5, 5, 6, 6, 6, 10, 11, 12] |
| 87 | + |
| 88 | + |
| 89 | +@pytest.mark.integration |
| 90 | +@pytest.mark.parametrize("format_version", [1, 2]) |
| 91 | +def test_overwrite_without_merging_increases_manifest_count(session_catalog: Catalog, format_version: int) -> None: |
| 92 | + """Control test: without manifest merging, a partial overwrite grows the manifest count.""" |
| 93 | + identifier = f"default.test_overwrite_no_merge_manifests_v{format_version}" |
| 94 | + |
| 95 | + tbl = _create_table( |
| 96 | + session_catalog, |
| 97 | + identifier, |
| 98 | + { |
| 99 | + "format-version": str(format_version), |
| 100 | + TableProperties.MANIFEST_MERGE_ENABLED: "false", |
| 101 | + }, |
| 102 | + partition_spec=_PARTITION_SPEC, |
| 103 | + schema=_SCHEMA, |
| 104 | + ) |
| 105 | + |
| 106 | + for _ in range(3): |
| 107 | + tbl.append(_DATA_P1) |
| 108 | + for _ in range(3): |
| 109 | + tbl.append(_DATA_P2) |
| 110 | + |
| 111 | + assert len(tbl.inspect.manifests()) == 6 |
| 112 | + |
| 113 | + # Overwrite without merging: new manifest is added on top of the existing ones |
| 114 | + tbl.overwrite(_DATA_P1_NEW, EqualTo("partition_col", 1)) |
| 115 | + |
| 116 | + assert len(tbl.inspect.manifests()) == 4 |
| 117 | + |
| 118 | + # Data correctness is identical regardless of merging strategy |
| 119 | + result = tbl.scan().to_arrow() |
| 120 | + assert len(result) == 12 |
| 121 | + assert sorted(result.column("id").to_pylist()) == [4, 4, 4, 5, 5, 5, 6, 6, 6, 10, 11, 12] |
| 122 | + |
| 123 | + |
| 124 | +@pytest.mark.integration |
| 125 | +@pytest.mark.parametrize("format_version", [1, 2]) |
| 126 | +def test_fast_append_does_not_merge_manifests(session_catalog: Catalog, format_version: int) -> None: |
| 127 | + """Fast append bypasses _MergingSnapshotProducer, so manifests grow with each append |
| 128 | + even when manifest merging properties are set to trigger early.""" |
| 129 | + identifier = f"default.test_fast_append_no_merge_v{format_version}" |
| 130 | + |
| 131 | + tbl = _create_table( |
| 132 | + session_catalog, |
| 133 | + identifier, |
| 134 | + { |
| 135 | + "format-version": str(format_version), |
| 136 | + TableProperties.MANIFEST_MERGE_ENABLED: "false", |
| 137 | + TableProperties.MANIFEST_MIN_MERGE_COUNT: "2", |
| 138 | + }, |
| 139 | + schema=_SCHEMA, |
| 140 | + ) |
| 141 | + |
| 142 | + for expected_count in range(1, 6): |
| 143 | + tbl.append(_DATA_P1) |
| 144 | + assert len(tbl.inspect.manifests()) == expected_count |
0 commit comments