-
Notifications
You must be signed in to change notification settings - Fork 535
Make partition expression evaluation stateless #3664
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dossett
wants to merge
3
commits into
apache:main
Choose a base branch
from
dossett:stateless-expression-evaluator
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
| """Benchmark a realistic 15-leaf partition predicate when a prepared evaluator is shared across manifests. | ||
|
|
||
| Run with: | ||
| uv run pytest tests/benchmark/test_partition_evaluator_benchmark.py -v -s -m benchmark | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import statistics | ||
| import timeit | ||
|
|
||
| import pytest | ||
|
|
||
| from pyiceberg.expressions import And, BooleanExpression, EqualTo, GreaterThanOrEqual, LessThanOrEqual, Or | ||
| from pyiceberg.manifest import DataFile, FileFormat | ||
| from pyiceberg.partitioning import PartitionField, PartitionSpec | ||
| from pyiceberg.schema import Schema | ||
| from pyiceberg.table import ManifestGroupPlanner, Table | ||
| from pyiceberg.table.metadata import TableMetadataV2 | ||
| from pyiceberg.transforms import IdentityTransform | ||
| from pyiceberg.typedef import Record | ||
| from pyiceberg.types import LongType, NestedField | ||
|
|
||
|
|
||
| def _data_file(file_number: int) -> DataFile: | ||
| return DataFile.from_args( | ||
| file_path=f"s3://bucket/data-{file_number}.parquet", | ||
| file_format=FileFormat.PARQUET, | ||
| partition=Record(file_number % 11, file_number % 15), | ||
| record_count=100, | ||
| file_size_in_bytes=1, | ||
| ) | ||
|
|
||
|
|
||
| def _partition_filter() -> BooleanExpression: | ||
| """Select five day ranges, each scoped to a region.""" | ||
| windows = ((0, 1, 1), (2, 3, 4), (4, 5, 7), (6, 7, 10), (8, 10, 13)) | ||
| branches = [ | ||
| And( | ||
| And(GreaterThanOrEqual("event_day", start_day), LessThanOrEqual("event_day", end_day)), | ||
| EqualTo("region_id", region_id), | ||
| ) | ||
| for start_day, end_day, region_id in windows | ||
| ] | ||
|
|
||
| combined = branches[0] | ||
| for branch in branches[1:]: | ||
| combined = Or(combined, branch) | ||
| return combined | ||
|
|
||
|
|
||
| @pytest.mark.benchmark | ||
| @pytest.mark.parametrize( | ||
| "files_per_manifest", | ||
| [1_000, 1], | ||
| ids=["many-files-per-manifest", "one-file-per-manifest"], | ||
| ) | ||
| def test_partition_evaluator_reuse(table_v2: Table, files_per_manifest: int) -> None: | ||
| num_files = 1_000 | ||
| schema = Schema( | ||
| NestedField(1, "event_day", LongType(), required=True), | ||
| NestedField(2, "region_id", LongType(), required=True), | ||
| ) | ||
| spec = PartitionSpec( | ||
| PartitionField(1, 1000, IdentityTransform(), "event_day"), | ||
| PartitionField(2, 1001, IdentityTransform(), "region_id"), | ||
| spec_id=0, | ||
| ) | ||
| metadata = TableMetadataV2( | ||
| location="s3://bucket/table", | ||
| last_column_id=2, | ||
| schemas=[schema], | ||
| current_schema_id=schema.schema_id, | ||
| partition_specs=[spec], | ||
| default_spec_id=spec.spec_id, | ||
| ) | ||
| planner = ManifestGroupPlanner(table_metadata=metadata, io=table_v2.io, row_filter=_partition_filter()) | ||
| data_files = [_data_file(file_number) for file_number in range(num_files)] | ||
|
|
||
| def evaluate_files() -> int: | ||
| partition_evaluator = planner._build_partition_evaluator(spec.spec_id) | ||
| matches = 0 | ||
| for start in range(0, num_files, files_per_manifest): | ||
| matches += sum(partition_evaluator(data_file) for data_file in data_files[start : start + files_per_manifest]) | ||
| return matches | ||
|
|
||
| assert evaluate_files() == 67 | ||
| iterations = 100 | ||
| timings_ms = [timing * 1_000 / iterations for timing in timeit.repeat(evaluate_files, number=iterations, repeat=3)] | ||
| file_label = "file" if files_per_manifest == 1 else "files" | ||
|
|
||
| print( | ||
| f"Evaluated partitions for {num_files} files with {files_per_manifest} {file_label} per manifest " | ||
| f"and a 15-leaf predicate in " | ||
| f"{statistics.mean(timings_ms):.3f}ms (best: {min(timings_ms):.3f}ms)" | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from collections.abc import Callable | ||
|
|
||
| import pytest | ||
|
|
||
| import pyiceberg.table as table_module | ||
| from pyiceberg.expressions import BooleanExpression, GreaterThan | ||
| from pyiceberg.io import FileIO | ||
| from pyiceberg.manifest import DataFile, FileFormat, ManifestContent, ManifestEntry, ManifestFile | ||
| from pyiceberg.schema import Schema | ||
| from pyiceberg.table import ManifestGroupPlanner, Table | ||
| from pyiceberg.typedef import Record, StructProtocol | ||
|
|
||
|
|
||
| def _data_file(file_number: int, partition_value: int) -> DataFile: | ||
| return DataFile.from_args( | ||
| file_path=f"s3://bucket/data-{file_number}.parquet", | ||
| file_format=FileFormat.PARQUET, | ||
| partition=Record(partition_value), | ||
| record_count=100, | ||
| file_size_in_bytes=1, | ||
| ) | ||
|
|
||
|
|
||
| def _manifest_file(file_number: int) -> ManifestFile: | ||
| return ManifestFile.from_args( | ||
| manifest_path=f"s3://bucket/manifest-{file_number}.avro", | ||
| manifest_length=1, | ||
| partition_spec_id=0, | ||
| content=ManifestContent.DATA, | ||
| sequence_number=1, | ||
| min_sequence_number=1, | ||
| added_snapshot_id=1, | ||
| ) | ||
|
|
||
|
|
||
| def test_partition_evaluator_prepares_once_per_spec(table_v2: Table, monkeypatch: pytest.MonkeyPatch) -> None: | ||
| evaluator_calls: list[list[int]] = [] | ||
|
|
||
| def counting_expression_evaluator( | ||
| schema: Schema, unbound: BooleanExpression, case_sensitive: bool | ||
| ) -> Callable[[StructProtocol], bool]: | ||
| calls: list[int] = [] | ||
| evaluator_calls.append(calls) | ||
|
|
||
| def evaluate(struct: StructProtocol) -> bool: | ||
| value = struct[0] | ||
| calls.append(value) | ||
| return value > 5 | ||
|
|
||
| return evaluate | ||
|
|
||
| monkeypatch.setattr(table_module, "expression_evaluator", counting_expression_evaluator) | ||
| planner = ManifestGroupPlanner(table_metadata=table_v2.metadata, io=table_v2.io, row_filter=GreaterThan("x", 5)) | ||
| partition_evaluator = planner._build_partition_evaluator(0) | ||
|
|
||
| assert len(evaluator_calls) == 1 | ||
| assert not partition_evaluator(_data_file(1, 1)) | ||
| assert partition_evaluator(_data_file(2, 10)) | ||
| assert evaluator_calls == [[1, 10]] | ||
|
|
||
|
|
||
| def test_manifest_group_planner_shares_partition_evaluator_across_manifests( | ||
| table_v2: Table, monkeypatch: pytest.MonkeyPatch | ||
| ) -> None: | ||
| planner = ManifestGroupPlanner(table_metadata=table_v2.metadata, io=table_v2.io, row_filter=GreaterThan("x", 5)) | ||
| built_specs: list[int] = [] | ||
| opened_evaluators: list[Callable[[DataFile], bool]] = [] | ||
|
|
||
| def build_partition_evaluator(spec_id: int) -> Callable[[DataFile], bool]: | ||
| built_specs.append(spec_id) | ||
| return lambda _: True | ||
|
|
||
| def open_manifest( | ||
| io: FileIO, | ||
| manifest: ManifestFile, | ||
| partition_evaluator: Callable[[DataFile], bool], | ||
| metrics_evaluator: Callable[[DataFile], bool], | ||
| ) -> list[ManifestEntry]: | ||
| opened_evaluators.append(partition_evaluator) | ||
| return [] | ||
|
|
||
| monkeypatch.setattr(planner, "_build_manifest_evaluator", lambda _: lambda _: True) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not a huge fan of monkey patching in tests. If we ever do a refactor, this test immediately breaks. |
||
| monkeypatch.setattr(planner, "_build_partition_evaluator", build_partition_evaluator) | ||
| monkeypatch.setattr(table_module, "_open_manifest", open_manifest) | ||
|
|
||
| list(planner.plan_manifest_entries([_manifest_file(1), _manifest_file(2)])) | ||
|
|
||
| assert built_specs == [0] | ||
| assert len(opened_evaluators) == 2 | ||
| assert opened_evaluators[0] is opened_evaluators[1] | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we rename this file to
test_evaluator_planning.pyto try and consolidate tests with #3665?