### Question:
TransformUncorrelatedInPredicateSubqueryToSemiJoin implements Rule<ApplyNode> { @Override public Result apply(ApplyNode applyNode, Captures captures, Context context) { if (applyNode.getSubqueryAssignments().size() != 1) { return Result.empty(); } Expression expression = getOnlyElement(applyNode.getSubqueryAssignments().getExpressions()); if (!(expression instanceof InPredicate)) { return Result.empty(); } InPredicate inPredicate = (InPredicate) expression; Symbol semiJoinSymbol = getOnlyElement(applyNode.getSubqueryAssignments().getSymbols()); SemiJoinNode replacement = new SemiJoinNode(context.getIdAllocator().getNextId(), applyNode.getInput(), applyNode.getSubquery(), Symbol.from(inPredicate.getValue()), Symbol.from(inPredicate.getValueList()), semiJoinSymbol, Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty()); return Result.ofPlanNode(replacement); }  @Override Pattern<ApplyNode> getPattern(); @Override Result apply(ApplyNode applyNode, Captures captures, Context context);  }### Answer:
@Test public void testDoesNotFireOnNoCorrelation() { tester().assertThat(new TransformUncorrelatedInPredicateSubqueryToSemiJoin()) .on(p -> p.apply( Assignments.of(), emptyList(), p.values(), p.values())) .doesNotFire(); }
@Test public void testDoesNotFireOnNonInPredicateSubquery() { tester().assertThat(new TransformUncorrelatedInPredicateSubqueryToSemiJoin()) .on(p -> p.apply( Assignments.of(p.symbol("x"), new ExistsPredicate(new LongLiteral("1"))), emptyList(), p.values(), p.values())) .doesNotFire(); }
@Test public void testFiresForInPredicate() { tester().assertThat(new TransformUncorrelatedInPredicateSubqueryToSemiJoin()) .on(p -> p.apply( Assignments.of( p.symbol("x"), new InPredicate( new SymbolReference("y"), new SymbolReference("z"))), emptyList(), p.values(p.symbol("y")), p.values(p.symbol("z")))) .matches(node(SemiJoinNode.class, values("y"), values("z"))); }