Skip to content

Commit

Permalink
Add test for hive views with unions
Browse files Browse the repository at this point in the history
  • Loading branch information
jirassimok authored and findepi committed Jun 24, 2021
1 parent 2ee7c94 commit 7458520
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import io.trino.tempto.assertions.QueryAssert;
import io.trino.tempto.fulfillment.table.hive.tpch.ImmutableTpchTablesRequirements.ImmutableNationTable;
import io.trino.tempto.fulfillment.table.hive.tpch.ImmutableTpchTablesRequirements.ImmutableOrdersTable;
import io.trino.tempto.fulfillment.table.hive.tpch.ImmutableTpchTablesRequirements.ImmutableRegionTable;
import io.trino.tempto.query.QueryExecutor;
import io.trino.tempto.query.QueryResult;
import io.trino.testng.services.Flaky;
Expand All @@ -26,6 +27,7 @@
import java.math.BigDecimal;
import java.sql.Date;
import java.time.LocalDate;
import java.util.List;
import java.util.function.Consumer;

import static io.trino.tempto.assertions.QueryAssert.Row.row;
Expand All @@ -41,6 +43,7 @@
@Requires({
ImmutableNationTable.class,
ImmutableOrdersTable.class,
ImmutableRegionTable.class,
})
public abstract class AbstractTestHiveViews
extends HiveProductTest
Expand Down Expand Up @@ -362,6 +365,72 @@ public void testNestedGroupBy()
row(4, 1)));
}

@Test(groups = HIVE_VIEWS)
public void testUnionAllViews()
{
onHive().executeQuery("DROP TABLE IF EXISTS union_helper");
onHive().executeQuery("CREATE TABLE union_helper (\n"
+ "r_regionkey BIGINT,\n"
+ "r_name VARCHAR(25),\n"
+ "r_comment VARCHAR(152)\n"
+ ")");
onHive().executeQuery("INSERT INTO union_helper\n"
+ "SELECT r_regionkey % 3, r_name, r_comment FROM region");

onHive().executeQuery("DROP VIEW IF EXISTS union_all_view");
onHive().executeQuery("CREATE VIEW union_all_view AS\n"
+ "SELECT r_regionkey FROM region\n"
+ "UNION ALL\n"
+ "SELECT r_regionkey FROM union_helper\n");

assertThat(query("SELECT r_regionkey FROM union_all_view"))
// Copy the keys 5 times because there are 5 nations per region
.containsOnly(
// base rows
row(0),
row(1),
row(2),
row(3),
row(4),
// mod 3
row(0),
row(1),
row(2),
row(0),
row(1));
}

@Test(groups = HIVE_VIEWS)
public void testUnionDistinctViews()
{
onHive().executeQuery("DROP TABLE IF EXISTS union_helper");
onHive().executeQuery("CREATE TABLE union_helper (\n"
+ "r_regionkey BIGINT,\n"
+ "r_name VARCHAR(25),\n"
+ "r_comment VARCHAR(152)\n"
+ ")");
onHive().executeQuery("INSERT INTO union_helper\n"
+ "SELECT r_regionkey % 3, r_name, r_comment FROM region");

for (String operator : List.of("UNION", "UNION DISTINCT")) {
String name = format("%s_view", operator.replace(" ", "_"));
onHive().executeQuery(format("DROP VIEW IF EXISTS %s", name));
// Add mod to one side to add duplicate and non-overlapping values
onHive().executeQuery(format(
"CREATE VIEW %s AS\n"
+ "SELECT r_regionkey FROM region\n"
+ "%s\n"
+ "SELECT r_regionkey FROM union_helper\n",
name,
operator));

assertViewQuery(
format("SELECT r_regionkey FROM %s", name),
assertion -> assertion.as("View with %s", operator)
.containsOnly(row(0), row(1), row(2), row(3), row(4)));
}
}

protected static void assertViewQuery(String query, Consumer<QueryAssert> assertion)
{
// Ensure Hive and Presto view compatibility by comparing the results
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,4 +173,3 @@ public void testLateralViewJsonTupleAs()
queryAssert -> queryAssert.containsOnly(row(3, "Mateusz", "Gajewski", "true", "1000", null, null)));
}
}
/**/

0 comments on commit 7458520

Please sign in to comment.