use of com.facebook.presto.sql.planner.assertions.ExpectedValueProvider 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 com.facebook.presto.sql.planner.assertions.ExpectedValueProvider 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 com.facebook.presto.sql.planner.assertions.ExpectedValueProvider in project presto by prestodb.
the class TestMixedDistinctAggregationOptimizer 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(ImmutableList.of(groupByKeysSecond), aggregationsSecond, ImmutableMap.of(), Optional.empty(), project(aggregation(ImmutableList.of(groupByKeysFirst), aggregationsFirst, ImmutableMap.of(), Optional.empty(), groupingSet(groups.build(), group, anyTree(tableScan))))));
assertUnitPlan(sql, expectedPlanPattern);
}
use of com.facebook.presto.sql.planner.assertions.ExpectedValueProvider in project presto by prestodb.
the class TestHiveLogicalPlanner method testPartialAggregatePushdown.
@Test
public void testPartialAggregatePushdown() {
QueryRunner queryRunner = getQueryRunner();
try {
queryRunner.execute("CREATE TABLE orders_partitioned_parquet WITH (partitioned_by = ARRAY['ds'], format='PARQUET') AS " + "SELECT orderkey, orderpriority, comment, '2019-11-01' as ds FROM orders WHERE orderkey < 1000 " + "UNION ALL " + "SELECT orderkey, orderpriority, comment, '2019-11-02' as ds FROM orders WHERE orderkey < 1000");
Map<Optional<String>, ExpectedValueProvider<FunctionCall>> aggregations = ImmutableMap.of(Optional.of("count"), PlanMatchPattern.functionCall("count", false, ImmutableList.of(anySymbol())));
List<String> groupByKey = ImmutableList.of("count_star");
assertPlan(partialAggregatePushdownEnabled(), "select count(*) from orders_partitioned_parquet", anyTree(aggregation(globalAggregation(), aggregations, ImmutableMap.of(), Optional.empty(), AggregationNode.Step.FINAL, exchange(LOCAL, GATHER, new PlanMatchPattern[] { exchange(REMOTE_STREAMING, GATHER, new PlanMatchPattern[] { tableScan("orders_partitioned_parquet", ImmutableMap.of()) }) }))));
aggregations = ImmutableMap.of(Optional.of("count_1"), PlanMatchPattern.functionCall("count", false, ImmutableList.of(anySymbol())), Optional.of("max"), PlanMatchPattern.functionCall("max", false, ImmutableList.of(anySymbol())), Optional.of("min"), PlanMatchPattern.functionCall("max", false, ImmutableList.of(anySymbol())));
assertPlan(partialAggregatePushdownEnabled(), "select count(orderkey), max(orderpriority), min(ds) from orders_partitioned_parquet", anyTree(new PlanMatchPattern[] { aggregation(globalAggregation(), aggregations, ImmutableMap.of(), Optional.empty(), AggregationNode.Step.FINAL, exchange(LOCAL, GATHER, new PlanMatchPattern[] { exchange(REMOTE_STREAMING, GATHER, new PlanMatchPattern[] { tableScan("orders_partitioned_parquet", ImmutableMap.of("orderkey", ImmutableSet.of(), "orderpriority", ImmutableSet.of(), "ds", ImmutableSet.of())) }) })) }));
// Negative tests
assertPlan(partialAggregatePushdownEnabled(), "select count(orderkey), max(orderpriority), min(ds) from orders_partitioned_parquet where orderkey = 100", anyTree(PlanMatchPattern.tableScan("orders_partitioned_parquet")), plan -> assertNoAggregatedColumns(plan, "orders_partitioned_parquet"));
aggregations = ImmutableMap.of(Optional.of("count_1"), PlanMatchPattern.functionCall("count", false, ImmutableList.of(anySymbol())), Optional.of("arbitrary"), PlanMatchPattern.functionCall("arbitrary", false, ImmutableList.of(anySymbol())), Optional.of("min"), PlanMatchPattern.functionCall("max", false, ImmutableList.of(anySymbol())));
assertPlan(partialAggregatePushdownEnabled(), "select count(orderkey), arbitrary(orderpriority), min(ds) from orders_partitioned_parquet", anyTree(PlanMatchPattern.tableScan("orders_partitioned_parquet")), plan -> assertNoAggregatedColumns(plan, "orders_partitioned_parquet"));
assertPlan(partialAggregatePushdownEnabled(), "select count(orderkey), max(orderpriority), min(ds) from orders_partitioned_parquet where ds = '2019-11-01' and orderkey = 100", anyTree(PlanMatchPattern.tableScan("orders_partitioned_parquet")), plan -> assertNoAggregatedColumns(plan, "orders_partitioned_parquet"));
Session session = Session.builder(getQueryRunner().getDefaultSession()).setCatalogSessionProperty(HIVE_CATALOG, PARTIAL_AGGREGATION_PUSHDOWN_ENABLED, "true").build();
queryRunner.execute("CREATE TABLE variable_length_table(col1 varchar, col2 varchar(100), col3 varbinary) with (format='PARQUET')");
queryRunner.execute("INSERT INTO variable_length_table values ('foo','bar',cast('baz' as varbinary))");
assertPlan(session, "select min(col1) from variable_length_table", anyTree(PlanMatchPattern.tableScan("variable_length_table")), plan -> assertNoAggregatedColumns(plan, "variable_length_table"));
assertPlan(session, "select max(col3) from variable_length_table", anyTree(PlanMatchPattern.tableScan("variable_length_table")), plan -> assertNoAggregatedColumns(plan, "variable_length_table"));
} finally {
queryRunner.execute("DROP TABLE IF EXISTS orders_partitioned_parquet");
queryRunner.execute("DROP TABLE IF EXISTS variable_length_table");
}
}
use of com.facebook.presto.sql.planner.assertions.ExpectedValueProvider in project presto by prestodb.
the class TestSwapAdjacentWindowsBySpecifications method subsetComesFirst.
@Test
public void subsetComesFirst() {
String columnAAlias = "ALIAS_A";
String columnBAlias = "ALIAS_B";
ExpectedValueProvider<WindowNode.Specification> specificationA = specification(ImmutableList.of(columnAAlias), ImmutableList.of(), ImmutableMap.of());
ExpectedValueProvider<WindowNode.Specification> specificationAB = specification(ImmutableList.of(columnAAlias, columnBAlias), ImmutableList.of(), ImmutableMap.of());
Optional<Window> windowAB = Optional.of(new Window(ImmutableList.of(new SymbolReference("a"), new SymbolReference("b")), Optional.empty(), Optional.empty()));
Optional<Window> windowA = Optional.of(new Window(ImmutableList.of(new SymbolReference("a")), Optional.empty(), Optional.empty()));
tester().assertThat(new GatherAndMergeWindows.SwapAdjacentWindowsBySpecifications(0)).on(p -> p.window(new WindowNode.Specification(ImmutableList.of(p.variable("a")), Optional.empty()), ImmutableMap.of(p.variable("avg_1", DOUBLE), newWindowNodeFunction(ImmutableList.of(new Symbol("a")))), p.window(new WindowNode.Specification(ImmutableList.of(p.variable("a"), p.variable("b")), Optional.empty()), ImmutableMap.of(p.variable("avg_2", DOUBLE), newWindowNodeFunction(ImmutableList.of(new Symbol("b")))), p.values(p.variable("a"), p.variable("b"))))).matches(window(windowMatcherBuilder -> windowMatcherBuilder.specification(specificationAB).addFunction(functionCall("avg", Optional.empty(), ImmutableList.of(columnBAlias))), window(windowMatcherBuilder -> windowMatcherBuilder.specification(specificationA).addFunction(functionCall("avg", Optional.empty(), ImmutableList.of(columnAAlias))), values(ImmutableMap.of(columnAAlias, 0, columnBAlias, 1)))));
}
Aggregations