private static boolean areSelfJoinKeysUnique(RelMetadataQuery mq,
                                               RelNode leftRel, RelNode rightRel, RexNode joinFilters) {
    final JoinInfo joinInfo = JoinInfo.of(leftRel, rightRel, joinFilters);

    // Make sure each key on the left maps to the same simple column as the
    // corresponding key on the right
    for (IntPair pair : joinInfo.pairs()) {
      final RelColumnOrigin leftOrigin =
              mq.getColumnOrigin(leftRel, pair.source);
      if (leftOrigin == null || leftOrigin.isDerived()) {
        return false;
      }
      final RelColumnOrigin rightOrigin =
              mq.getColumnOrigin(rightRel, pair.target);
      if (rightOrigin == null || rightOrigin.isDerived()) {
        return false;
      }
      if (leftOrigin.getOriginColumnOrdinal()
              != rightOrigin.getOriginColumnOrdinal()) {
        return false;
      }
    }

    // Now that we've verified that the keys are the same, see if they
    // are unique.  When removing self-joins, if needed, we'll later add an
    // IS NOT NULL filter on the join keys that are nullable.  Therefore,
    // it's ok if there are nulls in the unique key.
    return RelMdUtil.areColumnsDefinitelyUniqueWhenNullsFiltered(mq, leftRel,
            joinInfo.leftSet());
  }
Determines if the equality portion of a self-join condition is between
identical keys that are unique.

@param mq Metadata query
@param leftRel left side of the join
@param rightRel right side of the join
@param joinFilters the join condition

@return true if the equality join keys are the same and unique
ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/rules/HiveLoptOptimizeJoinRule.java