Skip to content

Commit 583a7aa

Browse files
committed
Add tests for optimistic concurrency
I think it would be great to also have integration tests that makes it easy to replicate certain scenarios. Added some simple ones, but we can extend by having two tables that modify a different partition etc.
1 parent eb8756a commit 583a7aa

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
import pyarrow as pa
19+
import pytest
20+
from pyspark.sql import SparkSession
21+
22+
from pyiceberg.catalog import Catalog
23+
from pyiceberg.exceptions import CommitFailedException
24+
from utils import _create_table
25+
26+
27+
@pytest.mark.integration
28+
@pytest.mark.parametrize("format_version", [1, 2])
29+
def test_conflict_delete_delete(
30+
spark: SparkSession, session_catalog: Catalog, arrow_table_with_null: pa.Table, format_version: int
31+
) -> None:
32+
identifier = "default.test_conflict"
33+
tbl1 = _create_table(session_catalog, identifier, {"format-version": "1"}, [arrow_table_with_null])
34+
tbl2 = session_catalog.load_table(identifier)
35+
36+
tbl1.delete("string == 'z'")
37+
38+
with pytest.raises(CommitFailedException, match="(branch main has changed: expected id ).*"):
39+
# tbl2 isn't aware of the commit by tbl1
40+
tbl2.delete("string == 'z'")
41+
42+
43+
@pytest.mark.integration
44+
@pytest.mark.parametrize("format_version", [1, 2])
45+
def test_conflict_delete_append(
46+
spark: SparkSession, session_catalog: Catalog, arrow_table_with_null: pa.Table, format_version: int
47+
) -> None:
48+
identifier = "default.test_conflict"
49+
tbl1 = _create_table(session_catalog, identifier, {"format-version": "1"}, [arrow_table_with_null])
50+
tbl2 = session_catalog.load_table(identifier)
51+
52+
# This is allowed
53+
tbl1.delete("string == 'z'")
54+
55+
with pytest.raises(CommitFailedException, match="(branch main has changed: expected id ).*"):
56+
# tbl2 isn't aware of the commit by tbl1
57+
tbl2.append(arrow_table_with_null)
58+
59+
60+
@pytest.mark.integration
61+
@pytest.mark.parametrize("format_version", [1, 2])
62+
def test_conflict_append_delete(
63+
spark: SparkSession, session_catalog: Catalog, arrow_table_with_null: pa.Table, format_version: int
64+
) -> None:
65+
identifier = "default.test_conflict"
66+
tbl1 = _create_table(session_catalog, identifier, {"format-version": "1"}, [arrow_table_with_null])
67+
tbl2 = session_catalog.load_table(identifier)
68+
69+
tbl1.delete("string == 'z'")
70+
71+
with pytest.raises(CommitFailedException, match="(branch main has changed: expected id ).*"):
72+
# tbl2 isn't aware of the commit by tbl1
73+
tbl2.delete("string == 'z'")
74+
75+
76+
@pytest.mark.integration
77+
@pytest.mark.parametrize("format_version", [1, 2])
78+
def test_conflict_append_append(
79+
spark: SparkSession, session_catalog: Catalog, arrow_table_with_null: pa.Table, format_version: int
80+
) -> None:
81+
identifier = "default.test_conflict"
82+
tbl1 = _create_table(session_catalog, identifier, {"format-version": "1"}, [arrow_table_with_null])
83+
tbl2 = session_catalog.load_table(identifier)
84+
85+
tbl1.append(arrow_table_with_null)
86+
87+
with pytest.raises(CommitFailedException, match="(branch main has changed: expected id ).*"):
88+
# tbl2 isn't aware of the commit by tbl1
89+
tbl2.append(arrow_table_with_null)

0 commit comments

Comments
 (0)