use of io.prestosql.spi.plan.AggregationNode.Aggregation in project hetu-core by openlookeng.
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<Symbol>, Symbol> markers = new HashMap<>();
Map<Symbol, Aggregation> newAggregations = new HashMap<>();
PlanNode subPlan = parent.getSource();
for (Map.Entry<Symbol, Aggregation> entry : parent.getAggregations().entrySet()) {
Aggregation aggregation = entry.getValue();
if (aggregation.isDistinct() && !aggregation.getFilter().isPresent() && !aggregation.getMask().isPresent()) {
Set<Symbol> inputs = aggregation.getArguments().stream().map(OriginalExpressionUtils::castToExpression).map(SymbolUtils::from).collect(toSet());
Symbol marker = markers.get(inputs);
if (marker == null) {
marker = context.getSymbolAllocator().newSymbol(Iterables.getLast(inputs).getName(), BOOLEAN, "distinct");
markers.put(inputs, marker);
ImmutableSet.Builder<Symbol> distinctSymbols = ImmutableSet.<Symbol>builder().addAll(parent.getGroupingKeys()).addAll(inputs);
parent.getGroupIdSymbol().ifPresent(distinctSymbols::add);
subPlan = new MarkDistinctNode(context.getIdAllocator().getNextId(), subPlan, marker, ImmutableList.copyOf(distinctSymbols.build()), Optional.empty());
}
// remove the distinct flag and set the distinct marker
newAggregations.put(entry.getKey(), new Aggregation(aggregation.getFunctionCall(), aggregation.getArguments(), false, aggregation.getFilter(), aggregation.getOrderingScheme(), Optional.of(marker)));
} else {
newAggregations.put(entry.getKey(), aggregation);
}
}
return Result.ofPlanNode(new AggregationNode(parent.getId(), subPlan, newAggregations, parent.getGroupingSets(), ImmutableList.of(), parent.getStep(), parent.getHashSymbol(), parent.getGroupIdSymbol(), parent.getAggregationType(), parent.getFinalizeSymbol()));
}
use of io.prestosql.spi.plan.AggregationNode.Aggregation in project hetu-core by openlookeng.
the class PruneOrderByInAggregation method apply.
@Override
public Result apply(AggregationNode node, Captures captures, Context context) {
if (!node.hasOrderings()) {
return Result.empty();
}
boolean anyRewritten = false;
ImmutableMap.Builder<Symbol, Aggregation> aggregations = ImmutableMap.builder();
for (Map.Entry<Symbol, Aggregation> entry : node.getAggregations().entrySet()) {
Aggregation aggregation = entry.getValue();
if (!aggregation.getOrderingScheme().isPresent()) {
aggregations.put(entry);
} else // getAggregateFunctionImplementation can be expensive, so check it last.
if (metadata.getFunctionAndTypeManager().getAggregateFunctionImplementation(aggregation.getFunctionHandle()).isOrderSensitive()) {
aggregations.put(entry);
} else {
anyRewritten = true;
aggregations.put(entry.getKey(), new Aggregation(aggregation.getFunctionCall(), aggregation.getArguments(), aggregation.isDistinct(), aggregation.getFilter(), Optional.empty(), aggregation.getMask()));
}
}
if (!anyRewritten) {
return Result.empty();
}
return Result.ofPlanNode(new AggregationNode(node.getId(), node.getSource(), 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 PlanPrinter method formatAggregation.
public static String formatAggregation(Aggregation aggregation) {
StringBuilder builder = new StringBuilder();
String arguments = Joiner.on(", ").join(aggregation.getArguments());
if (aggregation.getArguments().isEmpty() && "count".equalsIgnoreCase(aggregation.getFunctionCall().getDisplayName())) {
arguments = "*";
}
if (aggregation.isDistinct()) {
arguments = "DISTINCT " + arguments;
}
builder.append(aggregation.getFunctionCall().getDisplayName()).append('(').append(arguments);
aggregation.getOrderingScheme().ifPresent(orderingScheme -> builder.append(' ').append(orderingScheme.getOrderBy().stream().map(input -> input + " " + orderingScheme.getOrdering(input)).collect(joining(", "))));
builder.append(')');
aggregation.getFilter().ifPresent(expression -> builder.append(" FILTER (WHERE ").append(expression).append(")"));
aggregation.getMask().ifPresent(symbol -> builder.append(" (mask = ").append(symbol).append(")"));
return builder.toString();
}
use of io.prestosql.spi.plan.AggregationNode.Aggregation in project hetu-core by openlookeng.
the class TestTypeValidator method testInvalidAggregationFunctionSignature.
@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "type of symbol 'sum(_[0-9]+)?' is expected to be double, but the actual type is bigint")
public void testInvalidAggregationFunctionSignature() {
Symbol aggregationSymbol = planSymbolAllocator.newSymbol("sum", DOUBLE);
PlanNode node = new AggregationNode(newId(), baseTableScan, ImmutableMap.of(aggregationSymbol, new Aggregation(new CallExpression("sum", // should be DOUBLE
FUNCTION_MANAGER.lookupFunction("sum", fromTypes(BIGINT)), DOUBLE, ImmutableList.of(castToRowExpression(toSymbolReference(columnC)))), ImmutableList.of(castToRowExpression(toSymbolReference(columnC))), false, Optional.empty(), Optional.empty(), Optional.empty())), singleGroupingSet(ImmutableList.of(columnA, columnB)), ImmutableList.of(), SINGLE, Optional.empty(), Optional.empty(), AggregationNode.AggregationType.HASH, Optional.empty());
assertTypesValid(node);
}
use of io.prestosql.spi.plan.AggregationNode.Aggregation in project hetu-core by openlookeng.
the class TestTypeValidator method testValidAggregation.
@Test
public void testValidAggregation() {
Symbol aggregationSymbol = planSymbolAllocator.newSymbol("sum", DOUBLE);
PlanNode node = new AggregationNode(newId(), baseTableScan, ImmutableMap.of(aggregationSymbol, new Aggregation(new CallExpression("sum", SUM, DOUBLE, ImmutableList.of(castToRowExpression(toSymbolReference(columnC)))), ImmutableList.of(castToRowExpression(toSymbolReference(columnC))), false, Optional.empty(), Optional.empty(), Optional.empty())), singleGroupingSet(ImmutableList.of(columnA, columnB)), ImmutableList.of(), SINGLE, Optional.empty(), Optional.empty(), AggregationNode.AggregationType.HASH, Optional.empty());
assertTypesValid(node);
}
Aggregations