|
| 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 | +"""Benchmark a realistic 15-leaf partition predicate when a prepared evaluator is shared across manifests. |
| 18 | +
|
| 19 | +Run with: |
| 20 | + uv run pytest tests/benchmark/test_partition_evaluator_benchmark.py -v -s -m benchmark |
| 21 | +""" |
| 22 | + |
| 23 | +from __future__ import annotations |
| 24 | + |
| 25 | +import statistics |
| 26 | +import timeit |
| 27 | + |
| 28 | +import pytest |
| 29 | + |
| 30 | +from pyiceberg.expressions import And, BooleanExpression, EqualTo, GreaterThanOrEqual, LessThanOrEqual, Or |
| 31 | +from pyiceberg.manifest import DataFile, FileFormat |
| 32 | +from pyiceberg.partitioning import PartitionField, PartitionSpec |
| 33 | +from pyiceberg.schema import Schema |
| 34 | +from pyiceberg.table import ManifestGroupPlanner, Table |
| 35 | +from pyiceberg.table.metadata import TableMetadataV2 |
| 36 | +from pyiceberg.transforms import IdentityTransform |
| 37 | +from pyiceberg.typedef import Record |
| 38 | +from pyiceberg.types import LongType, NestedField |
| 39 | + |
| 40 | + |
| 41 | +def _data_file(file_number: int) -> DataFile: |
| 42 | + return DataFile.from_args( |
| 43 | + file_path=f"s3://bucket/data-{file_number}.parquet", |
| 44 | + file_format=FileFormat.PARQUET, |
| 45 | + partition=Record(file_number % 11, file_number % 15), |
| 46 | + record_count=100, |
| 47 | + file_size_in_bytes=1, |
| 48 | + ) |
| 49 | + |
| 50 | + |
| 51 | +def _partition_filter() -> BooleanExpression: |
| 52 | + """Select five day ranges, each scoped to a region.""" |
| 53 | + windows = ((0, 1, 1), (2, 3, 4), (4, 5, 7), (6, 7, 10), (8, 10, 13)) |
| 54 | + branches = [ |
| 55 | + And( |
| 56 | + And(GreaterThanOrEqual("event_day", start_day), LessThanOrEqual("event_day", end_day)), |
| 57 | + EqualTo("region_id", region_id), |
| 58 | + ) |
| 59 | + for start_day, end_day, region_id in windows |
| 60 | + ] |
| 61 | + |
| 62 | + combined = branches[0] |
| 63 | + for branch in branches[1:]: |
| 64 | + combined = Or(combined, branch) |
| 65 | + return combined |
| 66 | + |
| 67 | + |
| 68 | +@pytest.mark.benchmark |
| 69 | +@pytest.mark.parametrize( |
| 70 | + "files_per_manifest", |
| 71 | + [1_000, 1], |
| 72 | + ids=["many-files-per-manifest", "one-file-per-manifest"], |
| 73 | +) |
| 74 | +def test_partition_evaluator_reuse(table_v2: Table, files_per_manifest: int) -> None: |
| 75 | + num_files = 1_000 |
| 76 | + schema = Schema( |
| 77 | + NestedField(1, "event_day", LongType(), required=True), |
| 78 | + NestedField(2, "region_id", LongType(), required=True), |
| 79 | + ) |
| 80 | + spec = PartitionSpec( |
| 81 | + PartitionField(1, 1000, IdentityTransform(), "event_day"), |
| 82 | + PartitionField(2, 1001, IdentityTransform(), "region_id"), |
| 83 | + spec_id=0, |
| 84 | + ) |
| 85 | + metadata = TableMetadataV2( |
| 86 | + location="s3://bucket/table", |
| 87 | + last_column_id=2, |
| 88 | + schemas=[schema], |
| 89 | + current_schema_id=schema.schema_id, |
| 90 | + partition_specs=[spec], |
| 91 | + default_spec_id=spec.spec_id, |
| 92 | + ) |
| 93 | + planner = ManifestGroupPlanner(table_metadata=metadata, io=table_v2.io, row_filter=_partition_filter()) |
| 94 | + data_files = [_data_file(file_number) for file_number in range(num_files)] |
| 95 | + |
| 96 | + def evaluate_files() -> int: |
| 97 | + partition_evaluator = planner._build_partition_evaluator(spec.spec_id) |
| 98 | + matches = 0 |
| 99 | + for start in range(0, num_files, files_per_manifest): |
| 100 | + matches += sum(partition_evaluator(data_file) for data_file in data_files[start : start + files_per_manifest]) |
| 101 | + return matches |
| 102 | + |
| 103 | + assert evaluate_files() == 67 |
| 104 | + iterations = 100 |
| 105 | + timings_ms = [timing * 1_000 / iterations for timing in timeit.repeat(evaluate_files, number=iterations, repeat=3)] |
| 106 | + file_label = "file" if files_per_manifest == 1 else "files" |
| 107 | + |
| 108 | + print( |
| 109 | + f"Evaluated partitions for {num_files} files with {files_per_manifest} {file_label} per manifest " |
| 110 | + f"and a 15-leaf predicate in " |
| 111 | + f"{statistics.mean(timings_ms):.3f}ms (best: {min(timings_ms):.3f}ms)" |
| 112 | + ) |
0 commit comments