Skip to content

Commit

Permalink
Simplify & unify test code for transactional tables
Browse files Browse the repository at this point in the history
  • Loading branch information
findepi committed Feb 21, 2020
1 parent 88dc473 commit 41625e4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
*/
package io.prestosql.tests.hive.acid;

import io.prestosql.tempto.query.QueryResult;
import io.prestosql.tests.hive.HiveProductTest;
import org.testng.SkipException;
import org.testng.annotations.DataProvider;
Expand All @@ -36,71 +35,45 @@ public void testRead(boolean isPartitioned)
throw new SkipException("Presto Hive transactional tables are supported with Hive version 3 or above");
}

String tableName = isPartitioned ? "full_acid_read_partitioned" : "full_acid_read";
createTable(tableName, isPartitioned);

String tableName = "test_full_acid_table_read";
onHive().executeQuery("DROP TABLE IF EXISTS " + tableName);
onHive().executeQuery("CREATE TABLE " + tableName + " (col INT, fcol INT) " +
(isPartitioned ? "PARTITIONED BY (part_col INT) " : "") +
"STORED AS ORC " +
"TBLPROPERTIES ('transactional'='true') ");
try {
onHive().executeQuery("INSERT OVERWRITE TABLE " + tableName + getHivePartitionString(isPartitioned) + " VALUES (21, 1)");
String hivePartitionString = isPartitioned ? " PARTITION (part_col=2) " : "";
onHive().executeQuery("INSERT OVERWRITE TABLE " + tableName + hivePartitionString + " VALUES (21, 1)");

String selectFromOnePartitionsSql = "SELECT col, fcol FROM " + tableName + " ORDER BY col";
QueryResult onePartitionQueryResult = query(selectFromOnePartitionsSql);
assertThat(onePartitionQueryResult).containsOnly(row(21, 1));
assertThat(query(selectFromOnePartitionsSql)).containsOnly(row(21, 1));

onHive().executeQuery("INSERT INTO TABLE " + tableName + getHivePartitionString(isPartitioned) + " VALUES (22, 2)");
onePartitionQueryResult = query(selectFromOnePartitionsSql);
assertThat(onePartitionQueryResult).containsExactly(row(21, 1), row(22, 2));
onHive().executeQuery("INSERT INTO TABLE " + tableName + hivePartitionString + " VALUES (22, 2)");
assertThat(query(selectFromOnePartitionsSql)).containsExactly(row(21, 1), row(22, 2));

// test filtering
onePartitionQueryResult = query("SELECT col, fcol FROM " + tableName + " WHERE fcol = 1 ORDER BY col");
assertThat(onePartitionQueryResult).containsOnly(row(21, 1));
assertThat(query("SELECT col, fcol FROM " + tableName + " WHERE fcol = 1 ORDER BY col")).containsOnly(row(21, 1));

// delete a row
onHive().executeQuery("DELETE FROM " + tableName + " WHERE fcol=2");
onePartitionQueryResult = query(selectFromOnePartitionsSql);
assertThat(onePartitionQueryResult).containsOnly(row(21, 1));
assertThat(query(selectFromOnePartitionsSql)).containsOnly(row(21, 1));

// update the existing row
onHive().executeQuery("UPDATE " + tableName + " SET col = 23 " + addPartitionPredicate(isPartitioned, "fcol = 1"));
onePartitionQueryResult = query(selectFromOnePartitionsSql);
assertThat(onePartitionQueryResult).containsOnly(row(23, 1));
String predicate = "fcol = 1" + (isPartitioned ? " AND part_col = 2 " : "");
onHive().executeQuery("UPDATE " + tableName + " SET col = 23 WHERE " + predicate);
assertThat(query(selectFromOnePartitionsSql)).containsOnly(row(23, 1));
}
finally {
onHive().executeQuery("DROP TABLE " + tableName);
}
}

private static String getHivePartitionString(boolean isPartitioned)
{
if (!isPartitioned) {
return "";
}

return " PARTITION (part_col=2) ";
}

