-
Notifications
You must be signed in to change notification settings - Fork 302
validate added data files for snapshot compatibility #2050
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
kaushiksrini
wants to merge
7
commits into
apache:main
Choose a base branch
from
kaushiksrini:feat/validate_added_data_files
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
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5285cac
add validations for added data files
kaushiksrini 3dd8a74
make function hidden in validate
kaushiksrini 38a4cdf
add tests for added data files validations
kaushiksrini 98c47ef
docs: add docstrings to the functions
kaushiksrini 5adb926
change variable name to be more specific
kaushiksrini 9081c33
refactor code to avoid redundancies in validations
kaushiksrini dc0860b
Merge branch 'main' into feat/validate_added_data_files
kaushiksrini 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
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -14,17 +14,19 @@ | |||||
# KIND, either express or implied. See the License for the | ||||||
# specific language governing permissions and limitations | ||||||
# under the License. | ||||||
from typing import Iterator, Optional | ||||||
from typing import Iterator, Optional, Set | ||||||
|
||||||
from pyiceberg.exceptions import ValidationException | ||||||
from pyiceberg.expressions import BooleanExpression | ||||||
from pyiceberg.expressions.visitors import ROWS_CANNOT_MATCH, _InclusiveMetricsEvaluator | ||||||
from pyiceberg.manifest import ManifestContent, ManifestEntry, ManifestEntryStatus, ManifestFile | ||||||
from pyiceberg.schema import Schema | ||||||
from pyiceberg.table import Table | ||||||
from pyiceberg.table.snapshots import Operation, Snapshot, ancestors_between | ||||||
from pyiceberg.typedef import Record | ||||||
|
||||||
VALIDATE_DATA_FILES_EXIST_OPERATIONS = {Operation.OVERWRITE, Operation.REPLACE, Operation.DELETE} | ||||||
VALIDATE_DATA_FILES_EXIST_OPERATIONS: Set[Operation] = {Operation.OVERWRITE, Operation.REPLACE, Operation.DELETE} | ||||||
VALIDATE_ADDED_DATA_FILES_OPERATIONS: Set[Operation] = {Operation.APPEND, Operation.OVERWRITE} | ||||||
|
||||||
|
||||||
def _validation_history( | ||||||
|
@@ -77,6 +79,47 @@ def _validation_history( | |||||
return manifests_files, snapshots | ||||||
|
||||||
|
||||||
def _filter_manifest_entries( | ||||||
entry: ManifestEntry, | ||||||
snapshot_ids: set[int], | ||||||
data_filter: Optional[BooleanExpression], | ||||||
partition_set: Optional[dict[int, set[Record]]], | ||||||
entry_status: Optional[ManifestEntryStatus], | ||||||
schema: Schema, | ||||||
) -> bool: | ||||||
"""Filter manifest entries based on data filter and partition set. | ||||||
|
||||||
Args: | ||||||
entry: Manifest entry to filter | ||||||
snapshot_ids: set of snapshot ids to match data files | ||||||
data_filter: Optional filter to match data files | ||||||
partition_set: Optional set of partitions to match data files | ||||||
status: Optional status to match data files | ||||||
table: Table containing the schema for filtering | ||||||
|
||||||
Returns: | ||||||
True if the entry should be included, False otherwise | ||||||
""" | ||||||
if entry.snapshot_id not in snapshot_ids: | ||||||
return False | ||||||
|
||||||
if entry_status is not None and entry.status != entry_status: | ||||||
return False | ||||||
|
||||||
if data_filter is not None: | ||||||
evaluator = _InclusiveMetricsEvaluator(schema, data_filter) | ||||||
if evaluator.eval(entry.data_file) is ROWS_CANNOT_MATCH: | ||||||
return False | ||||||
|
||||||
if partition_set is not None: | ||||||
partition = entry.data_file.partition | ||||||
spec_id = entry.data_file.spec_id | ||||||
if spec_id not in partition_set or partition not in partition_set[spec_id]: | ||||||
return False | ||||||
|
||||||
return True | ||||||
|
||||||
|
||||||
def _deleted_data_files( | ||||||
table: Table, | ||||||
starting_snapshot: Snapshot, | ||||||
|
@@ -108,27 +151,12 @@ def _deleted_data_files( | |||||
ManifestContent.DATA, | ||||||
) | ||||||
|
||||||
if data_filter is not None: | ||||||
evaluator = _InclusiveMetricsEvaluator(table.schema(), data_filter).eval | ||||||
|
||||||
for manifest in manifests: | ||||||
for entry in manifest.fetch_manifest_entry(table.io, discard_deleted=False): | ||||||
if entry.snapshot_id not in snapshot_ids: | ||||||
continue | ||||||
|
||||||
if entry.status != ManifestEntryStatus.DELETED: | ||||||
continue | ||||||
|
||||||
if data_filter is not None and evaluator(entry.data_file) is ROWS_CANNOT_MATCH: | ||||||
continue | ||||||
|
||||||
if partition_set is not None: | ||||||
spec_id = entry.data_file.spec_id | ||||||
partition = entry.data_file.partition | ||||||
if spec_id not in partition_set or partition not in partition_set[spec_id]: | ||||||
continue | ||||||
|
||||||
yield entry | ||||||
if _filter_manifest_entries( | ||||||
entry, snapshot_ids, data_filter, partition_set, ManifestEntryStatus.DELETED, table.schema() | ||||||
): | ||||||
yield entry | ||||||
|
||||||
|
||||||
def _validate_deleted_data_files( | ||||||
|
@@ -150,3 +178,60 @@ def _validate_deleted_data_files( | |||||
if any(conflicting_entries): | ||||||
conflicting_snapshots = {entry.snapshot_id for entry in conflicting_entries} | ||||||
raise ValidationException(f"Deleted data files were found matching the filter for snapshots {conflicting_snapshots}!") | ||||||
|
||||||
|
||||||
def _added_data_files( | ||||||
table: Table, | ||||||
starting_snapshot: Snapshot, | ||||||
data_filter: Optional[BooleanExpression], | ||||||
partition_set: Optional[dict[int, set[Record]]], | ||||||
parent_snapshot: Optional[Snapshot], | ||||||
) -> Iterator[ManifestEntry]: | ||||||
"""Return manifest entries for data files added between the starting snapshot and parent snapshot. | ||||||
|
||||||
Args: | ||||||
table: Table to get the history from | ||||||
starting_snapshot: Starting snapshot to get the history from | ||||||
data_filter: Optional filter to match data files | ||||||
partition_set: Optional set of partitions to match data files | ||||||
parent_snapshot: Parent snapshot to get the history from | ||||||
|
||||||
Returns: | ||||||
Iterator of manifest entries for added data files matching the conditions | ||||||
""" | ||||||
if parent_snapshot is None: | ||||||
return | ||||||
|
||||||
manifests, snapshot_ids = validation_history( | ||||||
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.
Suggested change
This was made private here #2054 |
||||||
table, | ||||||
parent_snapshot, | ||||||
starting_snapshot, | ||||||
VALIDATE_ADDED_DATA_FILES_OPERATIONS, | ||||||
ManifestContent.DATA, | ||||||
) | ||||||
|
||||||
for manifest in manifests: | ||||||
for entry in manifest.fetch_manifest_entry(table.io): | ||||||
if _filter_manifest_entries(entry, snapshot_ids, data_filter, partition_set, None, table.schema()): | ||||||
yield entry | ||||||
|
||||||
|
||||||
def _validate_added_data_files( | ||||||
table: Table, | ||||||
starting_snapshot: Snapshot, | ||||||
data_filter: Optional[BooleanExpression], | ||||||
parent_snapshot: Optional[Snapshot], | ||||||
) -> None: | ||||||
"""Validate that no files matching a filter have been added to the table since a starting snapshot. | ||||||
|
||||||
Args: | ||||||
table: Table to validate | ||||||
starting_snapshot: Snapshot current at the start of the operation | ||||||
data_filter: Expression used to find added data files | ||||||
parent_snapshot: Ending snapshot on the branch being validated | ||||||
|
||||||
""" | ||||||
conflicting_entries = _added_data_files(table, starting_snapshot, data_filter, None, parent_snapshot) | ||||||
if any(conflicting_entries): | ||||||
conflicting_snapshots = {entry.snapshot_id for entry in conflicting_entries if entry.snapshot_id is not None} | ||||||
raise ValidationException(f"Added data files were found matching the filter for snapshots {conflicting_snapshots}!") |
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
Oops, something went wrong.
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.
Could we introduce a doc string for each function?