class PruneApplyCorrelation implements Rule<ApplyNode> {

    @Override
    public Result apply(ApplyNode applyNode, Captures captures, Context context)
    {
        Set<Symbol> subquerySymbols = extractUnique(applyNode.getSubquery(), context.getLookup());
        List<Symbol> newCorrelation = applyNode.getCorrelation().stream()
                .filter(subquerySymbols::contains)
                .collect(toImmutableList());

        if (newCorrelation.size() < applyNode.getCorrelation().size()) {
            return Result.ofPlanNode(new ApplyNode(
                    applyNode.getId(),
                    applyNode.getInput(),
                    applyNode.getSubquery(),
                    applyNode.getSubqueryAssignments(),
                    newCorrelation,
                    applyNode.getOriginSubquery()));
        }

        return Result.empty();
    }

}