[ 
https://issues.apache.org/jira/browse/HIVE-24663?focusedWorklogId=604452&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-604452
 ]

ASF GitHub Bot logged work on HIVE-24663:
-----------------------------------------

                Author: ASF GitHub Bot
            Created on: 01/Jun/21 11:44
            Start Date: 01/Jun/21 11:44
    Worklog Time Spent: 10m 
      Work Description: maheshk114 commented on a change in pull request #2266:
URL: https://github.com/apache/hive/pull/2266#discussion_r643019982



##########
File path: 
standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/txn/TxnHandler.java
##########
@@ -5394,6 +5410,476 @@ public void countOpenTxns() throws MetaException {
     }
   }
 
+  private void cleanOldStatsFromPartColStatTable(Map<PartitionInfo, 
ColumnStatistics> statsPartInfoMap,
+                                                 Connection dbConn) throws 
SQLException {
+    PreparedStatement preparedStatement = null;
+    int numRows = 0;
+    int maxNumRows = MetastoreConf.getIntVar(conf, 
ConfVars.DIRECT_SQL_MAX_ELEMENTS_VALUES_CLAUSE);
+
+    // Index is present on DB_NAME,TABLE_NAME,COLUMN_NAME,PARTITION_NAME. use 
that.
+    // TODO : Need to add catalog name to the index
+    String delete = "DELETE FROM \"PART_COL_STATS\" where \"DB_NAME\" = ? AND "
+            + "\"TABLE_NAME\" = ? AND \"COLUMN_NAME\" = ? AND 
\"PARTITION_NAME\" = ? "
+            + "AND \"PART_ID\" = ?";
+
+    try {
+      preparedStatement = sqlGenerator.prepareStmtWithParameters(dbConn, 
delete, null);

Review comment:
       removed the use

##########
File path: 
standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/txn/TxnHandler.java
##########
@@ -5394,6 +5410,476 @@ public void countOpenTxns() throws MetaException {
     }
   }
 
+  private void cleanOldStatsFromPartColStatTable(Map<PartitionInfo, 
ColumnStatistics> statsPartInfoMap,
+                                                 Connection dbConn) throws 
SQLException {
+    PreparedStatement preparedStatement = null;
+    int numRows = 0;
+    int maxNumRows = MetastoreConf.getIntVar(conf, 
ConfVars.DIRECT_SQL_MAX_ELEMENTS_VALUES_CLAUSE);
+
+    // Index is present on DB_NAME,TABLE_NAME,COLUMN_NAME,PARTITION_NAME. use 
that.
+    // TODO : Need to add catalog name to the index
+    String delete = "DELETE FROM \"PART_COL_STATS\" where \"DB_NAME\" = ? AND "
+            + "\"TABLE_NAME\" = ? AND \"COLUMN_NAME\" = ? AND 
\"PARTITION_NAME\" = ? "
+            + "AND \"PART_ID\" = ?";
+
+    try {
+      preparedStatement = sqlGenerator.prepareStmtWithParameters(dbConn, 
delete, null);
+      for (Map.Entry entry : statsPartInfoMap.entrySet()) {
+        ColumnStatistics colStats = (ColumnStatistics) entry.getValue();
+        PartitionInfo partitionInfo = (PartitionInfo) entry.getKey();
+        for (ColumnStatisticsObj statisticsObj : colStats.getStatsObj()) {
+          preparedStatement.setString(1, colStats.getStatsDesc().getDbName());
+          preparedStatement.setString(2, 
colStats.getStatsDesc().getTableName());
+          preparedStatement.setString(3, statisticsObj.getColName());
+          preparedStatement.setString(4, 
colStats.getStatsDesc().getPartName());
+          preparedStatement.setLong(5, partitionInfo.partitionId);
+          numRows++;
+          preparedStatement.addBatch();
+          if (numRows == maxNumRows) {
+            preparedStatement.executeBatch();
+            numRows = 0;
+            LOG.debug("Executed delete " + delete + " for numRows " + numRows);
+          }
+        }
+      }
+
+      if (numRows != 0) {
+        preparedStatement.executeBatch();
+        LOG.debug("Executed delete " + delete + " for numRows " + numRows);
+      }
+    } finally {
+      closeStmt(preparedStatement);
+    }
+  }
+
+  private void insertIntoPartColStatTable(Map<PartitionInfo, ColumnStatistics> 
partitionInfoMap,
+                                          long maxCsId,
+                                          Connection dbConn) throws 
SQLException, MetaException, NoSuchObjectException {
+    PreparedStatement preparedStatement = null;
+    int numRows = 0;
+    int maxNumRows = MetastoreConf.getIntVar(conf, 
ConfVars.DIRECT_SQL_MAX_ELEMENTS_VALUES_CLAUSE);
+    String insert = "INSERT INTO \"PART_COL_STATS\" (\"CS_ID\", \"CAT_NAME\", 
\"DB_NAME\","
+            + "\"TABLE_NAME\", \"PARTITION_NAME\", \"COLUMN_NAME\", 
\"COLUMN_TYPE\", \"PART_ID\","
+            + " \"LONG_LOW_VALUE\", \"LONG_HIGH_VALUE\", 
\"DOUBLE_HIGH_VALUE\", \"DOUBLE_LOW_VALUE\","
+            + " \"BIG_DECIMAL_LOW_VALUE\", \"BIG_DECIMAL_HIGH_VALUE\", 
\"NUM_NULLS\", \"NUM_DISTINCTS\", \"BIT_VECTOR\" ,"
+            + " \"AVG_COL_LEN\", \"MAX_COL_LEN\", \"NUM_TRUES\", 
\"NUM_FALSES\", \"LAST_ANALYZED\", \"ENGINE\") values "
+            + "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 
?, ?)";
+
+    try {
+      preparedStatement = sqlGenerator.prepareStmtWithParameters(dbConn, 
insert, null);
+      for (Map.Entry entry : partitionInfoMap.entrySet()) {
+        ColumnStatistics colStats = (ColumnStatistics) entry.getValue();
+        PartitionInfo partitionInfo = (PartitionInfo)entry.getKey();
+        ColumnStatisticsDesc statsDesc = colStats.getStatsDesc();
+        long partId = partitionInfo.partitionId;
+
+        for (ColumnStatisticsObj statisticsObj : colStats.getStatsObj()) {
+          MPartitionColumnStatistics mPartitionColumnStatistics = 
StatObjectConverter.
+                  convertToMPartitionColumnStatistics(null, statsDesc, 
statisticsObj, colStats.getEngine());
+
+          preparedStatement.setLong(1, maxCsId);
+          preparedStatement.setString(2, 
mPartitionColumnStatistics.getCatName());
+          preparedStatement.setString(3, 
mPartitionColumnStatistics.getDbName());
+          preparedStatement.setString(4, 
mPartitionColumnStatistics.getTableName());
+          preparedStatement.setString(5, 
mPartitionColumnStatistics.getPartitionName());
+          preparedStatement.setString(6, 
mPartitionColumnStatistics.getColName());
+          preparedStatement.setString(7, 
mPartitionColumnStatistics.getColType());
+          preparedStatement.setLong(8, partId);
+          preparedStatement.setObject(9, 
mPartitionColumnStatistics.getLongLowValue());
+          preparedStatement.setObject(10, 
mPartitionColumnStatistics.getLongHighValue());
+          preparedStatement.setObject(11, 
mPartitionColumnStatistics.getDoubleHighValue());
+          preparedStatement.setObject(12, 
mPartitionColumnStatistics.getDoubleLowValue());
+          preparedStatement.setString(13, 
mPartitionColumnStatistics.getDecimalLowValue());
+          preparedStatement.setString(14, 
mPartitionColumnStatistics.getDecimalHighValue());
+          preparedStatement.setObject(15, 
mPartitionColumnStatistics.getNumNulls());
+          preparedStatement.setObject(16, 
mPartitionColumnStatistics.getNumDVs());
+          preparedStatement.setObject(17, 
mPartitionColumnStatistics.getBitVector());
+          preparedStatement.setObject(18, 
mPartitionColumnStatistics.getAvgColLen());
+          preparedStatement.setObject(19, 
mPartitionColumnStatistics.getMaxColLen());
+          preparedStatement.setObject(20, 
mPartitionColumnStatistics.getNumTrues());
+          preparedStatement.setObject(21, 
mPartitionColumnStatistics.getNumFalses());
+          preparedStatement.setLong(22, 
mPartitionColumnStatistics.getLastAnalyzed());
+          preparedStatement.setString(23, 
mPartitionColumnStatistics.getEngine());
+
+          maxCsId++;
+          numRows++;
+          preparedStatement.addBatch();
+          if (numRows == maxNumRows) {
+            preparedStatement.executeBatch();
+            numRows = 0;
+          }
+        }
+      }
+
+      if (numRows != 0) {
+        preparedStatement.executeBatch();
+      }
+    } finally {
+      closeStmt(preparedStatement);
+    }
+  }
+
+  private Map<Long, String> getParamValues(Connection dbConn, List<Long> 
partIdList) throws SQLException {
+    List<String> queries = new ArrayList<>();
+    StringBuilder prefix = new StringBuilder();
+    StringBuilder suffix = new StringBuilder();
+    Statement statement = null;
+    ResultSet rs = null;
+
+    prefix.append("select \"PART_ID\", \"PARAM_VALUE\" "
+            + " from \"PARTITION_PARAMS\" where "
+            + " \"PARAM_KEY\" = 'COLUMN_STATS_ACCURATE' "
+            + " and ");
+    TxnUtils.buildQueryWithINClause(conf, queries, prefix, suffix,
+            partIdList, "\"PART_ID\"", true, false);
+
+    Map<Long, String> partIdToParaMap = new HashMap<>();
+    for (String query : queries) {
+      try {
+        statement = dbConn.createStatement();
+        LOG.debug("Going to execute query " + query);
+        rs = statement.executeQuery(query);
+        while (rs.next()) {
+          partIdToParaMap.put(rs.getLong(1), rs.getString(2));
+        }
+      } finally {
+        close(rs, statement, null);
+      }
+    }
+    return partIdToParaMap;
+  }
+
+  private void updateWriteIdForPartitions(Connection dbConn, long writeId, 
List<Long> partIdList) throws SQLException {
+    StringBuilder prefix = new StringBuilder();
+    List<String> queries = new ArrayList<>();
+    StringBuilder suffix = new StringBuilder();
+
+    prefix.append("UPDATE \"PARTITIONS\" set \"WRITE_ID\" = " + writeId + " 
where ");
+    TxnUtils.buildQueryWithINClause(conf, queries, prefix, suffix,
+            partIdList, "\"PART_ID\"", false, false);
+
+    Statement statement = null;
+    for (String query : queries) {
+      try {
+        statement = dbConn.createStatement();
+        LOG.debug("Going to execute update " + query);
+        statement.executeUpdate(query);
+      } finally {
+        closeStmt(statement);
+      }
+    }
+  }
+
+  private Map<String, Map<String, String>> 
updatePartitionParamTable(Connection dbConn,
+                                                               
Map<PartitionInfo, ColumnStatistics> partitionInfoMap,
+                                                               String 
validWriteIds,
+                                                               long writeId,
+                                                               boolean 
isAcidTable)
+          throws SQLException, MetaException {
+    Map<String, Map<String, String>> result = new HashMap<>();
+    boolean areTxnStatsSupported = MetastoreConf.getBoolVar(conf, 
ConfVars.HIVE_TXN_STATS_ENABLED);
+    int maxNumRows = MetastoreConf.getIntVar(conf, 
ConfVars.DIRECT_SQL_MAX_ELEMENTS_VALUES_CLAUSE);
+    PreparedStatement statementInsert = null;
+    PreparedStatement statementDelete = null;
+    PreparedStatement statementUpdate = null;
+    String insert = "INSERT INTO \"PARTITION_PARAMS\" (\"PART_ID\", 
\"PARAM_KEY\", \"PARAM_VALUE\") "
+            + "VALUES( ? , 'COLUMN_STATS_ACCURATE'  , ? )";
+    String delete = "DELETE from \"PARTITION_PARAMS\" "
+            + " where \"PART_ID\" = ? "
+            + " and \"PARAM_KEY\" = 'COLUMN_STATS_ACCURATE'";
+    String update = "UPDATE \"PARTITION_PARAMS\" set \"PARAM_VALUE\" = ? "
+            + " where \"PART_ID\" = ? "
+            + " and \"PARAM_KEY\" = 'COLUMN_STATS_ACCURATE'";
+    int numInsert = 0;
+    int numDelete = 0;
+    int numUpdate = 0;
+
+    List<Long> partIdList = partitionInfoMap.keySet().stream().map(
+            e -> e.partitionId).collect(Collectors.toList()
+             );
+
+    // get the old parameters from PARTITION_PARAMS table.
+    Map<Long, String> partIdToParaMap = getParamValues(dbConn, partIdList);
+
+    try {
+      statementInsert = sqlGenerator.prepareStmtWithParameters(dbConn, insert, 
null);
+      statementDelete = sqlGenerator.prepareStmtWithParameters(dbConn,delete, 
null);
+      statementUpdate = sqlGenerator.prepareStmtWithParameters(dbConn,update, 
null);
+      for (Map.Entry entry : partitionInfoMap.entrySet()) {
+        PartitionInfo partitionInfo = (PartitionInfo) entry.getKey();
+        ColumnStatistics colStats = (ColumnStatistics) entry.getValue();
+        List<String> colNames = colStats.getStatsObj().stream().map(e -> 
e.getColName()).collect(Collectors.toList());
+        long partWriteId = partitionInfo.writeId;
+        long partId = partitionInfo.partitionId;
+        Map<String, String> newParameter;
+
+        if (!partIdToParaMap.containsKey(partId)) {
+          newParameter = new HashMap<>();
+          newParameter.put(COLUMN_STATS_ACCURATE, "TRUE");
+          StatsSetupConst.setColumnStatsState(newParameter, colNames);
+          statementInsert.setLong(1, partId);
+          statementInsert.setString(2, 
newParameter.get(COLUMN_STATS_ACCURATE));
+          numInsert++;
+          statementInsert.addBatch();
+          if (numInsert == maxNumRows) {
+            LOG.debug(" Executing insert " + insert);
+            statementInsert.executeBatch();
+            numInsert = 0;
+          }
+        } else {
+          String oldStats = partIdToParaMap.get(partId);
+
+          Map<String, String> oldParameter = new HashMap<>();
+          oldParameter.put(COLUMN_STATS_ACCURATE, oldStats);
+
+          newParameter = new HashMap<>();
+          newParameter.put(COLUMN_STATS_ACCURATE, oldStats);
+          StatsSetupConst.setColumnStatsState(newParameter, colNames);
+
+          if (isAcidTable) {
+            String errorMsg = ObjectStore.verifyStatsChangeCtx(
+                    colStats.getStatsDesc().getDbName() + "." + 
colStats.getStatsDesc().getTableName(),
+                    oldParameter, newParameter, writeId, validWriteIds, true);
+            if (errorMsg != null) {
+              throw new MetaException(errorMsg);
+            }
+          }
+
+          if (isAcidTable &&
+              (!areTxnStatsSupported || 
!ObjectStore.isCurrentStatsValidForTheQuery(oldParameter, partWriteId,
+                      validWriteIds, true))) {
+            statementDelete.setLong(1, partId);
+            statementDelete.addBatch();
+            numDelete++;
+            if (numDelete == maxNumRows) {
+              statementDelete.executeBatch();
+              numDelete = 0;
+              LOG.debug("Removed COLUMN_STATS_ACCURATE from the parameters of 
the partition "
+                      + colStats.getStatsDesc().getDbName() + "." + 
colStats.getStatsDesc().getTableName() + "."
+                      + colStats.getStatsDesc().getPartName());
+            }
+          } else {
+            statementUpdate.setString(1, 
newParameter.get(COLUMN_STATS_ACCURATE));
+            statementUpdate.setLong(2, partId);
+            statementUpdate.addBatch();
+            numUpdate++;
+            if (numUpdate == maxNumRows) {
+              LOG.debug(" Executing update " + statementUpdate);
+              statementUpdate.executeBatch();
+              numUpdate = 0;
+            }
+          }
+        }
+        result.put(partitionInfo.partitionName, newParameter);
+      }
+
+      if (numInsert != 0) {
+        statementInsert.executeBatch();
+      }
+
+      if (numUpdate != 0) {
+        statementUpdate.executeBatch();
+      }
+
+      if (numDelete != 0) {
+        statementDelete.executeBatch();
+      }
+
+      if (isAcidTable) {
+        updateWriteIdForPartitions(dbConn, writeId, partIdList);
+      }
+      return result;
+    } finally {
+      closeStmt(statementInsert);
+      closeStmt(statementUpdate);
+      closeStmt(statementDelete);
+    }
+  }
+
+  private static class PartitionInfo {

Review comment:
       removed the functionality to a separate class




-- 
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.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Issue Time Tracking
-------------------

    Worklog Id:     (was: 604452)
    Time Spent: 8h 40m  (was: 8.5h)

> Reduce overhead of partition column stats updation.
> ---------------------------------------------------
>
>                 Key: HIVE-24663
>                 URL: https://issues.apache.org/jira/browse/HIVE-24663
>             Project: Hive
>          Issue Type: Sub-task
>            Reporter: Rajesh Balamohan
>            Assignee: mahesh kumar behera
>            Priority: Major
>              Labels: performance, pull-request-available
>          Time Spent: 8h 40m
>  Remaining Estimate: 0h
>
> When large number of partitions (>20K) are processed, ColStatsProcessor runs 
> into DB issues. 
> {{ db.setPartitionColumnStatistics(request);}} gets stuck for hours together 
> and in some cases postgres stops processing. 
> It would be good to introduce small batches for stats gathering in 
> ColStatsProcessor instead of bulk update.
> Ref: 
> https://github.com/apache/hive/blob/master/ql/src/java/org/apache/hadoop/hive/ql/stats/ColStatsProcessor.java#L181
> https://github.com/apache/hive/blob/master/ql/src/java/org/apache/hadoop/hive/ql/stats/ColStatsProcessor.java#L199



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to