Search in sources :

Example 6 with Aggregation

use of io.prestosql.spi.plan.AggregationNode.Aggregation in project hetu-core by openlookeng.

the class RewriteSpatialPartitioningAggregation method apply.

@Override
public Result apply(AggregationNode node, Captures captures, Context context) {
    ImmutableMap.Builder<Symbol, Aggregation> aggregations = ImmutableMap.builder();
    Symbol partitionCountSymbol = context.getSymbolAllocator().newSymbol("partition_count", INTEGER);
    ImmutableMap.Builder<Symbol, RowExpression> envelopeAssignments = ImmutableMap.builder();
    for (Map.Entry<Symbol, Aggregation> entry : node.getAggregations().entrySet()) {
        Aggregation aggregation = entry.getValue();
        QualifiedObjectName name = metadata.getFunctionAndTypeManager().getFunctionMetadata(aggregation.getFunctionHandle()).getName();
        Type geometryType = metadata.getType(GEOMETRY_TYPE_SIGNATURE);
        if (name.equals(NAME) && aggregation.getArguments().size() == 1) {
            RowExpression geometry = getOnlyElement(aggregation.getArguments().stream().collect(toImmutableList()));
            Symbol envelopeSymbol = context.getSymbolAllocator().newSymbol("envelope", metadata.getType(GEOMETRY_TYPE_SIGNATURE));
            if (isFunctionNameMatch(geometry, "ST_Envelope")) {
                envelopeAssignments.put(envelopeSymbol, geometry);
            } else {
                envelopeAssignments.put(envelopeSymbol, castToRowExpression(new FunctionCallBuilder(metadata).setName(QualifiedName.of("ST_Envelope")).addArgument(GEOMETRY_TYPE_SIGNATURE, castToExpression(geometry)).build()));
            }
            aggregations.put(entry.getKey(), new Aggregation(new CallExpression(name.getObjectName(), metadata.getFunctionAndTypeManager().lookupFunction(NAME.getObjectName(), fromTypes(geometryType, INTEGER)), context.getSymbolAllocator().getTypes().get(entry.getKey()), ImmutableList.of(castToRowExpression(toSymbolReference(envelopeSymbol)), castToRowExpression(toSymbolReference(partitionCountSymbol))), Optional.empty()), ImmutableList.of(castToRowExpression(toSymbolReference(envelopeSymbol)), castToRowExpression(toSymbolReference(partitionCountSymbol))), false, Optional.empty(), Optional.empty(), aggregation.getMask()));
        } else {
            aggregations.put(entry);
        }
    }
    return Result.ofPlanNode(new AggregationNode(node.getId(), new ProjectNode(context.getIdAllocator().getNextId(), node.getSource(), Assignments.builder().putAll(AssignmentUtils.identityAsSymbolReferences(node.getSource().getOutputSymbols())).put(partitionCountSymbol, castToRowExpression(new LongLiteral(Integer.toString(getHashPartitionCount(context.getSession()))))).putAll(envelopeAssignments.build()).build()), aggregations.build(), node.getGroupingSets(), node.getPreGroupedSymbols(), node.getStep(), node.getHashSymbol(), node.getGroupIdSymbol(), node.getAggregationType(), node.getFinalizeSymbol()));
}
Also used : LongLiteral(io.prestosql.sql.tree.LongLiteral) Symbol(io.prestosql.spi.plan.Symbol) OriginalExpressionUtils.castToRowExpression(io.prestosql.sql.relational.OriginalExpressionUtils.castToRowExpression) RowExpression(io.prestosql.spi.relation.RowExpression) AggregationNode(io.prestosql.spi.plan.AggregationNode) ImmutableMap(com.google.common.collect.ImmutableMap) QualifiedObjectName(io.prestosql.spi.connector.QualifiedObjectName) Aggregation(io.prestosql.spi.plan.AggregationNode.Aggregation) Type(io.prestosql.spi.type.Type) ProjectNode(io.prestosql.spi.plan.ProjectNode) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) CallExpression(io.prestosql.spi.relation.CallExpression) FunctionCallBuilder(io.prestosql.sql.planner.FunctionCallBuilder)