private static String addPartitionPredicate(boolean isPartitioned, String columnPredicate)
{
String predicate = " WHERE " + columnPredicate;
if (isPartitioned) {
predicate += " AND part_col = 2 ";
}
return predicate;
}

private static void createTable(String tableName, boolean isPartitioned)
{
onHive().executeQuery("CREATE TABLE IF NOT EXISTS " + tableName + " (col INT, fcol INT) " +
(isPartitioned ? "PARTITIONED BY (part_col INT) " : "") +
"STORED AS ORC " +
"TBLPROPERTIES ('transactional'='true') ");
}

@DataProvider
public Object[][] isTablePartitioned()
{
return new Object[][] {
{true},
{false}
{true},
{false}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
*/
package io.prestosql.tests.hive.acid;

import io.prestosql.tempto.query.QueryResult;
import io.prestosql.tests.hive.HiveProductTest;
import org.testng.SkipException;
import org.testng.annotations.DataProvider;
Expand All @@ -29,44 +28,32 @@
public class TestInsertOnlyAcidTableRead
extends HiveProductTest
{
@Test(dataProvider = "isTablePartitioned", groups = {STORAGE_FORMATS, HIVE_TRANSACTIONAL})
@Test(groups = {STORAGE_FORMATS, HIVE_TRANSACTIONAL}, dataProvider = "isTablePartitioned")
public void testSelectFromInsertOnlyAcidTable(boolean isPartitioned)
{
if (getHiveVersionMajor() < 3) {
throw new SkipException("Presto Hive transactional tables are supported with Hive version 3 or above");
}

String tableName = "insert_only_acid_table" + (isPartitioned ? "_partitioned" : "");

String createTable = "CREATE TABLE IF NOT EXISTS " +
tableName +
" (col INT) " +
String tableName = "test_insert_only_table_read";
onHive().executeQuery("DROP TABLE IF EXISTS " + tableName);
onHive().executeQuery("CREATE TABLE " + tableName + " (col INT) " +
(isPartitioned ? "PARTITIONED BY (part_col INT) " : "") +
"STORED AS ORC " +
"TBLPROPERTIES ('transactional_properties'='insert_only', 'transactional'='true') ";

onHive().executeQuery(createTable);

"TBLPROPERTIES ('transactional_properties'='insert_only', 'transactional'='true') ");
try {
String hivePartitionString = isPartitioned ? " PARTITION (part_col=2) " : "";
String predicate = isPartitioned ? " WHERE part_col = 2 " : "";

onHive().executeQuery(
"INSERT OVERWRITE TABLE " + tableName + hivePartitionString + " select 1");

onHive().executeQuery("INSERT OVERWRITE TABLE " + tableName + hivePartitionString + " SELECT 1");
String selectFromOnePartitionsSql = "SELECT col FROM " + tableName + predicate + " ORDER BY COL";
QueryResult onePartitionQueryResult = query(selectFromOnePartitionsSql);
assertThat(onePartitionQueryResult).containsOnly(row(1));
assertThat(query(selectFromOnePartitionsSql)).containsOnly(row(1));

onHive().executeQuery(
"INSERT INTO TABLE " + tableName + hivePartitionString + " select 2");
onePartitionQueryResult = query(selectFromOnePartitionsSql);
assertThat(onePartitionQueryResult).containsExactly(row(1), row(2));
onHive().executeQuery("INSERT INTO TABLE " + tableName + hivePartitionString + " SELECT 2");
assertThat(query(selectFromOnePartitionsSql)).containsExactly(row(1), row(2));

onHive().executeQuery(
"INSERT OVERWRITE TABLE " + tableName + hivePartitionString + " select 3");
onePartitionQueryResult = query(selectFromOnePartitionsSql);
assertThat(onePartitionQueryResult).containsOnly(row(3));
onHive().executeQuery("INSERT OVERWRITE TABLE " + tableName + hivePartitionString + " SELECT 3");
assertThat(query(selectFromOnePartitionsSql)).containsOnly(row(3));
}
finally {
onHive().executeQuery("DROP TABLE " + tableName);
Expand Down

0 comments on commit 41625e4

Please sign in to comment.