Search in sources :

Example 1 with TopNRankingSymbolMatcher

use of io.trino.sql.planner.assertions.TopNRankingSymbolMatcher in project trino by trinodb.

the class TestWindowFilterPushDown method assertFilterAboveWindow.

private void assertFilterAboveWindow(String rankingFunction, RankingType rankingType) {
    @Language("SQL") String sql = format("SELECT * FROM " + "(SELECT %s() OVER (PARTITION BY suppkey ORDER BY orderkey) partition_ranking FROM lineitem) " + "WHERE partition_ranking < 10", rankingFunction);
    assertPlanWithSession(sql, optimizeTopNRanking(true), true, anyTree(anyNot(FilterNode.class, topNRanking(pattern -> pattern.rankingType(rankingType).maxRankingPerPartition(9), anyTree(tableScan("lineitem"))))));
    assertPlanWithSession(sql, optimizeTopNRanking(false), true, anyTree(node(FilterNode.class, anyTree(node(WindowNode.class, anyTree(tableScan("lineitem")))))));
    // remove subplan if predicate on row number symbol can't be satisfied
    assertPlanWithSession(format("SELECT * FROM (SELECT name, %s() OVER(ORDER BY name) FROM nation) t(name, ranking) WHERE ranking < 0", rankingFunction), optimizeTopNRanking(true), true, output(ImmutableList.of("name", "ranking"), values("name", "ranking")));
    // optimize to TopNRanking on the basis of predicate; remove filter because predicate is satisfied
    assertPlanWithSession(format("SELECT * FROM (SELECT name, %s() OVER(ORDER BY name) FROM nation) t(name, ranking) WHERE ranking > 0 AND ranking < 2", rankingFunction), optimizeTopNRanking(true), true, output(ImmutableList.of("name", "ranking"), topNRanking(pattern -> pattern.rankingType(rankingType).maxRankingPerPartition(1).specification(ImmutableList.of(), ImmutableList.of("name"), ImmutableMap.of("name", ASC_NULLS_LAST)), any(tableScan("nation", ImmutableMap.of("name", "name")))).withAlias("ranking", new TopNRankingSymbolMatcher())));
    // optimize to TopNRanking on the basis of predicate; remove filter because predicate is satisfied
    assertPlanWithSession(format("SELECT * FROM (SELECT name, %s() OVER(ORDER BY name) FROM nation) t(name, ranking) WHERE ranking <= 1", rankingFunction), optimizeTopNRanking(true), true, output(ImmutableList.of("name", "ranking"), topNRanking(pattern -> pattern.rankingType(rankingType).maxRankingPerPartition(1).specification(ImmutableList.of(), ImmutableList.of("name"), ImmutableMap.of("name", ASC_NULLS_LAST)), any(tableScan("nation", ImmutableMap.of("name", "name")))).withAlias("ranking", new TopNRankingSymbolMatcher())));
    // optimize to TopNRanking on the basis of predicate; remove filter because predicate is satisfied
    assertPlanWithSession(format("SELECT * FROM (SELECT name, %s() OVER(ORDER BY name) FROM nation) t(name, ranking) WHERE ranking <= 1 AND ranking > -10", rankingFunction), optimizeTopNRanking(true), true, output(ImmutableList.of("name", "ranking"), topNRanking(pattern -> pattern.rankingType(rankingType).maxRankingPerPartition(1).specification(ImmutableList.of(), ImmutableList.of("name"), ImmutableMap.of("name", ASC_NULLS_LAST)), any(tableScan("nation", ImmutableMap.of("name", "name")))).withAlias("ranking", new TopNRankingSymbolMatcher())));
    // optimize to TopNRanking on the basis of predicate; cannot remove filter because predicate is not satisfied
    assertPlanWithSession(format("SELECT * FROM (SELECT name, %s() OVER(ORDER BY name) FROM nation) t(name, ranking) WHERE ranking > 1 AND ranking < 3", rankingFunction), optimizeTopNRanking(true), true, output(ImmutableList.of("name", "ranking"), filter("(ranking > BIGINT '1') AND (ranking < BIGINT '3')", topNRanking(pattern -> pattern.rankingType(rankingType).maxRankingPerPartition(2).specification(ImmutableList.of(), ImmutableList.of("name"), ImmutableMap.of("name", ASC_NULLS_LAST)), any(tableScan("nation", ImmutableMap.of("name", "name")))).withAlias("ranking", new TopNRankingSymbolMatcher()))));
}
Also used : ROW_NUMBER(io.trino.sql.planner.plan.TopNRankingNode.RankingType.ROW_NUMBER) PlanMatchPattern.any(io.trino.sql.planner.assertions.PlanMatchPattern.any) TopNRankingSymbolMatcher(io.trino.sql.planner.assertions.TopNRankingSymbolMatcher) Test(org.testng.annotations.Test) PlanMatchPattern.filter(io.trino.sql.planner.assertions.PlanMatchPattern.filter) FilterNode(io.trino.sql.planner.plan.FilterNode) PlanMatchPattern.limit(io.trino.sql.planner.assertions.PlanMatchPattern.limit) ImmutableList(com.google.common.collect.ImmutableList) BasePlanTest(io.trino.sql.planner.assertions.BasePlanTest) RowNumberSymbolMatcher(io.trino.sql.planner.assertions.RowNumberSymbolMatcher) ImmutableMap(com.google.common.collect.ImmutableMap) Language(org.intellij.lang.annotations.Language) PlanMatchPattern.topNRanking(io.trino.sql.planner.assertions.PlanMatchPattern.topNRanking) RANK(io.trino.sql.planner.plan.TopNRankingNode.RankingType.RANK) PlanMatchPattern.values(io.trino.sql.planner.assertions.PlanMatchPattern.values) String.format(java.lang.String.format) OPTIMIZE_TOP_N_RANKING(io.trino.SystemSessionProperties.OPTIMIZE_TOP_N_RANKING) PlanMatchPattern.node(io.trino.sql.planner.assertions.PlanMatchPattern.node) ASC_NULLS_LAST(io.trino.spi.connector.SortOrder.ASC_NULLS_LAST) PlanMatchPattern.anyTree(io.trino.sql.planner.assertions.PlanMatchPattern.anyTree) PlanMatchPattern.project(io.trino.sql.planner.assertions.PlanMatchPattern.project) RankingType(io.trino.sql.planner.plan.TopNRankingNode.RankingType) Optional(java.util.Optional) WindowNode(io.trino.sql.planner.plan.WindowNode) PlanMatchPattern.output(io.trino.sql.planner.assertions.PlanMatchPattern.output) PlanMatchPattern.rowNumber(io.trino.sql.planner.assertions.PlanMatchPattern.rowNumber) PlanMatchPattern.tableScan(io.trino.sql.planner.assertions.PlanMatchPattern.tableScan) Session(io.trino.Session) PlanMatchPattern.anyNot(io.trino.sql.planner.assertions.PlanMatchPattern.anyNot) TopNRankingSymbolMatcher(io.trino.sql.planner.assertions.TopNRankingSymbolMatcher) Language(org.intellij.lang.annotations.Language)

