|
| 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 | + |
| 18 | +from __future__ import annotations |
| 19 | + |
| 20 | +from collections.abc import Callable |
| 21 | + |
| 22 | +import pytest |
| 23 | + |
| 24 | +import pyiceberg.table as table_module |
| 25 | +from pyiceberg.expressions import BooleanExpression, GreaterThan |
| 26 | +from pyiceberg.io import FileIO |
| 27 | +from pyiceberg.manifest import DataFile, FileFormat, ManifestContent, ManifestEntry, ManifestFile |
| 28 | +from pyiceberg.schema import Schema |
| 29 | +from pyiceberg.table import ManifestGroupPlanner, Table |
| 30 | +from pyiceberg.typedef import Record, StructProtocol |
| 31 | + |
| 32 | + |
| 33 | +def _data_file(file_number: int, partition_value: int) -> DataFile: |
| 34 | + return DataFile.from_args( |
| 35 | + file_path=f"s3://bucket/data-{file_number}.parquet", |
| 36 | + file_format=FileFormat.PARQUET, |
| 37 | + partition=Record(partition_value), |
| 38 | + record_count=100, |
| 39 | + file_size_in_bytes=1, |
| 40 | + ) |
| 41 | + |
| 42 | + |
| 43 | +def _manifest_file(file_number: int) -> ManifestFile: |
| 44 | + return ManifestFile.from_args( |
| 45 | + manifest_path=f"s3://bucket/manifest-{file_number}.avro", |
| 46 | + manifest_length=1, |
| 47 | + partition_spec_id=0, |
| 48 | + content=ManifestContent.DATA, |
| 49 | + sequence_number=1, |
| 50 | + min_sequence_number=1, |
| 51 | + added_snapshot_id=1, |
| 52 | + ) |
| 53 | + |
| 54 | + |
| 55 | +def test_partition_evaluator_reuses_instance_per_manifest_callable(table_v2: Table, monkeypatch: pytest.MonkeyPatch) -> None: |
| 56 | + evaluator_calls: list[list[int]] = [] |
| 57 | + |
| 58 | + def counting_expression_evaluator( |
| 59 | + schema: Schema, unbound: BooleanExpression, case_sensitive: bool |
| 60 | + ) -> Callable[[StructProtocol], bool]: |
| 61 | + calls: list[int] = [] |
| 62 | + evaluator_calls.append(calls) |
| 63 | + |
| 64 | + def evaluate(struct: StructProtocol) -> bool: |
| 65 | + calls.append(struct[0]) |
| 66 | + return True |
| 67 | + |
| 68 | + return evaluate |
| 69 | + |
| 70 | + monkeypatch.setattr(table_module, "expression_evaluator", counting_expression_evaluator) |
| 71 | + planner = ManifestGroupPlanner(table_metadata=table_v2.metadata, io=table_v2.io, row_filter=GreaterThan("x", 5)) |
| 72 | + first_file = _data_file(1, 1) |
| 73 | + second_file = _data_file(2, 10) |
| 74 | + |
| 75 | + partition_evaluator_factory = planner._build_partition_evaluator_factory(0) |
| 76 | + first_callable = partition_evaluator_factory() |
| 77 | + assert not evaluator_calls |
| 78 | + assert first_callable(first_file) |
| 79 | + assert first_callable(second_file) |
| 80 | + |
| 81 | + second_callable = partition_evaluator_factory() |
| 82 | + assert len(evaluator_calls) == 1 |
| 83 | + assert second_callable(first_file) |
| 84 | + |
| 85 | + assert evaluator_calls == [[1, 10], [1]] |
| 86 | + |
| 87 | + |
| 88 | +def test_manifest_group_planner_creates_partition_evaluator_per_manifest( |
| 89 | + table_v2: Table, monkeypatch: pytest.MonkeyPatch |
| 90 | +) -> None: |
| 91 | + planner = ManifestGroupPlanner(table_metadata=table_v2.metadata, io=table_v2.io, row_filter=GreaterThan("x", 5)) |
| 92 | + built_factories: list[int] = [] |
| 93 | + built_evaluators: list[Callable[[DataFile], bool]] = [] |
| 94 | + opened_evaluators: list[Callable[[DataFile], bool]] = [] |
| 95 | + |
| 96 | + def build_partition_evaluator_factory(spec_id: int) -> Callable[[], Callable[[DataFile], bool]]: |
| 97 | + built_factories.append(spec_id) |
| 98 | + |
| 99 | + def partition_evaluator_factory() -> Callable[[DataFile], bool]: |
| 100 | + def partition_evaluator(data_file: DataFile) -> bool: |
| 101 | + return True |
| 102 | + |
| 103 | + built_evaluators.append(partition_evaluator) |
| 104 | + return partition_evaluator |
| 105 | + |
| 106 | + return partition_evaluator_factory |
| 107 | + |
| 108 | + def open_manifest( |
| 109 | + io: FileIO, |
| 110 | + manifest: ManifestFile, |
| 111 | + partition_evaluator: Callable[[DataFile], bool], |
| 112 | + metrics_evaluator: Callable[[DataFile], bool], |
| 113 | + ) -> list[ManifestEntry]: |
| 114 | + opened_evaluators.append(partition_evaluator) |
| 115 | + return [] |
| 116 | + |
| 117 | + monkeypatch.setattr(planner, "_build_manifest_evaluator", lambda _: lambda _: True) |
| 118 | + monkeypatch.setattr(planner, "_build_partition_evaluator_factory", build_partition_evaluator_factory) |
| 119 | + monkeypatch.setattr(table_module, "_open_manifest", open_manifest) |
| 120 | + |
| 121 | + list(planner.plan_manifest_entries([_manifest_file(1), _manifest_file(2)])) |
| 122 | + |
| 123 | + assert built_factories == [0] |
| 124 | + assert len(built_evaluators) == 2 |
| 125 | + assert {id(evaluator) for evaluator in opened_evaluators} == {id(evaluator) for evaluator in built_evaluators} |
| 126 | + |
| 127 | + |
| 128 | +def test_reused_partition_evaluator_replaces_file_state(table_v2: Table) -> None: |
| 129 | + planner = ManifestGroupPlanner(table_metadata=table_v2.metadata, io=table_v2.io, row_filter=GreaterThan("x", 5)) |
| 130 | + partition_evaluator = planner._build_partition_evaluator_factory(0)() |
| 131 | + |
| 132 | + assert not partition_evaluator(_data_file(1, 1)) |
| 133 | + assert partition_evaluator(_data_file(2, 10)) |
| 134 | + assert not partition_evaluator(_data_file(3, 2)) |
0 commit comments