-
Notifications
You must be signed in to change notification settings - Fork 308
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 3 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 |
---|---|---|
|
@@ -25,7 +25,7 @@ | |
from pydantic import Field, PrivateAttr, model_serializer | ||
|
||
from pyiceberg.io import FileIO | ||
from pyiceberg.manifest import DataFile, DataFileContent, ManifestFile, _manifests | ||
from pyiceberg.manifest import DataFile, DataFileContent, ManifestContent, ManifestFile, _manifests | ||
from pyiceberg.partitioning import UNPARTITIONED_PARTITION_SPEC, PartitionSpec | ||
from pyiceberg.schema import Schema | ||
|
||
|
@@ -255,6 +255,14 @@ def manifests(self, io: FileIO) -> List[ManifestFile]: | |
"""Return the manifests for the given snapshot.""" | ||
return list(_manifests(io, self.manifest_list)) | ||
|
||
def data_manifests(self, io: FileIO) -> List[ManifestFile]: | ||
"""Return the data manifests for the given snapshot.""" | ||
return [manifest for manifest in self.manifests(io) if manifest.content == ManifestContent.DATA] | ||
|
||
def delete_manifests(self, io: FileIO) -> List[ManifestFile]: | ||
"""Return the delete manifests for the given snapshot.""" | ||
return [manifest for manifest in self.manifests(io) if manifest.content == ManifestContent.DELETES] | ||
|
||
|
||
class MetadataLogEntry(IcebergBaseModel): | ||
metadata_file: str = Field(alias="metadata-file") | ||
|
@@ -429,3 +437,13 @@ def ancestors_of(current_snapshot: Optional[Snapshot], table_metadata: TableMeta | |
if snapshot.parent_snapshot_id is None: | ||
break | ||
snapshot = table_metadata.snapshot_by_id(snapshot.parent_snapshot_id) | ||
|
||
|
||
def ancestors_between( | ||
current_snapshot: Optional[Snapshot], oldest_snapshot: Snapshot, table_metadata: TableMetadata | ||
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 doesn't looks correct: the I think the implementation that's in progress on this PR is closer to what we'd want: https://github.com/apache/iceberg-python/pull/533/files 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 should now be resolved, thank you for the link to that |
||
) -> Iterable[Snapshot]: | ||
"""Get the ancestors of and including the given snapshot between the latest and oldest snapshot.""" | ||
for snapshot in ancestors_of(current_snapshot, table_metadata): | ||
if snapshot.snapshot_id == oldest_snapshot.snapshot_id: | ||
break | ||
yield snapshot |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# 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.manifest import ManifestContent, ManifestFile | ||
from pyiceberg.table import Table | ||
from pyiceberg.table.snapshots import Operation, Snapshot, ancestors_between | ||
|
||
|
||
class ValidationException(Exception): | ||
Fokko marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"""Raised when validation fails.""" | ||
|
||
|
||
def validation_history( | ||
table: Table, | ||
starting_snapshot: Snapshot, | ||
Fokko marked this conversation as resolved.
Show resolved
Hide resolved
|
||
matching_operations: set[Operation], | ||
manifest_content: ManifestContent, | ||
parent_snapshot: Snapshot, | ||
) -> 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 | ||
starting_snapshot: Starting snapshot | ||
matching_operations: Operations to match on | ||
manifest_content: Manifest content type to filter | ||
parent_snapshot: Parent snapshot to get the history from | ||
|
||
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(starting_snapshot, parent_snapshot, table.metadata): | ||
last_snapshot = snapshot | ||
summary = snapshot.summary | ||
if summary is None: | ||
continue | ||
if summary.operation in matching_operations: | ||
snapshots.add(snapshot) | ||
if manifest_content == ManifestContent.DATA: | ||
manifests_files.extend( | ||
[ | ||
manifest | ||
for manifest in snapshot.data_manifests(table.io) | ||
if manifest.added_snapshot_id == snapshot.snapshot_id | ||
] | ||
) | ||
else: | ||
manifests_files.extend( | ||
[ | ||
manifest | ||
for manifest in snapshot.delete_manifests(table.io) | ||
if manifest.added_snapshot_id == snapshot.snapshot_id | ||
] | ||
) | ||
|
||
if last_snapshot is None or last_snapshot.snapshot_id == starting_snapshot.snapshot_id: | ||
raise ValidationException("No matching snapshot found.") | ||
|
||
return manifests_files, snapshots |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# 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 pyiceberg.manifest import ManifestContent | ||
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.""" | ||
oldest_snapshot = table_v2_with_extensive_snapshots.snapshots()[0] | ||
newest_snapshot = cast(Snapshot, table_v2_with_extensive_snapshots.current_snapshot()) | ||
manifests, snapshots = validation_history( | ||
table_v2_with_extensive_snapshots, newest_snapshot, {Operation.APPEND}, ManifestContent.DATA, oldest_snapshot | ||
) | ||
assert len(snapshots) == 2 |
Uh oh!
There was an error while loading. Please reload this page.