Search in sources :

Example 21 with AggregationNode

use of com.facebook.presto.spi.plan.AggregationNode in project presto by prestodb.

the class TestTranslateExpressions method testTranslateIntermediateAggregationWithLambda.

@Test
public void testTranslateIntermediateAggregationWithLambda() {
    PlanNode result = tester().assertThat(new TranslateExpressions(METADATA, new SqlParser()).aggregationRowExpressionRewriteRule()).on(p -> p.aggregation(builder -> builder.globalGrouping().addAggregation(variable("reduce_agg", INTEGER), new AggregationNode.Aggregation(new CallExpression("reduce_agg", REDUCE_AGG, INTEGER, ImmutableList.of(castToRowExpression(expression("input")), castToRowExpression(expression("(x,y) -> x*y")), castToRowExpression(expression("(a,b) -> a*b")))), Optional.of(castToRowExpression(expression("input > 10"))), Optional.empty(), false, Optional.empty())).source(p.values(p.variable("input", INTEGER))))).get();
    AggregationNode.Aggregation translated = ((AggregationNode) result).getAggregations().get(variable("reduce_agg", INTEGER));
    assertEquals(translated, new AggregationNode.Aggregation(new CallExpression("reduce_agg", REDUCE_AGG, INTEGER, ImmutableList.of(variable("input", INTEGER), new LambdaDefinitionExpression(Optional.empty(), ImmutableList.of(INTEGER, INTEGER), ImmutableList.of("x", "y"), multiply(variable("x", INTEGER), variable("y", INTEGER))), new LambdaDefinitionExpression(Optional.empty(), ImmutableList.of(INTEGER, INTEGER), ImmutableList.of("a", "b"), multiply(variable("a", INTEGER), variable("b", INTEGER))))), Optional.of(greaterThan(variable("input", INTEGER), constant(10L, INTEGER))), Optional.empty(), false, Optional.empty()));
    assertFalse(isUntranslated(translated));
}
Also used : FunctionAndTypeManager(com.facebook.presto.metadata.FunctionAndTypeManager) AggregationNode(com.facebook.presto.spi.plan.AggregationNode) OriginalExpressionUtils(com.facebook.presto.sql.relational.OriginalExpressionUtils) Assert.assertEquals(org.testng.Assert.assertEquals) Test(org.testng.annotations.Test) TypeSignatureProvider.fromTypes(com.facebook.presto.sql.analyzer.TypeSignatureProvider.fromTypes) Expressions.call(com.facebook.presto.sql.relational.Expressions.call) Expressions.constant(com.facebook.presto.sql.relational.Expressions.constant) ImmutableList(com.google.common.collect.ImmutableList) BOOLEAN(com.facebook.presto.common.type.BooleanType.BOOLEAN) FunctionResolution(com.facebook.presto.sql.relational.FunctionResolution) Expressions.variable(com.facebook.presto.sql.relational.Expressions.variable) CallExpression(com.facebook.presto.spi.relation.CallExpression) Assert.assertFalse(org.testng.Assert.assertFalse) RowExpression(com.facebook.presto.spi.relation.RowExpression) LambdaDefinitionExpression(com.facebook.presto.spi.relation.LambdaDefinitionExpression) OperatorType(com.facebook.presto.common.function.OperatorType) FunctionType(com.facebook.presto.common.type.FunctionType) SqlParser(com.facebook.presto.sql.parser.SqlParser) PlanNode(com.facebook.presto.spi.plan.PlanNode) BaseRuleTest(com.facebook.presto.sql.planner.iterative.rule.test.BaseRuleTest) MetadataManager.createTestMetadataManager(com.facebook.presto.metadata.MetadataManager.createTestMetadataManager) INTEGER(com.facebook.presto.common.type.IntegerType.INTEGER) FunctionHandle(com.facebook.presto.spi.function.FunctionHandle) Optional(java.util.Optional) PlanBuilder.expression(com.facebook.presto.sql.planner.iterative.rule.test.PlanBuilder.expression) Metadata(com.facebook.presto.metadata.Metadata) OriginalExpressionUtils.castToRowExpression(com.facebook.presto.sql.relational.OriginalExpressionUtils.castToRowExpression) PlanNode(com.facebook.presto.spi.plan.PlanNode) SqlParser(com.facebook.presto.sql.parser.SqlParser) AggregationNode(com.facebook.presto.spi.plan.AggregationNode) CallExpression(com.facebook.presto.spi.relation.CallExpression) LambdaDefinitionExpression(com.facebook.presto.spi.relation.LambdaDefinitionExpression) Test(org.testng.annotations.Test) BaseRuleTest(com.facebook.presto.sql.planner.iterative.rule.test.BaseRuleTest)