Example 7 with Aggregation

use of io.prestosql.spi.plan.AggregationNode.Aggregation in project hetu-core by openlookeng.

the class ScalarAggregationToJoinRewriter method createAggregationNode.

private Optional<AggregationNode> createAggregationNode(AggregationNode scalarAggregation, JoinNode leftOuterJoin, Symbol nonNullableAggregationSourceSymbol) {
    ImmutableMap.Builder<Symbol, Aggregation> aggregations = ImmutableMap.builder();
    for (Map.Entry<Symbol, Aggregation> entry : scalarAggregation.getAggregations().entrySet()) {
        Aggregation aggregation = entry.getValue();
        Symbol symbol = entry.getKey();
        if (functionResolution.isCountFunction(entry.getValue().getFunctionHandle())) {
            aggregations.put(symbol, new Aggregation(new CallExpression("count", functionResolution.countFunction(planSymbolAllocator.getTypes().get(nonNullableAggregationSourceSymbol)), BIGINT, ImmutableList.of(castToRowExpression(toSymbolReference(nonNullableAggregationSourceSymbol)))), ImmutableList.of(castToRowExpression(toSymbolReference(nonNullableAggregationSourceSymbol))), false, Optional.empty(), Optional.empty(), aggregation.getMask()));
        } else {
            aggregations.put(symbol, aggregation);
        }
    }
    return Optional.of(new AggregationNode(idAllocator.getNextId(), leftOuterJoin, aggregations.build(), singleGroupingSet(leftOuterJoin.getLeft().getOutputSymbols()), ImmutableList.of(), scalarAggregation.getStep(), scalarAggregation.getHashSymbol(), Optional.empty(), scalarAggregation.getAggregationType(), scalarAggregation.getFinalizeSymbol()));
}
Also used : Aggregation(io.prestosql.spi.plan.AggregationNode.Aggregation) Symbol(io.prestosql.spi.plan.Symbol) AggregationNode(io.prestosql.spi.plan.AggregationNode) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) CallExpression(io.prestosql.spi.relation.CallExpression) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 8 with Aggregation

use of io.prestosql.spi.plan.AggregationNode.Aggregation in project hetu-core by openlookeng.

the class StatisticAggregations method createPartialAggregations.

public Parts createPartialAggregations(PlanSymbolAllocator planSymbolAllocator, Metadata metadata) {
    ImmutableMap.Builder<Symbol, Aggregation> partialAggregation = ImmutableMap.builder();
    ImmutableMap.Builder<Symbol, Aggregation> finalAggregation = ImmutableMap.builder();
    ImmutableMap.Builder<Symbol, Symbol> mappings = ImmutableMap.builder();
    for (Map.Entry<Symbol, Aggregation> entry : aggregations.entrySet()) {
        Aggregation originalAggregation = entry.getValue();
        FunctionHandle functionHandle = originalAggregation.getFunctionHandle();
        InternalAggregationFunction function = metadata.getFunctionAndTypeManager().getAggregateFunctionImplementation(functionHandle);
        Symbol partialSymbol = planSymbolAllocator.newSymbol(originalAggregation.getFunctionCall().getDisplayName(), function.getIntermediateType());
        mappings.put(entry.getKey(), partialSymbol);
        partialAggregation.put(partialSymbol, new Aggregation(new CallExpression(originalAggregation.getFunctionCall().getDisplayName(), functionHandle, function.getIntermediateType(), originalAggregation.getArguments(), Optional.empty()), originalAggregation.getArguments(), originalAggregation.isDistinct(), originalAggregation.getFilter(), originalAggregation.getOrderingScheme(), originalAggregation.getMask()));
        finalAggregation.put(entry.getKey(), new Aggregation(new CallExpression(originalAggregation.getFunctionCall().getDisplayName(), functionHandle, function.getFinalType(), ImmutableList.of(castToRowExpression(toSymbolReference(partialSymbol))), Optional.empty()), ImmutableList.of(castToRowExpression(toSymbolReference(partialSymbol))), false, Optional.empty(), Optional.empty(), Optional.empty()));
    }
    groupingSymbols.forEach(symbol -> mappings.put(symbol, symbol));
    return new Parts(new StatisticAggregations(partialAggregation.build(), groupingSymbols), new StatisticAggregations(finalAggregation.build(), groupingSymbols), mappings.build());
}
Also used : Aggregation(io.prestosql.spi.plan.AggregationNode.Aggregation) Symbol(io.prestosql.spi.plan.Symbol) InternalAggregationFunction(io.prestosql.operator.aggregation.InternalAggregationFunction) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map) FunctionHandle(io.prestosql.spi.function.FunctionHandle) CallExpression(io.prestosql.spi.relation.CallExpression) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 9 with Aggregation

