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()));
}
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()));
}
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());
}
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));
}
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());
}
Aggregations