Skip to content

Commit 7466adf

Browse files
Add tests for optimistic concurrency (#1962)
I think it would be great to also have integration tests that make it easy to replicate certain scenarios. Added some simple ones, but we can extend by having two tables that modify a different partition etc. <!-- Thanks for opening a pull request! --> <!-- In the case this PR will resolve an issue, please replace ${GITHUB_ISSUE_ID} below with the actual Github issue id. --> <!-- Closes #${GITHUB_ISSUE_ID} --> # Rationale for this change # Are these changes tested? # Are there any user-facing changes? <!-- In the case of user-facing changes, please add the changelog label. --> --------- Co-authored-by: smaheshwar-pltr <[email protected]>
1 parent 6456a8d commit 7466adf

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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+
"""This test should start passing once optimistic concurrency control has been implemented."""
33+
identifier = "default.test_conflict"
34+
tbl1 = _create_table(session_catalog, identifier, {"format-version": format_version}, [arrow_table_with_null])
35+
tbl2 = session_catalog.load_table(identifier)
36+
37+
tbl1.delete("string == 'z'")
38+
39+
with pytest.raises(CommitFailedException, match="(branch main has changed: expected id ).*"):
40+
# tbl2 isn't aware of the commit by tbl1
41+
tbl2.delete("string == 'z'")
42+
43+
44+
@pytest.mark.integration
45+
@pytest.mark.parametrize("format_version", [1, 2])
46+
def test_conflict_delete_append(
47+
spark: SparkSession, session_catalog: Catalog, arrow_table_with_null: pa.Table, format_version: int
48+
) -> None:
49+
"""This test should start passing once optimistic concurrency control has been implemented."""
50+
identifier = "default.test_conflict"
51+
tbl1 = _create_table(session_catalog, identifier, {"format-version": format_version}, [arrow_table_with_null])
52+
tbl2 = session_catalog.load_table(identifier)
53+
54+
# This is allowed
55+
tbl1.delete("string == 'z'")
56+
57+
with pytest.raises(CommitFailedException, match="(branch main has changed: expected id ).*"):
58+
# tbl2 isn't aware of the commit by tbl1
59+
tbl2.append(arrow_table_with_null)
60+
61+
62+
@pytest.mark.integration
63+
@pytest.mark.parametrize("format_version", [1, 2])
64+
def test_conflict_append_delete(
65+
spark: SparkSession, session_catalog: Catalog, arrow_table_with_null: pa.Table, format_version: int
66+
) -> None:
67+
"""This test should start passing once optimistic concurrency control has been implemented."""
68+
identifier = "default.test_conflict"
69+
tbl1 = _create_table(session_catalog, identifier, {"format-version": format_version}, [arrow_table_with_null])
70+
tbl2 = session_catalog.load_table(identifier)
71+
72+
tbl1.append(arrow_table_with_null)
73+
74+
with pytest.raises(CommitFailedException, match="(branch main has changed: expected id ).*"):
75+
# tbl2 isn't aware of the commit by tbl1
76+
tbl2.delete("string == 'z'")
77+
78+
79+
@pytest.mark.integration
80+
@pytest.mark.parametrize("format_version", [1, 2])
81+
def test_conflict_append_append(
82+
spark: SparkSession, session_catalog: Catalog, arrow_table_with_null: pa.Table, format_version: int
83+
) -> None:
84+
"""This test should start passing once optimistic concurrency control has been implemented."""
85+
identifier = "default.test_conflict"
86+
tbl1 = _create_table(session_catalog, identifier, {"format-version": format_version}, [arrow_table_with_null])
87+
tbl2 = session_catalog.load_table(identifier)
88+
89+
tbl1.append(arrow_table_with_null)
90+
91+
with pytest.raises(CommitFailedException, match="(branch main has changed: expected id ).*"):
92+
# tbl2 isn't aware of the commit by tbl1
93+
tbl2.append(arrow_table_with_null)

0 commit comments

Comments
 (0)