### Question:
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())); }  @Override Pattern<ApplyNode> getPattern(); @Override Result apply(ApplyNode node, Captures captures, Context context);  }### Answer:
@Test public void testExistsFalse() { tester().assertThat(new RemoveRedundantExists()) .on(p -> p.apply(Assignments.of(p.symbol("exists"), new ExistsPredicate(TRUE_LITERAL)), ImmutableList.of(), p.values(1), p.values(0))) .matches( project( ImmutableMap.of("exists", expression("false")), values())); }
@Test public void testExistsTrue() { tester().assertThat(new RemoveRedundantExists()) .on(p -> p.apply(Assignments.of(p.symbol("exists"), new ExistsPredicate(TRUE_LITERAL)), ImmutableList.of(), p.values(1), p.values(1))) .matches( project( ImmutableMap.of("exists", expression("true")), values())); }
@Test public void testDoesNotFire() { tester().assertThat(new RemoveRedundantExists()) .on(p -> p.apply(Assignments.of(p.symbol("exists"), new ExistsPredicate(TRUE_LITERAL)), ImmutableList.of(), p.values(1), p.tableScan(ImmutableList.of(), ImmutableMap.of()))) .doesNotFire(); tester().assertThat(new RemoveRedundantExists()) .on(p -> p.apply( Assignments.builder() .put(p.symbol("exists"), new ExistsPredicate(TRUE_LITERAL)) .put(p.symbol("other"), new InPredicate(new SymbolReference("value"), new SymbolReference("list"))) .build(), ImmutableList.of(), p.values(1, p.symbol("value")), p.tableScan(ImmutableList.of(p.symbol("list")), ImmutableMap.of(p.symbol("list"), new TestingMetadata.TestingColumnHandle("list"))))) .doesNotFire(); }