[ https://issues.apache.org/jira/browse/HIVE-24663?focusedWorklogId=603964&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-603964 ]
ASF GitHub Bot logged work on HIVE-24663: ----------------------------------------- Author: ASF GitHub Bot Created on: 31/May/21 06:23 Start Date: 31/May/21 06:23 Worklog Time Spent: 10m Work Description: deniskuzZ commented on a change in pull request #2266: URL: https://github.com/apache/hive/pull/2266#discussion_r642237902 ########## 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); Review comment: same as above, check if maxBatchSize is applicable -- 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: 603964) Time Spent: 3h 40m (was: 3.5h) > Batch process in ColStatsProcessor for partitions. > -------------------------------------------------- > > Key: HIVE-24663 > URL: https://issues.apache.org/jira/browse/HIVE-24663 > Project: Hive > Issue Type: Improvement > Reporter: Rajesh Balamohan > Assignee: mahesh kumar behera > Priority: Major > Labels: performance, pull-request-available > Time Spent: 3h 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)