Search in sources :

Example 1 with ExpressionMatcher

use of com.facebook.presto.sql.planner.assertions.ExpressionMatcher in project presto by prestodb.

the class TestRewriteAggregationIfToFilter method testRewriteStrategies.

@Test
public void testRewriteStrategies() {
    Function<PlanBuilder, PlanNode> planProvider = p -> {
        VariableReferenceExpression a = p.variable("a");
        VariableReferenceExpression column0 = p.variable("column0", BIGINT);
        return p.aggregation(ap -> ap.globalGrouping().step(AggregationNode.Step.FINAL).addAggregation(p.variable("expr0"), p.rowExpression("SUM(a)")).source(p.project(assignment(a, p.rowExpression("IF(column0 > 1, column0)")), p.values(column0))));
    };
    tester().assertThat(new RewriteAggregationIfToFilter(getFunctionManager())).setSystemProperty(AGGREGATION_IF_TO_FILTER_REWRITE_STRATEGY, "disabled").on(planProvider).doesNotFire();
    tester().assertThat(new RewriteAggregationIfToFilter(getFunctionManager())).setSystemProperty(AGGREGATION_IF_TO_FILTER_REWRITE_STRATEGY, "filter_with_if").on(planProvider).matches(aggregation(globalAggregation(), ImmutableMap.of(Optional.of("expr0"), functionCall("sum", ImmutableList.of("a"))), ImmutableMap.of(new Symbol("expr0"), new Symbol("greater_than")), Optional.empty(), AggregationNode.Step.FINAL, filter("greater_than", project(new ImmutableMap.Builder<String, ExpressionMatcher>().put("a", expression("IF(column0 > 1, column0)")).put("greater_than", expression("column0 > 1")).build(), values("column0")))));
    tester().assertThat(new RewriteAggregationIfToFilter(getFunctionManager())).setSystemProperty(AGGREGATION_IF_TO_FILTER_REWRITE_STRATEGY, "unwrap_if_safe").on(planProvider).matches(aggregation(globalAggregation(), ImmutableMap.of(Optional.of("expr0"), functionCall("sum", ImmutableList.of("a"))), ImmutableMap.of(new Symbol("expr0"), new Symbol("greater_than")), Optional.empty(), AggregationNode.Step.FINAL, filter("greater_than", project(new ImmutableMap.Builder<String, ExpressionMatcher>().put("a", expression("IF(column0 > 1, column0)")).put("greater_than", expression("column0 > 1")).build(), values("column0")))));
    tester().assertThat(new RewriteAggregationIfToFilter(getFunctionManager())).setSystemProperty(AGGREGATION_IF_TO_FILTER_REWRITE_STRATEGY, "unwrap_if").on(planProvider).matches(aggregation(globalAggregation(), ImmutableMap.of(Optional.of("expr0"), functionCall("sum", ImmutableList.of("column0_0"))), ImmutableMap.of(new Symbol("expr0"), new Symbol("greater_than")), Optional.empty(), AggregationNode.Step.FINAL, filter("greater_than", project(new ImmutableMap.Builder<String, ExpressionMatcher>().put("a", expression("IF(column0 > 1, column0)")).put("greater_than", expression("column0 > 1")).put("column0_0", expression("column0")).build(), values("column0")))));
}
Also used : AggregationNode(com.facebook.presto.spi.plan.AggregationNode) PlanMatchPattern.filter(com.facebook.presto.sql.planner.assertions.PlanMatchPattern.filter) BooleanType(com.facebook.presto.common.type.BooleanType) VARCHAR(com.facebook.presto.common.type.VarcharType.VARCHAR) AGGREGATION_IF_TO_FILTER_REWRITE_STRATEGY(com.facebook.presto.SystemSessionProperties.AGGREGATION_IF_TO_FILTER_REWRITE_STRATEGY) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) Test(org.testng.annotations.Test) Function(java.util.function.Function) ImmutableList(com.google.common.collect.ImmutableList) Symbol(com.facebook.presto.sql.planner.Symbol) PlanBuilder.assignment(com.facebook.presto.sql.planner.iterative.rule.test.PlanBuilder.assignment) PlanBuilder(com.facebook.presto.sql.planner.iterative.rule.test.PlanBuilder) ArrayType(com.facebook.presto.common.type.ArrayType) PlanMatchPattern.expression(com.facebook.presto.sql.planner.assertions.PlanMatchPattern.expression) PlanMatchPattern.functionCall(com.facebook.presto.sql.planner.assertions.PlanMatchPattern.functionCall) PlanMatchPattern.aggregation(com.facebook.presto.sql.planner.assertions.PlanMatchPattern.aggregation) BIGINT(com.facebook.presto.common.type.BigintType.BIGINT) PlanMatchPattern.globalAggregation(com.facebook.presto.sql.planner.assertions.PlanMatchPattern.globalAggregation) PlanMatchPattern.project(com.facebook.presto.sql.planner.assertions.PlanMatchPattern.project) ImmutableMap(com.google.common.collect.ImmutableMap) DOUBLE(com.facebook.presto.common.type.DoubleType.DOUBLE) PlanNode(com.facebook.presto.spi.plan.PlanNode) BaseRuleTest(com.facebook.presto.sql.planner.iterative.rule.test.BaseRuleTest) ExpressionMatcher(com.facebook.presto.sql.planner.assertions.ExpressionMatcher) Optional(java.util.Optional) PlanMatchPattern.values(com.facebook.presto.sql.planner.assertions.PlanMatchPattern.values) ExpressionMatcher(com.facebook.presto.sql.planner.assertions.ExpressionMatcher) PlanNode(com.facebook.presto.spi.plan.PlanNode) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) Symbol(com.facebook.presto.sql.planner.Symbol) PlanBuilder(com.facebook.presto.sql.planner.iterative.rule.test.PlanBuilder) PlanBuilder(com.facebook.presto.sql.planner.iterative.rule.test.PlanBuilder) ImmutableMap(com.google.common.collect.ImmutableMap) Test(org.testng.annotations.Test) BaseRuleTest(com.facebook.presto.sql.planner.iterative.rule.test.BaseRuleTest)

