Search in sources :

Example 1 with ExpectedValueProvider

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);
}
Also used : ExpectedValueProvider(com.facebook.presto.sql.planner.assertions.ExpectedValueProvider) Language(org.intellij.lang.annotations.Language) Optional(java.util.Optional) ImmutableList(com.google.common.collect.ImmutableList) PlanMatchPattern(com.facebook.presto.sql.planner.assertions.PlanMatchPattern) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) BasePlanTest(com.facebook.presto.sql.planner.assertions.BasePlanTest) Test(org.testng.annotations.Test)

Example 2 with ExpectedValueProvider

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)))))));
}
Also used : TranslateExpressions(com.facebook.presto.sql.planner.iterative.rule.TranslateExpressions) PlanMatchPattern.specification(com.facebook.presto.sql.planner.assertions.PlanMatchPattern.specification) PlanMatchPattern.filter(com.facebook.presto.sql.planner.assertions.PlanMatchPattern.filter) BasePlanTest(com.facebook.presto.sql.planner.assertions.BasePlanTest) FrameBound(com.facebook.presto.sql.tree.FrameBound) PlanMatchPattern.anyTree(com.facebook.presto.sql.planner.assertions.PlanMatchPattern.anyTree) IterativeOptimizer(com.facebook.presto.sql.planner.iterative.IterativeOptimizer) PlanMatchPattern.anyNot(com.facebook.presto.sql.planner.assertions.PlanMatchPattern.anyNot) Test(org.testng.annotations.Test) RemoveRedundantIdentityProjections(com.facebook.presto.sql.planner.iterative.rule.RemoveRedundantIdentityProjections) PlanMatchPattern.window(com.facebook.presto.sql.planner.assertions.PlanMatchPattern.window) PlanMatchPattern(com.facebook.presto.sql.planner.assertions.PlanMatchPattern) WindowFrame(com.facebook.presto.sql.tree.WindowFrame) PlanMatchPattern.join(com.facebook.presto.sql.planner.assertions.PlanMatchPattern.join) ImmutableList(com.google.common.collect.ImmutableList) Map(java.util.Map) RuleStatsRecorder(com.facebook.presto.sql.planner.RuleStatsRecorder) PlanMatchPattern.any(com.facebook.presto.sql.planner.assertions.PlanMatchPattern.any) PlanMatchPattern.expression(com.facebook.presto.sql.planner.assertions.PlanMatchPattern.expression) PlanMatchPattern.functionCall(com.facebook.presto.sql.planner.assertions.PlanMatchPattern.functionCall) JoinNode(com.facebook.presto.sql.planner.plan.JoinNode) ImmutableSet(com.google.common.collect.ImmutableSet) PlanMatchPattern.project(com.facebook.presto.sql.planner.assertions.PlanMatchPattern.project) SortOrder(com.facebook.presto.common.block.SortOrder) WindowNode(com.facebook.presto.sql.planner.plan.WindowNode) ImmutableMap(com.google.common.collect.ImmutableMap) Language(org.intellij.lang.annotations.Language) Rule(com.facebook.presto.sql.planner.iterative.Rule) PlanMatchPattern.tableScan(com.facebook.presto.sql.planner.assertions.PlanMatchPattern.tableScan) SqlParser(com.facebook.presto.sql.parser.SqlParser) List(java.util.List) Optional(java.util.Optional) ExpectedValueProvider(com.facebook.presto.sql.planner.assertions.ExpectedValueProvider) GatherAndMergeWindows(com.facebook.presto.sql.planner.iterative.rule.GatherAndMergeWindows) Language(org.intellij.lang.annotations.Language) PlanMatchPattern(com.facebook.presto.sql.planner.assertions.PlanMatchPattern) ImmutableMap(com.google.common.collect.ImmutableMap) BasePlanTest(com.facebook.presto.sql.planner.assertions.BasePlanTest) Test(org.testng.annotations.Test)

Example 3 with ExpectedValueProvider

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);
}
Also used : ExpectedValueProvider(com.facebook.presto.sql.planner.assertions.ExpectedValueProvider) Language(org.intellij.lang.annotations.Language) Optional(java.util.Optional) ImmutableList(com.google.common.collect.ImmutableList) PlanMatchPattern(com.facebook.presto.sql.planner.assertions.PlanMatchPattern) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) Test(org.testng.annotations.Test)

