Skip to content

Commit 245f5ac

Browse files
committed
[test] Rename to SimpleTableTestBase and refactor overwrite tests
1 parent a4a8db1 commit 245f5ac

File tree

5 files changed

+275
-435
lines changed

5 files changed

+275
-435
lines changed

paimon-core/src/test/java/org/apache/paimon/table/AppendOnlyFileStoreTableTest.java renamed to paimon-core/src/test/java/org/apache/paimon/table/AppendOnlySimpleTableTest.java

Lines changed: 3 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@
9494
import static org.assertj.core.api.Assertions.assertThatThrownBy;
9595

9696
/** Tests for {@link AppendOnlyFileStoreTable}. */
97-
public class AppendOnlyFileStoreTableTest extends FileStoreTableTestBase {
97+
public class AppendOnlySimpleTableTest extends SimpleTableTestBase {
9898

9999
@Test
100100
public void testMultipleWriters() throws Exception {
@@ -218,7 +218,7 @@ public void testBranchBatchReadWrite() throws Exception {
218218
FileStoreTable table = createFileStoreTable();
219219
generateBranch(table);
220220

221-
FileStoreTable tableBranch = createFileStoreTable(BRANCH_NAME);
221+
FileStoreTable tableBranch = createBranchTable(BRANCH_NAME);
222222
writeBranchData(tableBranch);
223223
List<Split> splits = toSplits(tableBranch.newSnapshotReader().read().dataSplits());
224224
TableRead read = tableBranch.newRead();
@@ -408,7 +408,7 @@ public void testBranchStreamingReadWrite() throws Exception {
408408
FileStoreTable table = createFileStoreTable();
409409
generateBranch(table);
410410

411-
FileStoreTable tableBranch = createFileStoreTable(BRANCH_NAME);
411+
FileStoreTable tableBranch = createBranchTable(BRANCH_NAME);
412412
writeBranchData(tableBranch);
413413

414414
List<Split> splits =
@@ -1198,44 +1198,6 @@ protected FileStoreTable createFileStoreTable(Consumer<Options> configure, RowTy
11981198
return new AppendOnlyFileStoreTable(FileIOFinder.find(tablePath), tablePath, tableSchema);
11991199
}
12001200

1201-
@Override
1202-
protected FileStoreTable createFileStoreTable(String branch, Consumer<Options> configure)
1203-
throws Exception {
1204-
Options conf = new Options();
1205-
conf.set(CoreOptions.PATH, tablePath.toString());
1206-
conf.set(CoreOptions.BRANCH, branch);
1207-
configure.accept(conf);
1208-
if (!conf.contains(BUCKET_KEY) && conf.get(BUCKET) != -1) {
1209-
conf.set(BUCKET_KEY, "a");
1210-
}
1211-
TableSchema tableSchema =
1212-
SchemaUtils.forceCommit(
1213-
new SchemaManager(LocalFileIO.create(), tablePath, branch),
1214-
new Schema(
1215-
ROW_TYPE.getFields(),
1216-
Collections.singletonList("pt"),
1217-
Collections.emptyList(),
1218-
conf.toMap(),
1219-
""));
1220-
return new AppendOnlyFileStoreTable(FileIOFinder.find(tablePath), tablePath, tableSchema);
1221-
}
1222-
1223-
@Override
1224-
protected FileStoreTable overwriteTestFileStoreTable() throws Exception {
1225-
Options conf = new Options();
1226-
conf.set(CoreOptions.PATH, tablePath.toString());
1227-
TableSchema tableSchema =
1228-
SchemaUtils.forceCommit(
1229-
new SchemaManager(LocalFileIO.create(), tablePath),
1230-
new Schema(
1231-
OVERWRITE_TEST_ROW_TYPE.getFields(),
1232-
Arrays.asList("pt0", "pt1"),
1233-
Collections.emptyList(),
1234-
conf.toMap(),
1235-
""));
1236-
return new AppendOnlyFileStoreTable(FileIOFinder.find(tablePath), tablePath, tableSchema);
1237-
}
1238-
12391201
protected FileStoreTable createUnawareBucketFileStoreTable(Consumer<Options> configure)
12401202
throws Exception {
12411203
Options conf = new Options();
Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
package org.apache.paimon.table;
20+
21+
import org.apache.paimon.CoreOptions;
22+
import org.apache.paimon.catalog.Identifier;
23+
import org.apache.paimon.data.BinaryString;
24+
import org.apache.paimon.data.DataFormatTestUtil;
25+
import org.apache.paimon.data.GenericRow;
26+
import org.apache.paimon.data.InternalRow;
27+
import org.apache.paimon.schema.Schema;
28+
import org.apache.paimon.table.sink.InnerTableCommit;
29+
import org.apache.paimon.table.sink.StreamTableWrite;
30+
import org.apache.paimon.table.source.Split;
31+
import org.apache.paimon.table.source.TableRead;
32+
import org.apache.paimon.types.DataTypes;
33+
34+
import org.junit.jupiter.api.BeforeEach;
35+
import org.junit.jupiter.params.ParameterizedTest;
36+
import org.junit.jupiter.params.provider.Arguments;
37+
import org.junit.jupiter.params.provider.MethodSource;
38+
39+
import java.util.ArrayList;
40+
import java.util.Arrays;
41+
import java.util.Collections;
42+
import java.util.List;
43+
import java.util.Map;
44+
45+
import static org.apache.paimon.table.SimpleTableTestBase.getResult;
46+
import static org.assertj.core.api.Assertions.assertThat;
47+
import static org.junit.jupiter.params.provider.Arguments.arguments;
48+
49+
/** Unit tests for overwrite table. */
50+
public class OverwriteTableTest extends TableTestBase {
51+
52+
private Table bucketsTable;
53+
54+
@BeforeEach
55+
public void before() throws Exception {
56+
Identifier identifier = identifier("T");
57+
Schema schema =
58+
Schema.newBuilder()
59+
.column("pk", DataTypes.INT())
60+
.column("pt0", DataTypes.INT())
61+
.column("pt1", DataTypes.STRING())
62+
.column("v", DataTypes.STRING())
63+
.partitionKeys("pt0", "pt1")
64+
.build();
65+
catalog.createTable(identifier, schema, true);
66+
}
67+
68+
@ParameterizedTest(name = "dynamic = {0}, partition={2}")
69+
@MethodSource("overwriteTestData")
70+
public void testOverwriteNothing(
71+
boolean dynamicPartitionOverwrite,
72+
List<InternalRow> overwriteData,
73+
Map<String, String> overwritePartition,
74+
List<String> expected)
75+
throws Exception {
76+
Table originTable = catalog.getTable(identifier("T"));
77+
FileStoreTable table = (FileStoreTable) originTable;
78+
if (!dynamicPartitionOverwrite) {
79+
table =
80+
table.copy(
81+
Collections.singletonMap(
82+
CoreOptions.DYNAMIC_PARTITION_OVERWRITE.key(), "false"));
83+
}
84+
85+
// prepare data
86+
// (1, 1, 'A', 'Hi'), (2, 1, 'A', 'Hello'), (3, 1, 'A', 'World'),
87+
// (4, 1, 'B', 'To'), (5, 1, 'B', 'Apache'), (6, 1, 'B', 'Paimon')
88+
// (7, 2, 'A', 'Test')
89+
// (8, 2, 'B', 'Case')
90+
try (StreamTableWrite write = table.newWrite(commitUser);
91+
InnerTableCommit commit = table.newCommit(commitUser)) {
92+
write.write(overwriteRow(1, 1, "A", "Hi"));
93+
write.write(overwriteRow(2, 1, "A", "Hello"));
94+
write.write(overwriteRow(3, 1, "A", "World"));
95+
write.write(overwriteRow(4, 1, "B", "To"));
96+
write.write(overwriteRow(5, 1, "B", "Apache"));
97+
write.write(overwriteRow(6, 1, "B", "Paimon"));
98+
write.write(overwriteRow(7, 2, "A", "Test"));
99+
write.write(overwriteRow(8, 2, "B", "Case"));
100+
commit.commit(0, write.prepareCommit(true, 0));
101+
}
102+
103+
// overwrite data
104+
try (StreamTableWrite write = table.newWrite(commitUser).withIgnorePreviousFiles(true);
105+
InnerTableCommit commit = table.newCommit(commitUser)) {
106+
for (InternalRow row : overwriteData) {
107+
write.write(row);
108+
}
109+
commit.withOverwrite(overwritePartition).commit(1, write.prepareCommit(true, 1));
110+
}
111+
112+
// validate
113+
List<Split> splits = new ArrayList<>(table.newSnapshotReader().read().dataSplits());
114+
TableRead read = table.newRead();
115+
assertThat(
116+
getResult(
117+
read,
118+
splits,
119+
row ->
120+
DataFormatTestUtil.toStringNoRowKind(
121+
row, originTable.rowType())))
122+
.hasSameElementsAs(expected);
123+
}
124+
125+
private static List<Arguments> overwriteTestData() {
126+
// dynamic, overwrite data, overwrite partition, expected
127+
return Arrays.asList(
128+
// nothing happen
129+
arguments(
130+
true,
131+
Collections.emptyList(),
132+
Collections.emptyMap(),
133+
Arrays.asList(
134+
"1, 1, A, Hi",
135+
"2, 1, A, Hello",
136+
"3, 1, A, World",
137+
"4, 1, B, To",
138+
"5, 1, B, Apache",
139+
"6, 1, B, Paimon",
140+
"7, 2, A, Test",
141+
"8, 2, B, Case")),
142+
// delete all data
143+
arguments(
144+
false,
145+
Collections.emptyList(),
146+
Collections.emptyMap(),
147+
Collections.emptyList()),
148+
// specify one partition key
149+
arguments(
150+
true,
151+
Arrays.asList(
152+
overwriteRow(1, 1, "A", "Where"), overwriteRow(2, 1, "A", "When")),
153+
Collections.singletonMap("pt0", "1"),
154+
Arrays.asList(
155+
"1, 1, A, Where",
156+
"2, 1, A, When",
157+
"4, 1, B, To",
158+
"5, 1, B, Apache",
159+
"6, 1, B, Paimon",
160+
"7, 2, A, Test",
161+
"8, 2, B, Case")),
162+
arguments(
163+
false,
164+
Arrays.asList(
165+
overwriteRow(1, 1, "A", "Where"), overwriteRow(2, 1, "A", "When")),
166+
Collections.singletonMap("pt0", "1"),
167+
Arrays.asList(
168+
"1, 1, A, Where",
169+
"2, 1, A, When",
170+
"7, 2, A, Test",
171+
"8, 2, B, Case")),
172+
// all dynamic
173+
arguments(
174+
true,
175+
Arrays.asList(
176+
overwriteRow(4, 1, "B", "Where"),
177+
overwriteRow(5, 1, "B", "When"),
178+
overwriteRow(10, 2, "A", "Static"),
179+
overwriteRow(11, 2, "A", "Dynamic")),
180+
Collections.emptyMap(),
181+
Arrays.asList(
182+
"1, 1, A, Hi",
183+
"2, 1, A, Hello",
184+
"3, 1, A, World",
185+
"4, 1, B, Where",
186+
"5, 1, B, When",
187+
"10, 2, A, Static",
188+
"11, 2, A, Dynamic",
189+
"8, 2, B, Case")),
190+
arguments(
191+
false,
192+
Arrays.asList(
193+
overwriteRow(4, 1, "B", "Where"),
194+
overwriteRow(5, 1, "B", "When"),
195+
overwriteRow(10, 2, "A", "Static"),
196+
overwriteRow(11, 2, "A", "Dynamic")),
197+
Collections.emptyMap(),
198+
Arrays.asList(
199+
"4, 1, B, Where",
200+
"5, 1, B, When",
201+
"10, 2, A, Static",
202+
"11, 2, A, Dynamic")));
203+
}
204+
205+
private static InternalRow overwriteRow(Object... values) {
206+
return GenericRow.of(
207+
values[0],
208+
values[1],
209+
BinaryString.fromString((String) values[2]),
210+
BinaryString.fromString((String) values[3]));
211+
}
212+
}

0 commit comments

Comments
 (0)