use of org.intellij.lang.annotations.Language in project presto by prestodb.
the class TestRaptorIntegrationSmokeTest method testTableStatsSystemTableWithDeltaDelete.
@SuppressWarnings("OverlyStrongTypeCast")
@Test
public void testTableStatsSystemTableWithDeltaDelete() {
// create empty table
assertUpdate("CREATE TABLE test_table_stats_with_delta_delete (x bigint) WITH (table_supports_delta_delete = true)");
@Language("SQL") String sql = "" + "SELECT create_time, update_time, table_version," + " shard_count, row_count, uncompressed_size, delta_count\n" + "FROM system.table_stats\n" + "WHERE table_schema = 'tpch'\n" + " AND table_name = 'test_table_stats_with_delta_delete'";
MaterializedRow row = getOnlyElement(computeActual(sql).getMaterializedRows());
LocalDateTime createTime = (LocalDateTime) row.getField(0);
LocalDateTime updateTime1 = (LocalDateTime) row.getField(1);
assertEquals(createTime, updateTime1);
// table_version
assertEquals(row.getField(2), 1L);
// shard_count
assertEquals(row.getField(3), 0L);
// row_count
assertEquals(row.getField(4), 0L);
// uncompressed_size
long size1 = (long) row.getField(5);
// insert
assertUpdate("INSERT INTO test_table_stats_with_delta_delete VALUES (1), (2), (3), (4)", 4);
row = getOnlyElement(computeActual(sql).getMaterializedRows());
assertEquals(row.getField(0), createTime);
LocalDateTime updateTime2 = (LocalDateTime) row.getField(1);
assertLessThan(updateTime1, updateTime2);
// table_version
assertEquals(row.getField(2), 2L);
// shard_count
assertGreaterThanOrEqual((Long) row.getField(3), 1L);
// row_count
assertEquals(row.getField(4), 4L);
// delta_count
assertGreaterThanOrEqual((Long) row.getField(6), 0L);
// uncompressed_size
long size2 = (long) row.getField(5);
assertGreaterThan(size2, size1);
// delete
assertUpdate("DELETE FROM test_table_stats_with_delta_delete WHERE x IN (2, 4)", 2);
row = getOnlyElement(computeActual(sql).getMaterializedRows());
assertEquals(row.getField(0), createTime);
LocalDateTime updateTime3 = (LocalDateTime) row.getField(1);
assertLessThan(updateTime2, updateTime3);
// table_version
assertEquals(row.getField(2), 3L);
// shard_count
assertGreaterThanOrEqual((Long) row.getField(3), 1L);
// row_count
assertEquals(row.getField(4), 2L);
// delta_count
assertGreaterThanOrEqual((Long) row.getField(6), 1L);
// uncompressed_Size
long size3 = (long) row.getField(5);
// without compaction, the size will grow with delta delete
assertGreaterThan(size3, size2);
// add column
assertUpdate("ALTER TABLE test_table_stats_with_delta_delete ADD COLUMN y bigint");
row = getOnlyElement(computeActual(sql).getMaterializedRows());
assertEquals(row.getField(0), createTime);
assertLessThan(updateTime3, (LocalDateTime) row.getField(1));
// table_version
assertEquals(row.getField(2), 4L);
// row_count
assertEquals(row.getField(4), 2L);
// uncompressed_size
assertEquals(row.getField(5), size3);
// cleanup
assertUpdate("DROP TABLE test_table_stats_with_delta_delete");
}
use of org.intellij.lang.annotations.Language in project presto by prestodb.
the class MemoryLocalQueryRunner method execute.
public List<Page> execute(@Language("SQL") String query) {
MemoryPool memoryPool = new MemoryPool(new MemoryPoolId("test"), new DataSize(2, GIGABYTE));
SpillSpaceTracker spillSpaceTracker = new SpillSpaceTracker(new DataSize(1, GIGABYTE));
QueryContext queryContext = new QueryContext(new QueryId("test"), new DataSize(1, GIGABYTE), new DataSize(2, GIGABYTE), new DataSize(1, GIGABYTE), new DataSize(2, GIGABYTE), memoryPool, new TestingGcMonitor(), localQueryRunner.getExecutor(), localQueryRunner.getScheduler(), new DataSize(4, GIGABYTE), spillSpaceTracker, listJsonCodec(TaskMemoryReservationSummary.class));
TaskContext taskContext = queryContext.addTaskContext(new TaskStateMachine(new TaskId("query", 0, 0, 0), localQueryRunner.getExecutor()), localQueryRunner.getDefaultSession(), Optional.empty(), false, false, false, false, false);
// Use NullOutputFactory to avoid coping out results to avoid affecting benchmark results
ImmutableList.Builder<Page> output = ImmutableList.builder();
List<Driver> drivers = localQueryRunner.createDrivers(query, new PageConsumerOperator.PageConsumerOutputFactory(types -> output::add), taskContext);
boolean done = false;
while (!done) {
boolean processed = false;
for (Driver driver : drivers) {
if (!driver.isFinished()) {
driver.process();
processed = true;
}
}
done = !processed;
}
return output.build();
}
use of org.intellij.lang.annotations.Language in project presto by prestodb.
the class TestHiveIntegrationSmokeTest method testPartitionPerScanLimit.
private void testPartitionPerScanLimit(Session session, HiveStorageFormat storageFormat) {
String tableName = "test_partition_per_scan_limit";
String partitionsTable = "\"" + tableName + "$partitions\"";
@Language("SQL") String createTable = "" + "CREATE TABLE " + tableName + " " + "(" + " foo VARCHAR," + " part BIGINT" + ") " + "WITH (" + "format = '" + storageFormat + "', " + "partitioned_by = ARRAY[ 'part' ]" + ") ";
assertUpdate(session, createTable);
TableMetadata tableMetadata = getTableMetadata(catalog, TPCH_SCHEMA, tableName);
assertEquals(tableMetadata.getMetadata().getProperties().get(STORAGE_FORMAT_PROPERTY), storageFormat);
assertEquals(tableMetadata.getMetadata().getProperties().get(PARTITIONED_BY_PROPERTY), ImmutableList.of("part"));
// insert 1200 partitions
for (int i = 0; i < 12; i++) {
int partStart = i * 100;
int partEnd = (i + 1) * 100 - 1;
@Language("SQL") String insertPartitions = "" + "INSERT INTO " + tableName + " " + "SELECT 'bar' foo, part " + "FROM UNNEST(SEQUENCE(" + partStart + ", " + partEnd + ")) AS TMP(part)";
assertUpdate(session, insertPartitions, 100);
}
// we are not constrained by hive.max-partitions-per-scan when listing partitions
assertQuery(session, "SELECT * FROM " + partitionsTable + " WHERE part > 490 and part <= 500", "VALUES 491, 492, 493, 494, 495, 496, 497, 498, 499, 500");
assertQuery(session, "SELECT * FROM " + partitionsTable + " WHERE part < 0", "SELECT null WHERE false");
assertQuery(session, "SELECT * FROM " + partitionsTable, "VALUES " + LongStream.range(0, 1200).mapToObj(String::valueOf).collect(joining(",")));
// verify can query 1000 partitions
assertQuery(session, "SELECT count(foo) FROM " + tableName + " WHERE part < 1000", "SELECT 1000");
// verify the rest 200 partitions are successfully inserted
assertQuery(session, "SELECT count(foo) FROM " + tableName + " WHERE part >= 1000 AND part < 1200", "SELECT 200");
// verify cannot query more than 1000 partitions
assertQueryFails(session, "SELECT * from " + tableName + " WHERE part < 1001", format("Query over table 'tpch.%s' can potentially read more than 1000 partitions", tableName));
// verify cannot query all partitions
assertQueryFails(session, "SELECT * from " + tableName, format("Query over table 'tpch.%s' can potentially read more than 1000 partitions", tableName));
assertUpdate(session, "DROP TABLE " + tableName);
assertFalse(getQueryRunner().tableExists(session, tableName));
}
use of org.intellij.lang.annotations.Language in project presto by prestodb.
the class TestHiveIntegrationSmokeTest method testCreateTableAs.
private void testCreateTableAs(Session session, HiveStorageFormat storageFormat) {
if (!insertOperationsSupported(storageFormat)) {
return;
}
@Language("SQL") String select = "SELECT" + " 'foo' _varchar" + ", CAST('bar' AS CHAR(10)) _char" + ", CAST (1 AS BIGINT) _bigint" + ", 2 _integer" + ", CAST (3 AS SMALLINT) _smallint" + ", CAST (4 AS TINYINT) _tinyint" + ", CAST ('123.45' as REAL) _real" + ", CAST('3.14' AS DOUBLE) _double" + ", true _boolean" + ", CAST('3.14' AS DECIMAL(3,2)) _decimal_short" + ", CAST('12345678901234567890.0123456789' AS DECIMAL(30,10)) _decimal_long";
if (storageFormat == HiveStorageFormat.AVRO) {
select = select.replace(" CAST (3 AS SMALLINT) _smallint,", " 3 _smallint,");
select = select.replace(" CAST (4 AS TINYINT) _tinyint,", " 4 _tinyint,");
}
String createTableAs = format("CREATE TABLE test_format_table WITH (format = '%s') AS %s", storageFormat, select);
assertUpdate(session, createTableAs, 1);
TableMetadata tableMetadata = getTableMetadata(catalog, TPCH_SCHEMA, "test_format_table");
assertEquals(tableMetadata.getMetadata().getProperties().get(STORAGE_FORMAT_PROPERTY), storageFormat);
assertColumnType(tableMetadata, "_varchar", createVarcharType(3));
assertColumnType(tableMetadata, "_char", createCharType(10));
// assure reader supports basic column reordering and pruning
assertQuery(session, "SELECT _integer, _varchar, _integer from test_format_table", "SELECT 2, 'foo', 2");
assertQuery(session, "SELECT * from test_format_table", select);
assertUpdate(session, "DROP TABLE test_format_table");
assertFalse(getQueryRunner().tableExists(session, "test_format_table"));
}
use of org.intellij.lang.annotations.Language in project presto by prestodb.
the class TestHiveIntegrationSmokeTest method testGroupedExecution.
private void testGroupedExecution(Session session) {
try {
assertUpdate(session, "CREATE TABLE test_grouped_join1\n" + "WITH (bucket_count = 13, bucketed_by = ARRAY['key1']) AS\n" + "SELECT orderkey key1, comment value1 FROM orders", 15000);
assertUpdate(session, "CREATE TABLE test_grouped_join2\n" + "WITH (bucket_count = 13, bucketed_by = ARRAY['key2']) AS\n" + "SELECT orderkey key2, comment value2 FROM orders", 15000);
assertUpdate(session, "CREATE TABLE test_grouped_join3\n" + "WITH (bucket_count = 13, bucketed_by = ARRAY['key3']) AS\n" + "SELECT orderkey key3, comment value3 FROM orders", 15000);
assertUpdate(session, "CREATE TABLE test_grouped_join4\n" + "WITH (bucket_count = 13, bucketed_by = ARRAY['key4_bucket']) AS\n" + "SELECT orderkey key4_bucket, orderkey key4_non_bucket, comment value4 FROM orders", 15000);
assertUpdate(session, "CREATE TABLE test_grouped_joinN AS\n" + "SELECT orderkey keyN, comment valueN FROM orders", 15000);
assertUpdate(session, "CREATE TABLE test_grouped_joinDual\n" + "WITH (bucket_count = 13, bucketed_by = ARRAY['keyD']) AS\n" + "SELECT orderkey keyD, comment valueD FROM orders CROSS JOIN UNNEST(repeat(NULL, 2))", 30000);
assertUpdate(session, "CREATE TABLE test_grouped_window\n" + "WITH (bucket_count = 5, bucketed_by = ARRAY['key']) AS\n" + "SELECT custkey key, orderkey value FROM orders WHERE custkey <= 5 ORDER BY orderkey LIMIT 10", 10);
// NO colocated join, no grouped execution
Session notColocatedNotGrouped = Session.builder(session).setSystemProperty(COLOCATED_JOIN, "false").setSystemProperty(GROUPED_EXECUTION, "false").build();
// colocated join, no grouped execution
Session colocatedNotGrouped = Session.builder(session).setSystemProperty(COLOCATED_JOIN, "true").setSystemProperty(GROUPED_EXECUTION, "false").build();
// No colocated join, grouped execution
Session notColocatedGrouped = Session.builder(session).setSystemProperty(COLOCATED_JOIN, "false").setSystemProperty(GROUPED_EXECUTION, "true").build();
// Co-located JOIN with all groups at once
Session colocatedAllGroupsAtOnce = Session.builder(session).setSystemProperty(COLOCATED_JOIN, "true").setSystemProperty(GROUPED_EXECUTION, "true").setSystemProperty(CONCURRENT_LIFESPANS_PER_NODE, "0").build();
// Co-located JOIN, 1 group per worker at a time
Session colocatedOneGroupAtATime = Session.builder(session).setSystemProperty(COLOCATED_JOIN, "true").setSystemProperty(GROUPED_EXECUTION, "true").setSystemProperty(CONCURRENT_LIFESPANS_PER_NODE, "1").build();
// Broadcast JOIN, 1 group per worker at a time
Session broadcastOneGroupAtATime = Session.builder(session).setSystemProperty(JOIN_DISTRIBUTION_TYPE, BROADCAST.name()).setSystemProperty(COLOCATED_JOIN, "true").setSystemProperty(GROUPED_EXECUTION, "true").setSystemProperty(CONCURRENT_LIFESPANS_PER_NODE, "1").build();
//
// HASH JOIN
// =========
@Language("SQL") String joinThreeBucketedTable = "SELECT key1, value1, key2, value2, key3, value3\n" + "FROM test_grouped_join1\n" + "JOIN test_grouped_join2\n" + "ON key1 = key2\n" + "JOIN test_grouped_join3\n" + "ON key2 = key3";
@Language("SQL") String joinThreeMixedTable = "SELECT key1, value1, key2, value2, keyN, valueN\n" + "FROM test_grouped_join1\n" + "JOIN test_grouped_join2\n" + "ON key1 = key2\n" + "JOIN test_grouped_joinN\n" + "ON key2 = keyN";
@Language("SQL") String expectedJoinQuery = "SELECT orderkey, comment, orderkey, comment, orderkey, comment from orders";
@Language("SQL") String leftJoinBucketedTable = "SELECT key1, value1, key2, value2\n" + "FROM test_grouped_join1\n" + "LEFT JOIN (SELECT * FROM test_grouped_join2 WHERE key2 % 2 = 0)\n" + "ON key1 = key2";
@Language("SQL") String rightJoinBucketedTable = "SELECT key1, value1, key2, value2\n" + "FROM (SELECT * FROM test_grouped_join2 WHERE key2 % 2 = 0)\n" + "RIGHT JOIN test_grouped_join1\n" + "ON key1 = key2";
@Language("SQL") String expectedOuterJoinQuery = "SELECT orderkey, comment, CASE mod(orderkey, 2) WHEN 0 THEN orderkey END, CASE mod(orderkey, 2) WHEN 0 THEN comment END from orders";
assertQuery(notColocatedNotGrouped, joinThreeBucketedTable, expectedJoinQuery);
assertQuery(notColocatedNotGrouped, leftJoinBucketedTable, expectedOuterJoinQuery);
assertQuery(notColocatedNotGrouped, rightJoinBucketedTable, expectedOuterJoinQuery);
assertQuery(notColocatedGrouped, joinThreeBucketedTable, expectedJoinQuery);
assertQuery(notColocatedGrouped, leftJoinBucketedTable, expectedOuterJoinQuery);
assertQuery(notColocatedGrouped, rightJoinBucketedTable, expectedOuterJoinQuery);
assertQuery(colocatedNotGrouped, joinThreeBucketedTable, expectedJoinQuery, assertRemoteExchangesCount(1));
assertQuery(colocatedNotGrouped, joinThreeMixedTable, expectedJoinQuery, assertRemoteExchangesCount(2));
assertQuery(colocatedAllGroupsAtOnce, joinThreeBucketedTable, expectedJoinQuery, assertRemoteExchangesCount(1));
assertQuery(colocatedAllGroupsAtOnce, joinThreeMixedTable, expectedJoinQuery, assertRemoteExchangesCount(2));
assertQuery(colocatedOneGroupAtATime, joinThreeBucketedTable, expectedJoinQuery, assertRemoteExchangesCount(1));
assertQuery(colocatedOneGroupAtATime, joinThreeMixedTable, expectedJoinQuery, assertRemoteExchangesCount(2));
assertQuery(colocatedNotGrouped, leftJoinBucketedTable, expectedOuterJoinQuery, assertRemoteExchangesCount(1));
assertQuery(colocatedNotGrouped, rightJoinBucketedTable, expectedOuterJoinQuery, assertRemoteExchangesCount(1));
assertQuery(colocatedAllGroupsAtOnce, leftJoinBucketedTable, expectedOuterJoinQuery, assertRemoteExchangesCount(1));
assertQuery(colocatedAllGroupsAtOnce, rightJoinBucketedTable, expectedOuterJoinQuery, assertRemoteExchangesCount(1));
assertQuery(colocatedOneGroupAtATime, leftJoinBucketedTable, expectedOuterJoinQuery, assertRemoteExchangesCount(1));
assertQuery(colocatedOneGroupAtATime, rightJoinBucketedTable, expectedOuterJoinQuery, assertRemoteExchangesCount(1));
//
// CROSS JOIN and HASH JOIN mixed
// ==============================
@Language("SQL") String crossJoin = "SELECT key1, value1, key2, value2, key3, value3\n" + "FROM test_grouped_join1\n" + "JOIN test_grouped_join2\n" + "ON key1 = key2\n" + "CROSS JOIN (SELECT * FROM test_grouped_join3 WHERE key3 <= 3)";
@Language("SQL") String expectedCrossJoinQuery = "SELECT key1, value1, key1, value1, key3, value3\n" + "FROM\n" + " (SELECT orderkey key1, comment value1 FROM orders)\n" + "CROSS JOIN\n" + " (SELECT orderkey key3, comment value3 FROM orders where orderkey <= 3)";
assertQuery(notColocatedNotGrouped, crossJoin, expectedCrossJoinQuery);
assertQuery(notColocatedGrouped, crossJoin, expectedCrossJoinQuery);
assertQuery(colocatedNotGrouped, crossJoin, expectedCrossJoinQuery, assertRemoteExchangesCount(2));
assertQuery(colocatedAllGroupsAtOnce, crossJoin, expectedCrossJoinQuery, assertRemoteExchangesCount(2));
assertQuery(colocatedOneGroupAtATime, crossJoin, expectedCrossJoinQuery, assertRemoteExchangesCount(2));
//
// Bucketed and unbucketed HASH JOIN mixed
// =======================================
@Language("SQL") String bucketedAndUnbucketedJoin = "SELECT key1, value1, keyN, valueN, key2, value2, key3, value3\n" + "FROM\n" + " test_grouped_join1\n" + "JOIN (\n" + " SELECT *\n" + " FROM test_grouped_joinN\n" + " JOIN test_grouped_join2\n" + " ON keyN = key2\n" + ")\n" + "ON key1 = keyN\n" + "JOIN test_grouped_join3\n" + "ON key1 = key3";
@Language("SQL") String expectedBucketedAndUnbucketedJoinQuery = "SELECT orderkey, comment, orderkey, comment, orderkey, comment, orderkey, comment from orders";
assertQuery(notColocatedNotGrouped, bucketedAndUnbucketedJoin, expectedBucketedAndUnbucketedJoinQuery);
assertQuery(notColocatedGrouped, bucketedAndUnbucketedJoin, expectedBucketedAndUnbucketedJoinQuery);
assertQuery(colocatedNotGrouped, bucketedAndUnbucketedJoin, expectedBucketedAndUnbucketedJoinQuery, assertRemoteExchangesCount(2));
assertQuery(colocatedAllGroupsAtOnce, bucketedAndUnbucketedJoin, expectedBucketedAndUnbucketedJoinQuery, assertRemoteExchangesCount(2));
assertQuery(colocatedOneGroupAtATime, bucketedAndUnbucketedJoin, expectedBucketedAndUnbucketedJoinQuery, assertRemoteExchangesCount(2));
//
// UNION ALL / GROUP BY
// ====================
@Language("SQL") String groupBySingleBucketed = "SELECT\n" + " keyD,\n" + " count(valueD)\n" + "FROM\n" + " test_grouped_joinDual\n" + "GROUP BY keyD";
@Language("SQL") String expectedSingleGroupByQuery = "SELECT orderkey, 2 from orders";
@Language("SQL") String groupByOfUnionBucketed = "SELECT\n" + " key\n" + ", arbitrary(value1)\n" + ", arbitrary(value2)\n" + ", arbitrary(value3)\n" + "FROM (\n" + " SELECT key1 key, value1, NULL value2, NULL value3\n" + " FROM test_grouped_join1\n" + "UNION ALL\n" + " SELECT key2 key, NULL value1, value2, NULL value3\n" + " FROM test_grouped_join2\n" + " WHERE key2 % 2 = 0\n" + "UNION ALL\n" + " SELECT key3 key, NULL value1, NULL value2, value3\n" + " FROM test_grouped_join3\n" + " WHERE key3 % 3 = 0\n" + ")\n" + "GROUP BY key";
@Language("SQL") String groupByOfUnionMixed = "SELECT\n" + " key\n" + ", arbitrary(value1)\n" + ", arbitrary(value2)\n" + ", arbitrary(valueN)\n" + "FROM (\n" + " SELECT key1 key, value1, NULL value2, NULL valueN\n" + " FROM test_grouped_join1\n" + "UNION ALL\n" + " SELECT key2 key, NULL value1, value2, NULL valueN\n" + " FROM test_grouped_join2\n" + " WHERE key2 % 2 = 0\n" + "UNION ALL\n" + " SELECT keyN key, NULL value1, NULL value2, valueN\n" + " FROM test_grouped_joinN\n" + " WHERE keyN % 3 = 0\n" + ")\n" + "GROUP BY key";
@Language("SQL") String expectedGroupByOfUnion = "SELECT orderkey, comment, CASE mod(orderkey, 2) WHEN 0 THEN comment END, CASE mod(orderkey, 3) WHEN 0 THEN comment END from orders";
// In this case:
// * left side can take advantage of bucketed execution
// * right side does not have the necessary organization to allow its parent to take advantage of bucketed execution
// In this scenario, we give up bucketed execution altogether. This can potentially be improved.
//
// AGG(key)
// |
// UNION ALL
// / \
// AGG(key) Scan (not bucketed)
// |
// Scan (bucketed on key)
@Language("SQL") String groupByOfUnionOfGroupByMixed = "SELECT\n" + " key, sum(cnt) cnt\n" + "FROM (\n" + " SELECT keyD key, count(valueD) cnt\n" + " FROM test_grouped_joinDual\n" + " GROUP BY keyD\n" + "UNION ALL\n" + " SELECT keyN key, 1 cnt\n" + " FROM test_grouped_joinN\n" + ")\n" + "group by key";
@Language("SQL") String expectedGroupByOfUnionOfGroupBy = "SELECT orderkey, 3 from orders";
// Eligible GROUP BYs run in the same fragment regardless of colocated_join flag
assertQuery(notColocatedGrouped, groupBySingleBucketed, expectedSingleGroupByQuery, assertRemoteExchangesCount(1));
assertQuery(colocatedAllGroupsAtOnce, groupBySingleBucketed, expectedSingleGroupByQuery, assertRemoteExchangesCount(1));
assertQuery(colocatedOneGroupAtATime, groupBySingleBucketed, expectedSingleGroupByQuery, assertRemoteExchangesCount(1));
assertQuery(notColocatedGrouped, groupByOfUnionBucketed, expectedGroupByOfUnion, assertRemoteExchangesCount(1));
assertQuery(colocatedAllGroupsAtOnce, groupByOfUnionBucketed, expectedGroupByOfUnion, assertRemoteExchangesCount(1));
assertQuery(colocatedOneGroupAtATime, groupByOfUnionBucketed, expectedGroupByOfUnion, assertRemoteExchangesCount(1));
// cannot be executed in a grouped manner but should still produce correct result
assertQuery(colocatedOneGroupAtATime, groupByOfUnionMixed, expectedGroupByOfUnion, assertRemoteExchangesCount(2));
assertQuery(colocatedOneGroupAtATime, groupByOfUnionOfGroupByMixed, expectedGroupByOfUnionOfGroupBy, assertRemoteExchangesCount(2));
//
// GROUP BY and JOIN mixed
// ========================
@Language("SQL") String joinGroupedWithGrouped = "SELECT key1, count1, count2\n" + "FROM (\n" + " SELECT keyD key1, count(valueD) count1\n" + " FROM test_grouped_joinDual\n" + " GROUP BY keyD\n" + ") JOIN (\n" + " SELECT keyD key2, count(valueD) count2\n" + " FROM test_grouped_joinDual\n" + " GROUP BY keyD\n" + ")\n" + "ON key1 = key2";
@Language("SQL") String expectedJoinGroupedWithGrouped = "SELECT orderkey, 2, 2 from orders";
@Language("SQL") String joinGroupedWithUngrouped = "SELECT keyD, countD, valueN\n" + "FROM (\n" + " SELECT keyD, count(valueD) countD\n" + " FROM test_grouped_joinDual\n" + " GROUP BY keyD\n" + ") JOIN (\n" + " SELECT keyN, valueN\n" + " FROM test_grouped_joinN\n" + ")\n" + "ON keyD = keyN";
@Language("SQL") String expectedJoinGroupedWithUngrouped = "SELECT orderkey, 2, comment from orders";
@Language("SQL") String joinUngroupedWithGrouped = "SELECT keyN, valueN, countD\n" + "FROM (\n" + " SELECT keyN, valueN\n" + " FROM test_grouped_joinN\n" + ") JOIN (\n" + " SELECT keyD, count(valueD) countD\n" + " FROM test_grouped_joinDual\n" + " GROUP BY keyD\n" + ")\n" + "ON keyN = keyD";
@Language("SQL") String expectedJoinUngroupedWithGrouped = "SELECT orderkey, comment, 2 from orders";
@Language("SQL") String groupOnJoinResult = "SELECT keyD, count(valueD), count(valueN)\n" + "FROM\n" + " test_grouped_joinDual\n" + "JOIN\n" + " test_grouped_joinN\n" + "ON keyD=keyN\n" + "GROUP BY keyD";
@Language("SQL") String expectedGroupOnJoinResult = "SELECT orderkey, 2, 2 from orders";
@Language("SQL") String groupOnUngroupedJoinResult = "SELECT key4_bucket, count(value4), count(valueN)\n" + "FROM\n" + " test_grouped_join4\n" + "JOIN\n" + " test_grouped_joinN\n" + "ON key4_non_bucket=keyN\n" + "GROUP BY key4_bucket";
@Language("SQL") String expectedGroupOnUngroupedJoinResult = "SELECT orderkey, count(*), count(*) from orders group by orderkey";
// Eligible GROUP BYs run in the same fragment regardless of colocated_join flag
assertQuery(notColocatedGrouped, joinGroupedWithGrouped, expectedJoinGroupedWithGrouped, assertRemoteExchangesCount(2));
assertQuery(colocatedAllGroupsAtOnce, joinGroupedWithGrouped, expectedJoinGroupedWithGrouped, assertRemoteExchangesCount(1));
assertQuery(colocatedOneGroupAtATime, joinGroupedWithGrouped, expectedJoinGroupedWithGrouped, assertRemoteExchangesCount(1));
assertQuery(notColocatedGrouped, joinGroupedWithUngrouped, expectedJoinGroupedWithUngrouped, assertRemoteExchangesCount(2));
assertQuery(colocatedAllGroupsAtOnce, joinGroupedWithUngrouped, expectedJoinGroupedWithUngrouped, assertRemoteExchangesCount(2));
assertQuery(colocatedOneGroupAtATime, joinGroupedWithUngrouped, expectedJoinGroupedWithUngrouped, assertRemoteExchangesCount(2));
assertQuery(notColocatedGrouped, groupOnJoinResult, expectedGroupOnJoinResult, assertRemoteExchangesCount(2));
assertQuery(colocatedAllGroupsAtOnce, groupOnJoinResult, expectedGroupOnJoinResult, assertRemoteExchangesCount(2));
assertQuery(colocatedOneGroupAtATime, groupOnJoinResult, expectedGroupOnJoinResult, assertRemoteExchangesCount(2));
assertQuery(broadcastOneGroupAtATime, groupOnUngroupedJoinResult, expectedGroupOnUngroupedJoinResult, assertRemoteExchangesCount(2));
// cannot be executed in a grouped manner but should still produce correct result
assertQuery(colocatedOneGroupAtATime, joinUngroupedWithGrouped, expectedJoinUngroupedWithGrouped, assertRemoteExchangesCount(2));
assertQuery(colocatedOneGroupAtATime, groupOnUngroupedJoinResult, expectedGroupOnUngroupedJoinResult, assertRemoteExchangesCount(4));
//
// Outer JOIN (that involves LookupOuterOperator)
// ==============================================
// Chain on the probe side to test duplicating OperatorFactory
@Language("SQL") String chainedOuterJoin = "SELECT key1, value1, key2, value2, key3, value3\n" + "FROM\n" + " (SELECT * FROM test_grouped_join1 where mod(key1, 2) = 0)\n" + "RIGHT JOIN\n" + " (SELECT * FROM test_grouped_join2 where mod(key2, 3) = 0)\n" + "ON key1 = key2\n" + "FULL JOIN\n" + " (SELECT * FROM test_grouped_join3 where mod(key3, 5) = 0)\n" + "ON key2 = key3";
// Probe is grouped execution, but build is not
@Language("SQL") String sharedBuildOuterJoin = "SELECT key1, value1, keyN, valueN\n" + "FROM\n" + " (SELECT key1, arbitrary(value1) value1 FROM test_grouped_join1 where mod(key1, 2) = 0 group by key1)\n" + "RIGHT JOIN\n" + " (SELECT * FROM test_grouped_joinN where mod(keyN, 3) = 0)\n" + "ON key1 = keyN";
// The preceding test case, which then feeds into another join
@Language("SQL") String chainedSharedBuildOuterJoin = "SELECT key1, value1, keyN, valueN, key3, value3\n" + "FROM\n" + " (SELECT key1, arbitrary(value1) value1 FROM test_grouped_join1 where mod(key1, 2) = 0 group by key1)\n" + "RIGHT JOIN\n" + " (SELECT * FROM test_grouped_joinN where mod(keyN, 3) = 0)\n" + "ON key1 = keyN\n" + "FULL JOIN\n" + " (SELECT * FROM test_grouped_join3 where mod(key3, 5) = 0)\n" + "ON keyN = key3";
@Language("SQL") String expectedChainedOuterJoinResult = "SELECT\n" + " CASE WHEN mod(orderkey, 2 * 3) = 0 THEN orderkey END,\n" + " CASE WHEN mod(orderkey, 2 * 3) = 0 THEN comment END,\n" + " CASE WHEN mod(orderkey, 3) = 0 THEN orderkey END,\n" + " CASE WHEN mod(orderkey, 3) = 0 THEN comment END,\n" + " CASE WHEN mod(orderkey, 5) = 0 THEN orderkey END,\n" + " CASE WHEN mod(orderkey, 5) = 0 THEN comment END\n" + "FROM ORDERS\n" + "WHERE mod(orderkey, 3) = 0 OR mod(orderkey, 5) = 0";
@Language("SQL") String expectedSharedBuildOuterJoinResult = "SELECT\n" + " CASE WHEN mod(orderkey, 2) = 0 THEN orderkey END,\n" + " CASE WHEN mod(orderkey, 2) = 0 THEN comment END,\n" + " orderkey,\n" + " comment\n" + "FROM ORDERS\n" + "WHERE mod(orderkey, 3) = 0";
assertQuery(notColocatedNotGrouped, chainedOuterJoin, expectedChainedOuterJoinResult);
assertQuery(notColocatedGrouped, chainedOuterJoin, expectedChainedOuterJoinResult);
assertQuery(colocatedNotGrouped, chainedOuterJoin, expectedChainedOuterJoinResult, assertRemoteExchangesCount(1));
assertQuery(colocatedAllGroupsAtOnce, chainedOuterJoin, expectedChainedOuterJoinResult, assertRemoteExchangesCount(1));
assertQuery(colocatedOneGroupAtATime, chainedOuterJoin, expectedChainedOuterJoinResult, assertRemoteExchangesCount(1));
assertQuery(notColocatedNotGrouped, sharedBuildOuterJoin, expectedSharedBuildOuterJoinResult);
assertQuery(notColocatedGrouped, sharedBuildOuterJoin, expectedSharedBuildOuterJoinResult);
assertQuery(colocatedNotGrouped, sharedBuildOuterJoin, expectedSharedBuildOuterJoinResult, assertRemoteExchangesCount(2));
assertQuery(colocatedAllGroupsAtOnce, sharedBuildOuterJoin, expectedSharedBuildOuterJoinResult, assertRemoteExchangesCount(2));
assertQuery(colocatedOneGroupAtATime, sharedBuildOuterJoin, expectedSharedBuildOuterJoinResult, assertRemoteExchangesCount(2));
assertQuery(colocatedOneGroupAtATime, chainedSharedBuildOuterJoin, expectedChainedOuterJoinResult, assertRemoteExchangesCount(2));
//
// Window function
// ===============
assertQuery(colocatedOneGroupAtATime, "SELECT key, count(*) OVER (PARTITION BY key ORDER BY value) FROM test_grouped_window", "VALUES\n" + "(1, 1),\n" + "(2, 1),\n" + "(2, 2),\n" + "(4, 1),\n" + "(4, 2),\n" + "(4, 3),\n" + "(4, 4),\n" + "(4, 5),\n" + "(5, 1),\n" + "(5, 2)", assertRemoteExchangesCount(1));
assertQuery(colocatedOneGroupAtATime, "SELECT key, row_number() OVER (PARTITION BY key ORDER BY value) FROM test_grouped_window", "VALUES\n" + "(1, 1),\n" + "(2, 1),\n" + "(2, 2),\n" + "(4, 1),\n" + "(4, 2),\n" + "(4, 3),\n" + "(4, 4),\n" + "(4, 5),\n" + "(5, 1),\n" + "(5, 2)", assertRemoteExchangesCount(1));
assertQuery(colocatedOneGroupAtATime, "SELECT key, n FROM (SELECT key, row_number() OVER (PARTITION BY key ORDER BY value) AS n FROM test_grouped_window) WHERE n <= 2", "VALUES\n" + "(1, 1),\n" + "(2, 1),\n" + "(2, 2),\n" + "(4, 1),\n" + "(4, 2),\n" + "(5, 1),\n" + "(5, 2)", assertRemoteExchangesCount(1));
//
// Filter out all or majority of splits
// ====================================
@Language("SQL") String noSplits = "SELECT key1, arbitrary(value1)\n" + "FROM test_grouped_join1\n" + "WHERE \"$bucket\" < 0\n" + "GROUP BY key1";
@Language("SQL") String joinMismatchedBuckets = "SELECT key1, value1, key2, value2\n" + "FROM (\n" + " SELECT *\n" + " FROM test_grouped_join1\n" + " WHERE \"$bucket\"=1\n" + ")\n" + "FULL OUTER JOIN (\n" + " SELECT *\n" + " FROM test_grouped_join2\n" + " WHERE \"$bucket\"=11\n" + ")\n" + "ON key1=key2";
@Language("SQL") String expectedNoSplits = "SELECT 1, 'a' WHERE FALSE";
@Language("SQL") String expectedJoinMismatchedBuckets = "SELECT\n" + " CASE WHEN mod(orderkey, 13) = 1 THEN orderkey END,\n" + " CASE WHEN mod(orderkey, 13) = 1 THEN comment END,\n" + " CASE WHEN mod(orderkey, 13) = 11 THEN orderkey END,\n" + " CASE WHEN mod(orderkey, 13) = 11 THEN comment END\n" + "FROM ORDERS\n" + "WHERE mod(orderkey, 13) IN (1, 11)";
assertQuery(notColocatedNotGrouped, noSplits, expectedNoSplits);
assertQuery(notColocatedGrouped, noSplits, expectedNoSplits);
assertQuery(colocatedNotGrouped, noSplits, expectedNoSplits, assertRemoteExchangesCount(1));
assertQuery(colocatedAllGroupsAtOnce, noSplits, expectedNoSplits, assertRemoteExchangesCount(1));
assertQuery(colocatedOneGroupAtATime, noSplits, expectedNoSplits, assertRemoteExchangesCount(1));
assertQuery(notColocatedNotGrouped, joinMismatchedBuckets, expectedJoinMismatchedBuckets);
assertQuery(notColocatedGrouped, joinMismatchedBuckets, expectedJoinMismatchedBuckets);
assertQuery(colocatedNotGrouped, joinMismatchedBuckets, expectedJoinMismatchedBuckets, assertRemoteExchangesCount(1));
assertQuery(colocatedAllGroupsAtOnce, joinMismatchedBuckets, expectedJoinMismatchedBuckets, assertRemoteExchangesCount(1));
assertQuery(colocatedOneGroupAtATime, joinMismatchedBuckets, expectedJoinMismatchedBuckets, assertRemoteExchangesCount(1));
} finally {
assertUpdate(session, "DROP TABLE IF EXISTS test_grouped_join1");
assertUpdate(session, "DROP TABLE IF EXISTS test_grouped_join2");
assertUpdate(session, "DROP TABLE IF EXISTS test_grouped_join3");
assertUpdate(session, "DROP TABLE IF EXISTS test_grouped_join4");
assertUpdate(session, "DROP TABLE IF EXISTS test_grouped_joinN");
assertUpdate(session, "DROP TABLE IF EXISTS test_grouped_joinDual");
assertUpdate(session, "DROP TABLE IF EXISTS test_grouped_window");
}
}
Aggregations