Example 4 with ExpectedValueProvider

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");
    }
}
Also used : ExpectedValueProvider(com.facebook.presto.sql.planner.assertions.ExpectedValueProvider) Optional(java.util.Optional) PlanMatchPattern(com.facebook.presto.sql.planner.assertions.PlanMatchPattern) QueryRunner(com.facebook.presto.testing.QueryRunner) DistributedQueryRunner(com.facebook.presto.tests.DistributedQueryRunner) Session(com.facebook.presto.Session) Test(org.testng.annotations.Test)

Example 5 with ExpectedValueProvider

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)))));
}
Also used : Window(com.facebook.presto.sql.tree.Window) PlanMatchPattern.specification(com.facebook.presto.sql.planner.assertions.PlanMatchPattern.specification) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) Test(org.testng.annotations.Test) TypeSignatureProvider.fromTypes(com.facebook.presto.sql.analyzer.TypeSignatureProvider.fromTypes) Expressions.call(com.facebook.presto.sql.relational.Expressions.call) PlanMatchPattern.window(com.facebook.presto.sql.planner.assertions.PlanMatchPattern.window) RANGE(com.facebook.presto.sql.planner.plan.WindowNode.Frame.WindowType.RANGE) ImmutableList(com.google.common.collect.ImmutableList) Symbol(com.facebook.presto.sql.planner.Symbol) UNBOUNDED_PRECEDING(com.facebook.presto.sql.planner.plan.WindowNode.Frame.BoundType.UNBOUNDED_PRECEDING) PlanMatchPattern.functionCall(com.facebook.presto.sql.planner.assertions.PlanMatchPattern.functionCall) BIGINT(com.facebook.presto.common.type.BigintType.BIGINT) SymbolReference(com.facebook.presto.sql.tree.SymbolReference) CURRENT_ROW(com.facebook.presto.sql.planner.plan.WindowNode.Frame.BoundType.CURRENT_ROW) WindowNode(com.facebook.presto.sql.planner.plan.WindowNode) ImmutableMap(com.google.common.collect.ImmutableMap) DOUBLE(com.facebook.presto.common.type.DoubleType.DOUBLE) Collectors(java.util.stream.Collectors) List(java.util.List) BaseRuleTest(com.facebook.presto.sql.planner.iterative.rule.test.BaseRuleTest) Window(com.facebook.presto.sql.tree.Window) MetadataManager.createTestMetadataManager(com.facebook.presto.metadata.MetadataManager.createTestMetadataManager) FunctionHandle(com.facebook.presto.spi.function.FunctionHandle) Optional(java.util.Optional) PlanMatchPattern.values(com.facebook.presto.sql.planner.assertions.PlanMatchPattern.values) ExpectedValueProvider(com.facebook.presto.sql.planner.assertions.ExpectedValueProvider) WindowNode(com.facebook.presto.sql.planner.plan.WindowNode) SymbolReference(com.facebook.presto.sql.tree.SymbolReference) Symbol(com.facebook.presto.sql.planner.Symbol) Test(org.testng.annotations.Test) BaseRuleTest(com.facebook.presto.sql.planner.iterative.rule.test.BaseRuleTest)

Aggregations

ExpectedValueProvider (com.facebook.presto.sql.planner.assertions.ExpectedValueProvider)6 Test (org.testng.annotations.Test)6 PlanMatchPattern (com.facebook.presto.sql.planner.assertions.PlanMatchPattern)5 Optional (java.util.Optional)5 ImmutableList (com.google.common.collect.ImmutableList)4 List (java.util.List)4 Language (org.intellij.lang.annotations.Language)3 VariableReferenceExpression (com.facebook.presto.spi.relation.VariableReferenceExpression)2 BasePlanTest (com.facebook.presto.sql.planner.assertions.BasePlanTest)2 PlanMatchPattern.functionCall (com.facebook.presto.sql.planner.assertions.PlanMatchPattern.functionCall)2 PlanMatchPattern.specification (com.facebook.presto.sql.planner.assertions.PlanMatchPattern.specification)2 PlanMatchPattern.window (com.facebook.presto.sql.planner.assertions.PlanMatchPattern.window)2 WindowNode (com.facebook.presto.sql.planner.plan.WindowNode)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 Session (com.facebook.presto.Session)1 SortOrder (com.facebook.presto.common.block.SortOrder)1 BIGINT (com.facebook.presto.common.type.BigintType.BIGINT)1 DOUBLE (com.facebook.presto.common.type.DoubleType.DOUBLE)1 MetadataManager.createTestMetadataManager (com.facebook.presto.metadata.MetadataManager.createTestMetadataManager)1 FunctionHandle (com.facebook.presto.spi.function.FunctionHandle)1