@Override public PlanNode visitGroupId(GroupIdNode node, RewriteContext<Void> context) { PlanNode source = context.rewrite(node.getSource()); Map<Symbol, Symbol> newGroupingMappings = new HashMap<>(); ImmutableList.Builder<List<Symbol>> newGroupingSets = ImmutableList.builder(); for (List<Symbol> groupingSet : node.getGroupingSets()) { ImmutableList.Builder<Symbol> newGroupingSet = ImmutableList.builder(); for (Symbol output : groupingSet) { newGroupingMappings.putIfAbsent(canonicalize(output), canonicalize(node.getGroupingColumns().get(output))); newGroupingSet.add(canonicalize(output)); } newGroupingSets.add(newGroupingSet.build()); } return new GroupIdNode(node.getId(), source, newGroupingSets.build(), newGroupingMappings, canonicalizeAndDistinct(node.getAggregationArguments()), canonicalize(node.getGroupIdSymbol())); }
@Override public Void visitGroupId(GroupIdNode node, Set<Symbol> boundSymbols) { PlanNode source = node.getSource(); source.accept(this, boundSymbols); // visit child checkDependencies(source.getOutputSymbols(), node.getInputSymbols(), "Invalid node. Grouping symbols (%s) not in source plan output (%s)", node.getInputSymbols(), source.getOutputSymbols()); return null; }
@Override public Void visitGroupId(GroupIdNode node, Integer indent) { // grouping sets are easier to understand in terms of inputs List<List<Symbol>> inputGroupingSetSymbols = node.getGroupingSets().stream() .map(set -> set.stream() .map(symbol -> node.getGroupingColumns().get(symbol)) .collect(Collectors.toList())) .collect(Collectors.toList()); print(indent, "- GroupId%s => [%s]", inputGroupingSetSymbols, formatOutputs(node.getOutputSymbols())); printPlanNodesStatsAndCost(indent + 2, node); printStats(indent + 2, node.getId()); for (Map.Entry<Symbol, Symbol> mapping : node.getGroupingColumns().entrySet()) { print(indent + 2, "%s := %s", mapping.getKey(), mapping.getValue()); } return processChildren(node, indent + 1); }
@Override public PlanNode replaceChildren(List<PlanNode> newChildren) { return new GroupIdNode(getId(), Iterables.getOnlyElement(newChildren), groupingSets, groupingColumns, aggregationArguments, groupIdSymbol); } }
private Function<Symbol, Optional<Symbol>> translateGroupIdSymbols(GroupIdNode node) { return symbol -> { if (node.getAggregationArguments().contains(symbol)) { return Optional.of(symbol); } if (node.getCommonGroupingColumns().contains(symbol)) { return Optional.of(node.getGroupingColumns().get(symbol)); } return Optional.empty(); }; }
@Override public Void visitGroupId(GroupIdNode node, Void context) { // grouping sets are easier to understand in terms of inputs List<String> inputGroupingSetSymbols = node.getGroupingSets().stream() .map(set -> "(" + Joiner.on(", ").join(set.stream() .map(symbol -> node.getGroupingColumns().get(symbol)) .collect(Collectors.toList())) + ")") .collect(Collectors.toList()); printNode(node, "GroupId", Joiner.on(", ").join(inputGroupingSetSymbols), NODE_COLORS.get(NodeType.AGGREGATE)); return node.getSource().accept(this, context); }
@Override public PlanNode visitGroupId(GroupIdNode node, List<PlanNode> newChildren) { return new GroupIdNode(node.getId(), Iterables.getOnlyElement(newChildren), node.getInputSymbols(), node.getGroupingSets(), node.getGroupIdSymbol()); }
@Override public PlanNode visitGroupId(GroupIdNode node, RewriteContext<Set<Symbol>> context) { checkState(node.getDistinctGroupingColumns().stream().allMatch(column -> context.get().contains(column))); PlanNode source = context.rewrite(node.getSource(), ImmutableSet.copyOf(context.get())); List<Symbol> requiredSymbols = context.get().stream() .filter(symbol -> !symbol.equals(node.getGroupIdSymbol())) .collect(toImmutableList()); return new GroupIdNode(node.getId(), source, requiredSymbols, node.getGroupingSets(), node.getGroupIdSymbol()); }
@Override public PhysicalOperation visitGroupId(GroupIdNode node, LocalExecutionPlanContext context) { PhysicalOperation source = node.getSource().accept(this, context); // add groupId to the layout int groupIdChannel = source.getLayout().values().stream() .mapToInt(Integer::intValue) .max() .orElse(-1) + 1; Map<Symbol, Integer> newLayout = ImmutableMap.<Symbol, Integer>builder() .putAll(source.getLayout()) .put(node.getGroupIdSymbol(), groupIdChannel) .build(); List<List<Integer>> groupingSetChannels = node.getGroupingSets().stream() .map(groupingSet -> getChannelsForSymbols(groupingSet, source.getLayout())) .collect(toImmutableList()); OperatorFactory groupIdOperatorFactory = new GroupIdOperator.GroupIdOperatorFactory(context.getNextOperatorId(), node.getId(), source.getTypes(), groupingSetChannels); return new PhysicalOperation(groupIdOperatorFactory, newLayout, source); }
@Override public MatchResult detailMatches(PlanNode node, StatsProvider stats, Session session, Metadata metadata, SymbolAliases symbolAliases) { checkState(shapeMatches(node), "Plan testing framework error: shapeMatches returned false in detailMatches in %s", this.getClass().getName()); GroupIdNode groudIdNode = (GroupIdNode) node; List<List<Symbol>> actualGroups = groudIdNode.getGroupingSets(); List<Symbol> actualAggregationArguments = groudIdNode.getAggregationArguments(); if (actualGroups.size() != groups.size()) { return NO_MATCH; } for (int i = 0; i < actualGroups.size(); i++) { if (!AggregationMatcher.matches(groups.get(i), actualGroups.get(i), symbolAliases)) { return NO_MATCH; } } if (!AggregationMatcher.matches(identityMappings.keySet(), actualAggregationArguments, symbolAliases)) { return NO_MATCH; } return match(groupIdAlias, groudIdNode.getGroupIdSymbol().toSymbolReference()); }
@Override public Void visitGroupId(GroupIdNode node, Void context) { node.getSource().accept(this, context); builder.add(node.getGroupIdSymbol()); return null; }
@Override public Void visitGroupId(GroupIdNode node, Integer indent) { print(indent, "- GroupId%s => [%s]", node.getGroupingSets(), formatOutputs(node.getOutputSymbols())); return processChildren(node, indent + 1); }
@Override public Map<PlanNodeId, SplitSource> visitGroupId(GroupIdNode node, Void context) { return node.getSource().accept(this, context); }
groups.add(ImmutableList.copyOf(group1)); return new GroupIdNode( idAllocator.getNextId(), source,
@Override public PlanNode visitGroupId(GroupIdNode node, RewriteContext<Expression> context) { checkState(!DependencyExtractor.extractUnique(context.get()).contains(node.getGroupIdSymbol()), "groupId symbol cannot be referenced in predicate"); return context.defaultRewrite(node, context.get()); }
@Override public PlanNode visitGroupId(GroupIdNode node, RewriteContext<Expression> context) { Map<Symbol, SymbolReference> commonGroupingSymbolMapping = node.getGroupingColumns().entrySet().stream() .filter(entry -> node.getCommonGroupingColumns().contains(entry.getKey())) .collect(Collectors.toMap(Map.Entry::getKey, entry -> entry.getValue().toSymbolReference())); Predicate<Expression> pushdownEligiblePredicate = conjunct -> SymbolsExtractor.extractUnique(conjunct).stream() .allMatch(commonGroupingSymbolMapping.keySet()::contains); Map<Boolean, List<Expression>> conjuncts = extractConjuncts(context.get()).stream().collect(Collectors.partitioningBy(pushdownEligiblePredicate)); // Push down conjuncts from the inherited predicate that apply to common grouping symbols PlanNode rewrittenNode = context.defaultRewrite(node, inlineSymbols(commonGroupingSymbolMapping, combineConjuncts(conjuncts.get(true)))); // All other conjuncts, if any, will be in the filter node. if (!conjuncts.get(false).isEmpty()) { rewrittenNode = new FilterNode(idAllocator.getNextId(), rewrittenNode, combineConjuncts(conjuncts.get(false))); } return rewrittenNode; }
@Override public PlanNode visitGroupId(GroupIdNode node, RewriteContext<Void> context) { PlanNode source = context.rewrite(node.getSource()); List<List<Symbol>> groupingSetsSymbols = node.getGroupingSets().stream() .map(this::canonicalize) .collect(Collectors.toList()); return new GroupIdNode(node.getId(), source, canonicalize(node.getInputSymbols()), groupingSetsSymbols, canonicalize(node.getGroupIdSymbol())); }
@Override public ActualProperties visitGroupId(GroupIdNode node, List<ActualProperties> inputProperties) { Map<Symbol, Symbol> inputToOutputMappings = new HashMap<>(); for (Map.Entry<Symbol, Symbol> setMapping : node.getGroupingColumns().entrySet()) { if (node.getCommonGroupingColumns().contains(setMapping.getKey())) { // TODO: Add support for translating a property on a single column to multiple columns // when GroupIdNode is copying a single input grouping column into multiple output grouping columns (i.e. aliases), this is basically picking one arbitrarily inputToOutputMappings.putIfAbsent(setMapping.getValue(), setMapping.getKey()); } } // TODO: Add support for translating a property on a single column to multiple columns // this is deliberately placed after the grouping columns, because preserving properties has a bigger perf impact for (Symbol argument : node.getAggregationArguments()) { inputToOutputMappings.putIfAbsent(argument, argument); } return Iterables.getOnlyElement(inputProperties).translate(column -> Optional.ofNullable(inputToOutputMappings.get(column))); }
@Override public PlanWithProperties visitGroupId(GroupIdNode node, HashComputationSet parentPreference) { // remove any hash symbols not exported by the source of this node return planSimpleNodeWithProperties(node, parentPreference.pruneSymbols(node.getSource().getOutputSymbols())); }
if (groupingSets.size() > 1) { groupIdSymbol = Optional.of(symbolAllocator.newSymbol("groupId", BIGINT)); GroupIdNode groupId = new GroupIdNode(idAllocator.getNextId(), subPlan.getRoot(), groupingSets, groupingSetMappings, aggregationArguments, groupIdSymbol.get()); subPlan = new PlanBuilder(groupingTranslations, groupId, analysis.getParameters());