Example 2 with ExpressionMatcher

use of com.facebook.presto.sql.planner.assertions.ExpressionMatcher in project presto by prestodb.

the class TestLogicalPlanner method testOffset.

@Test
public void testOffset() {
    Session enableOffset = Session.builder(this.getQueryRunner().getDefaultSession()).setSystemProperty(OFFSET_CLAUSE_ENABLED, "true").build();
    assertPlanWithSession("SELECT name FROM nation OFFSET 2 ROWS", enableOffset, true, any(strictProject(ImmutableMap.of("name", new ExpressionMatcher("name")), filter("(row_num > BIGINT '2')", rowNumber(pattern -> pattern.partitionBy(ImmutableList.of()), any(tableScan("nation", ImmutableMap.of("NAME", "name")))).withAlias("row_num", new RowNumberSymbolMatcher())))));
    assertPlanWithSession("SELECT name FROM nation ORDER BY regionkey OFFSET 2 ROWS", enableOffset, true, any(strictProject(ImmutableMap.of("name", new ExpressionMatcher("name")), filter("row_num > BIGINT '2'", rowNumber(pattern -> pattern.partitionBy(ImmutableList.of()), anyTree(sort(ImmutableList.of(sort("regionkey", ASCENDING, LAST)), any(tableScan("nation", ImmutableMap.of("name", "name", "regionkey", "regionkey")))))).withAlias("row_num", new RowNumberSymbolMatcher())))));
}
Also used : ExpressionMatcher(com.facebook.presto.sql.planner.assertions.ExpressionMatcher) FORCE_SINGLE_NODE_OUTPUT(com.facebook.presto.SystemSessionProperties.FORCE_SINGLE_NODE_OUTPUT) PlanMatchPattern.filter(com.facebook.presto.sql.planner.assertions.PlanMatchPattern.filter) PlanMatchPattern.anyTree(com.facebook.presto.sql.planner.assertions.PlanMatchPattern.anyTree) REPLICATE(com.facebook.presto.sql.planner.plan.ExchangeNode.Type.REPLICATE) PlanMatchPattern.anyNot(com.facebook.presto.sql.planner.assertions.PlanMatchPattern.anyNot) Test(org.testng.annotations.Test) DESCENDING(com.facebook.presto.sql.tree.SortItem.Ordering.DESCENDING) ValuesNode(com.facebook.presto.spi.plan.ValuesNode) PlanMatchPattern(com.facebook.presto.sql.planner.assertions.PlanMatchPattern) PlanMatchPattern.join(com.facebook.presto.sql.planner.assertions.PlanMatchPattern.join) ASC_NULLS_LAST(com.facebook.presto.common.block.SortOrder.ASC_NULLS_LAST) IndexJoinNode(com.facebook.presto.sql.planner.plan.IndexJoinNode) PlanMatchPattern.expression(com.facebook.presto.sql.planner.assertions.PlanMatchPattern.expression) Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) Assert.assertFalse(org.testng.Assert.assertFalse) PARTIAL(com.facebook.presto.spi.plan.AggregationNode.Step.PARTIAL) LateralJoinNode(com.facebook.presto.sql.planner.plan.LateralJoinNode) SUBQUERY_MULTIPLE_ROWS(com.facebook.presto.spi.StandardErrorCode.SUBQUERY_MULTIPLE_ROWS) PlanMatchPattern.semiJoin(com.facebook.presto.sql.planner.assertions.PlanMatchPattern.semiJoin) LAST(com.facebook.presto.sql.tree.SortItem.NullOrdering.LAST) PlanMatchPattern.apply(com.facebook.presto.sql.planner.assertions.PlanMatchPattern.apply) PlanMatchPattern.rowNumber(com.facebook.presto.sql.planner.assertions.PlanMatchPattern.rowNumber) JOIN_DISTRIBUTION_TYPE(com.facebook.presto.SystemSessionProperties.JOIN_DISTRIBUTION_TYPE) TASK_CONCURRENCY(com.facebook.presto.SystemSessionProperties.TASK_CONCURRENCY) REPLICATED(com.facebook.presto.sql.planner.plan.JoinNode.DistributionType.REPLICATED) OPTIMIZE_NULLS_IN_JOINS(com.facebook.presto.SystemSessionProperties.OPTIMIZE_NULLS_IN_JOINS) LongLiteral(com.facebook.presto.sql.tree.LongLiteral) ApplyNode(com.facebook.presto.sql.planner.plan.ApplyNode) PlanMatchPattern.strictProject(com.facebook.presto.sql.planner.assertions.PlanMatchPattern.strictProject) PlanMatchPattern.node(com.facebook.presto.sql.planner.assertions.PlanMatchPattern.node) PlanMatchPattern.specification(com.facebook.presto.sql.planner.assertions.PlanMatchPattern.specification) PlanOptimizer(com.facebook.presto.sql.planner.optimizations.PlanOptimizer) GATHER(com.facebook.presto.sql.planner.plan.ExchangeNode.Type.GATHER) OFFSET_CLAUSE_ENABLED(com.facebook.presto.SystemSessionProperties.OFFSET_CLAUSE_ENABLED) DistinctLimitNode(com.facebook.presto.spi.plan.DistinctLimitNode) AddLocalExchanges(com.facebook.presto.sql.planner.optimizations.AddLocalExchanges) ASCENDING(com.facebook.presto.sql.tree.SortItem.Ordering.ASCENDING) PlanMatchPattern.functionCall(com.facebook.presto.sql.planner.assertions.PlanMatchPattern.functionCall) BIGINT(com.facebook.presto.common.type.BigintType.BIGINT) Session(com.facebook.presto.Session) ELIMINATE_CROSS_JOINS(com.facebook.presto.sql.analyzer.FeaturesConfig.JoinReorderingStrategy.ELIMINATE_CROSS_JOINS) PlanMatchPattern.exchange(com.facebook.presto.sql.planner.assertions.PlanMatchPattern.exchange) RIGHT(com.facebook.presto.sql.planner.plan.JoinNode.Type.RIGHT) OPTIMIZE_JOINS_WITH_EMPTY_SOURCES(com.facebook.presto.SystemSessionProperties.OPTIMIZE_JOINS_WITH_EMPTY_SOURCES) TableScanNode(com.facebook.presto.spi.plan.TableScanNode) SemiJoinNode(com.facebook.presto.sql.planner.plan.SemiJoinNode) AggregationNode(com.facebook.presto.spi.plan.AggregationNode) QueryTemplate.queryTemplate(com.facebook.presto.tests.QueryTemplate.queryTemplate) BasePlanTest(com.facebook.presto.sql.planner.assertions.BasePlanTest) PlanMatchPattern.output(com.facebook.presto.sql.planner.assertions.PlanMatchPattern.output) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) MorePredicates.isInstanceOfAny(com.facebook.presto.util.MorePredicates.isInstanceOfAny) PlanMatchPattern.window(com.facebook.presto.sql.planner.assertions.PlanMatchPattern.window) JoinDistributionType(com.facebook.presto.sql.analyzer.FeaturesConfig.JoinDistributionType) PlanMatchPattern.aggregation(com.facebook.presto.sql.planner.assertions.PlanMatchPattern.aggregation) PlanMatchPattern.sort(com.facebook.presto.sql.planner.assertions.PlanMatchPattern.sort) OPTIMIZE_HASH_GENERATION(com.facebook.presto.SystemSessionProperties.OPTIMIZE_HASH_GENERATION) PlanMatchPattern.project(com.facebook.presto.sql.planner.assertions.PlanMatchPattern.project) ImmutableMap(com.google.common.collect.ImmutableMap) PlanMatchPattern.topN(com.facebook.presto.sql.planner.assertions.PlanMatchPattern.topN) Predicate(java.util.function.Predicate) Domain.singleValue(com.facebook.presto.common.predicate.Domain.singleValue) PlanMatchPattern.constrainedTableScan(com.facebook.presto.sql.planner.assertions.PlanMatchPattern.constrainedTableScan) String.format(java.lang.String.format) REMOTE_STREAMING(com.facebook.presto.sql.planner.plan.ExchangeNode.Scope.REMOTE_STREAMING) List(java.util.List) RowNumberSymbolMatcher(com.facebook.presto.sql.planner.assertions.RowNumberSymbolMatcher) ExpressionMatcher(com.facebook.presto.sql.planner.assertions.ExpressionMatcher) MorePredicates(com.facebook.presto.util.MorePredicates) JOIN_REORDERING_STRATEGY(com.facebook.presto.SystemSessionProperties.JOIN_REORDERING_STRATEGY) PlanMatchPattern.markDistinct(com.facebook.presto.sql.planner.assertions.PlanMatchPattern.markDistinct) Optional(java.util.Optional) INNER(com.facebook.presto.sql.planner.plan.JoinNode.Type.INNER) PlanMatchPattern.assignUniqueId(com.facebook.presto.sql.planner.assertions.PlanMatchPattern.assignUniqueId) Assert.assertEquals(org.testng.Assert.assertEquals) SINGLE(com.facebook.presto.spi.plan.AggregationNode.Step.SINGLE) VarcharType.createVarcharType(com.facebook.presto.common.type.VarcharType.createVarcharType) DISTRIBUTED_SORT(com.facebook.presto.SystemSessionProperties.DISTRIBUTED_SORT) ENFORCE_FIXED_DISTRIBUTION_FOR_OUTPUT_OPERATOR(com.facebook.presto.SystemSessionProperties.ENFORCE_FIXED_DISTRIBUTION_FOR_OUTPUT_OPERATOR) PlanMatchPattern.singleGroupingSet(com.facebook.presto.sql.planner.assertions.PlanMatchPattern.singleGroupingSet) FilterNode(com.facebook.presto.spi.plan.FilterNode) QueryTemplate(com.facebook.presto.tests.QueryTemplate) ImmutableList(com.google.common.collect.ImmutableList) LOCAL(com.facebook.presto.sql.planner.plan.ExchangeNode.Scope.LOCAL) REPARTITION(com.facebook.presto.sql.planner.plan.ExchangeNode.Type.REPARTITION) PlanMatchPattern.equiJoinClause(com.facebook.presto.sql.planner.assertions.PlanMatchPattern.equiJoinClause) FINAL(com.facebook.presto.spi.plan.AggregationNode.Step.FINAL) PlanMatchPattern.topNRowNumber(com.facebook.presto.sql.planner.assertions.PlanMatchPattern.topNRowNumber) PlanMatchPattern.any(com.facebook.presto.sql.planner.assertions.PlanMatchPattern.any) PlanMatchPattern.strictTableScan(com.facebook.presto.sql.planner.assertions.PlanMatchPattern.strictTableScan) PlanNodeSearcher.searchFrom(com.facebook.presto.sql.planner.optimizations.PlanNodeSearcher.searchFrom) JoinNode(com.facebook.presto.sql.planner.plan.JoinNode) PlanMatchPattern.constrainedTableScanWithTableLayout(com.facebook.presto.sql.planner.assertions.PlanMatchPattern.constrainedTableScanWithTableLayout) PlanMatchPattern.limit(com.facebook.presto.sql.planner.assertions.PlanMatchPattern.limit) PlanMatchPattern.tableScan(com.facebook.presto.sql.planner.assertions.PlanMatchPattern.tableScan) Consumer(java.util.function.Consumer) PlanNode(com.facebook.presto.spi.plan.PlanNode) LEFT(com.facebook.presto.sql.planner.plan.JoinNode.Type.LEFT) PlanMatchPattern.values(com.facebook.presto.sql.planner.assertions.PlanMatchPattern.values) PARTITIONED(com.facebook.presto.sql.planner.plan.JoinNode.DistributionType.PARTITIONED) EnforceSingleRowNode(com.facebook.presto.sql.planner.plan.EnforceSingleRowNode) ExchangeNode(com.facebook.presto.sql.planner.plan.ExchangeNode) StatisticsWriterNode(com.facebook.presto.sql.planner.plan.StatisticsWriterNode) RowNumberSymbolMatcher(com.facebook.presto.sql.planner.assertions.RowNumberSymbolMatcher) Session(com.facebook.presto.Session) Test(org.testng.annotations.Test) BasePlanTest(com.facebook.presto.sql.planner.assertions.BasePlanTest)

