Search in sources :

Example 1 with AGGREGATION_IF_TO_FILTER_REWRITE_STRATEGY

use of com.facebook.presto.SystemSessionProperties.AGGREGATION_IF_TO_FILTER_REWRITE_STRATEGY 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)

Aggregations

AGGREGATION_IF_TO_FILTER_REWRITE_STRATEGY (com.facebook.presto.SystemSessionProperties.AGGREGATION_IF_TO_FILTER_REWRITE_STRATEGY)1 ArrayType (com.facebook.presto.common.type.ArrayType)1 BIGINT (com.facebook.presto.common.type.BigintType.BIGINT)1 BooleanType (com.facebook.presto.common.type.BooleanType)1 DOUBLE (com.facebook.presto.common.type.DoubleType.DOUBLE)1 VARCHAR (com.facebook.presto.common.type.VarcharType.VARCHAR)1 AggregationNode (com.facebook.presto.spi.plan.AggregationNode)1 PlanNode (com.facebook.presto.spi.plan.PlanNode)1 VariableReferenceExpression (com.facebook.presto.spi.relation.VariableReferenceExpression)1 Symbol (com.facebook.presto.sql.planner.Symbol)1 ExpressionMatcher (com.facebook.presto.sql.planner.assertions.ExpressionMatcher)1 PlanMatchPattern.aggregation (com.facebook.presto.sql.planner.assertions.PlanMatchPattern.aggregation)1 PlanMatchPattern.expression (com.facebook.presto.sql.planner.assertions.PlanMatchPattern.expression)1 PlanMatchPattern.filter (com.facebook.presto.sql.planner.assertions.PlanMatchPattern.filter)1 PlanMatchPattern.functionCall (com.facebook.presto.sql.planner.assertions.PlanMatchPattern.functionCall)1 PlanMatchPattern.globalAggregation (com.facebook.presto.sql.planner.assertions.PlanMatchPattern.globalAggregation)1 PlanMatchPattern.project (com.facebook.presto.sql.planner.assertions.PlanMatchPattern.project)1 PlanMatchPattern.values (com.facebook.presto.sql.planner.assertions.PlanMatchPattern.values)1 BaseRuleTest (com.facebook.presto.sql.planner.iterative.rule.test.BaseRuleTest)1 PlanBuilder (com.facebook.presto.sql.planner.iterative.rule.test.PlanBuilder)1