class RemoveRedundantExists implements Rule<ApplyNode> {

    @Override
    public Result apply(ApplyNode node, Captures captures, Context context)
    {
        Assignments.Builder assignments = Assignments.builder();
        assignments.putIdentities(node.getInput().getOutputSymbols());

        Expression result;
        if (QueryCardinalityUtil.isEmpty(node.getSubquery(), context.getLookup())) {
            result = FALSE_LITERAL;
        }
        else if (QueryCardinalityUtil.isAtLeastScalar(node.getSubquery(), context.getLookup())) {
            result = TRUE_LITERAL;
        }
        else {
            return Result.empty();
        }

        for (Symbol output : node.getSubqueryAssignments().getOutputs()) {
            assignments.put(output, result);
        }

        return Result.ofPlanNode(new ProjectNode(
                context.getIdAllocator().getNextId(),
                node.getInput(),
                assignments.build()));
    }

}