Skip to content

Commit

Permalink
Extract AggregationNode.singleAggregation
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasz-stec authored and sopel39 committed May 27, 2022
1 parent 0bff849 commit beba0b8
Show file tree
Hide file tree
Showing 18 changed files with 68 additions and 140 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
import io.trino.sql.analyzer.Scope;
import io.trino.sql.planner.StatisticsAggregationPlanner.TableStatisticAggregation;
import io.trino.sql.planner.optimizations.PlanOptimizer;
import io.trino.sql.planner.plan.AggregationNode;
import io.trino.sql.planner.plan.Assignments;
import io.trino.sql.planner.plan.DeleteNode;
import io.trino.sql.planner.plan.ExplainAnalyzeNode;
Expand Down Expand Up @@ -134,6 +133,7 @@
import static io.trino.sql.planner.PlanBuilder.newPlanBuilder;
import static io.trino.sql.planner.QueryPlanner.visibleFields;
import static io.trino.sql.planner.SystemPartitioningHandle.FIXED_HASH_DISTRIBUTION;
import static io.trino.sql.planner.plan.AggregationNode.singleAggregation;
import static io.trino.sql.planner.plan.AggregationNode.singleGroupingSet;
import static io.trino.sql.planner.plan.TableWriterNode.CreateReference;
import static io.trino.sql.planner.plan.TableWriterNode.InsertReference;
Expand Down Expand Up @@ -362,15 +362,11 @@ private RelationPlan createAnalyzePlan(Analysis analysis, Analyze analyzeStateme

PlanNode planNode = new StatisticsWriterNode(
idAllocator.getNextId(),
new AggregationNode(
singleAggregation(
idAllocator.getNextId(),
TableScanNode.newInstance(idAllocator.getNextId(), targetTable, tableScanOutputs.build(), symbolToColumnHandle.buildOrThrow(), false, Optional.empty()),
statisticAggregations.getAggregations(),
singleGroupingSet(groupingSymbols),
ImmutableList.of(),
AggregationNode.Step.SINGLE,
Optional.empty(),
Optional.empty()),
singleGroupingSet(groupingSymbols)),
new StatisticsWriterNode.WriteStatisticsReference(targetTable),
symbolAllocator.newSymbol("rows", BIGINT),
tableStatisticsMetadata.getTableStatistics().contains(ROW_COUNT),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@
import static io.trino.sql.planner.PlanBuilder.newPlanBuilder;
import static io.trino.sql.planner.ScopeAware.scopeAwareKey;
import static io.trino.sql.planner.plan.AggregationNode.groupingSets;
import static io.trino.sql.planner.plan.AggregationNode.singleAggregation;
import static io.trino.sql.planner.plan.AggregationNode.singleGroupingSet;
import static io.trino.sql.planner.plan.WindowNode.Frame.DEFAULT_FRAME;
import static io.trino.sql.tree.BooleanLiteral.TRUE_LITERAL;
Expand Down Expand Up @@ -333,15 +334,11 @@ public RelationPlan planExpand(Query query)
PlanNode result = new UnionNode(idAllocator.getNextId(), nodesToUnion, unionSymbolMapping.build(), unionOutputSymbols);

if (union.isDistinct()) {
result = new AggregationNode(
result = singleAggregation(
idAllocator.getNextId(),
result,
ImmutableMap.of(),
singleGroupingSet(result.getOutputSymbols()),
ImmutableList.of(),
AggregationNode.Step.SINGLE,
Optional.empty(),
Optional.empty());
singleGroupingSet(result.getOutputSymbols()));
}

return new RelationPlan(result, anchorPlan.getScope(), unionOutputSymbols, outerContext);
Expand Down Expand Up @@ -1654,15 +1651,11 @@ private PlanBuilder distinct(PlanBuilder subPlan, QuerySpecification node, List<
.collect(Collectors.toList());

return subPlan.withNewRoot(
new AggregationNode(
singleAggregation(
idAllocator.getNextId(),
subPlan.getRoot(),
ImmutableMap.of(),
singleGroupingSet(symbols),
ImmutableList.of(),
AggregationNode.Step.SINGLE,
Optional.empty(),
Optional.empty()));
singleGroupingSet(symbols)));
}

return subPlan;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import io.trino.sql.analyzer.Field;
import io.trino.sql.analyzer.RelationType;
import io.trino.sql.analyzer.Scope;
import io.trino.sql.planner.plan.AggregationNode;
import io.trino.sql.planner.plan.Assignments;
import io.trino.sql.planner.plan.CorrelatedJoinNode;
import io.trino.sql.planner.plan.ExceptNode;
Expand Down Expand Up @@ -122,6 +121,7 @@
import static io.trino.sql.planner.QueryPlanner.extractPatternRecognitionExpressions;
import static io.trino.sql.planner.QueryPlanner.planWindowSpecification;
import static io.trino.sql.planner.QueryPlanner.pruneInvisibleFields;
import static io.trino.sql.planner.plan.AggregationNode.singleAggregation;
import static io.trino.sql.planner.plan.AggregationNode.singleGroupingSet;
import static io.trino.sql.tree.BooleanLiteral.TRUE_LITERAL;
import static io.trino.sql.tree.Join.Type.CROSS;
Expand Down Expand Up @@ -1160,14 +1160,10 @@ private SetOperationPlan process(SetOperation node)

private PlanNode distinct(PlanNode node)
{
return new AggregationNode(idAllocator.getNextId(),
return singleAggregation(idAllocator.getNextId(),
node,
ImmutableMap.of(),
singleGroupingSet(node.getOutputSymbols()),
ImmutableList.of(),
AggregationNode.Step.SINGLE,
Optional.empty(),
Optional.empty());
singleGroupingSet(node.getOutputSymbols()));
}

private static final class SetOperationPlan
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import static io.trino.sql.planner.iterative.rule.Util.restrictOutputs;
import static io.trino.sql.planner.optimizations.QueryCardinalityUtil.isScalar;
import static io.trino.sql.planner.plan.AggregationNode.Step.SINGLE;
import static io.trino.sql.planner.plan.AggregationNode.singleAggregation;
import static io.trino.sql.planner.plan.AggregationNode.singleGroupingSet;
import static io.trino.sql.planner.plan.JoinNode.Type.INNER;
import static io.trino.sql.planner.plan.JoinNode.Type.LEFT;
Expand Down Expand Up @@ -337,15 +338,11 @@ private static AggregationNode withGroupingAndMask(AggregationNode aggregationNo
.build());
}

