Source:
private Scope computeAndAssignOrderByScopeWithAggregation(OrderBy node, Scope sourceScope, Scope outputScope, List<FunctionCall> aggregations, List<List<Expression>> groupByExpressions, List<GroupingOperation> groupingOperations) { ImmutableList.Builder<Expression> orderByAggregationExpressionsBuilder = ImmutableList.builder(); groupByExpressions.stream() .flatMap(List::stream) .forEach(orderByAggregationExpressionsBuilder::add); orderByAggregationExpressionsBuilder.addAll(aggregations); orderByAggregationExpressionsBuilder.addAll(groupingOperations); List<Expression> orderByExpressionsReferencingOutputScope = AstUtils.preOrder(node) .filter(Expression.class::isInstance) .map(Expression.class::cast) .filter(expression -> hasReferencesToScope(expression, analysis, outputScope)) .collect(toImmutableList()); List<Expression> orderByAggregationExpressions = orderByAggregationExpressionsBuilder.build().stream() .filter(expression -> !orderByExpressionsReferencingOutputScope.contains(expression)) .collect(toImmutableList()); Set<Field> seen = new HashSet<>(); List<Field> orderByAggregationSourceFields = orderByAggregationExpressions.stream() .map(expression -> { Optional<Field> sourceField = sourceScope.tryResolveField(expression) .filter(ResolvedField::isLocal) .filter(resolvedField -> seen.add(resolvedField.getField())) .map(resolvedField -> resolvedField.getField()); return sourceField .orElse(Field.newUnqualified(Optional.empty(), analysis.getType(expression))); }) .collect(toImmutableList()); Scope orderByAggregationScope = Scope.builder() .withRelationType(RelationId.anonymous(), new RelationType(orderByAggregationSourceFields)) .build(); Scope orderByScope = Scope.builder() .withParent(orderByAggregationScope) .withRelationType(outputScope.getRelationId(), outputScope.getRelationType()) .build(); analysis.setScope(node, orderByScope); analysis.setOrderByAggregates(node, orderByAggregationExpressions); return orderByScope; }

Target:
private Scope computeAndAssignOrderByScopeWithAggregation(OrderBy node, Scope sourceScope, Scope outputScope, List<FunctionCall> aggregations, List<List<Expression>> groupByExpressions, List<GroupingOperation> groupingOperations) { ImmutableList.Builder<Expression> orderByAggregationExpressionsBuilder = ImmutableList.builder(); groupByExpressions.stream() .flatMap(List::stream) .forEach(orderByAggregationExpressionsBuilder::add); orderByAggregationExpressionsBuilder.addAll(aggregations); orderByAggregationExpressionsBuilder.addAll(groupingOperations); List<Expression> orderByExpressionsReferencingOutputScope = AstUtils.preOrder(node) .filter(Expression.class::isInstance) .map(Expression.class::cast) .filter(expression -> hasReferencesToScope(expression, analysis, outputScope)) .collect(toImmutableList()); List<Expression> orderByAggregationExpressions = orderByAggregationExpressionsBuilder.build().stream() .filter(expression -> !orderByExpressionsReferencingOutputScope.contains(expression) || analysis.getColumnReferences().contains(NodeRef.of(expression))) .collect(toImmutableList()); Set<Field> seen = new HashSet<>(); List<Field> orderByAggregationSourceFields = orderByAggregationExpressions.stream() .map(expression -> { Optional<Field> sourceField = sourceScope.tryResolveField(expression) .filter(resolvedField -> seen.add(resolvedField.getField())) .map(ResolvedField::getField); return sourceField .orElse(Field.newUnqualified(Optional.empty(), analysis.getType(expression))); }) .collect(toImmutableList()); Scope orderByAggregationScope = Scope.builder() .withRelationType(RelationId.anonymous(), new RelationType(orderByAggregationSourceFields)) .build(); Scope orderByScope = Scope.builder() .withParent(orderByAggregationScope) .withRelationType(outputScope.getRelationId(), outputScope.getRelationType()) .build(); analysis.setScope(node, orderByScope); analysis.setOrderByAggregates(node, orderByAggregationExpressions); return orderByScope; }