Example 22 with AggregationNode

use of com.facebook.presto.spi.plan.AggregationNode in project presto by prestodb.

the class TestTranslateExpressions method testTranslateAggregationWithLambda.

@Test
public void testTranslateAggregationWithLambda() {
    PlanNode result = tester().assertThat(new TranslateExpressions(METADATA, new SqlParser()).aggregationRowExpressionRewriteRule()).on(p -> p.aggregation(builder -> builder.globalGrouping().addAggregation(variable("reduce_agg", INTEGER), new AggregationNode.Aggregation(new CallExpression("reduce_agg", REDUCE_AGG, INTEGER, ImmutableList.of(castToRowExpression(expression("input")), castToRowExpression(expression("0")), castToRowExpression(expression("(x,y) -> x*y")), castToRowExpression(expression("(a,b) -> a*b")))), Optional.of(castToRowExpression(expression("input > 10"))), Optional.empty(), false, Optional.empty())).source(p.values(p.variable("input", INTEGER))))).get();
    // TODO migrate this to RowExpressionMatcher
    AggregationNode.Aggregation translated = ((AggregationNode) result).getAggregations().get(variable("reduce_agg", INTEGER));
    assertEquals(translated, new AggregationNode.Aggregation(new CallExpression("reduce_agg", REDUCE_AGG, INTEGER, ImmutableList.of(variable("input", INTEGER), constant(0L, INTEGER), new LambdaDefinitionExpression(Optional.empty(), ImmutableList.of(INTEGER, INTEGER), ImmutableList.of("x", "y"), multiply(variable("x", INTEGER), variable("y", INTEGER))), new LambdaDefinitionExpression(Optional.empty(), ImmutableList.of(INTEGER, INTEGER), ImmutableList.of("a", "b"), multiply(variable("a", INTEGER), variable("b", INTEGER))))), Optional.of(greaterThan(variable("input", INTEGER), constant(10L, INTEGER))), Optional.empty(), false, Optional.empty()));
    assertFalse(isUntranslated(translated));
}
Also used : FunctionAndTypeManager(com.facebook.presto.metadata.FunctionAndTypeManager) AggregationNode(com.facebook.presto.spi.plan.AggregationNode) OriginalExpressionUtils(com.facebook.presto.sql.relational.OriginalExpressionUtils) Assert.assertEquals(org.testng.Assert.assertEquals) Test(org.testng.annotations.Test) TypeSignatureProvider.fromTypes(com.facebook.presto.sql.analyzer.TypeSignatureProvider.fromTypes) Expressions.call(com.facebook.presto.sql.relational.Expressions.call) Expressions.constant(com.facebook.presto.sql.relational.Expressions.constant) ImmutableList(com.google.common.collect.ImmutableList) BOOLEAN(com.facebook.presto.common.type.BooleanType.BOOLEAN) FunctionResolution(com.facebook.presto.sql.relational.FunctionResolution) Expressions.variable(com.facebook.presto.sql.relational.Expressions.variable) CallExpression(com.facebook.presto.spi.relation.CallExpression) Assert.assertFalse(org.testng.Assert.assertFalse) RowExpression(com.facebook.presto.spi.relation.RowExpression) LambdaDefinitionExpression(com.facebook.presto.spi.relation.LambdaDefinitionExpression) OperatorType(com.facebook.presto.common.function.OperatorType) FunctionType(com.facebook.presto.common.type.FunctionType) SqlParser(com.facebook.presto.sql.parser.SqlParser) PlanNode(com.facebook.presto.spi.plan.PlanNode) BaseRuleTest(com.facebook.presto.sql.planner.iterative.rule.test.BaseRuleTest) MetadataManager.createTestMetadataManager(com.facebook.presto.metadata.MetadataManager.createTestMetadataManager) INTEGER(com.facebook.presto.common.type.IntegerType.INTEGER) FunctionHandle(com.facebook.presto.spi.function.FunctionHandle) Optional(java.util.Optional) PlanBuilder.expression(com.facebook.presto.sql.planner.iterative.rule.test.PlanBuilder.expression) Metadata(com.facebook.presto.metadata.Metadata) OriginalExpressionUtils.castToRowExpression(com.facebook.presto.sql.relational.OriginalExpressionUtils.castToRowExpression) PlanNode(com.facebook.presto.spi.plan.PlanNode) SqlParser(com.facebook.presto.sql.parser.SqlParser) AggregationNode(com.facebook.presto.spi.plan.AggregationNode) CallExpression(com.facebook.presto.spi.relation.CallExpression) LambdaDefinitionExpression(com.facebook.presto.spi.relation.LambdaDefinitionExpression) Test(org.testng.annotations.Test) BaseRuleTest(com.facebook.presto.sql.planner.iterative.rule.test.BaseRuleTest)

