Skip to content

Commit 37d1c22

Browse files
committed
Merge checkForPartitionColumns & validateSpecifiedColumnNames in getFieldSchemasByColName
1 parent 68446bd commit 37d1c22

1 file changed

Lines changed: 34 additions & 49 deletions

File tree

ql/src/java/org/apache/hadoop/hive/ql/parse/ColumnStatsSemanticAnalyzer.java

Lines changed: 34 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,9 @@
2525
import java.util.ArrayList;
2626
import java.util.Collections;
2727
import java.util.HashMap;
28-
import java.util.HashSet;
2928
import java.util.List;
3029
import java.util.Map;
3130
import java.util.Objects;
32-
import java.util.Set;
3331
import java.util.stream.Collectors;
3432

3533
import com.google.common.collect.Maps;
@@ -218,24 +216,43 @@ private static String getColTypeOf(Table tbl, String partKey) {
218216
throw new RuntimeException("Unknown partition key : " + partKey);
219217
}
220218

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);
226236
}
237+
227238
List<FieldSchema> result = new ArrayList<>();
239+
List<String> tableColNames = new FieldSchemas(tbl.getCols()).getColName();
228240
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()));
239256
}
240257
}
241258
return result;
@@ -585,35 +602,6 @@ private ASTNode genRewrittenTree(String rewrittenQuery) throws SemanticException
585602
}
586603
}
587604

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-
617605
private static void logTypeWarning(String colName, String colType) {
618606
String warning = "Only primitive type arguments are accepted but " + colType
619607
+ " is passed for " + colName + ".";
@@ -744,9 +732,6 @@ protected List<FieldSchema> getColumnsFromAst(ASTNode ast) throws SemanticExcept
744732
columnNames = getExplicitColumnNamesFromAst(ast);
745733
}
746734

747-
checkForPartitionColumns(columnNames);
748-
validateSpecifiedColumnNames(columnNames);
749-
750735
return statsEligibleFS != null ? statsEligibleFS : getFieldSchemasByColName(tbl, columnNames);
751736
}
752737

0 commit comments

Comments
 (0)