|
25 | 25 | import java.util.ArrayList; |
26 | 26 | import java.util.Collections; |
27 | 27 | import java.util.HashMap; |
28 | | -import java.util.HashSet; |
29 | 28 | import java.util.List; |
30 | 29 | import java.util.Map; |
31 | 30 | import java.util.Objects; |
32 | | -import java.util.Set; |
33 | 31 | import java.util.stream.Collectors; |
34 | 32 |
|
35 | 33 | import com.google.common.collect.Maps; |
@@ -218,24 +216,43 @@ private static String getColTypeOf(Table tbl, String partKey) { |
218 | 216 | throw new RuntimeException("Unknown partition key : " + partKey); |
219 | 217 | } |
220 | 218 |
|
221 | | - protected static List<FieldSchema> getFieldSchemasByColName(Table tbl, List<String> colNames) { |
222 | | - List<FieldSchema> cols = tbl.getCols(); |
223 | | - Map<String, FieldSchema> colFsMap = new HashMap<>(); |
224 | | - for (FieldSchema col : cols) { |
225 | | - colFsMap.put(col.getName().toLowerCase(), col); |
| 219 | + protected static List<FieldSchema> getFieldSchemasByColName(Table tbl, List<String> colNames) |
| 220 | + throws SemanticException { |
| 221 | + Map<String, FieldSchema> specifiedColsMap = new HashMap<>(); |
| 222 | + for (String colName : colNames) { |
| 223 | + specifiedColsMap.put(colName.toLowerCase(), new FieldSchema(colName, null, null)); |
| 224 | + } |
| 225 | + |
| 226 | + for (FieldSchema pk : tbl.getPartitionKeys()) { |
| 227 | + FieldSchema fs = specifiedColsMap.get(pk.getName().toLowerCase()); |
| 228 | + if (fs != null) { |
| 229 | + throw new SemanticException(ErrorMsg.COLUMNSTATSCOLLECTOR_INVALID_COLUMN.getMsg() |
| 230 | + + " [Try removing column '" + fs.getName() + "' from column list]"); |
| 231 | + } |
| 232 | + } |
| 233 | + |
| 234 | + for (FieldSchema col : tbl.getCols()) { |
| 235 | + specifiedColsMap.computeIfPresent(col.getName().toLowerCase(), (key, value) -> col); |
226 | 236 | } |
| 237 | + |
227 | 238 | List<FieldSchema> result = new ArrayList<>(); |
| 239 | + List<String> tableColNames = new FieldSchemas(tbl.getCols()).getColName(); |
228 | 240 | for (String colName : colNames) { |
229 | | - FieldSchema fs = colFsMap.get(colName.toLowerCase()); |
230 | | - if (fs != null) { |
231 | | - String type = fs.getType(); |
232 | | - TypeInfo typeInfo = TypeInfoUtils.getTypeInfoFromTypeString(type); |
233 | | - boolean isSupported = ColumnStatsAutoGatherContext.isColumnSupported(typeInfo.getCategory(), () -> typeInfo); |
234 | | - if (!isSupported) { |
235 | | - logTypeWarning(colName, type); |
236 | | - } else { |
237 | | - result.add(new FieldSchema(colName, type, fs.getComment())); |
238 | | - } |
| 241 | + FieldSchema fs = specifiedColsMap.get(colName.toLowerCase()); |
| 242 | + |
| 243 | + // If the type is null, the column does not exist as its FieldSchema was not populated from tbl.getCols() |
| 244 | + if (fs.getType() == null) { |
| 245 | + String msg = "'" + colName + "' (possible columns are " + tableColNames + ")"; |
| 246 | + throw new SemanticException(ErrorMsg.INVALID_COLUMN.getMsg(msg)); |
| 247 | + } |
| 248 | + |
| 249 | + String type = fs.getType(); |
| 250 | + TypeInfo typeInfo = TypeInfoUtils.getTypeInfoFromTypeString(type); |
| 251 | + boolean isSupported = ColumnStatsAutoGatherContext.isColumnSupported(typeInfo.getCategory(), () -> typeInfo); |
| 252 | + if (!isSupported) { |
| 253 | + logTypeWarning(colName, type); |
| 254 | + } else { |
| 255 | + result.add(new FieldSchema(colName, type, fs.getComment())); |
239 | 256 | } |
240 | 257 | } |
241 | 258 | return result; |
@@ -585,35 +602,6 @@ private ASTNode genRewrittenTree(String rewrittenQuery) throws SemanticException |
585 | 602 | } |
586 | 603 | } |
587 | 604 |
|
588 | | - private void validateSpecifiedColumnNames(List<String> specifiedCols) throws SemanticException { |
589 | | - FieldSchemas tableCols = new FieldSchemas(tbl.getCols()); |
590 | | - Set<String> tableColNamesLc = new HashSet<>(); |
591 | | - for (FieldSchema fs : tableCols.getSchemas()) { |
592 | | - tableColNamesLc.add(fs.getName().toLowerCase()); |
593 | | - } |
594 | | - List<String> tableColNames = tableCols.getColName(); |
595 | | - for (String sc : specifiedCols) { |
596 | | - if (!tableColNamesLc.contains(sc.toLowerCase())) { |
597 | | - String msg = "'" + sc + "' (possible columns are " + tableColNames + ")"; |
598 | | - throw new SemanticException(ErrorMsg.INVALID_COLUMN.getMsg(msg)); |
599 | | - } |
600 | | - } |
601 | | - } |
602 | | - |
603 | | - private void checkForPartitionColumns(List<String> specifiedCols) throws SemanticException { |
604 | | - Map<String, String> specifiedColsMap = new HashMap<>(); |
605 | | - for (String sc : specifiedCols) { |
606 | | - specifiedColsMap.put(sc.toLowerCase(), sc); |
607 | | - } |
608 | | - for (FieldSchema pk : tbl.getPartitionKeys()) { |
609 | | - String specifiedCol = specifiedColsMap.get(pk.getName().toLowerCase()); |
610 | | - if (specifiedCol != null) { |
611 | | - throw new SemanticException(ErrorMsg.COLUMNSTATSCOLLECTOR_INVALID_COLUMN.getMsg() |
612 | | - + " [Try removing column '" + specifiedCol + "' from column list]"); |
613 | | - } |
614 | | - } |
615 | | - } |
616 | | - |
617 | 605 | private static void logTypeWarning(String colName, String colType) { |
618 | 606 | String warning = "Only primitive type arguments are accepted but " + colType |
619 | 607 | + " is passed for " + colName + "."; |
@@ -744,9 +732,6 @@ protected List<FieldSchema> getColumnsFromAst(ASTNode ast) throws SemanticExcept |
744 | 732 | columnNames = getExplicitColumnNamesFromAst(ast); |
745 | 733 | } |
746 | 734 |
|
747 | | - checkForPartitionColumns(columnNames); |
748 | | - validateSpecifiedColumnNames(columnNames); |
749 | | - |
750 | 735 | return statsEligibleFS != null ? statsEligibleFS : getFieldSchemasByColName(tbl, columnNames); |
751 | 736 | } |
752 | 737 |
|
|
0 commit comments