okumin commented on code in PR #5106: URL: https://github.com/apache/hive/pull/5106#discussion_r1517770690
########## ql/src/java/org/apache/hadoop/hive/ql/parse/SemanticAnalyzer.java: ########## @@ -1600,26 +1600,35 @@ Table materializeCTE(String cteName, CTEClause cte) throws HiveException { LOG.info("{} will be materialized into {}", cteName, location); cte.source = analyzer; - ctx.addMaterializedTable(cteName, table, getMaterializedTableStats(analyzer.getSinkOp(), table)); + ctx.addMaterializedTable(cteName, table, getMaterializedTableStats(analyzer.getSinkOp())); return table; } - static Statistics getMaterializedTableStats(Operator<?> sinkOp, Table table) { + protected Statistics getMaterializedTableStats(Operator<?> sinkOp) { final Statistics tableStats = sinkOp.getStatistics().clone(); - final List<ColStatistics> sourceColStatsList = tableStats.getColumnStats(); - final List<String> colNames = table.getCols().stream().map(FieldSchema::getName).collect(Collectors.toList()); - if (sourceColStatsList.size() != colNames.size()) { - throw new IllegalStateException(String.format( - "The size of col stats must be equal to that of schema. Stats = %s, Schema = %s", - sourceColStatsList, colNames)); - } - final List<ColStatistics> colStatsList = new ArrayList<>(sourceColStatsList.size()); - for (int i = 0; i < sourceColStatsList.size(); i++) { - final ColStatistics colStats = sourceColStatsList.get(i); - // FileSinkOperator stores column stats with internal names such as "_col1" - colStats.setColumnName(colNames.get(i)); - colStatsList.add(colStats); + if (tableStats.getColumnStatsState() == Statistics.State.NONE || sinkOp.getNumParent() == 0) { + return tableStats; + } + + final List<String> parentColumnNames = sinkOp.getParentOperators().get(0).getSchema().getColumnNames(); + final List<String> childColumnNames = sinkOp.getSchema().getColumnNames(); + if (parentColumnNames.size() != childColumnNames.size()) { + LOG.warn("The number of columns of FileSinkOperator is inconsistent. Parent = {}, Child = {}", + parentColumnNames, childColumnNames); + tableStats.setColumnStatsState(Statistics.State.NONE); + return tableStats; + } + final Map<String, String> mapping = new HashMap<>(parentColumnNames.size()); + for (int i = 0; i < parentColumnNames.size(); i++) { + mapping.put(parentColumnNames.get(i), childColumnNames.get(i)); + } + + final List<ColStatistics> colStatsList = tableStats.getColumnStats(); + for (ColStatistics colStats : colStatsList) { + if (mapping.containsKey(colStats.getColumnName())) { Review Comment: I added a logging. https://github.com/apache/hive/pull/5106/commits/111bcdf6e50b29ff1967c0a6e9a512dcf261c366 -- 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: gitbox-unsubscr...@hive.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: gitbox-unsubscr...@hive.apache.org For additional commands, e-mail: gitbox-h...@hive.apache.org