public void onMatch(final RelOptRuleCall call) {
    Project topProject = call.rel(0);
    Join join = call.rel(1);

    // 1. We check if it is a permutation project. If it is
    //    not, or this is the identity, the rule will do nothing
    final Permutation topPermutation = topProject.getPermutation();
    if (topPermutation == null) {
      return;
    }
    if (topPermutation.isIdentity()) {
      return;
    }

    // 2. We swap the join
    final RelNode swapped = JoinCommuteRule.swap(join,true);
    if (swapped == null) {
      return;
    }

    // 3. The result should have a project on top, otherwise we
    //    bail out.
    if (swapped instanceof Join) {
      return;
    }

    // 4. We check if it is a permutation project. If it is
    //    not, or this is the identity, the rule will do nothing
    final Project bottomProject = (Project) swapped;
    final Permutation bottomPermutation = bottomProject.getPermutation();
    if (bottomPermutation == null) {
      return;
    }
    if (bottomPermutation.isIdentity()) {
      return;
    }

    // 5. If the product of the topPermutation and bottomPermutation yields
    //    the identity, then we can swap the join and remove the project on
    //    top.
    final Permutation product = topPermutation.product(bottomPermutation);
    if (!product.isIdentity()) {
      return;
    }

    // 6. Return the new join as a replacement
    final Join swappedJoin = (Join) bottomProject.getInput(0);
    call.transformTo(swappedJoin);
  }
Planner rule that permutes the inputs of a Join, if it has a Project on top
that simply swaps the fields of both inputs.
ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/rules/HiveJoinCommuteRule.java