Example 23 with AggregationNode

use of com.facebook.presto.spi.plan.AggregationNode in project presto by prestodb.

the class ScalarAggregationToJoinRewriter method createAggregationNode.

private Optional<AggregationNode> createAggregationNode(AggregationNode scalarAggregation, JoinNode leftOuterJoin, VariableReferenceExpression nonNull) {
    ImmutableMap.Builder<VariableReferenceExpression, Aggregation> aggregations = ImmutableMap.builder();
    for (Map.Entry<VariableReferenceExpression, Aggregation> entry : scalarAggregation.getAggregations().entrySet()) {
        VariableReferenceExpression variable = entry.getKey();
        if (functionResolution.isCountFunction(entry.getValue().getFunctionHandle())) {
            Type scalarAggregationSourceType = nonNull.getType();
            aggregations.put(variable, new Aggregation(new CallExpression(variable.getSourceLocation(), "count", functionResolution.countFunction(scalarAggregationSourceType), BIGINT, ImmutableList.of(castToRowExpression(asSymbolReference(nonNull)))), Optional.empty(), Optional.empty(), false, entry.getValue().getMask()));
        } else {
            aggregations.put(variable, entry.getValue());
        }
    }
    return Optional.of(new AggregationNode(scalarAggregation.getSourceLocation(), idAllocator.getNextId(), leftOuterJoin, aggregations.build(), singleGroupingSet(leftOuterJoin.getLeft().getOutputVariables()), ImmutableList.of(), scalarAggregation.getStep(), scalarAggregation.getHashVariable(), Optional.empty()));
}
Also used : Aggregation(com.facebook.presto.spi.plan.AggregationNode.Aggregation) BooleanType(com.facebook.presto.common.type.BooleanType) Type(com.facebook.presto.common.type.Type) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) AggregationNode(com.facebook.presto.spi.plan.AggregationNode) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) CallExpression(com.facebook.presto.spi.relation.CallExpression) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 24 with AggregationNode

use of com.facebook.presto.spi.plan.AggregationNode in project presto by prestodb.

the class TestCostCalculator method testAggregation.

@Test
public void testAggregation() {
    TableScanNode tableScan = tableScan("ts", "orderkey");
    AggregationNode aggregation = aggregation("agg", tableScan);
    Map<String, PlanCostEstimate> costs = ImmutableMap.of("ts", cpuCost(6000));
    Map<String, PlanNodeStatsEstimate> stats = ImmutableMap.of("ts", statsEstimate(tableScan, 6000), "agg", statsEstimate(aggregation, 13));
    Map<String, Type> types = ImmutableMap.of("orderkey", BIGINT, "count", BIGINT);
    assertCost(aggregation, costs, stats).cpu(6000 * IS_NULL_OVERHEAD + 6000).memory(13 * IS_NULL_OVERHEAD).network(0);
    assertCostEstimatedExchanges(aggregation, costs, stats).cpu((6000 + 6000 + 6000) * IS_NULL_OVERHEAD + 6000).memory(13 * IS_NULL_OVERHEAD).network(6000 * IS_NULL_OVERHEAD);
    assertCostSingleStageFragmentedPlan(aggregation, costs, stats, types).cpu(6000 + 6000 * IS_NULL_OVERHEAD).memory(13 * IS_NULL_OVERHEAD).network(0 * IS_NULL_OVERHEAD);
    assertCostHasUnknownComponentsForUnknownStats(aggregation);
}
Also used : Type(com.facebook.presto.common.type.Type) TableScanNode(com.facebook.presto.spi.plan.TableScanNode) AggregationNode(com.facebook.presto.spi.plan.AggregationNode) Test(org.testng.annotations.Test)

Example 25 with AggregationNode

use of com.facebook.presto.spi.plan.AggregationNode in project presto by prestodb.

