@Override
    public void onMatch(RelOptRuleCall call) {
      Aggregate singleAggregate = call.rel(0);
      Project project = call.rel(1);
      Aggregate aggregate = call.rel(2);

      // check singleAggRel is single_value agg
      if ((!singleAggregate.getGroupSet().isEmpty())
              || (singleAggregate.getAggCallList().size() != 1)
              || !(singleAggregate.getAggCallList().get(0).getAggregation()
              instanceof SqlSingleValueAggFunction)) {
        return;
      }

      // check projRel only projects one expression
      // check this project only projects one expression, i.e. scalar
      // subqueries.
      List<RexNode> projExprs = project.getProjects();
      if (projExprs.size() != 1) {
        return;
      }

      // check the input to projRel is an aggregate on the entire input
      if (!aggregate.getGroupSet().isEmpty()) {
        return;
      }

      // singleAggRel produces a nullable type, so create the new
      // projection that casts proj expr to a nullable type.
      final RelOptCluster cluster = project.getCluster();
      RelNode newProject = RelOptUtil.createProject(aggregate,
          ImmutableList.of(rexBuilder.makeCast(
                  cluster.getTypeFactory().createTypeWithNullability(projExprs.get(0).getType(), true),
                  projExprs.get(0))),
          null, false, relBuilder);
      call.transformTo(newProject);
    }
Rule to remove single_value rel. For cases like

<blockquote>AggRel single_value proj/filter/agg/ join on unique LHS key
AggRel single group</blockquote>
ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/rules/HiveRelDecorrelator.java