return new AggregationNode(
return singleAggregation(
aggregationNode.getId(),
source,
rewriteWithMasks(aggregationNode.getAggregations(), masks.buildOrThrow()),
singleGroupingSet(groupingSymbols),
ImmutableList.of(),
SINGLE,
Optional.empty(),
Optional.empty());
singleGroupingSet(groupingSymbols));
}

private static AggregationNode withGrouping(AggregationNode aggregationNode, List<Symbol> groupingSymbols, PlanNode source)
Expand All @@ -354,14 +351,10 @@ private static AggregationNode withGrouping(AggregationNode aggregationNode, Lis
.distinct()
.collect(toImmutableList()));

return new AggregationNode(
return singleAggregation(
aggregationNode.getId(),
source,
aggregationNode.getAggregations(),
groupingSet,
ImmutableList.of(),
SINGLE,
Optional.empty(),
Optional.empty());
groupingSet);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
*/
package io.trino.sql.planner.iterative.rule;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Streams;
import io.trino.matching.Captures;
Expand Down Expand Up @@ -42,6 +41,7 @@
import static io.trino.sql.planner.iterative.rule.Util.restrictOutputs;
import static io.trino.sql.planner.optimizations.QueryCardinalityUtil.isScalar;
import static io.trino.sql.planner.plan.AggregationNode.Step.SINGLE;
import static io.trino.sql.planner.plan.AggregationNode.singleAggregation;
import static io.trino.sql.planner.plan.AggregationNode.singleGroupingSet;
import static io.trino.sql.planner.plan.JoinNode.Type.LEFT;
import static io.trino.sql.planner.plan.Patterns.CorrelatedJoin.correlation;
Expand Down Expand Up @@ -264,14 +264,10 @@ private static AggregationNode withGrouping(AggregationNode aggregationNode, Lis
.distinct()
.collect(toImmutableList()));

