-
Notifications
You must be signed in to change notification settings - Fork 458
feat: Add metadata-only replace API to Table for REPLACE snapshot operations #3131
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
qzyu999
wants to merge
5
commits into
apache:main
Choose a base branch
from
qzyu999:feature/core-rewrite-api
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
5 commits
Select commit
Hold shift + click to select a range
19e8dee
feat: Add metadata-only replace API to Table for REPLACE snapshot ope…
qzyu999 6800bb4
chore: fix linter and mypy errors in replace API and tests
qzyu999 e5e11b9
test: fix invalid operation assertion and add license headers
qzyu999 070c885
chore: Remove comment for linter
qzyu999 f12fa5d
test: fix invalid operation assertion by using model_construct to byp…
qzyu999 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
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
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
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 |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| # 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.catalog import Catalog | ||
| from pyiceberg.manifest import DataFile, DataFileContent, FileFormat | ||
| from pyiceberg.schema import Schema | ||
| from pyiceberg.table.snapshots import Operation | ||
| from pyiceberg.typedef import Record | ||
|
|
||
|
|
||
| def test_replace_api(catalog: Catalog) -> None: | ||
| # Setup a basic table using the catalog fixture | ||
| catalog.create_namespace("default") | ||
| table = catalog.create_table( | ||
| identifier="default.test_replace", | ||
| schema=Schema(), | ||
| ) | ||
|
|
||
| # Create mock DataFiles for the test | ||
| file_to_delete = DataFile.from_args( | ||
| file_path="s3://bucket/test/data/deleted.parquet", | ||
| file_format=FileFormat.PARQUET, | ||
| partition=Record(), | ||
| record_count=100, | ||
| file_size_in_bytes=1024, | ||
| content=DataFileContent.DATA, | ||
| ) | ||
| file_to_delete.spec_id = 0 | ||
|
|
||
| file_to_add = DataFile.from_args( | ||
| file_path="s3://bucket/test/data/added.parquet", | ||
| file_format=FileFormat.PARQUET, | ||
| partition=Record(), | ||
| record_count=100, | ||
| file_size_in_bytes=1024, | ||
| content=DataFileContent.DATA, | ||
| ) | ||
| file_to_add.spec_id = 0 | ||
|
|
||
| # Initially append to have something to replace | ||
| with table.transaction() as tx: | ||
| with tx.update_snapshot().fast_append() as append_snapshot: | ||
| append_snapshot.append_data_file(file_to_delete) | ||
|
|
||
| # Verify initial append snapshot | ||
| assert len(table.history()) == 1 | ||
| snapshot = table.current_snapshot() | ||
| assert snapshot is not None | ||
| assert snapshot.summary is not None | ||
| assert snapshot.summary["operation"] == Operation.APPEND | ||
|
|
||
| # Call the replace API | ||
| table.replace(files_to_delete=[file_to_delete], files_to_add=[file_to_add]) | ||
|
|
||
| # Verify the replacement created a REPLACE snapshot | ||
| assert len(table.history()) == 2 | ||
| snapshot = table.current_snapshot() | ||
| assert snapshot is not None | ||
| assert snapshot.summary is not None | ||
| assert snapshot.summary["operation"] == Operation.REPLACE | ||
|
|
||
| # Verify the correct files are added and deleted | ||
| # The summary property tracks these counts | ||
| assert snapshot.summary["added-data-files"] == "1" | ||
| assert snapshot.summary["deleted-data-files"] == "1" | ||
| assert snapshot.summary["added-records"] == "100" | ||
| assert snapshot.summary["deleted-records"] == "100" | ||
|
|
||
| # Verify the new file exists in the new manifest | ||
| manifest_files = snapshot.manifests(table.io) | ||
| assert len(manifest_files) == 2 # One for ADDED, one for DELETED | ||
|
|
||
| # Check that sequence numbers were handled properly natively by verifying the manifest contents | ||
| entries = [] | ||
| for manifest in manifest_files: | ||
| for entry in manifest.fetch_manifest_entry(table.io, discard_deleted=False): | ||
| entries.append(entry) | ||
|
|
||
| # One entry for ADDED (new file), one for DELETED (old file) | ||
| assert len(entries) == 2 | ||
|
Comment on lines
+84
to
+93
Contributor
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 need to test a bit more on the status of each file. https://github.com/apache/iceberg/blob/main/core/src/test/java/org/apache/iceberg/TestRewriteFiles.java is a good reference |
||
|
|
||
|
|
||
| def test_replace_empty_files(catalog: Catalog) -> None: | ||
| # Setup a basic table using the catalog fixture | ||
| catalog.create_namespace("default") | ||
| table = catalog.create_table( | ||
| identifier="default.test_replace_empty", | ||
| schema=Schema(), | ||
| ) | ||
|
|
||
| # Replacing empty lists should not throw errors, but should produce no changes. | ||
| table.replace([], []) | ||
|
|
||
| # History should be completely empty since no files were rewritten | ||
| assert len(table.history()) == 0 | ||
| assert table.current_snapshot() is None | ||
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
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.
we should not expose
replaceas a public function as we cannot guarantee that thefiles_to_deleteandfiles_to_addcontains the same records.I think we should start at
_RewriteFiles