@Override public AggrStats get_aggr_stats_for(String catName, String dbName, String tblName, List<String> partNames,
      List<String> colNames, String engine, String writeIdList) throws MetaException, NoSuchObjectException {
    if (!CacheUtils.HIVE_ENGINE.equals(engine)) {
      throw new RuntimeException("CachedStore can only be enabled for Hive engine");
    }
    List<ColumnStatisticsObj> colStats;
    catName = normalizeIdentifier(catName);
    dbName = StringUtils.normalizeIdentifier(dbName);
    tblName = StringUtils.normalizeIdentifier(tblName);
    // TODO: we currently cannot do transactional checks for stats here
    //       (incl. due to lack of sync w.r.t. the below rawStore call).
    // In case the cache is updated using events, aggregate is calculated locally and thus can be read from cache.
    if (!shouldCacheTable(catName, dbName, tblName) || (writeIdList != null && !canUseEvents)) {
      return rawStore.get_aggr_stats_for(catName, dbName, tblName, partNames, colNames, engine, writeIdList);
    }
    Table table = sharedCache.getTableFromCache(catName, dbName, tblName);
    if (table == null) {
      // The table is not yet loaded in cache
      return rawStore.get_aggr_stats_for(catName, dbName, tblName, partNames, colNames, engine, writeIdList);
    }

    List<String> allPartNames = rawStore.listPartitionNames(catName, dbName, tblName, (short) -1);
    StatsType type = StatsType.PARTIAL;
    if (partNames.size() == allPartNames.size()) {
      colStats = sharedCache.getAggrStatsFromCache(catName, dbName, tblName, colNames, StatsType.ALL);
      if (colStats != null) {
        return new AggrStats(colStats, partNames.size());
      }
      type = StatsType.ALL;
    } else if (partNames.size() == (allPartNames.size() - 1)) {
      String defaultPartitionName = MetastoreConf.getVar(getConf(), ConfVars.DEFAULTPARTITIONNAME);
      if (!partNames.contains(defaultPartitionName)) {
        colStats = sharedCache.getAggrStatsFromCache(catName, dbName, tblName, colNames, StatsType.ALLBUTDEFAULT);
        if (colStats != null) {
          return new AggrStats(colStats, partNames.size());
        }
        type = StatsType.ALLBUTDEFAULT;
      }
    }

    LOG.debug("Didn't find aggr stats in cache. Merging them. tblName= {}, parts= {}, cols= {}", tblName, partNames,
        colNames);
    MergedColumnStatsForPartitions mergedColStats =
        mergeColStatsForPartitions(catName, dbName, tblName, partNames, colNames, sharedCache, type, writeIdList);
    if (mergedColStats == null) {
      LOG.info("Aggregate stats of partition " + TableName.getQualified(catName, dbName, tblName) + "." + partNames
          + " for columns " + colNames + " is not present in cache. Getting it from raw store");
      return rawStore.get_aggr_stats_for(catName, dbName, tblName, partNames, colNames, engine, writeIdList);
    }
    return new AggrStats(mergedColStats.getColStats(), mergedColStats.getPartsFound());
  }
getPartitionSpecsByFilterAndProjection interface is currently non-cacheable.
standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/cache/CachedStore.java