Example 2 with TopNRankingSymbolMatcher

use of io.trino.sql.planner.assertions.TopNRankingSymbolMatcher in project trino by trinodb.

the class TestPushdownFilterIntoWindow method assertKeepFilter.

private void assertKeepFilter(String rankingFunctionName) {
    ResolvedFunction ranking = tester().getMetadata().resolveFunction(tester().getSession(), QualifiedName.of(rankingFunctionName), fromTypes());
    tester().assertThat(new PushdownFilterIntoWindow(tester().getPlannerContext())).on(p -> {
        Symbol rowNumberSymbol = p.symbol("row_number_1");
        Symbol a = p.symbol("a", BIGINT);
        OrderingScheme orderingScheme = new OrderingScheme(ImmutableList.of(a), ImmutableMap.of(a, SortOrder.ASC_NULLS_FIRST));
        return p.filter(expression("cast(3 as bigint) < row_number_1 and row_number_1 < cast(100 as bigint)"), p.window(new WindowNode.Specification(ImmutableList.of(a), Optional.of(orderingScheme)), ImmutableMap.of(rowNumberSymbol, newWindowNodeFunction(ranking, a)), p.values(p.symbol("a"))));
    }).matches(filter("cast(3 as bigint) < row_number_1 and row_number_1 < cast(100 as bigint)", topNRanking(pattern -> pattern.partial(false).maxRankingPerPartition(99).specification(ImmutableList.of("a"), ImmutableList.of("a"), ImmutableMap.of("a", SortOrder.ASC_NULLS_FIRST)), values("a")).withAlias("row_number_1", new TopNRankingSymbolMatcher())));
    tester().assertThat(new PushdownFilterIntoWindow(tester().getPlannerContext())).on(p -> {
        Symbol rowNumberSymbol = p.symbol("row_number_1");
        Symbol a = p.symbol("a", BIGINT);
        OrderingScheme orderingScheme = new OrderingScheme(ImmutableList.of(a), ImmutableMap.of(a, SortOrder.ASC_NULLS_FIRST));
        return p.filter(expression("row_number_1 < cast(100 as bigint) and a = 1"), p.window(new WindowNode.Specification(ImmutableList.of(a), Optional.of(orderingScheme)), ImmutableMap.of(rowNumberSymbol, newWindowNodeFunction(ranking, a)), p.values(p.symbol("a"))));
    }).matches(filter("a = 1", topNRanking(pattern -> pattern.partial(false).maxRankingPerPartition(99).specification(ImmutableList.of("a"), ImmutableList.of("a"), ImmutableMap.of("a", SortOrder.ASC_NULLS_FIRST)), values("a")).withAlias("row_number_1", new TopNRankingSymbolMatcher())));
}
Also used : Symbol(io.trino.sql.planner.Symbol) ImmutableMap(com.google.common.collect.ImmutableMap) BaseRuleTest(io.trino.sql.planner.iterative.rule.test.BaseRuleTest) PlanMatchPattern.topNRanking(io.trino.sql.planner.assertions.PlanMatchPattern.topNRanking) ResolvedFunction(io.trino.metadata.ResolvedFunction) TopNRankingSymbolMatcher(io.trino.sql.planner.assertions.TopNRankingSymbolMatcher) TypeSignatureProvider.fromTypes(io.trino.sql.analyzer.TypeSignatureProvider.fromTypes) Test(org.testng.annotations.Test) PlanMatchPattern.filter(io.trino.sql.planner.assertions.PlanMatchPattern.filter) PlanMatchPattern.values(io.trino.sql.planner.assertions.PlanMatchPattern.values) OrderingScheme(io.trino.sql.planner.OrderingScheme) DEFAULT_FRAME(io.trino.sql.planner.plan.WindowNode.Frame.DEFAULT_FRAME) SortOrder(io.trino.spi.connector.SortOrder) QualifiedName(io.trino.sql.tree.QualifiedName) ImmutableList(com.google.common.collect.ImmutableList) BIGINT(io.trino.spi.type.BigintType.BIGINT) Optional(java.util.Optional) WindowNode(io.trino.sql.planner.plan.WindowNode) PlanBuilder.expression(io.trino.sql.planner.iterative.rule.test.PlanBuilder.expression) OrderingScheme(io.trino.sql.planner.OrderingScheme) WindowNode(io.trino.sql.planner.plan.WindowNode) TopNRankingSymbolMatcher(io.trino.sql.planner.assertions.TopNRankingSymbolMatcher) ResolvedFunction(io.trino.metadata.ResolvedFunction) Symbol(io.trino.sql.planner.Symbol)

