@Override
  protected void explain_(RelNode rel, List<Pair<String, Object>> values) {
    super.explain_(rel, values);
    RelMetadataQuery mq = rel.getCluster().getMetadataQuery();
    Map<String, Object> map = (Map<String, Object>) relList.get(relList.size() - 1);
    map.put("rowCount", mq.getRowCount(rel));
    if (rel.getInputs().size() == 0) {
      // This is a leaf, we will print the average row size and schema
      map.put("avgRowSize", mq.getAverageRowSize(rel));
      map.put("rowType", relJson.toJson(rel.getRowType()));
      // We also include partition columns information
      RelOptHiveTable table = (RelOptHiveTable) rel.getTable();
      List<Object> list = jsonBuilder.list();
      list.addAll(table.getHiveTableMD().getPartColNames());
      if (!list.isEmpty()) {
        map.put("partitionColumns", list);
      }
      // We also include column stats
      List<ColStatistics> colStats = table.getColStat(
          ImmutableBitSet.range(0, table.getNoOfNonVirtualCols()).asList(), true);
      list = jsonBuilder.list();
      for (ColStatistics cs : colStats) {
        final Map<String, Object> csMap = jsonBuilder.map();
        csMap.put("name", cs.getColumnName());
        csMap.put("ndv", cs.getCountDistint());
        if (cs.getRange() != null) {
          csMap.put("minValue", cs.getRange().minValue);
          csMap.put("maxValue", cs.getRange().maxValue);
        }
        list.add(csMap);
      }
      if (!list.isEmpty()) {
        map.put("colStats", list);
      }
    }
  }
Writer implementation for rel nodes that produces an output in json that is easily
parseable back into rel nodes.
ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/HiveRelJsonImpl.java