return new AggregationNode(
return singleAggregation(
aggregationNode.getId(),
source,
aggregationNode.getAggregations(),
groupingSet,
ImmutableList.of(),
SINGLE,
Optional.empty(),
Optional.empty());
groupingSet);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import static io.trino.sql.planner.optimizations.DistinctOutputQueryUtil.isDistinct;
import static io.trino.sql.planner.optimizations.SymbolMapper.symbolMapper;
import static io.trino.sql.planner.plan.AggregationNode.globalAggregation;
import static io.trino.sql.planner.plan.AggregationNode.singleAggregation;
import static io.trino.sql.planner.plan.AggregationNode.singleGroupingSet;
import static io.trino.sql.planner.plan.Patterns.aggregation;
import static io.trino.sql.planner.plan.Patterns.join;
Expand Down Expand Up @@ -309,15 +310,11 @@ private MappedAggregationInfo createAggregationOverNull(AggregationNode referenc
Map<Symbol, Symbol> aggregationsSymbolMapping = aggregationsSymbolMappingBuilder.buildOrThrow();

// create an aggregation node whose source is the null row.
AggregationNode aggregationOverNullRow = new AggregationNode(
AggregationNode aggregationOverNullRow = singleAggregation(
idAllocator.getNextId(),
nullRow,
aggregationsOverNullBuilder.buildOrThrow(),
globalAggregation(),
ImmutableList.of(),
AggregationNode.Step.SINGLE,
Optional.empty(),
Optional.empty());
globalAggregation());

return new MappedAggregationInfo(aggregationOverNullRow, aggregationsSymbolMapping);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,16 @@
import io.trino.matching.Pattern;
import io.trino.sql.planner.Symbol;
import io.trino.sql.planner.iterative.Rule;
import io.trino.sql.planner.plan.AggregationNode;
import io.trino.sql.planner.plan.AggregationNode.Step;
import io.trino.sql.planner.plan.Assignments;
import io.trino.sql.planner.plan.ExceptNode;
import io.trino.sql.planner.plan.PlanNode;
import io.trino.sql.planner.plan.ProjectNode;
import io.trino.sql.planner.plan.ValuesNode;

import java.util.List;
import java.util.Optional;

import static io.trino.sql.planner.optimizations.QueryCardinalityUtil.isEmpty;
import static io.trino.sql.planner.plan.AggregationNode.singleAggregation;
import static io.trino.sql.planner.plan.AggregationNode.singleGroupingSet;
import static io.trino.sql.planner.plan.Patterns.except;

Expand Down Expand Up @@ -96,18 +94,14 @@ public Result apply(ExceptNode node, Captures captures, Context context)

if (node.isDistinct()) {
return Result.ofPlanNode(
new AggregationNode(
singleAggregation(
node.getId(),
new ProjectNode(
context.getIdAllocator().getNextId(),
newSources.get(0),
assignments.build()),
ImmutableMap.of(),
singleGroupingSet(node.getOutputSymbols()),
ImmutableList.of(),
Step.SINGLE,
Optional.empty(),
Optional.empty()));
singleGroupingSet(node.getOutputSymbols())));
}

return Result.ofPlanNode(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import static io.trino.spi.type.BooleanType.BOOLEAN;
import static io.trino.sql.analyzer.TypeSignatureProvider.fromTypes;
import static io.trino.sql.analyzer.TypeSignatureTranslator.toSqlType;
import static io.trino.sql.planner.plan.AggregationNode.singleAggregation;
import static io.trino.sql.planner.plan.AggregationNode.singleGroupingSet;
import static io.trino.sql.tree.BooleanLiteral.TRUE_LITERAL;
import static io.trino.sql.tree.FrameBound.Type.UNBOUNDED_FOLLOWING;
Expand Down Expand Up @@ -180,14 +181,10 @@ private AggregationNode computeCounts(UnionNode sourceNode, List<Symbol> origina
Optional.empty()));
}

return new AggregationNode(idAllocator.getNextId(),
return singleAggregation(idAllocator.getNextId(),
sourceNode,
aggregations.buildOrThrow(),
singleGroupingSet(originalColumns),
ImmutableList.of(),
AggregationNode.Step.SINGLE,
Optional.empty(),
Optional.empty());
singleGroupingSet(originalColumns));
}

