Search in sources :

Example 1 with ExpectedValueProvider

use of io.prestosql.sql.planner.assertions.ExpectedValueProvider in project hetu-core by openlookeng.

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

Example 2 with ExpectedValueProvider

use of io.prestosql.sql.planner.assertions.ExpectedValueProvider in project hetu-core by openlookeng.

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(io.prestosql.sql.planner.assertions.ExpectedValueProvider) Language(org.intellij.lang.annotations.Language) Optional(java.util.Optional) ImmutableList(com.google.common.collect.ImmutableList) PlanMatchPattern(io.prestosql.sql.planner.assertions.PlanMatchPattern) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) Test(org.testng.annotations.Test) BasePlanTest(io.prestosql.sql.planner.assertions.BasePlanTest)

Example 3 with ExpectedValueProvider

use of io.prestosql.sql.planner.assertions.ExpectedValueProvider in project hetu-core by openlookeng.

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.symbol("a")), Optional.empty()), ImmutableMap.of(p.symbol("avg_1", DOUBLE), newWindowNodeFunction(ImmutableList.of(p.variable("a")))), p.window(new WindowNode.Specification(ImmutableList.of(p.symbol("a"), p.symbol("b")), Optional.empty()), ImmutableMap.of(p.symbol("avg_2", DOUBLE), newWindowNodeFunction(ImmutableList.of(p.variable("b")))), p.values(p.symbol("a"), p.symbol("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(io.prestosql.sql.tree.Window) BaseRuleTest(io.prestosql.sql.planner.iterative.rule.test.BaseRuleTest) TypeSignatureProvider.fromTypes(io.prestosql.sql.analyzer.TypeSignatureProvider.fromTypes) ImmutableMap(com.google.common.collect.ImmutableMap) UNBOUNDED_PRECEDING(io.prestosql.spi.sql.expression.Types.FrameBoundType.UNBOUNDED_PRECEDING) MetadataManager.createTestMetadataManager(io.prestosql.metadata.MetadataManager.createTestMetadataManager) PlanMatchPattern.specification(io.prestosql.sql.planner.assertions.PlanMatchPattern.specification) Test(org.testng.annotations.Test) Types(io.prestosql.spi.sql.expression.Types) FunctionHandle(io.prestosql.spi.function.FunctionHandle) List(java.util.List) PlanMatchPattern.values(io.prestosql.sql.planner.assertions.PlanMatchPattern.values) ImmutableList(com.google.common.collect.ImmutableList) Expressions.call(io.prestosql.sql.relational.Expressions.call) SymbolReference(io.prestosql.sql.tree.SymbolReference) WindowNode(io.prestosql.spi.plan.WindowNode) PlanMatchPattern.functionCall(io.prestosql.sql.planner.assertions.PlanMatchPattern.functionCall) RowExpression(io.prestosql.spi.relation.RowExpression) ExpectedValueProvider(io.prestosql.sql.planner.assertions.ExpectedValueProvider) Window(io.prestosql.sql.tree.Window) Optional(java.util.Optional) DOUBLE(io.prestosql.spi.type.DoubleType.DOUBLE) BIGINT(io.prestosql.spi.type.BigintType.BIGINT) PlanMatchPattern.window(io.prestosql.sql.planner.assertions.PlanMatchPattern.window) CURRENT_ROW(io.prestosql.spi.sql.expression.Types.FrameBoundType.CURRENT_ROW) WindowNode(io.prestosql.spi.plan.WindowNode) SymbolReference(io.prestosql.sql.tree.SymbolReference) BaseRuleTest(io.prestosql.sql.planner.iterative.rule.test.BaseRuleTest) Test(org.testng.annotations.Test)

Aggregations

ImmutableList (com.google.common.collect.ImmutableList)3 ExpectedValueProvider (io.prestosql.sql.planner.assertions.ExpectedValueProvider)3 List (java.util.List)3 Optional (java.util.Optional)3 Test (org.testng.annotations.Test)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 WindowNode (io.prestosql.spi.plan.WindowNode)2 BasePlanTest (io.prestosql.sql.planner.assertions.BasePlanTest)2 PlanMatchPattern (io.prestosql.sql.planner.assertions.PlanMatchPattern)2 PlanMatchPattern.functionCall (io.prestosql.sql.planner.assertions.PlanMatchPattern.functionCall)2 PlanMatchPattern.specification (io.prestosql.sql.planner.assertions.PlanMatchPattern.specification)2 PlanMatchPattern.window (io.prestosql.sql.planner.assertions.PlanMatchPattern.window)2 Language (org.intellij.lang.annotations.Language)2 ImmutableSet (com.google.common.collect.ImmutableSet)1 MetadataManager.createTestMetadataManager (io.prestosql.metadata.MetadataManager.createTestMetadataManager)1 SortOrder (io.prestosql.spi.block.SortOrder)1 FunctionHandle (io.prestosql.spi.function.FunctionHandle)1 JoinNode (io.prestosql.spi.plan.JoinNode)1 RowExpression (io.prestosql.spi.relation.RowExpression)1 Types (io.prestosql.spi.sql.expression.Types)1