-
Notifications
You must be signed in to change notification settings - Fork 166
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
[Bug Fix] cast None current-snapshot-id
as -1 for Backwards Compatibility
#473
Merged
Merged
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
710f913
current-snapshot-id as -1
sungwy d401045
fix
sungwy cd38c5a
legacy-current-snapshot-id
sungwy 3a4ada5
lint
sungwy 7b810c7
Update mkdocs/docs/configuration.md
sungwy fecbd4d
adopt review feedback
sungwy cc9ead5
Merge branch 'current-snapshot-bf' of https://github.com/syun64/icebe…
sungwy a929d20
Merge branch 'main' into current-snapshot-bf
sungwy a102467
lint
sungwy f4a302b
Update pyiceberg/table/metadata.py
sungwy 130b3cc
adopt review comment
sungwy 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 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 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 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 |
---|---|---|
|
@@ -28,7 +28,7 @@ | |
Union, | ||
) | ||
|
||
from pydantic import Field, field_validator, model_validator | ||
from pydantic import Field, field_serializer, field_validator, model_validator | ||
from pydantic import ValidationError as PydanticValidationError | ||
from typing_extensions import Annotated | ||
|
||
|
@@ -50,6 +50,7 @@ | |
Properties, | ||
) | ||
from pyiceberg.types import transform_dict_value_to_str | ||
from pyiceberg.utils.config import Config | ||
from pyiceberg.utils.datetime import datetime_to_millis | ||
|
||
CURRENT_SNAPSHOT_ID = "current-snapshot-id" | ||
|
@@ -263,6 +264,12 @@ def sort_order_by_id(self, sort_order_id: int) -> Optional[SortOrder]: | |
"""Get the sort order by sort_order_id.""" | ||
return next((sort_order for sort_order in self.sort_orders if sort_order.order_id == sort_order_id), None) | ||
|
||
@field_serializer('current_snapshot_id') | ||
def serialize_current_snapshot_id(self, current_snapshot_id: Optional[int]) -> Optional[int]: | ||
if current_snapshot_id is None and Config().get_bool("legacy-current-snapshot-id"): | ||
return -1 | ||
return current_snapshot_id | ||
Comment on lines
+267
to
+271
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. Beautiful! |
||
|
||
|
||
def _generate_snapshot_id() -> int: | ||
"""Generate a new Snapshot ID from a UUID. | ||
|
This file contains 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 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 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 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,50 @@ | ||
# 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. | ||
|
||
import json | ||
import os | ||
import uuid | ||
from typing import Any, Dict | ||
|
||
import pytest | ||
from pytest_mock import MockFixture | ||
|
||
from pyiceberg.serializers import ToOutputFile | ||
from pyiceberg.table import StaticTable | ||
from pyiceberg.table.metadata import TableMetadataV1 | ||
|
||
|
||
def test_legacy_current_snapshot_id( | ||
mocker: MockFixture, tmp_path_factory: pytest.TempPathFactory, example_table_metadata_no_snapshot_v1: Dict[str, Any] | ||
) -> None: | ||
from pyiceberg.io.pyarrow import PyArrowFileIO | ||
|
||
metadata_location = str(tmp_path_factory.mktemp("metadata") / f"{uuid.uuid4()}.metadata.json") | ||
metadata = TableMetadataV1(**example_table_metadata_no_snapshot_v1) | ||
ToOutputFile.table_metadata(metadata, PyArrowFileIO().new_output(location=metadata_location), overwrite=True) | ||
static_table = StaticTable.from_metadata(metadata_location) | ||
assert static_table.metadata.current_snapshot_id is None | ||
|
||
mocker.patch.dict(os.environ, values={"PYICEBERG_LEGACY_CURRENT_SNAPSHOT_ID": "True"}) | ||
|
||
ToOutputFile.table_metadata(metadata, PyArrowFileIO().new_output(location=metadata_location), overwrite=True) | ||
with PyArrowFileIO().new_input(location=metadata_location).open() as input_stream: | ||
metadata_json_bytes = input_stream.read() | ||
assert json.loads(metadata_json_bytes)['current-snapshot-id'] == -1 | ||
backwards_compatible_static_table = StaticTable.from_metadata(metadata_location) | ||
assert backwards_compatible_static_table.metadata.current_snapshot_id is None | ||
assert backwards_compatible_static_table.metadata == static_table.metadata |
This file contains 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
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.
Maybe not a big deal: Shall we set
exclude_none=False
only in the legacy mode?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.
I would prefer that. Let me put in this update