-
Notifications
You must be signed in to change notification settings - Fork 378
feat: validation_history
and ancestors_between
#1935
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
Changes from 23 commits
beff92b
c369720
41bb8a4
763e9f4
f200beb
7f6bf9d
c63cc55
f2f3a88
74d5569
167f9e4
efe50b4
0793713
b10a0bf
d57133e
6ef1aa2
fce608f
a6624d9
0763056
cd0146f
d7c7088
fe8d103
f8c5fee
d95e6ed
87ee601
c16a3e6
e272b26
727845b
e2bba3f
a74d7a9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,71 @@ | ||||||
# 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 pyiceberg.exceptions import ValidationException | ||||||
from pyiceberg.manifest import ManifestContent, ManifestFile | ||||||
from pyiceberg.table import Table | ||||||
from pyiceberg.table.snapshots import Operation, Snapshot, ancestors_between | ||||||
|
||||||
|
||||||
def validation_history( | ||||||
table: Table, | ||||||
to_snapshot: Snapshot, | ||||||
from_snapshot: Snapshot, | ||||||
matching_operations: set[Operation], | ||||||
manifest_content_filter: ManifestContent, | ||||||
) -> tuple[list[ManifestFile], set[int]]: | ||||||
"""Return newly added manifests and snapshot IDs between the starting snapshot and parent snapshot. | ||||||
|
||||||
Args: | ||||||
table: Table to get the history from | ||||||
to_snapshot: Starting snapshot | ||||||
from_snapshot: Parent snapshot to get the history from | ||||||
matching_operations: Operations to match on | ||||||
manifest_content_filter: Manifest content type to filter | ||||||
|
||||||
Raises: | ||||||
ValidationException: If no matching snapshot is found or only one snapshot is found | ||||||
|
||||||
Returns: | ||||||
List of manifest files and set of snapshots ID's matching conditions | ||||||
""" | ||||||
manifests_files: list[ManifestFile] = [] | ||||||
snapshots: set[int] = set() | ||||||
|
||||||
last_snapshot = None | ||||||
for snapshot in ancestors_between(to_snapshot, from_snapshot, table.metadata): | ||||||
last_snapshot = snapshot | ||||||
summary = snapshot.summary | ||||||
if summary is None: | ||||||
raise ValidationException(f"No summary found for snapshot {snapshot}!") | ||||||
if summary.operation not in matching_operations: | ||||||
continue | ||||||
|
||||||
snapshots.add(snapshot.snapshot_id) | ||||||
# TODO: Maybe do the IO in a separate thread at some point, and collect at the bottom (we can easily merge the sets 🤤 | ||||||
manifests_files.extend( | ||||||
jayceslesar marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
[ | ||||||
manifest | ||||||
for manifest in snapshot.manifests(table.io) | ||||||
if manifest.added_snapshot_id == snapshot.snapshot_id and manifest.content == manifest_content_filter | ||||||
] | ||||||
) | ||||||
|
||||||
if last_snapshot is None or last_snapshot.snapshot_id == from_snapshot.snapshot_id: | ||||||
|
if last_snapshot is None or last_snapshot.snapshot_id == from_snapshot.snapshot_id: | |
if last_snapshot is not None and last_snapshot.snapshot_id != from_snapshot.snapshot_id: |
`if not (last_snapshot is not None and last_snapshot.snapshot_id != from_snapshot.snapshot_id):`
The ValidationCheck
in java throws an Exception with the provided error message if the boolean condition in doesn't hold. So we'd actually be throwing in the inverse of this condition.
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.
Made this change
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.
Awesome, changing this to be correct also caught an issue with ancestors_between
that is not fixed!
Outdated
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.
We can leave it like this for now, but it seems odd to throw here, since there is no history to validate, which in turn is valid.
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.
Possibly me just not translating this java correctly at all https://github.com/apache/iceberg/blob/main/core/src/main/java/org/apache/iceberg/MergingSnapshotProducer.java#L894
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.
This has been fixed!
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
# 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. | ||
# pylint:disable=redefined-outer-name,eval-used | ||
from typing import cast | ||
from unittest.mock import patch | ||
|
||
import pytest | ||
|
||
from pyiceberg.exceptions import ValidationException | ||
from pyiceberg.io import FileIO | ||
from pyiceberg.manifest import ManifestContent, ManifestFile | ||
from pyiceberg.table import Table | ||
from pyiceberg.table.snapshots import Operation, Snapshot | ||
from pyiceberg.table.update.validate import validation_history | ||
|
||
|
||
def test_validation_history(table_v2_with_extensive_snapshots: Table) -> None: | ||
|
||
"""Test the validation history function.""" | ||
mock_manifests = {} | ||
|
||
for i, snapshot in enumerate(table_v2_with_extensive_snapshots.snapshots()): | ||
mock_manifest = ManifestFile.from_args( | ||
manifest_path=f"foo/bar/{i}", | ||
manifest_length=1, | ||
partition_spec_id=1, | ||
content=ManifestContent.DATA if i % 2 == 0 else ManifestContent.DELETES, | ||
sequence_number=1, | ||
min_sequence_number=1, | ||
added_snapshot_id=snapshot.snapshot_id, | ||
) | ||
|
||
# Store the manifest for this specific snapshot | ||
mock_manifests[snapshot.snapshot_id] = [mock_manifest] | ||
|
||
expected_manifest_data_counts = len([m for m in mock_manifests.values() if m[0].content == ManifestContent.DATA]) - 1 | ||
|
||
oldest_snapshot = table_v2_with_extensive_snapshots.snapshots()[0] | ||
newest_snapshot = cast(Snapshot, table_v2_with_extensive_snapshots.current_snapshot()) | ||
|
||
def mock_read_manifest_side_effect(self: Snapshot, io: FileIO) -> list[ManifestFile]: | ||
"""Mock the manifests method to use the snapshot_id for lookup.""" | ||
snapshot_id = self.snapshot_id | ||
if snapshot_id in mock_manifests: | ||
return mock_manifests[snapshot_id] | ||
return [] | ||
|
||
with patch("pyiceberg.table.snapshots.Snapshot.manifests", new=mock_read_manifest_side_effect): | ||
manifests, snapshots = validation_history( | ||
table_v2_with_extensive_snapshots, | ||
newest_snapshot, | ||
oldest_snapshot, | ||
{Operation.APPEND}, | ||
ManifestContent.DATA, | ||
) | ||
|
||
assert len(manifests) == expected_manifest_data_counts | ||
|
||
snapshot_with_no_summary = Snapshot( | ||
snapshot_id="1234", | ||
parent_id="5678", | ||
timestamp_ms=0, | ||
operation=Operation.APPEND, | ||
summary=None, | ||
manifest_list="foo/bar", | ||
) | ||
with patch("pyiceberg.table.update.validate.ancestors_between", return_value=[snapshot_with_no_summary]): | ||
with pytest.raises(ValidationException): | ||
validation_history( | ||
table_v2_with_extensive_snapshots, | ||
newest_snapshot, | ||
oldest_snapshot, | ||
{Operation.APPEND}, | ||
ManifestContent.DATA, | ||
) | ||
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 think it would be best to create a separate test function that tests this failure case, like 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. Done |
Uh oh!
There was an error while loading. Please reload this page.