private WindowNode appendCounts(UnionNode sourceNode, List<Symbol> originalColumns, List<Symbol> markers, List<Symbol> countOutputs, Symbol rowNumberSymbol)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,12 @@
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static com.google.common.base.Preconditions.checkArgument;
import static io.trino.sql.planner.plan.AggregationNode.Step.SINGLE;
import static io.trino.sql.planner.plan.AggregationNode.singleAggregation;
import static io.trino.sql.planner.plan.AggregationNode.singleGroupingSet;
import static io.trino.sql.planner.plan.Patterns.aggregation;
import static java.util.Collections.emptyList;
Expand Down Expand Up @@ -125,18 +124,14 @@ public Result apply(AggregationNode aggregation, Captures captures, Context cont
return Result.ofPlanNode(
new AggregationNode(
aggregation.getId(),
new AggregationNode(
singleAggregation(
context.getIdAllocator().getNextId(),
aggregation.getSource(),
ImmutableMap.of(),
singleGroupingSet(ImmutableList.<Symbol>builder()
.addAll(aggregation.getGroupingKeys())
.addAll(symbols)
.build()),
ImmutableList.of(),
SINGLE,
Optional.empty(),
Optional.empty()),
.build())),
// remove DISTINCT flag from function calls
aggregation.getAggregations()
.entrySet().stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
import static io.trino.sql.ExpressionUtils.and;
import static io.trino.sql.ExpressionUtils.or;
import static io.trino.sql.analyzer.TypeSignatureTranslator.toSqlType;
import static io.trino.sql.planner.plan.AggregationNode.singleAggregation;
import static io.trino.sql.planner.plan.AggregationNode.singleGroupingSet;
import static io.trino.sql.planner.plan.Patterns.Apply.correlation;
import static io.trino.sql.planner.plan.Patterns.applyNode;
Expand Down Expand Up @@ -216,18 +217,14 @@ private PlanNode buildInPredicateEquivalent(
Symbol countMatchesSymbol = symbolAllocator.newSymbol("countMatches", BIGINT);
Symbol countNullMatchesSymbol = symbolAllocator.newSymbol("countNullMatches", BIGINT);

AggregationNode aggregation = new AggregationNode(
AggregationNode aggregation = singleAggregation(
idAllocator.getNextId(),
preProjection,
ImmutableMap.<Symbol, AggregationNode.Aggregation>builder()
.put(countMatchesSymbol, countWithFilter(session, matchConditionSymbol))
.put(countNullMatchesSymbol, countWithFilter(session, nullMatchConditionSymbol))
.buildOrThrow(),
singleGroupingSet(probeSide.getOutputSymbols()),
ImmutableList.of(),
AggregationNode.Step.SINGLE,
Optional.empty(),
Optional.empty());
singleGroupingSet(probeSide.getOutputSymbols()));

// TODO since we care only about "some count > 0", we could have specialized node instead of leftOuterJoin that does the job without materializing join results
SearchedCaseExpression inPredicateEquivalent = new SearchedCaseExpression(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import io.trino.sql.planner.Symbol;
import io.trino.sql.planner.iterative.Rule;
import io.trino.sql.planner.optimizations.PlanNodeDecorrelator;
import io.trino.sql.planner.plan.AggregationNode;
import io.trino.sql.planner.plan.AggregationNode.Aggregation;
import io.trino.sql.planner.plan.ApplyNode;
import io.trino.sql.planner.plan.Assignments;
Expand All @@ -47,6 +46,7 @@
import static io.trino.spi.type.BooleanType.BOOLEAN;
import static io.trino.sql.analyzer.TypeSignatureTranslator.toSqlType;
import static io.trino.sql.planner.plan.AggregationNode.globalAggregation;
import static io.trino.sql.planner.plan.AggregationNode.singleAggregation;
import static io.trino.sql.planner.plan.CorrelatedJoinNode.Type.INNER;
import static io.trino.sql.planner.plan.CorrelatedJoinNode.Type.LEFT;
import static io.trino.sql.planner.plan.Patterns.applyNode;
Expand Down Expand Up @@ -174,7 +174,7 @@ private PlanNode rewriteToDefaultAggregation(ApplyNode applyNode, Context contex
applyNode.getInput(),
new ProjectNode(
context.getIdAllocator().getNextId(),
new AggregationNode(
singleAggregation(
context.getIdAllocator().getNextId(),
applyNode.getSubquery(),
ImmutableMap.of(count, new Aggregation(
Expand All @@ -184,11 +184,7 @@ private PlanNode rewriteToDefaultAggregation(ApplyNode applyNode, Context contex
Optional.empty(),
Optional.empty(),
Optional.empty())),
globalAggregation(),
ImmutableList.of(),
AggregationNode.Step.SINGLE,
Optional.empty(),
Optional.empty()),
globalAggregation()),
Assignments.of(exists, new ComparisonExpression(GREATER_THAN, count.toSymbolReference(), new Cast(new LongLiteral("0"), toSqlType(BIGINT))))),
applyNode.getCorrelation(),
INNER,
Expand Down
Loading

0 comments on commit beba0b8

Please sign in to comment.