use of io.prestosql.spi.plan.AggregationNode.Aggregation in project hetu-core by openlookeng.

the class PushAggregationThroughOuterJoin method createAggregationOverNull.

private Optional<MappedAggregationInfo> createAggregationOverNull(AggregationNode referenceAggregation, PlanSymbolAllocator planSymbolAllocator, PlanNodeIdAllocator idAllocator, Lookup lookup) {
    // Create a values node that consists of a single row of nulls.
    // Map the output symbols from the referenceAggregation's source
    // to symbol references for the new values node.
    ImmutableList.Builder<Symbol> nullSymbols = ImmutableList.builder();
    ImmutableList.Builder<RowExpression> nullLiterals = ImmutableList.builder();
    ImmutableMap.Builder<VariableReferenceExpression, VariableReferenceExpression> sourcesSymbolMappingBuilder = ImmutableMap.builder();
    for (Symbol sourceSymbol : referenceAggregation.getSource().getOutputSymbols()) {
        RowExpression nullLiteral = new ConstantExpression(null, planSymbolAllocator.getTypes().get(sourceSymbol));
        nullLiterals.add(nullLiteral);
        Symbol nullSymbol = planSymbolAllocator.newSymbol(nullLiteral);
        nullSymbols.add(nullSymbol);
        sourcesSymbolMappingBuilder.put(toVariableReference(sourceSymbol, planSymbolAllocator.getTypes().get(sourceSymbol)), toVariableReference(nullSymbol, nullLiteral.getType()));
    }
    ValuesNode nullRow = new ValuesNode(idAllocator.getNextId(), nullSymbols.build(), ImmutableList.of(nullLiterals.build()));
    Map<VariableReferenceExpression, VariableReferenceExpression> sourcesSymbolMapping = sourcesSymbolMappingBuilder.build();
    // For each aggregation function in the reference node, create a corresponding aggregation function
    // that points to the nullRow. Map the symbols from the aggregations in referenceAggregation to the
    // symbols in these new aggregations.
    ImmutableMap.Builder<Symbol, Symbol> aggregationsSymbolMappingBuilder = ImmutableMap.builder();
    ImmutableMap.Builder<Symbol, AggregationNode.Aggregation> aggregationsOverNullBuilder = ImmutableMap.builder();
    for (Map.Entry<Symbol, AggregationNode.Aggregation> entry : referenceAggregation.getAggregations().entrySet()) {
        Symbol aggregationSymbol = entry.getKey();
        AggregationNode.Aggregation aggregation = entry.getValue();
        if (!isUsingVariables(aggregation, sourcesSymbolMapping.keySet())) {
            return Optional.empty();
        }
        Aggregation overNullAggregation = new Aggregation(new CallExpression(aggregation.getFunctionCall().getDisplayName(), aggregation.getFunctionCall().getFunctionHandle(), aggregation.getFunctionCall().getType(), aggregation.getArguments().stream().map(argument -> inlineVariables(sourcesSymbolMapping, argument)).collect(toImmutableList()), Optional.empty()), aggregation.getArguments().stream().map(argument -> inlineVariables(sourcesSymbolMapping, argument)).collect(toImmutableList()), aggregation.isDistinct(), aggregation.getFilter(), aggregation.getOrderingScheme(), aggregation.getMask());
        Symbol overNullSymbol = planSymbolAllocator.newSymbol(overNullAggregation.getFunctionCall().getDisplayName(), planSymbolAllocator.getTypes().get(aggregationSymbol));
        aggregationsOverNullBuilder.put(overNullSymbol, overNullAggregation);
        aggregationsSymbolMappingBuilder.put(aggregationSymbol, overNullSymbol);
    }
    Map<Symbol, Symbol> aggregationsSymbolMapping = aggregationsSymbolMappingBuilder.build();
    // create an aggregation node whose source is the null row.
    AggregationNode aggregationOverNullRow = new AggregationNode(idAllocator.getNextId(), nullRow, aggregationsOverNullBuilder.build(), globalAggregation(), ImmutableList.of(), AggregationNode.Step.SINGLE, Optional.empty(), Optional.empty(), AggregationNode.AggregationType.HASH, Optional.empty());
    return Optional.of(new MappedAggregationInfo(aggregationOverNullRow, aggregationsSymbolMapping));
}
Also used : Patterns.source(io.prestosql.sql.planner.plan.Patterns.source) ConstantExpression(io.prestosql.spi.relation.ConstantExpression) Lookup(io.prestosql.sql.planner.iterative.Lookup) Patterns.aggregation(io.prestosql.sql.planner.plan.Patterns.aggregation) Patterns.join(io.prestosql.sql.planner.plan.Patterns.join) Pattern(io.prestosql.matching.Pattern) SystemSessionProperties.shouldPushAggregationThroughJoin(io.prestosql.SystemSessionProperties.shouldPushAggregationThroughJoin) AggregationNode(io.prestosql.spi.plan.AggregationNode) HashSet(java.util.HashSet) CallExpression(io.prestosql.spi.relation.CallExpression) Capture.newCapture(io.prestosql.matching.Capture.newCapture) ImmutableList(com.google.common.collect.ImmutableList) RowExpressionVariableInliner.inlineVariables(io.prestosql.sql.planner.RowExpressionVariableInliner.inlineVariables) Map(java.util.Map) Session(io.prestosql.Session) SpecialForm(io.prestosql.spi.relation.SpecialForm) AggregationNode.globalAggregation(io.prestosql.spi.plan.AggregationNode.globalAggregation) JoinNode(io.prestosql.spi.plan.JoinNode) AggregationNode.singleGroupingSet(io.prestosql.spi.plan.AggregationNode.singleGroupingSet) Symbol(io.prestosql.spi.plan.Symbol) ImmutableMap(com.google.common.collect.ImmutableMap) Assignments(io.prestosql.spi.plan.Assignments) Rule(io.prestosql.sql.planner.iterative.Rule) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Set(java.util.Set) PlanNode(io.prestosql.spi.plan.PlanNode) VariableReferenceExpression(io.prestosql.spi.relation.VariableReferenceExpression) ProjectNode(io.prestosql.spi.plan.ProjectNode) VariableReferenceSymbolConverter.toVariableReference(io.prestosql.sql.planner.VariableReferenceSymbolConverter.toVariableReference) COALESCE(io.prestosql.spi.relation.SpecialForm.Form.COALESCE) Preconditions.checkState(com.google.common.base.Preconditions.checkState) PlanSymbolAllocator(io.prestosql.sql.planner.PlanSymbolAllocator) Captures(io.prestosql.matching.Captures) ValuesNode(io.prestosql.spi.plan.ValuesNode) List(java.util.List) Aggregation(io.prestosql.spi.plan.AggregationNode.Aggregation) DistinctOutputQueryUtil.isDistinct(io.prestosql.sql.planner.optimizations.DistinctOutputQueryUtil.isDistinct) Capture(io.prestosql.matching.Capture) PlanNodeIdAllocator(io.prestosql.spi.plan.PlanNodeIdAllocator) RowExpression(io.prestosql.spi.relation.RowExpression) Optional(java.util.Optional) ValuesNode(io.prestosql.spi.plan.ValuesNode) ImmutableList(com.google.common.collect.ImmutableList) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Symbol(io.prestosql.spi.plan.Symbol) ConstantExpression(io.prestosql.spi.relation.ConstantExpression) RowExpression(io.prestosql.spi.relation.RowExpression) AggregationNode(io.prestosql.spi.plan.AggregationNode) ImmutableMap(com.google.common.collect.ImmutableMap) Aggregation(io.prestosql.spi.plan.AggregationNode.Aggregation) AggregationNode.globalAggregation(io.prestosql.spi.plan.AggregationNode.globalAggregation) Aggregation(io.prestosql.spi.plan.AggregationNode.Aggregation) VariableReferenceExpression(io.prestosql.spi.relation.VariableReferenceExpression) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) CallExpression(io.prestosql.spi.relation.CallExpression)

