private static Set<ImmutableBitSet> generateKeysUsingStatsEstimation(Project rel,
                                                                       HiveTableScan tScan) {
    Map<Integer, Integer> posMap = new HashMap<Integer, Integer>();
    int projectPos = 0;
    int colStatsPos = 0;

    BitSet projectedCols = new BitSet();
    for (RexNode r : rel.getProjects()) {
      if (r instanceof RexInputRef) {
        projectedCols.set(((RexInputRef) r).getIndex());
        posMap.put(colStatsPos, projectPos);
        colStatsPos++;
      }
      projectPos++;
    }

    RelMetadataQuery mq = rel.getCluster().getMetadataQuery();
    double numRows = mq.getRowCount(tScan);
    List<ColStatistics> colStats = tScan.getColStat(BitSets.toList(projectedCols));
    Set<ImmutableBitSet> keys = new HashSet<ImmutableBitSet>();

    colStatsPos = 0;
    for (ColStatistics cStat : colStats) {
      boolean isKey = false;
      if (!cStat.isEstimated()) {
        if (cStat.getCountDistint() >= numRows) {
          isKey = true;
        }
        if (!isKey && cStat.getRange() != null && cStat.getRange().maxValue != null
            && cStat.getRange().minValue != null) {
          double r = cStat.getRange().maxValue.doubleValue() - cStat.getRange().minValue.doubleValue() + 1;
          isKey = (Math.abs(numRows - r) < RelOptUtil.EPSILON);
        }
        if (isKey) {
          ImmutableBitSet key = ImmutableBitSet.of(posMap.get(colStatsPos));
          keys.add(key);
        }
      }
      colStatsPos++;
    }

    return keys;

  }
EstimateUniqueKeys provides an ability to estimate unique keys based on statistics.
ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/stats/EstimateUniqueKeys.java