Skip to content

Commit 34ca959

Browse files
author
Yingjian Wu
committed
Address comments
1 parent 4eae64a commit 34ca959

File tree

2 files changed

+11
-51
lines changed

2 files changed

+11
-51
lines changed

pyiceberg/table/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ def __exit__(
238238
) -> None:
239239
"""Close and commit the transaction, or handle exceptions."""
240240
# Only commit the full transaction, if there is no exception in all updates on the chain
241-
if exctb is None:
241+
if exctype is None and excinst is None and exctb is None:
242242
self.commit_transaction()
243243

244244
def _apply(self, updates: Tuple[TableUpdate, ...], requirements: Tuple[TableRequirement, ...] = ()) -> Transaction:

tests/catalog/test_base.py

Lines changed: 10 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,14 @@
4141
NoSuchTableError,
4242
TableAlreadyExistsError,
4343
)
44-
from pyiceberg.expressions import BooleanExpression
45-
from pyiceberg.io import WAREHOUSE, FileIO, load_file_io
44+
from pyiceberg.io import WAREHOUSE, load_file_io
4645
from pyiceberg.partitioning import UNPARTITIONED_PARTITION_SPEC, PartitionField, PartitionSpec
4746
from pyiceberg.schema import Schema
4847
from pyiceberg.table import (
49-
ALWAYS_TRUE,
5048
CommitTableResponse,
5149
Table,
52-
Transaction,
5350
)
54-
from pyiceberg.table.metadata import TableMetadata, new_table_metadata
51+
from pyiceberg.table.metadata import new_table_metadata
5552
from pyiceberg.table.sorting import UNSORTED_SORT_ORDER, SortOrder
5653
from pyiceberg.table.update import (
5754
AddSchemaUpdate,
@@ -268,42 +265,6 @@ def drop_view(self, identifier: Union[str, Identifier]) -> None:
268265
raise NotImplementedError
269266

270267

271-
class TransactionThrowExceptionInOverwrite(Transaction):
272-
def __init__(self, table: Table):
273-
super().__init__(table)
274-
275-
# Override the default overwrite to simulate exception during append
276-
def overwrite(
277-
self,
278-
df: pa.Table,
279-
overwrite_filter: Union[BooleanExpression, str] = ALWAYS_TRUE,
280-
snapshot_properties: Dict[str, str] = EMPTY_DICT,
281-
) -> None:
282-
self.delete(delete_filter=overwrite_filter, snapshot_properties=snapshot_properties)
283-
raise Exception("Fail Append Commit Exception")
284-
285-
286-
class TableThrowExceptionInOverwrite(Table):
287-
def __init__(self, identifier: Identifier, metadata: TableMetadata, metadata_location: str, io: FileIO, catalog: Catalog):
288-
# Call the constructor of the parent class
289-
super().__init__(identifier, metadata, metadata_location, io, catalog)
290-
291-
def transaction(self) -> Transaction:
292-
return TransactionThrowExceptionInOverwrite(self)
293-
294-
295-
def given_catalog_has_a_table_throw_exception_in_overwrite(
296-
catalog: InMemoryCatalog, properties: Properties = EMPTY_DICT
297-
) -> TableThrowExceptionInOverwrite:
298-
table = catalog.create_table(
299-
identifier=TEST_TABLE_IDENTIFIER,
300-
schema=TEST_TABLE_SCHEMA,
301-
partition_spec=TEST_TABLE_PARTITION_SPEC,
302-
properties=properties or TEST_TABLE_PROPERTIES,
303-
)
304-
return TableThrowExceptionInOverwrite(table.identifier, table.metadata, table.metadata_location, table.io, table.catalog)
305-
306-
307268
@pytest.fixture
308269
def catalog(tmp_path: PosixPath) -> InMemoryCatalog:
309270
return InMemoryCatalog("test.in_memory.catalog", **{WAREHOUSE: tmp_path.absolute().as_posix(), "test.key": "test.value"})
@@ -808,24 +769,23 @@ def test_table_properties_raise_for_none_value(catalog: InMemoryCatalog) -> None
808769

809770

810771
def test_table_overwrite_with_exception(catalog: InMemoryCatalog) -> None:
811-
given_table = given_catalog_has_a_table_throw_exception_in_overwrite(catalog)
772+
tbl = given_catalog_has_a_table(catalog)
812773
# Populate some initial data
813774
data = pa.Table.from_pylist(
814775
[{"x": 1, "y": 2, "z": 3}, {"x": 4, "y": 5, "z": 6}],
815776
schema=TEST_TABLE_SCHEMA.as_arrow(),
816777
)
817-
given_table.append(data)
778+
tbl.append(data)
818779

819780
# Data to overwrite
820781
data = pa.Table.from_pylist(
821-
[{"x": 7, "y": 8, "z": 9}],
782+
[{"x": 7, "y": 8, "z": 9}, {"x": 7, "y": 8, "z": 9}, {"x": 7, "y": 8, "z": 9}],
822783
schema=TEST_TABLE_SCHEMA.as_arrow(),
823784
)
824785

825-
# Since overwrite has an exception, we should fail the whole overwrite transaction
826-
try:
827-
given_table.overwrite(data)
828-
except Exception as e:
829-
assert str(e) == "Fail Append Commit Exception", f"Expected 'Fail Append Commit Exception', but got '{str(e)}'"
786+
with pytest.raises(ValueError):
787+
with tbl.transaction() as txn:
788+
txn.overwrite(data)
789+
raise ValueError
830790

831-
assert len(given_table.scan().to_arrow()) == 2
791+
assert len(tbl.scan().to_pandas()) == 2 # type: ignore

0 commit comments

Comments
 (0)