@Override public Optional<Symbol> getAssignedSymbol(PlanNode node, Session session, Metadata metadata, SymbolAliases symbolAliases) { Optional<Symbol> result = Optional.empty(); if (!(node instanceof AggregationNode)) { return result; } AggregationNode aggregationNode = (AggregationNode) node; FunctionCall expectedCall = callMaker.getExpectedValue(symbolAliases); for (Map.Entry<Symbol, Aggregation> assignment : aggregationNode.getAggregations().entrySet()) { if (expectedCall.equals(assignment.getValue().getCall())) { checkState(!result.isPresent(), "Ambiguous function calls in %s", aggregationNode); result = Optional.of(assignment.getKey()); } } return result; }
@Override public Optional<Symbol> getAssignedSymbol(PlanNode node, Session session, Metadata metadata, SymbolAliases symbolAliases) { Optional<Symbol> result = Optional.empty(); if (!(node instanceof WindowNode)) { return result; } WindowNode windowNode = (WindowNode) node; FunctionCall expectedCall = callMaker.getExpectedValue(symbolAliases); Optional<WindowNode.Frame> expectedFrame = frameMaker.map(maker -> maker.getExpectedValue(symbolAliases)); List<Symbol> matchedOutputs = windowNode.getWindowFunctions().entrySet().stream() .filter(assignment -> expectedCall.equals(assignment.getValue().getFunctionCall()) && signature.map(assignment.getValue().getSignature()::equals).orElse(true) && expectedFrame.map(assignment.getValue().getFrame()::equals).orElse(true)) .map(Map.Entry::getKey) .collect(toImmutableList()); checkState(matchedOutputs.size() <= 1, "Ambiguous function calls in %s", windowNode); if (matchedOutputs.isEmpty()) { return Optional.empty(); } return Optional.of(matchedOutputs.get(0)); }
@Override public Result apply(AggregationNode aggregationNode, Captures captures, Context context) { boolean anyRewritten = false; ImmutableMap.Builder<Symbol, Aggregation> aggregations = ImmutableMap.builder(); for (Map.Entry<Symbol, Aggregation> entry : aggregationNode.getAggregations().entrySet()) { Aggregation aggregation = entry.getValue(); FunctionCall call = (FunctionCall) rewriter.rewrite(aggregation.getCall(), context); aggregations.put( entry.getKey(), new Aggregation(call, aggregation.getSignature(), aggregation.getMask())); if (!aggregation.getCall().equals(call)) { anyRewritten = true; } } if (anyRewritten) { return Result.ofPlanNode(new AggregationNode( aggregationNode.getId(), aggregationNode.getSource(), aggregations.build(), aggregationNode.getGroupingSets(), aggregationNode.getPreGroupedSymbols(), aggregationNode.getStep(), aggregationNode.getHashSymbol(), aggregationNode.getGroupIdSymbol())); } return Result.empty(); } }