use of org.intellij.lang.annotations.Language in project presto by prestodb.
the class TestOptimizeMixedDistinctAggregations method testMixedDistinctAggregationOptimizer.
@Test
public void testMixedDistinctAggregationOptimizer() {
@Language("SQL") String sql = "SELECT custkey, max(totalprice) AS s, count(DISTINCT orderdate) AS d FROM orders GROUP BY custkey";
String group = "GROUP";
// Original keys
String groupBy = "CUSTKEY";
String aggregate = "TOTALPRICE";
String distinctAggregation = "ORDERDATE";
// Second Aggregation data
List<String> groupByKeysSecond = ImmutableList.of(groupBy);
Map<Optional<String>, ExpectedValueProvider<FunctionCall>> aggregationsSecond = ImmutableMap.of(Optional.of("arbitrary"), PlanMatchPattern.functionCall("arbitrary", false, ImmutableList.of(anySymbol())), Optional.of("count"), PlanMatchPattern.functionCall("count", false, ImmutableList.of(anySymbol())));
// First Aggregation data
List<String> groupByKeysFirst = ImmutableList.of(groupBy, distinctAggregation, group);
Map<Optional<String>, ExpectedValueProvider<FunctionCall>> aggregationsFirst = ImmutableMap.of(Optional.of("MAX"), functionCall("max", ImmutableList.of("TOTALPRICE")));
PlanMatchPattern tableScan = tableScan("orders", ImmutableMap.of("TOTALPRICE", "totalprice", "CUSTKEY", "custkey", "ORDERDATE", "orderdate"));
// GroupingSet symbols
ImmutableList.Builder<List<String>> groups = ImmutableList.builder();
groups.add(ImmutableList.of(groupBy, aggregate));
groups.add(ImmutableList.of(groupBy, distinctAggregation));
PlanMatchPattern expectedPlanPattern = anyTree(aggregation(singleGroupingSet(groupByKeysSecond), aggregationsSecond, ImmutableMap.of(), Optional.empty(), SINGLE, project(aggregation(singleGroupingSet(groupByKeysFirst), aggregationsFirst, ImmutableMap.of(), Optional.empty(), SINGLE, groupingSet(groups.build(), group, anyTree(tableScan))))));
assertUnitPlan(sql, expectedPlanPattern);
}
use of org.intellij.lang.annotations.Language in project presto by prestodb.
the class TestMergeWindows method testNotMergeAcrossJoinBranches.
@Test
public void testNotMergeAcrossJoinBranches() {
String rOrderkeyAlias = "R_ORDERKEY";
String rShipdateAlias = "R_SHIPDATE";
String rQuantityAlias = "R_QUANTITY";
@Language("SQL") String sql = "WITH foo AS (" + "SELECT " + "suppkey, orderkey, partkey, " + "SUM(discount) OVER (PARTITION BY orderkey ORDER BY shipdate, quantity DESC ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) a " + "FROM lineitem WHERE (partkey = 272 OR partkey = 273) AND suppkey > 50 " + "), " + "bar AS ( " + "SELECT " + "suppkey, orderkey, partkey, " + "AVG(quantity) OVER (PARTITION BY orderkey ORDER BY shipdate, quantity DESC ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) b " + "FROM lineitem WHERE (partkey = 272 OR partkey = 273) AND suppkey > 50 " + ")" + "SELECT * FROM foo, bar WHERE foo.a = bar.b";
ExpectedValueProvider<WindowNode.Specification> leftSpecification = specification(ImmutableList.of(ORDERKEY_ALIAS), ImmutableList.of(SHIPDATE_ALIAS, QUANTITY_ALIAS), ImmutableMap.of(SHIPDATE_ALIAS, SortOrder.ASC_NULLS_LAST, QUANTITY_ALIAS, SortOrder.DESC_NULLS_LAST));
ExpectedValueProvider<WindowNode.Specification> rightSpecification = specification(ImmutableList.of(rOrderkeyAlias), ImmutableList.of(rShipdateAlias, rQuantityAlias), ImmutableMap.of(rShipdateAlias, SortOrder.ASC_NULLS_LAST, rQuantityAlias, SortOrder.DESC_NULLS_LAST));
// Too many items in the map to call ImmutableMap.of() :-(
ImmutableMap.Builder<String, String> leftTableScanBuilder = ImmutableMap.builder();
leftTableScanBuilder.put(DISCOUNT_ALIAS, "discount");
leftTableScanBuilder.put(ORDERKEY_ALIAS, "orderkey");
leftTableScanBuilder.put("PARTKEY", "partkey");
leftTableScanBuilder.put(SUPPKEY_ALIAS, "suppkey");
leftTableScanBuilder.put(QUANTITY_ALIAS, "quantity");
leftTableScanBuilder.put(SHIPDATE_ALIAS, "shipdate");
PlanMatchPattern leftTableScan = tableScan("lineitem", leftTableScanBuilder.build());
PlanMatchPattern rightTableScan = tableScan("lineitem", ImmutableMap.of(rOrderkeyAlias, "orderkey", "R_PARTKEY", "partkey", "R_SUPPKEY", "suppkey", rQuantityAlias, "quantity", rShipdateAlias, "shipdate"));
assertUnitPlan(sql, anyTree(filter("SUM = AVG", join(JoinNode.Type.INNER, ImmutableList.of(), any(window(windowMatcherBuilder -> windowMatcherBuilder.specification(leftSpecification).addFunction("SUM", functionCall("sum", COMMON_FRAME, ImmutableList.of(DISCOUNT_ALIAS))), any(leftTableScan))), any(window(windowMatcherBuilder -> windowMatcherBuilder.specification(rightSpecification).addFunction("AVG", functionCall("avg", COMMON_FRAME, ImmutableList.of(rQuantityAlias))), any(rightTableScan)))))));
}
use of org.intellij.lang.annotations.Language in project presto by prestodb.
the class TestMergeWindows method testMergeDifferentFramesWithDefault.
@Test
public void testMergeDifferentFramesWithDefault() {
Optional<WindowFrame> frameD = Optional.of(new WindowFrame(WindowFrame.Type.ROWS, new FrameBound(FrameBound.Type.CURRENT_ROW), Optional.of(new FrameBound(FrameBound.Type.UNBOUNDED_FOLLOWING))));
ExpectedValueProvider<WindowNode.Specification> specificationD = specification(ImmutableList.of(SUPPKEY_ALIAS), ImmutableList.of(ORDERKEY_ALIAS), ImmutableMap.of(ORDERKEY_ALIAS, SortOrder.ASC_NULLS_LAST));
@Language("SQL") String sql = "SELECT " + "SUM(quantity) OVER (PARTITION BY suppkey ORDER BY orderkey) sum_quantity_C, " + "AVG(quantity) OVER (PARTITION BY suppkey ORDER BY orderkey ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) avg_quantity_D, " + "SUM(discount) OVER (PARTITION BY suppkey ORDER BY orderkey) sum_discount_C " + "FROM lineitem";
assertUnitPlan(sql, anyTree(window(windowMatcherBuilder -> windowMatcherBuilder.specification(specificationD).addFunction(functionCall("avg", frameD, ImmutableList.of(QUANTITY_ALIAS))).addFunction(functionCall("sum", UNSPECIFIED_FRAME, ImmutableList.of(DISCOUNT_ALIAS))).addFunction(functionCall("sum", UNSPECIFIED_FRAME, ImmutableList.of(QUANTITY_ALIAS))), LINEITEM_TABLESCAN_DOQS)));
}
use of org.intellij.lang.annotations.Language in project presto by prestodb.
the class TestReorderWindows method testNonMergeableABAReordersToAABAllOptimizers.
@Test
public void testNonMergeableABAReordersToAABAllOptimizers() {
@Language("SQL") String sql = "select " + "sum(quantity) over(PARTITION BY suppkey ORDER BY orderkey ASC NULLS LAST) sum_quantity_A, " + "avg(discount) over(PARTITION BY partkey ORDER BY receiptdate ASC NULLS LAST) avg_discount_B, " + "min(tax) over(PARTITION BY suppkey ORDER BY shipdate ASC NULLS LAST) min_tax_A " + "from lineitem";
PlanMatchPattern pattern = anyTree(window(windowMatcherBuilder -> windowMatcherBuilder.specification(windowAp).addFunction(functionCall("min", commonFrame, ImmutableList.of(TAX_ALIAS))), project(window(windowMatcherBuilder -> windowMatcherBuilder.specification(windowA).addFunction(functionCall("sum", commonFrame, ImmutableList.of(QUANTITY_ALIAS))), project(window(windowMatcherBuilder -> windowMatcherBuilder.specification(windowB).addFunction(functionCall("avg", commonFrame, ImmutableList.of(DISCOUNT_ALIAS))), anyTree(LINEITEM_TABLESCAN_DOQPRSST)))))));
assertPlan(sql, pattern);
}
use of org.intellij.lang.annotations.Language in project presto by prestodb.
the class TestRaptorIntegrationSmokeTest method testTableStatsSystemTable.
@SuppressWarnings("OverlyStrongTypeCast")
@Test
public void testTableStatsSystemTable() {
// basic sanity tests
assertQuery("" + "SELECT table_schema, table_name, sum(row_count)\n" + "FROM system.table_stats\n" + "WHERE table_schema = 'tpch'\n" + " AND table_name IN ('orders', 'lineitem')\n" + "GROUP BY 1, 2", "" + "SELECT 'tpch', 'orders', (SELECT count(*) FROM orders)\n" + "UNION ALL\n" + "SELECT 'tpch', 'lineitem', (SELECT count(*) FROM lineitem)");
assertQuery("" + "SELECT\n" + " bool_and(row_count >= shard_count)\n" + ", bool_and(update_time >= create_time)\n" + ", bool_and(table_version >= 1)\n" + "FROM system.table_stats\n" + "WHERE row_count > 0", "SELECT true, true, true");
// create empty table
assertUpdate("CREATE TABLE test_table_stats (x bigint)");
@Language("SQL") String sql = "" + "SELECT create_time, update_time, table_version," + " shard_count, row_count, uncompressed_size\n" + "FROM system.table_stats\n" + "WHERE table_schema = 'tpch'\n" + " AND table_name = 'test_table_stats'";
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 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);
// uncompressed_size
long size2 = (long) row.getField(5);
assertGreaterThan(size2, size1);
// delete
assertUpdate("DELETE FROM test_table_stats 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);
// uncompressed_Size
long size3 = (long) row.getField(5);
assertLessThan(size3, size2);
// add column
assertUpdate("ALTER TABLE test_table_stats 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");
}
Aggregations