SourabhBadhya commented on code in PR #4431:
URL: https://github.com/apache/hive/pull/4431#discussion_r1239331586


##########
iceberg/iceberg-handler/src/main/java/org/apache/iceberg/mr/hive/HiveIcebergStorageHandler.java:
##########
@@ -493,18 +503,42 @@ private String getStatsSource() {
   }
 
   private Path getStatsPath(Table table) {
-    return new Path(table.location() + STATS + table.name() + 
table.currentSnapshot().snapshotId());
+    return getStatsPath(table, table.currentSnapshot().snapshotId());
   }
 
-  private void invalidateStats(Path statsPath) {
+  private Path getStatsPath(Table table, long snapshotId) {
+    return new Path(table.location() + STATS + table.name() + snapshotId);
+  }
+
+  private boolean checkAndInvalidateStats(Table tbl) {
+    Path statsPath = getStatsPath(tbl);
     try {
       FileSystem fs = statsPath.getFileSystem(conf);
       if (fs.exists(statsPath)) {
-        fs.delete(statsPath, true);
+        // Analyze table and stats updater thread
+        return fs.delete(statsPath, true);
       }
     } catch (IOException e) {
       LOG.error("Failed to invalidate stale column stats: {}", e.getMessage());
     }
+    return false;
+  }
+
+  private void checkAndMergeStats(ColumnStatistics statsObjNew, Table tbl,
+      org.apache.hadoop.hive.ql.metadata.Table hmsTable, ColumnStatsDesc 
columnStatsDesc) {
+    Long previousSnapshotId = tbl.currentSnapshot().parentId();
+    if (previousSnapshotId != null && canProvideColStatistics(tbl, 
previousSnapshotId)) {
+      ColumnStatistics statsObjOld = readColStats(tbl, getStatsPath(tbl, 
previousSnapshotId));
+      if (statsObjOld != null && statsObjOld.getStatsObjSize() != 0 && 
!statsObjNew.getStatsObj().isEmpty()) {
+        try {
+          MetaStoreServerUtils.mergeColStats(statsObjNew, statsObjOld);
+          StatsSetupConst.setColumnStatsState(hmsTable.getParameters(), 
columnStatsDesc.getColName());
+        } catch (InvalidObjectException e) {
+          StatsSetupConst.removeColumnStatsState(hmsTable.getParameters(), 
columnStatsDesc.getColName());

Review Comment:
   Why do we need to set or remove the column stats state here? I think setting 
it in a single place depending upon whether statistics is correctly written as 
done in #4440 should suffice right?



##########
iceberg/iceberg-handler/src/test/java/org/apache/iceberg/mr/hive/TestHiveIcebergColStats.java:
##########
@@ -0,0 +1,129 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.iceberg.mr.hive;
+
+import java.util.List;
+import org.apache.hadoop.hive.common.StatsSetupConst;
+import org.apache.hadoop.hive.conf.HiveConf;
+import org.apache.iceberg.PartitionSpec;
+import org.apache.iceberg.catalog.TableIdentifier;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class TestHiveIcebergColStats extends 
HiveIcebergStorageHandlerWithEngineBase {
+
+  @Test
+  public void testStatsWithInsert() {
+    TableIdentifier identifier = TableIdentifier.of("default", "customers");
+
+    shell.setHiveSessionValue(HiveConf.ConfVars.HIVESTATSAUTOGATHER.varname, 
true);
+    testTables.createTable(shell, identifier.name(), 
HiveIcebergStorageHandlerTestUtils.CUSTOMER_SCHEMA,
+        PartitionSpec.unpartitioned(), fileFormat, ImmutableList.of());
+
+    if (testTableType != TestTables.TestTableType.HIVE_CATALOG) {
+      // If the location is set and we have to gather stats, then we have to 
update the table stats now
+      shell.executeStatement("ANALYZE TABLE " + identifier + " COMPUTE 
STATISTICS FOR COLUMNS");
+    }
+
+    String insert = 
testTables.getInsertQuery(HiveIcebergStorageHandlerTestUtils.CUSTOMER_RECORDS, 
identifier, false);
+    shell.executeStatement(insert);
+
+    checkColStat(identifier.name(), "customer_id", true);
+    checkColStatMinMaxDistinctValue(identifier.name(), "customer_id", 0, 2, 3, 
0);
+
+    insert = 
testTables.getInsertQuery(HiveIcebergStorageHandlerTestUtils.OTHER_CUSTOMER_RECORDS_1,
 identifier, false);
+    shell.executeStatement(insert);
+
+    checkColStat(identifier.name(), "customer_id", true);
+    checkColStatMinMaxDistinctValue(identifier.name(), "customer_id", 0, 5, 6, 
0);
+
+    insert = 
testTables.getInsertQuery(HiveIcebergStorageHandlerTestUtils.OTHER_CUSTOMER_RECORDS_2,
 identifier, false);
+    shell.executeStatement(insert);
+    checkColStat(identifier.name(), "customer_id", true);
+    checkColStatMinMaxDistinctValue(identifier.name(), "customer_id", 0, 5, 6, 
0);
+  }
+
+  private void checkColStat(String tableName, String colName, boolean 
accurate) {

Review Comment:
   Probably move this function to the base class so that 
TestHiveIcebergStatistics can also use the same.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to