@Override public void onMatch(RelOptRuleCall call) {
    final Join topJoin= call.rel(0);
    final Join join = call.rel(2);
    final Aggregate aggregate = call.rel(6);

    // in presence of grouping sets we can't remove sq_count_check
    if(aggregate.indicator) {
      return;
    }
    if(isAggregateWithoutGbyKeys(aggregate) || isAggWithConstantGbyKeys(aggregate, call)) {
      // join(left, join.getRight)
      RelNode newJoin = HiveJoin.getJoin(topJoin.getCluster(), join.getLeft(),  topJoin.getRight(),
          topJoin.getCondition(), topJoin.getJoinType());
      call.transformTo(newJoin);
    }
  }
Planner rule that removes UDF sq_count_check from a plan if
 1) either group by keys in a subquery are constant and there is no windowing or grouping sets
 2) OR there are no group by keys but only aggregate
 Both of the above case will produce at most one row, therefore it is safe to remove sq_count_check
   which was introduced earlier in the plan to ensure that this condition is met at run time
ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/rules/HiveRemoveSqCountCheck.java