Example 10 with Aggregation

use of io.prestosql.spi.plan.AggregationNode.Aggregation in project hetu-core by openlookeng.

the class TransformExistsApplyToLateralNode method rewriteToDefaultAggregation.

private PlanNode rewriteToDefaultAggregation(ApplyNode parent, Context context) {
    Symbol count = context.getSymbolAllocator().newSymbol(COUNT.toString(), BIGINT);
    Symbol exists = getOnlyElement(parent.getSubqueryAssignments().getSymbols());
    return new LateralJoinNode(parent.getId(), parent.getInput(), new ProjectNode(context.getIdAllocator().getNextId(), new AggregationNode(context.getIdAllocator().getNextId(), parent.getSubquery(), ImmutableMap.of(count, new Aggregation(new CallExpression("count", functionResolution.countFunction(), BIGINT, ImmutableList.of(), Optional.empty()), ImmutableList.of(), false, Optional.empty(), Optional.empty(), Optional.empty())), globalAggregation(), ImmutableList.of(), AggregationNode.Step.SINGLE, Optional.empty(), Optional.empty(), AggregationNode.AggregationType.HASH, Optional.empty()), Assignments.of(exists, castToRowExpression(new ComparisonExpression(GREATER_THAN, toSymbolReference(count), new Cast(new LongLiteral("0"), BIGINT.toString()))))), parent.getCorrelation(), INNER, TRUE_LITERAL, parent.getOriginSubquery());
}
Also used : AggregationNode.globalAggregation(io.prestosql.spi.plan.AggregationNode.globalAggregation) Aggregation(io.prestosql.spi.plan.AggregationNode.Aggregation) Cast(io.prestosql.sql.tree.Cast) ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) LateralJoinNode(io.prestosql.sql.planner.plan.LateralJoinNode) LongLiteral(io.prestosql.sql.tree.LongLiteral) Symbol(io.prestosql.spi.plan.Symbol) ProjectNode(io.prestosql.spi.plan.ProjectNode) AggregationNode(io.prestosql.spi.plan.AggregationNode) CallExpression(io.prestosql.spi.relation.CallExpression)

Aggregations

Aggregation (io.prestosql.spi.plan.AggregationNode.Aggregation)17 AggregationNode (io.prestosql.spi.plan.AggregationNode)16 Symbol (io.prestosql.spi.plan.Symbol)16 Map (java.util.Map)12 CallExpression (io.prestosql.spi.relation.CallExpression)9 ImmutableMap (com.google.common.collect.ImmutableMap)8 PlanNode (io.prestosql.spi.plan.PlanNode)8 ProjectNode (io.prestosql.spi.plan.ProjectNode)6 ImmutableList (com.google.common.collect.ImmutableList)5 RowExpression (io.prestosql.spi.relation.RowExpression)5 Optional (java.util.Optional)5 Set (java.util.Set)5 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)4 Session (io.prestosql.Session)4 Assignments (io.prestosql.spi.plan.Assignments)4 OriginalExpressionUtils.castToRowExpression (io.prestosql.sql.relational.OriginalExpressionUtils.castToRowExpression)4 Expression (io.prestosql.sql.tree.Expression)4 List (java.util.List)4 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)3 ImmutableSet (com.google.common.collect.ImmutableSet)3