@Override
  public void onMatch(RelOptRuleCall call) {
    final Aggregate aggregate = call.rel(0);
    int numCountDistinct = getNumCountDistinctCall(aggregate);

    if (numCountDistinct == 0 || numCountDistinct + aggregate.getGroupCount() >= Long.SIZE
        || aggregate.getGroupType() != Group.SIMPLE) {
      return;
    }

    // Find all of the agg expressions. We use a List (for all count(distinct))
    // as well as a Set (for all others) to ensure determinism.
    int nonDistinctCount = 0;
    List<List<Integer>> argListList = new ArrayList<List<Integer>>();
    Set<List<Integer>> argListSets = new LinkedHashSet<List<Integer>>();
    ImmutableBitSet.Builder newGroupSet = ImmutableBitSet.builder();
    newGroupSet.addAll(aggregate.getGroupSet());
    for (AggregateCall aggCall : aggregate.getAggCallList()) {
      if (!aggCall.isDistinct()) {
        ++nonDistinctCount;
        continue;
      }
      ArrayList<Integer> argList = new ArrayList<Integer>();
      for (Integer arg : aggCall.getArgList()) {
        argList.add(arg);
        newGroupSet.set(arg);
      }
      // Aggr checks for sorted argList.
      argListList.add(argList);
      argListSets.add(argList);
    }
    Preconditions.checkArgument(argListSets.size() > 0, "containsDistinctCall lied");

    if (numCountDistinct > 1 && numCountDistinct == aggregate.getAggCallList().size()) {
      LOG.debug("Trigger countDistinct rewrite. numCountDistinct is " + numCountDistinct);
      // now positions contains all the distinct positions, i.e., $5, $4, $6
      // we need to first sort them as group by set
      // and then get their position later, i.e., $4->1, $5->2, $6->3
      cluster = aggregate.getCluster();
      rexBuilder = cluster.getRexBuilder();
      try {
        call.transformTo(convert(aggregate, argListList, newGroupSet.build()));
      } catch (CalciteSemanticException e) {
        LOG.debug(e.toString());
        throw new RuntimeException(e);
      }
      return;
    }

    // If all of the agg expressions are distinct and have the same
    // arguments then we can use a more efficient form.
    final RelMetadataQuery mq = call.getMetadataQuery();
    if ((nonDistinctCount == 0) && (argListSets.size() == 1)) {
      for (Integer arg : argListSets.iterator().next()) {
        Set<RelColumnOrigin> colOrigs = mq.getColumnOrigins(aggregate.getInput(), arg);
        if (null != colOrigs) {
          for (RelColumnOrigin colOrig : colOrigs) {
            RelOptHiveTable hiveTbl = (RelOptHiveTable)colOrig.getOriginTable();
            if(hiveTbl.getPartColInfoMap().containsKey(colOrig.getOriginColumnOrdinal())) {
              // Encountered partitioning column, this will be better handled by MetadataOnly optimizer.
              return;
            }
          }
        }
      }
      RelNode converted =
          convertMonopole(
              aggregate,
              argListSets.iterator().next());
      call.transformTo(converted);
      return;
    }
  }
The default instance of the rule; operates only on logical expressions.
ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/rules/HiveExpandDistinctAggregatesRule.java