Search in sources :

Example 1 with Aggregation

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

the class AggregatedOrcPageSource method getNextPage.

@Override
public Page getNextPage() {
    if (completed) {
        return null;
    }
    long start = System.nanoTime();
    Block[] blocks = new Block[columnHandles.size()];
    for (int fieldId = 0; fieldId < blocks.length; fieldId++) {
        HiveColumnHandle columnHandle = columnHandles.get(fieldId);
        Aggregation aggregation = columnHandle.getPartialAggregation().get();
        int columnIndex = columnHandle.getHiveColumnIndex();
        Type type = typeManager.getType(columnHandle.getTypeSignature());
        BlockBuilder blockBuilder = type.createBlockBuilder(null, batchSize, 0);
        FunctionHandle functionHandle = aggregation.getFunctionHandle();
        if (functionResolution.isCountFunction(functionHandle)) {
            if (aggregation.getArguments().isEmpty()) {
                blockBuilder = blockBuilder.writeLong(footer.getNumberOfRows());
            } else {
                writeNonNullCount(columnIndex, blockBuilder);
            }
            completedBytes += INTEGER.getFixedSize();
        } else if (functionResolution.isMaxFunction(functionHandle)) {
            writeMinMax(columnIndex, type, columnHandle.getHiveType(), blockBuilder, false);
        } else if (functionResolution.isMinFunction(functionHandle)) {
            writeMinMax(columnIndex, type, columnHandle.getHiveType(), blockBuilder, true);
        } else {
            throw new UnsupportedOperationException(aggregation.getFunctionHandle().toString() + " is not supported");
        }
        blocks[fieldId] = blockBuilder.build();
    }
    completed = true;
    readTimeNanos += System.nanoTime() - start;
    return new Page(batchSize, blocks);
}
Also used : Aggregation(com.facebook.presto.spi.plan.AggregationNode.Aggregation) HiveType(com.facebook.presto.hive.HiveType) FixedWidthType(com.facebook.presto.common.type.FixedWidthType) OrcType(com.facebook.presto.orc.metadata.OrcType) Type(com.facebook.presto.common.type.Type) Block(com.facebook.presto.common.block.Block) Page(com.facebook.presto.common.Page) FunctionHandle(com.facebook.presto.spi.function.FunctionHandle) HiveColumnHandle(com.facebook.presto.hive.HiveColumnHandle) BlockBuilder(com.facebook.presto.common.block.BlockBuilder)

Example 2 with Aggregation

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

the class SingleDistinctAggregationToGroupBy method apply.

@Override
public Result apply(AggregationNode aggregation, Captures captures, Context context) {
    List<Set<RowExpression>> argumentSets = extractArgumentSets(aggregation).collect(Collectors.toList());
    Set<VariableReferenceExpression> variables = Iterables.getOnlyElement(argumentSets).stream().map(OriginalExpressionUtils::castToExpression).map(context.getVariableAllocator()::toVariableReference).collect(Collectors.toSet());
    return Result.ofPlanNode(new AggregationNode(aggregation.getSourceLocation(), aggregation.getId(), new AggregationNode(aggregation.getSourceLocation(), context.getIdAllocator().getNextId(), aggregation.getSource(), ImmutableMap.of(), singleGroupingSet(ImmutableList.<VariableReferenceExpression>builder().addAll(aggregation.getGroupingKeys()).addAll(variables).build()), ImmutableList.of(), SINGLE, Optional.empty(), Optional.empty()), // remove DISTINCT flag from function calls
    aggregation.getAggregations().entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> removeDistinct(e.getValue()))), aggregation.getGroupingSets(), emptyList(), aggregation.getStep(), aggregation.getHashVariable(), aggregation.getGroupIdVariable()));
}
Also used : RowExpression(com.facebook.presto.spi.relation.RowExpression) Iterables(com.google.common.collect.Iterables) AggregationNode(com.facebook.presto.spi.plan.AggregationNode) Patterns.aggregation(com.facebook.presto.sql.planner.plan.Patterns.aggregation) ImmutableMap(com.google.common.collect.ImmutableMap) OriginalExpressionUtils(com.facebook.presto.sql.relational.OriginalExpressionUtils) Collections.emptyList(java.util.Collections.emptyList) Rule(com.facebook.presto.sql.planner.iterative.Rule) Captures(com.facebook.presto.matching.Captures) Set(java.util.Set) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) SINGLE(com.facebook.presto.spi.plan.AggregationNode.Step.SINGLE) Collectors(java.util.stream.Collectors) Pattern(com.facebook.presto.matching.Pattern) HashSet(java.util.HashSet) List(java.util.List) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) Stream(java.util.stream.Stream) ImmutableList(com.google.common.collect.ImmutableList) Map(java.util.Map) Aggregation(com.facebook.presto.spi.plan.AggregationNode.Aggregation) Optional(java.util.Optional) AggregationNode.singleGroupingSet(com.facebook.presto.spi.plan.AggregationNode.singleGroupingSet) Set(java.util.Set) HashSet(java.util.HashSet) AggregationNode.singleGroupingSet(com.facebook.presto.spi.plan.AggregationNode.singleGroupingSet) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) AggregationNode(com.facebook.presto.spi.plan.AggregationNode) OriginalExpressionUtils(com.facebook.presto.sql.relational.OriginalExpressionUtils) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map)

Example 3 with Aggregation

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

the class TransformExistsApplyToLateralNode method rewriteToDefaultAggregation.

