presto-main/src/main/java/io/prestosql/sql/planner/iterative/rule/PruneWindowColumns.java/PruneWindowColumns/pushDownProjectOff
class PruneWindowColumns:    @Override
    protected Optional<PlanNode> pushDownProjectOff(PlanNodeIdAllocator idAllocator, WindowNode windowNode, Set<Symbol> referencedOutputs)
    {
        Map<Symbol, WindowNode.Function> referencedFunctions = Maps.filterKeys(
                windowNode.getWindowFunctions(),
                referencedOutputs::contains);

        if (referencedFunctions.isEmpty()) {
            return Optional.of(windowNode.getSource());
        }

        ImmutableSet.Builder<Symbol> referencedInputs = ImmutableSet.<Symbol>builder()
                .addAll(windowNode.getSource().getOutputSymbols().stream()
                        .filter(referencedOutputs::contains)
                        .iterator())
                .addAll(windowNode.getPartitionBy());

        windowNode.getOrderingScheme().ifPresent(
                orderingScheme -> orderingScheme
                        .getOrderBy()
                        .forEach(referencedInputs::add));
        windowNode.getHashSymbol().ifPresent(referencedInputs::add);

        for (WindowNode.Function windowFunction : referencedFunctions.values()) {
            referencedInputs.addAll(SymbolsExtractor.extractUnique(windowFunction));
        }

        PlanNode prunedWindowNode = new WindowNode(
                windowNode.getId(),
                restrictOutputs(idAllocator, windowNode.getSource(), referencedInputs.build())
                        .orElse(windowNode.getSource()),
                windowNode.getSpecification(),
                referencedFunctions,
                windowNode.getHashSymbol(),
                windowNode.getPrePartitionedInputs(),
                windowNode.getPreSortedOrderPrefix());

        if (prunedWindowNode.getOutputSymbols().size() == windowNode.getOutputSymbols().size()) {
            // Neither function pruning nor input pruning was successful.
            return Optional.empty();
        }

        return Optional.of(prunedWindowNode);
    }