Skip to content

Commit 7dac029

Browse files
committed
test: Enhance Iceberg V3 table tests with additional scenarios and assertions
1 parent ebab5c3 commit 7dac029

File tree

1 file changed

+100
-27
lines changed

1 file changed

+100
-27
lines changed

presto-iceberg/src/test/java/com/facebook/presto/iceberg/TestIcebergV3.java

Lines changed: 100 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,14 @@ public void testInsertIntoV3Table()
122122
@Test
123123
public void testDeleteOnV3TableNotSupported()
124124
{
125-
String tableName = "test_delete_v3_table";
125+
String tableName = "test_v3_delete";
126126
try {
127-
assertUpdate("CREATE TABLE " + tableName + " (id integer, value varchar) WITH (\"format-version\" = '3', \"write.delete.mode\" = 'merge-on-read')");
128-
assertUpdate("INSERT INTO " + tableName + " VALUES (1, 'one'), (2, 'two')", 2);
127+
assertUpdate("CREATE TABLE " + tableName
128+
+ " (id INTEGER, name VARCHAR, value DOUBLE) WITH (\"format-version\" = '3', \"write.delete.mode\" = 'merge-on-read')");
129+
assertUpdate("INSERT INTO " + tableName
130+
+ " VALUES (1, 'Alice', 100.0), (2, 'Bob', 200.0), (3, 'Charlie', 300.0)", 3);
131+
assertQuery("SELECT * FROM " + tableName + " ORDER BY id",
132+
"VALUES (1, 'Alice', 100.0), (2, 'Bob', 200.0), (3, 'Charlie', 300.0)");
129133
assertThatThrownBy(() -> getQueryRunner().execute("DELETE FROM " + tableName + " WHERE id = 1"))
130134
.hasMessageContaining("Iceberg table updates for format version 3 are not supported yet");
131135
}
@@ -137,11 +141,17 @@ public void testDeleteOnV3TableNotSupported()
137141
@Test
138142
public void testUpdateOnV3TableNotSupported()
139143
{
140-
String tableName = "test_update_v3_table";
144+
String tableName = "test_v3_update";
141145
try {
142-
assertUpdate("CREATE TABLE " + tableName + " (id integer, value varchar) WITH (\"format-version\" = '3', \"write.update.mode\" = 'merge-on-read')");
143-
assertUpdate("INSERT INTO " + tableName + " VALUES (1, 'one'), (2, 'two')", 2);
144-
assertThatThrownBy(() -> getQueryRunner().execute("UPDATE " + tableName + " SET value = 'updated' WHERE id = 1"))
146+
assertUpdate("CREATE TABLE " + tableName
147+
+ " (id INTEGER, name VARCHAR, status VARCHAR, score DOUBLE) WITH (\"format-version\" = '3', \"write.update.mode\" = 'merge-on-read')");
148+
assertUpdate("INSERT INTO " + tableName
149+
+ " VALUES (1, 'Alice', 'active', 85.5), (2, 'Bob', 'active', 92.0), (3, 'Charlie', 'inactive', 78.3)",
150+
3);
151+
assertQuery("SELECT * FROM " + tableName + " ORDER BY id",
152+
"VALUES (1, 'Alice', 'active', 85.5), (2, 'Bob', 'active', 92.0), (3, 'Charlie', 'inactive', 78.3)");
153+
assertThatThrownBy(() -> getQueryRunner()
154+
.execute("UPDATE " + tableName + " SET status = 'updated', score = 95.0 WHERE id = 1"))
145155
.hasMessageContaining("Iceberg table updates for format version 3 are not supported yet");
146156
}
147157
finally {
@@ -152,15 +162,22 @@ public void testUpdateOnV3TableNotSupported()
152162
@Test
153163
public void testMergeOnV3TableNotSupported()
154164
{
155-
String tableName = "test_merge_v3_table";
156-
String sourceTable = "test_merge_v3_source";
165+
String tableName = "test_v3_merge_target";
166+
String sourceTable = "test_v3_merge_source";
157167
try {
158-
assertUpdate("CREATE TABLE " + tableName + " (id integer, value varchar) WITH (\"format-version\" = '3', \"write.update.mode\" = 'merge-on-read')");
159-
assertUpdate("CREATE TABLE " + sourceTable + " (id integer, value varchar)");
160-
assertUpdate("INSERT INTO " + tableName + " VALUES (1, 'one')", 1);
161-
assertUpdate("INSERT INTO " + sourceTable + " VALUES (1, 'updated')", 1);
168+
assertUpdate("CREATE TABLE " + tableName
169+
+ " (id INTEGER, name VARCHAR, value DOUBLE) WITH (\"format-version\" = '3', \"write.update.mode\" = 'merge-on-read')");
170+
assertUpdate("CREATE TABLE " + sourceTable + " (id INTEGER, name VARCHAR, value DOUBLE)");
171+
assertUpdate("INSERT INTO " + tableName + " VALUES (1, 'Alice', 100.0), (2, 'Bob', 200.0)", 2);
172+
assertUpdate("INSERT INTO " + sourceTable + " VALUES (1, 'Alice Updated', 150.0), (3, 'Charlie', 300.0)",
173+
2);
174+
assertQuery("SELECT * FROM " + tableName + " ORDER BY id", "VALUES (1, 'Alice', 100.0), (2, 'Bob', 200.0)");
175+
assertQuery("SELECT * FROM " + sourceTable + " ORDER BY id",
176+
"VALUES (1, 'Alice Updated', 150.0), (3, 'Charlie', 300.0)");
162177
assertThatThrownBy(() -> getQueryRunner().execute(
163-
"MERGE INTO " + tableName + " t USING " + sourceTable + " s ON t.id = s.id WHEN MATCHED THEN UPDATE SET value = s.value"))
178+
"MERGE INTO " + tableName + " t USING " + sourceTable + " s ON t.id = s.id " +
179+
"WHEN MATCHED THEN UPDATE SET name = s.name, value = s.value " +
180+
"WHEN NOT MATCHED THEN INSERT (id, name, value) VALUES (s.id, s.name, s.value)"))
164181
.hasMessageContaining("Iceberg table updates for format version 3 are not supported yet");
165182
}
166183
finally {
@@ -172,30 +189,82 @@ public void testMergeOnV3TableNotSupported()
172189
@Test
173190
public void testOptimizeOnV3TableNotSupported()
174191
{
175-
String tableName = "test_optimize_v3_table";
192+
String tableName = "test_v3_optimize";
176193
try {
177-
assertUpdate("CREATE TABLE " + tableName + " (id integer, value varchar) WITH (\"format-version\" = '3')");
178-
assertUpdate("INSERT INTO " + tableName + " VALUES (1, 'one')", 1);
179-
assertUpdate("INSERT INTO " + tableName + " VALUES (2, 'two')", 1);
180-
assertThatThrownBy(() -> getQueryRunner().execute(format("CALL system.rewrite_data_files('%s', '%s')", TEST_SCHEMA, tableName)))
194+
assertUpdate("CREATE TABLE " + tableName
195+
+ " (id INTEGER, category VARCHAR, value DOUBLE) WITH (\"format-version\" = '3')");
196+
assertUpdate("INSERT INTO " + tableName + " VALUES (1, 'A', 100.0)", 1);
197+
assertUpdate("INSERT INTO " + tableName + " VALUES (2, 'B', 200.0)", 1);
198+
assertUpdate("INSERT INTO " + tableName + " VALUES (3, 'A', 150.0)", 1);
199+
assertUpdate("INSERT INTO " + tableName + " VALUES (4, 'C', 300.0)", 1);
200+
assertQuery("SELECT * FROM " + tableName + " ORDER BY id",
201+
"VALUES (1, 'A', 100.0), (2, 'B', 200.0), (3, 'A', 150.0), (4, 'C', 300.0)");
202+
203+
assertThatThrownBy(() -> getQueryRunner()
204+
.execute(format("CALL system.rewrite_data_files('%s', '%s')", TEST_SCHEMA, tableName)))
181205
.hasMessageContaining("OPTIMIZE is not supported for Iceberg table format version > 2");
182206
}
183207
finally {
184208
dropTable(tableName);
185209
}
186210
}
187211

212+
@Test
213+
public void testV3SupportedOperations()
214+
{
215+
String tableName = "test_v3_supported";
216+
try {
217+
assertUpdate("CREATE TABLE " + tableName
218+
+ " (id INTEGER, name VARCHAR, created_date DATE, amount DECIMAL(10,2)) WITH (\"format-version\" = '3', partitioning = ARRAY['created_date'])");
219+
220+
assertUpdate("INSERT INTO " + tableName + " VALUES " +
221+
"(1, 'Transaction A', DATE '2024-01-01', 100.50), " +
222+
"(2, 'Transaction B', DATE '2024-01-02', 250.75), " +
223+
"(3, 'Transaction C', DATE '2024-01-01', 175.00)", 3);
224+
225+
assertQuery("SELECT * FROM " + tableName + " ORDER BY id",
226+
"VALUES " +
227+
"(1, 'Transaction A', DATE '2024-01-01', 100.50), " +
228+
"(2, 'Transaction B', DATE '2024-01-02', 250.75), " +
229+
"(3, 'Transaction C', DATE '2024-01-01', 175.00)");
230+
231+
assertQuery(
232+
"SELECT created_date, count(*), sum(amount) FROM " + tableName
233+
+ " GROUP BY created_date ORDER BY created_date",
234+
"VALUES " +
235+
"(DATE '2024-01-01', 2, 275.50), " +
236+
"(DATE '2024-01-02', 1, 250.75)");
237+
238+
assertQuery("SELECT * FROM " + tableName + " WHERE created_date = DATE '2024-01-01' ORDER BY id",
239+
"VALUES " +
240+
"(1, 'Transaction A', DATE '2024-01-01', 100.50), " +
241+
"(3, 'Transaction C', DATE '2024-01-01', 175.00)");
242+
243+
assertUpdate("INSERT INTO " + tableName + " VALUES (4, 'Transaction D', DATE '2024-01-03', 300.00)", 1);
244+
245+
assertQuery("SELECT count(*) as total_count FROM " + tableName, "SELECT 4");
246+
}
247+
finally {
248+
dropTable(tableName);
249+
}
250+
}
251+
188252
@Test
189253
public void testSelectFromV3TableAfterInsert()
190254
{
191255
String tableName = "test_select_v3_table";
192256
try {
193-
assertUpdate("CREATE TABLE " + tableName + " (id integer, name varchar, price decimal(10,2)) WITH (\"format-version\" = '3')");
194-
assertUpdate("INSERT INTO " + tableName + " VALUES (1, 'apple', 1.50), (2, 'banana', 0.75), (3, 'cherry', 2.00)", 3);
195-
assertQuery("SELECT * FROM " + tableName + " ORDER BY id", "VALUES (1, 'apple', 1.50), (2, 'banana', 0.75), (3, 'cherry', 2.00)");
257+
assertUpdate("CREATE TABLE " + tableName
258+
+ " (id integer, name varchar, price decimal(10,2)) WITH (\"format-version\" = '3')");
259+
assertUpdate(
260+
"INSERT INTO " + tableName + " VALUES (1, 'apple', 1.50), (2, 'banana', 0.75), (3, 'cherry', 2.00)",
261+
3);
262+
assertQuery("SELECT * FROM " + tableName + " ORDER BY id",
263+
"VALUES (1, 'apple', 1.50), (2, 'banana', 0.75), (3, 'cherry', 2.00)");
196264
assertQuery("SELECT count(*) FROM " + tableName, "SELECT 3");
197265
assertQuery("SELECT sum(price) FROM " + tableName, "SELECT 4.25");
198-
assertQuery("SELECT name FROM " + tableName + " WHERE price > 1.00 ORDER BY name", "VALUES ('apple'), ('cherry')");
266+
assertQuery("SELECT name FROM " + tableName + " WHERE price > 1.00 ORDER BY name",
267+
"VALUES ('apple'), ('cherry')");
199268
}
200269
finally {
201270
dropTable(tableName);
@@ -210,8 +279,10 @@ public void testV3TableWithPartitioning()
210279
assertUpdate("CREATE TABLE " + tableName + " (id integer, category varchar, value integer) " +
211280
"WITH (\"format-version\" = '3', partitioning = ARRAY['category'])");
212281
assertUpdate("INSERT INTO " + tableName + " VALUES (1, 'A', 100), (2, 'B', 200), (3, 'A', 150)", 3);
213-
assertQuery("SELECT * FROM " + tableName + " WHERE category = 'A' ORDER BY id", "VALUES (1, 'A', 100), (3, 'A', 150)");
214-
assertQuery("SELECT category, sum(value) FROM " + tableName + " GROUP BY category ORDER BY category", "VALUES ('A', 250), ('B', 200)");
282+
assertQuery("SELECT * FROM " + tableName + " WHERE category = 'A' ORDER BY id",
283+
"VALUES (1, 'A', 100), (3, 'A', 150)");
284+
assertQuery("SELECT category, sum(value) FROM " + tableName + " GROUP BY category ORDER BY category",
285+
"VALUES ('A', 250), ('B', 200)");
215286
}
216287
finally {
217288
dropTable(tableName);
@@ -220,7 +291,8 @@ public void testV3TableWithPartitioning()
220291

221292
private Table loadTable(String tableName)
222293
{
223-
Catalog catalog = CatalogUtil.loadCatalog(HadoopCatalog.class.getName(), ICEBERG_CATALOG, getProperties(), new Configuration());
294+
Catalog catalog = CatalogUtil.loadCatalog(HadoopCatalog.class.getName(), ICEBERG_CATALOG, getProperties(),
295+
new Configuration());
224296
return catalog.loadTable(TableIdentifier.of(TEST_SCHEMA, tableName));
225297
}
226298

@@ -233,7 +305,8 @@ private Map<String, String> getProperties()
233305
private File getCatalogDirectory()
234306
{
235307
Path dataDirectory = getDistributedQueryRunner().getCoordinator().getDataDirectory();
236-
Path catalogDirectory = getIcebergDataDirectoryPath(dataDirectory, HADOOP.name(), new IcebergConfig().getFileFormat(), false);
308+
Path catalogDirectory = getIcebergDataDirectoryPath(dataDirectory, HADOOP.name(),
309+
new IcebergConfig().getFileFormat(), false);
237310
return catalogDirectory.toFile();
238311
}
239312
}

0 commit comments

Comments
 (0)