the class MultipleDistinctAggregationToMarkDistinct method apply.

@Override
public Result apply(AggregationNode parent, Captures captures, Context context) {
    if (!SystemSessionProperties.useMarkDistinct(context.getSession())) {
        return Result.empty();
    }
    // the distinct marker for the given set of input columns
    Map<Set<VariableReferenceExpression>, VariableReferenceExpression> markers = new HashMap<>();
    Map<VariableReferenceExpression, Aggregation> newAggregations = new HashMap<>();
    PlanNode subPlan = parent.getSource();
    for (Map.Entry<VariableReferenceExpression, Aggregation> entry : parent.getAggregations().entrySet()) {
        Aggregation aggregation = entry.getValue();
        if (aggregation.isDistinct() && !aggregation.getFilter().isPresent() && !aggregation.getMask().isPresent()) {
            Set<VariableReferenceExpression> inputs = aggregation.getArguments().stream().map(OriginalExpressionUtils::castToExpression).map(context.getVariableAllocator()::toVariableReference).collect(toSet());
            VariableReferenceExpression marker = markers.get(inputs);
            if (marker == null) {
                marker = context.getVariableAllocator().newVariable(Iterables.getLast(inputs).getName(), BOOLEAN, "distinct");
                markers.put(inputs, marker);
                ImmutableSet.Builder<VariableReferenceExpression> distinctVariables = ImmutableSet.<VariableReferenceExpression>builder().addAll(parent.getGroupingKeys()).addAll(inputs);
                parent.getGroupIdVariable().ifPresent(distinctVariables::add);
                subPlan = new MarkDistinctNode(subPlan.getSourceLocation(), context.getIdAllocator().getNextId(), subPlan, marker, ImmutableList.copyOf(distinctVariables.build()), Optional.empty());
            }
            // remove the distinct flag and set the distinct marker
            newAggregations.put(entry.getKey(), new Aggregation(aggregation.getCall(), aggregation.getFilter(), aggregation.getOrderBy(), false, Optional.of(marker)));
        } else {
            newAggregations.put(entry.getKey(), aggregation);
        }
    }
    return Result.ofPlanNode(new AggregationNode(parent.getSourceLocation(), parent.getId(), subPlan, newAggregations, parent.getGroupingSets(), ImmutableList.of(), parent.getStep(), parent.getHashVariable(), parent.getGroupIdVariable()));
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) HashSet(java.util.HashSet) Collectors.toSet(java.util.stream.Collectors.toSet) MarkDistinctNode(com.facebook.presto.spi.plan.MarkDistinctNode) HashMap(java.util.HashMap) AggregationNode(com.facebook.presto.spi.plan.AggregationNode) Aggregation(com.facebook.presto.spi.plan.AggregationNode.Aggregation) PlanNode(com.facebook.presto.spi.plan.PlanNode) ImmutableSet(com.google.common.collect.ImmutableSet) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) OriginalExpressionUtils(com.facebook.presto.sql.relational.OriginalExpressionUtils) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

AggregationNode (com.facebook.presto.spi.plan.AggregationNode)51 VariableReferenceExpression (com.facebook.presto.spi.relation.VariableReferenceExpression)33 PlanNode (com.facebook.presto.spi.plan.PlanNode)23 CallExpression (com.facebook.presto.spi.relation.CallExpression)18 Test (org.testng.annotations.Test)18 Aggregation (com.facebook.presto.spi.plan.AggregationNode.Aggregation)15 ProjectNode (com.facebook.presto.spi.plan.ProjectNode)14 ImmutableList (com.google.common.collect.ImmutableList)14 Map (java.util.Map)14 ImmutableMap (com.google.common.collect.ImmutableMap)13 RowExpression (com.facebook.presto.spi.relation.RowExpression)12 TableScanNode (com.facebook.presto.spi.plan.TableScanNode)9 Optional (java.util.Optional)9 Assignments (com.facebook.presto.spi.plan.Assignments)8 Ordering (com.facebook.presto.spi.plan.Ordering)7 OrderingScheme (com.facebook.presto.spi.plan.OrderingScheme)7 PlanBuilder (com.facebook.presto.sql.planner.iterative.rule.test.PlanBuilder)7 OriginalExpressionUtils.castToRowExpression (com.facebook.presto.sql.relational.OriginalExpressionUtils.castToRowExpression)6 List (java.util.List)6 Assert.assertFalse (org.testng.Assert.assertFalse)6