diff --git a/metastore/src/java/org/apache/hadoop/hive/metastore/cache/SharedCache.java b/metastore/src/java/org/apache/hadoop/hive/metastore/cache/SharedCache.java index 6b6355b..b454372 100644 --- a/metastore/src/java/org/apache/hadoop/hive/metastore/cache/SharedCache.java +++ b/metastore/src/java/org/apache/hadoop/hive/metastore/cache/SharedCache.java @@ -137,7 +137,7 @@ public static synchronized void removeTableFromCache(String dbName, String tblNa } public static synchronized ColumnStatisticsObj getCachedTableColStats(String colStatsCacheKey) { - return tableColStatsCache.get(colStatsCacheKey); + return tableColStatsCache.get(colStatsCacheKey).deepCopy(); } public static synchronized void removeTableColStatsFromCache(String dbName, String tblName) { @@ -426,7 +426,7 @@ public static synchronized int getCachedPartitionCount() { } public static synchronized ColumnStatisticsObj getCachedPartitionColStats(String key) { - return partitionColStatsCache.get(key); + return partitionColStatsCache.get(key).deepCopy(); } public static synchronized void addPartitionColStatsToCache(String dbName, String tableName, diff --git a/metastore/src/test/org/apache/hadoop/hive/metastore/cache/TestCachedStore.java b/metastore/src/test/org/apache/hadoop/hive/metastore/cache/TestCachedStore.java index 7a3ec09..5a3c78d 100644 --- a/metastore/src/test/org/apache/hadoop/hive/metastore/cache/TestCachedStore.java +++ b/metastore/src/test/org/apache/hadoop/hive/metastore/cache/TestCachedStore.java @@ -27,6 +27,7 @@ import org.apache.hadoop.hive.metastore.ObjectStore; import org.apache.hadoop.hive.metastore.TableType; import org.apache.hadoop.hive.metastore.TestObjectStore.MockPartitionExpressionProxy; +import org.apache.hadoop.hive.metastore.api.AggrStats; import org.apache.hadoop.hive.metastore.api.BooleanColumnStatsData; import org.apache.hadoop.hive.metastore.api.ColumnStatistics; import org.apache.hadoop.hive.metastore.api.ColumnStatisticsData; @@ -673,4 +674,70 @@ public void testSharedStorePartition() { Assert.assertEquals(SharedCache.getCachedPartitionCount(), 2); Assert.assertEquals(SharedCache.getSdCache().size(), 2); } + + @Test + public void testAggrStatsRepeatedRead() throws Exception { + String dbName = "testTableColStatsOps"; + String tblName = "tbl"; + String colName = "col"; + + Database db = new Database(dbName, null, "some_location", null); + cachedStore.createDatabase(db); + + List cols = new ArrayList(); + cols.add(new FieldSchema("f1", "int", null)); + List partCols = new ArrayList(); + partCols.add(new FieldSchema("col", "int", null)); + StorageDescriptor sd = + new StorageDescriptor(cols, null, "input", "output", false, 0, new SerDeInfo("serde", "seriallib", new HashMap()), + null, null, null); + + Table tbl = + new Table(tblName, dbName, null, 0, 0, 0, sd, partCols, new HashMap(), + null, null, TableType.MANAGED_TABLE.toString()); + cachedStore.createTable(tbl); + + List partVals1 = new ArrayList(); + partVals1.add("1"); + List partVals2 = new ArrayList(); + partVals2.add("2"); + + Partition ptn1 = + new Partition(partVals1, dbName, tblName, 0, 0, sd, new HashMap()); + cachedStore.addPartition(ptn1); + Partition ptn2 = + new Partition(partVals2, dbName, tblName, 0, 0, sd, new HashMap()); + cachedStore.addPartition(ptn2); + + ColumnStatistics stats = new ColumnStatistics(); + ColumnStatisticsDesc statsDesc = new ColumnStatisticsDesc(true, dbName, tblName); + statsDesc.setPartName("col"); + List colStatObjs = new ArrayList(); + + ColumnStatisticsData data = new ColumnStatisticsData(); + ColumnStatisticsObj colStats = new ColumnStatisticsObj(colName, "int", data); + LongColumnStatsData longStats = new LongColumnStatsData(); + longStats.setLowValue(0); + longStats.setHighValue(100); + longStats.setNumNulls(50); + longStats.setNumDVs(30); + data.setLongStats(longStats); + colStatObjs.add(colStats); + + stats.setStatsDesc(statsDesc); + stats.setStatsObj(colStatObjs); + + cachedStore.updatePartitionColumnStatistics(stats.deepCopy(), partVals1); + cachedStore.updatePartitionColumnStatistics(stats.deepCopy(), partVals2); + + List colNames = new ArrayList(); + colNames.add(colName); + List aggrPartVals = new ArrayList(); + aggrPartVals.add("1"); + aggrPartVals.add("2"); + AggrStats aggrStats = cachedStore.get_aggr_stats_for(dbName, tblName, aggrPartVals, colNames); + Assert.assertEquals(aggrStats.getColStats().get(0).getStatsData().getLongStats().getNumNulls(), 100); + aggrStats = cachedStore.get_aggr_stats_for(dbName, tblName, aggrPartVals, colNames); + Assert.assertEquals(aggrStats.getColStats().get(0).getStatsData().getLongStats().getNumNulls(), 100); + } }