Aggregations

ImmutableList (com.google.common.collect.ImmutableList)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 PlanMatchPattern.filter (io.trino.sql.planner.assertions.PlanMatchPattern.filter)2 PlanMatchPattern.topNRanking (io.trino.sql.planner.assertions.PlanMatchPattern.topNRanking)2 PlanMatchPattern.values (io.trino.sql.planner.assertions.PlanMatchPattern.values)2 TopNRankingSymbolMatcher (io.trino.sql.planner.assertions.TopNRankingSymbolMatcher)2 WindowNode (io.trino.sql.planner.plan.WindowNode)2 Optional (java.util.Optional)2 Test (org.testng.annotations.Test)2 Session (io.trino.Session)1 OPTIMIZE_TOP_N_RANKING (io.trino.SystemSessionProperties.OPTIMIZE_TOP_N_RANKING)1 ResolvedFunction (io.trino.metadata.ResolvedFunction)1 SortOrder (io.trino.spi.connector.SortOrder)1 ASC_NULLS_LAST (io.trino.spi.connector.SortOrder.ASC_NULLS_LAST)1 BIGINT (io.trino.spi.type.BigintType.BIGINT)1 TypeSignatureProvider.fromTypes (io.trino.sql.analyzer.TypeSignatureProvider.fromTypes)1 OrderingScheme (io.trino.sql.planner.OrderingScheme)1 Symbol (io.trino.sql.planner.Symbol)1 BasePlanTest (io.trino.sql.planner.assertions.BasePlanTest)1 PlanMatchPattern.any (io.trino.sql.planner.assertions.PlanMatchPattern.any)1