private boolean isGroupingUnique(RelNode input, ImmutableBitSet groups) {
    if (groups.isEmpty()) {
      return false;
    }
    if(input instanceof HepRelVertex) {
      HepRelVertex vertex = (HepRelVertex) input;
      return isGroupingUnique(vertex.getCurrentRel(), groups);
    }

    RelMetadataQuery mq = input.getCluster().getMetadataQuery();
    Set<ImmutableBitSet> uKeys = mq.getUniqueKeys(input);
    if (uKeys == null) {
      return false;
    }
    for (ImmutableBitSet u : uKeys) {
      if (groups.contains(u)) {
        return true;
      }
    }
    if (input instanceof Join) {
      Join join = (Join) input;
      JoinInfo ji = JoinInfo.of(join.getLeft(), join.getRight(), join.getCondition());
      if (ji.isEqui()) {
        ImmutableBitSet newGroup = groups.intersect(InputFinder.bits(join.getCondition()));
        RelNode l = join.getLeft();
        RelNode r = join.getRight();

        int joinFieldCount = join.getRowType().getFieldCount();
        int lFieldCount = l.getRowType().getFieldCount();

        ImmutableBitSet groupL = newGroup.get(0, lFieldCount);
        ImmutableBitSet groupR = newGroup.get(lFieldCount, joinFieldCount).shift(-lFieldCount);

        if (isGroupingUnique(l, groupL)) {
          return true;
        }
        if (isGroupingUnique(r, groupR)) {
          return true;
        }
      }
    }
    if (input instanceof Project) {
      Project project = (Project) input;
      ImmutableBitSet.Builder newGroup = ImmutableBitSet.builder();
      for (int g : groups.asList()) {
        RexNode rex = project.getProjects().get(g);
        if (rex instanceof RexInputRef) {
          RexInputRef rexInputRef = (RexInputRef) rex;
          newGroup.set(rexInputRef.getIndex());
        }
      }
      return isGroupingUnique(project.getInput(), newGroup.build());
    }
    return false;
  }
Determines weather the give grouping is unique.

Consider a join which might produce non-unique rows; but later the results are aggregated again.
This method determines if there are sufficient columns in the grouping which have been present previously as unique column(s).
ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/rules/HiveAggregateJoinTransposeRule.java