-
Notifications
You must be signed in to change notification settings - Fork 309
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 13 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,72 @@ | ||||||
# 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 typing import Optional | ||||||
|
||||||
from pyiceberg.manifest import ManifestContent, ManifestFile | ||||||
from pyiceberg.table import Table | ||||||
from pyiceberg.table.snapshots import Operation, Snapshot, ancestors_between | ||||||
|
||||||
|
||||||
class ValidationException(Exception): | ||||||
"""Raised when validation fails.""" | ||||||
|
||||||
|
||||||
def validation_history( | ||||||
table: Table, | ||||||
from_snapshot: Snapshot, | ||||||
to_snapshot: Optional[Snapshot], | ||||||
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. What do you think of making the API a bit more strict and simplify code along the way. I don't think this needs to be
Suggested change
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 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. @Fokko I think the existing behavior of the Spark/Java API is for to check all snapshots if the Should we mirror that behavior in PyIceberg, or just require that the 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. My initial thought was that we could always widen the API and make it optional. Looking at it more closely, only the
|
||||||
matching_operations: set[Operation], | ||||||
manifest_content_filter: ManifestContent, | ||||||
) -> tuple[list[ManifestFile], set[Snapshot]]: | ||||||
"""Return newly added manifests and snapshot IDs between the starting snapshot and parent snapshot. | ||||||
|
||||||
Args: | ||||||
table: Table to get the history from | ||||||
from_snapshot: Parent snapshot to get the history from | ||||||
to_snapshot: Starting snapshot | ||||||
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 matching conditions | ||||||
""" | ||||||
manifests_files: list[ManifestFile] = [] | ||||||
snapshots: set[Snapshot] = set() | ||||||
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. Java just returns 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. great, I am happy to remove this being a set, I was just attempting to copy what made sense from java 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. This now returns a unique list, happy to modify that to allow duplicates if wanted 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. Yes, or return 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.
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. nah, will just return the 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 like that as well, but let's do that in a separate PR 👍 Now we're back at a set, we can remove the if snapshot not in snapshots:
snapshots.add(snapshot.snapshot_id) |
||||||
|
||||||
last_snapshot = None | ||||||
for snapshot in ancestors_between(from_snapshot, to_snapshot, table.metadata): | ||||||
last_snapshot = snapshot | ||||||
summary = snapshot.summary | ||||||
if summary is None or summary.operation not in matching_operations: | ||||||
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. This looks dangerous to me, I don't think we want to skip over this if the summary is 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 the right thing here is to assume that it is an 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. What is a case where summary would ever be 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. It now defaults to 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.
Instead of setting it as 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. 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. Sounds good to me, implemented and added to the existing test to assure that we raise an error when that happens. Planning on cleaning up tests in the next MR when I will likely need to make some fixtures & parameterize |
||||||
continue | ||||||
|
||||||
snapshots.add(snapshot) | ||||||
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: | ||||||
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
The 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. Made this change 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. Awesome, changing this to be correct also caught an issue with |
||||||
raise ValidationException("No matching snapshot found.") | ||||||
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. 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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. This has been fixed! |
||||||
|
||||||
return manifests_files, snapshots |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# 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 | ||
|
||
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: | ||
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. Could we add another test which tests the expected failure when 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. Added! |
||
"""Test the validation history function.""" | ||
mock_manifests = {} | ||
|
||
for i, snapshot in enumerate(table_v2_with_extensive_snapshots.snapshots()): | ||
mock_manifest = ManifestFile( | ||
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 |
Uh oh!
There was an error while loading. Please reload this page.