-
Notifications
You must be signed in to change notification settings - Fork 279
Move implementation of upsert from Table to Transaction #1817
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7abfee9
Move actual implementation of upsert from Table to Transaction
koenvo db334ae
Fix some incorrect usage of schema
koenvo cebfda3
Write a test for upsert transaction
koenvo 52fd35e
Add failing test for multiple upserts in same transaction
koenvo f336c0b
Fix test
koenvo 07890ac
Add failing test
koenvo ae0e60f
Use Transaction.table_metadata when doing the data scan in upsert
koenvo ce8d9ef
Remove as it's resolved
koenvo 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,7 +23,7 @@ | |
|
||
from pyiceberg.catalog import Catalog | ||
from pyiceberg.exceptions import NoSuchTableError | ||
from pyiceberg.expressions import And, EqualTo, Reference | ||
from pyiceberg.expressions import AlwaysTrue, And, EqualTo, Reference | ||
from pyiceberg.expressions.literals import LongLiteral | ||
from pyiceberg.io.pyarrow import schema_to_pyarrow | ||
from pyiceberg.schema import Schema | ||
|
@@ -709,3 +709,64 @@ def test_upsert_with_nulls(catalog: Catalog) -> None: | |
], | ||
schema=schema, | ||
) | ||
|
||
|
||
def test_transaction(catalog: Catalog) -> None: | ||
"""Test the upsert within a Transaction. Make sure that if something fails the entire Transaction is | ||
rolled back.""" | ||
identifier = "default.test_merge_source_dups" | ||
_drop_table(catalog, identifier) | ||
|
||
ctx = SessionContext() | ||
|
||
table = gen_target_iceberg_table(1, 10, False, ctx, catalog, identifier) | ||
df_before_transaction = table.scan().to_arrow() | ||
|
||
source_df = gen_source_dataset(5, 15, False, True, ctx) | ||
|
||
with pytest.raises(Exception, match="Duplicate rows found in source dataset based on the key columns. No upsert executed"): | ||
with table.transaction() as tx: | ||
tx.delete(delete_filter=AlwaysTrue()) | ||
tx.upsert(df=source_df, join_cols=["order_id"]) | ||
|
||
df = table.scan().to_arrow() | ||
|
||
assert df_before_transaction == df | ||
|
||
|
||
def test_transaction_multiple_upserts(catalog: Catalog) -> None: | ||
identifier = "default.test_multi_upsert" | ||
_drop_table(catalog, identifier) | ||
|
||
schema = Schema( | ||
NestedField(1, "id", IntegerType(), required=True), | ||
NestedField(2, "name", StringType(), required=True), | ||
identifier_field_ids=[1], | ||
) | ||
|
||
tbl = catalog.create_table(identifier, schema=schema) | ||
|
||
# Define exact schema: required int32 and required string | ||
arrow_schema = pa.schema( | ||
[ | ||
pa.field("id", pa.int32(), nullable=False), | ||
pa.field("name", pa.string(), nullable=False), | ||
] | ||
) | ||
|
||
tbl.append(pa.Table.from_pylist([{"id": 1, "name": "Alice"}], schema=arrow_schema)) | ||
|
||
df = pa.Table.from_pylist([{"id": 2, "name": "Bob"}, {"id": 1, "name": "Alicia"}], schema=arrow_schema) | ||
|
||
with tbl.transaction() as txn: | ||
txn.delete(delete_filter="id = 1") | ||
txn.append(df) | ||
|
||
# This should read the uncommitted changes | ||
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. Test uncommitted changes are read |
||
txn.upsert(df, join_cols=["id"]) | ||
|
||
result = tbl.scan().to_arrow().to_pylist() | ||
assert sorted(result, key=lambda x: x["id"]) == [ | ||
{"id": 1, "name": "Alicia"}, | ||
{"id": 2, "name": "Bob"}, | ||
] |
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.
Most important change. Required #1903
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.
Nice, thanks for pointing out, and also covering this in the tests 👍