Aggregations

BIGINT (com.facebook.presto.common.type.BigintType.BIGINT)2 AggregationNode (com.facebook.presto.spi.plan.AggregationNode)2 PlanNode (com.facebook.presto.spi.plan.PlanNode)2 VariableReferenceExpression (com.facebook.presto.spi.relation.VariableReferenceExpression)2 ExpressionMatcher (com.facebook.presto.sql.planner.assertions.ExpressionMatcher)2 PlanMatchPattern.aggregation (com.facebook.presto.sql.planner.assertions.PlanMatchPattern.aggregation)2 Session (com.facebook.presto.Session)1 AGGREGATION_IF_TO_FILTER_REWRITE_STRATEGY (com.facebook.presto.SystemSessionProperties.AGGREGATION_IF_TO_FILTER_REWRITE_STRATEGY)1 DISTRIBUTED_SORT (com.facebook.presto.SystemSessionProperties.DISTRIBUTED_SORT)1 ENFORCE_FIXED_DISTRIBUTION_FOR_OUTPUT_OPERATOR (com.facebook.presto.SystemSessionProperties.ENFORCE_FIXED_DISTRIBUTION_FOR_OUTPUT_OPERATOR)1 FORCE_SINGLE_NODE_OUTPUT (com.facebook.presto.SystemSessionProperties.FORCE_SINGLE_NODE_OUTPUT)1 JOIN_DISTRIBUTION_TYPE (com.facebook.presto.SystemSessionProperties.JOIN_DISTRIBUTION_TYPE)1 JOIN_REORDERING_STRATEGY (com.facebook.presto.SystemSessionProperties.JOIN_REORDERING_STRATEGY)1 OFFSET_CLAUSE_ENABLED (com.facebook.presto.SystemSessionProperties.OFFSET_CLAUSE_ENABLED)1 OPTIMIZE_HASH_GENERATION (com.facebook.presto.SystemSessionProperties.OPTIMIZE_HASH_GENERATION)1 OPTIMIZE_JOINS_WITH_EMPTY_SOURCES (com.facebook.presto.SystemSessionProperties.OPTIMIZE_JOINS_WITH_EMPTY_SOURCES)1 OPTIMIZE_NULLS_IN_JOINS (com.facebook.presto.SystemSessionProperties.OPTIMIZE_NULLS_IN_JOINS)1 TASK_CONCURRENCY (com.facebook.presto.SystemSessionProperties.TASK_CONCURRENCY)1 ASC_NULLS_LAST (com.facebook.presto.common.block.SortOrder.ASC_NULLS_LAST)1 Domain.singleValue (com.facebook.presto.common.predicate.Domain.singleValue)1