private PlanNode rewriteToDefaultAggregation(ApplyNode parent, Context context) {
    VariableReferenceExpression count = context.getVariableAllocator().newVariable("count", BIGINT);
    VariableReferenceExpression exists = getOnlyElement(parent.getSubqueryAssignments().getVariables());
    return new LateralJoinNode(parent.getSourceLocation(), parent.getId(), parent.getInput(), new ProjectNode(context.getIdAllocator().getNextId(), new AggregationNode(parent.getSourceLocation(), context.getIdAllocator().getNextId(), parent.getSubquery(), ImmutableMap.of(count, new Aggregation(new CallExpression(exists.getSourceLocation(), "count", functionResolution.countFunction(), BIGINT, ImmutableList.of()), Optional.empty(), Optional.empty(), false, Optional.empty())), globalAggregation(), ImmutableList.of(), AggregationNode.Step.SINGLE, Optional.empty(), Optional.empty()), Assignments.of(exists, castToRowExpression(new ComparisonExpression(GREATER_THAN, asSymbolReference(count), new Cast(new LongLiteral("0"), BIGINT.toString()))))), parent.getCorrelation(), INNER, parent.getOriginSubqueryError());
}
Also used : AggregationNode.globalAggregation(com.facebook.presto.spi.plan.AggregationNode.globalAggregation) Aggregation(com.facebook.presto.spi.plan.AggregationNode.Aggregation) Cast(com.facebook.presto.sql.tree.Cast) ComparisonExpression(com.facebook.presto.sql.tree.ComparisonExpression) LateralJoinNode(com.facebook.presto.sql.planner.plan.LateralJoinNode) LongLiteral(com.facebook.presto.sql.tree.LongLiteral) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) ProjectNode(com.facebook.presto.spi.plan.ProjectNode) AggregationNode(com.facebook.presto.spi.plan.AggregationNode) CallExpression(com.facebook.presto.spi.relation.CallExpression)

Example 4 with Aggregation

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

the class TestTypeValidator method testInvalidAggregationFunctionCall.

@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "Expected input types are \\[double\\] but getting \\[bigint\\]")
public void testInvalidAggregationFunctionCall() {
    VariableReferenceExpression aggregationVariable = variableAllocator.newVariable("sum", DOUBLE);
    PlanNode node = new AggregationNode(Optional.empty(), newId(), baseTableScan, ImmutableMap.of(aggregationVariable, new Aggregation(new CallExpression("sum", SUM, DOUBLE, ImmutableList.of(variableA)), Optional.empty(), Optional.empty(), false, Optional.empty())), singleGroupingSet(ImmutableList.of(variableA, variableB)), ImmutableList.of(), SINGLE, Optional.empty(), Optional.empty());
    assertTypesValid(node);
}
Also used : Aggregation(com.facebook.presto.spi.plan.AggregationNode.Aggregation) PlanNode(com.facebook.presto.spi.plan.PlanNode) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) AggregationNode(com.facebook.presto.spi.plan.AggregationNode) CallExpression(com.facebook.presto.spi.relation.CallExpression) Test(org.testng.annotations.Test)

Example 5 with Aggregation

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

the class TestTypeValidator method testValidAggregation.

@Test
public void testValidAggregation() {
    VariableReferenceExpression aggregationVariable = variableAllocator.newVariable("sum", DOUBLE);
    PlanNode node = new AggregationNode(Optional.empty(), newId(), baseTableScan, ImmutableMap.of(aggregationVariable, new Aggregation(new CallExpression("sum", SUM, DOUBLE, ImmutableList.of(variableC)), Optional.empty(), Optional.empty(), false, Optional.empty())), singleGroupingSet(ImmutableList.of(variableA, variableB)), ImmutableList.of(), SINGLE, Optional.empty(), Optional.empty());
    assertTypesValid(node);
}
Also used : Aggregation(com.facebook.presto.spi.plan.AggregationNode.Aggregation) PlanNode(com.facebook.presto.spi.plan.PlanNode) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) AggregationNode(com.facebook.presto.spi.plan.AggregationNode) CallExpression(com.facebook.presto.spi.relation.CallExpression) Test(org.testng.annotations.Test)

Aggregations

Aggregation (com.facebook.presto.spi.plan.AggregationNode.Aggregation)21 VariableReferenceExpression (com.facebook.presto.spi.relation.VariableReferenceExpression)19 AggregationNode (com.facebook.presto.spi.plan.AggregationNode)16 Map (java.util.Map)13 ImmutableMap (com.google.common.collect.ImmutableMap)11 CallExpression (com.facebook.presto.spi.relation.CallExpression)10 PlanNode (com.facebook.presto.spi.plan.PlanNode)7 Type (com.facebook.presto.common.type.Type)5 ProjectNode (com.facebook.presto.spi.plan.ProjectNode)5 Session (com.facebook.presto.Session)4 RowExpression (com.facebook.presto.spi.relation.RowExpression)4 ImmutableList (com.google.common.collect.ImmutableList)4 Pattern (com.facebook.presto.matching.Pattern)3 FunctionHandle (com.facebook.presto.spi.function.FunctionHandle)3 OriginalExpressionUtils (com.facebook.presto.sql.relational.OriginalExpressionUtils)3 Optional (java.util.Optional)3 Set (java.util.Set)3 Page (com.facebook.presto.common.Page)2 Block (com.facebook.presto.common.block.Block)2 BlockBuilder (com.facebook.presto.common.block.BlockBuilder)2