class MoveOrderBeneathNestedLoop implements Rule<Order> {

    private final Capture<NestedLoopJoin> nlCapture;
    private final Pattern<Order> pattern;

    public MoveOrderBeneathNestedLoop() {
        this.nlCapture = new Capture<>();
        this.pattern = typeOf(Order.class)
            .with(source(),
                  typeOf(NestedLoopJoin.class)
                      .capturedAs(nlCapture)
                      .with(nl -> !nl.joinType().isOuter())
            );
    }

    @Override
    public Pattern<Order> pattern() {
        return pattern;
    }

    @Override
    public LogicalPlan apply(Order order,
                             Captures captures,
                             PlanStats planStats,
                             TransactionContext txnCtx,
                             NodeContext nodeCtx,
                             UnaryOperator<LogicalPlan> resolvePlan) {<FILL_FUNCTION_BODY>}
}

        NestedLoopJoin nestedLoop = captures.get(nlCapture);
        Set<RelationName> relationsInOrderBy =
            Collections.newSetFromMap(new IdentityHashMap<>());
        Consumer<ScopedSymbol> gatherRelationsFromField = f -> relationsInOrderBy.add(f.relation());
        Consumer<Reference> gatherRelationsFromRef = r -> relationsInOrderBy.add(r.ident().tableIdent());
        OrderBy orderBy = order.orderBy();
        for (Symbol orderExpr : orderBy.orderBySymbols()) {
            FieldsVisitor.visitFields(orderExpr, gatherRelationsFromField);
            RefVisitor.visitRefs(orderExpr, gatherRelationsFromRef);
        }
        if (relationsInOrderBy.size() == 1) {
            var relationInOrderBy = relationsInOrderBy.iterator().next();
            var topMostLeftRelation = nestedLoop.relationNames().get(0);
            if (relationInOrderBy.equals(topMostLeftRelation)) {
                LogicalPlan lhs = nestedLoop.sources().get(0);
                LogicalPlan newLhs = order.replaceSources(List.of(lhs));
                return new NestedLoopJoin(
                    newLhs,
                    nestedLoop.sources().get(1),
                    nestedLoop.joinType(),
                    nestedLoop.joinCondition(),
                    nestedLoop.isFiltered(),
                    true,
                    nestedLoop.isRewriteNestedLoopJoinToHashJoinDone()
                );
            }
        }
        return null;
    
<no_super_class>