Skip to content

Commit f837d66

Browse files
committed
commit
1 parent 81dba94 commit f837d66

1 file changed

Lines changed: 56 additions & 19 deletions

File tree

  • ql/src/java/org/apache/hadoop/hive/ql/metadata

ql/src/java/org/apache/hadoop/hive/ql/metadata/Table.java

Lines changed: 56 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,13 @@
3232
import java.util.Objects;
3333
import java.util.Properties;
3434
import java.util.Set;
35+
import java.util.TreeMap;
3536
import java.util.stream.Collectors;
3637

3738

3839
import org.apache.commons.collections4.CollectionUtils;
3940
import org.apache.commons.lang3.StringUtils;
41+
import org.apache.commons.lang3.tuple.Pair;
4042
import org.apache.hadoop.conf.Configuration;
4143
import org.apache.hadoop.fs.FileStatus;
4244
import org.apache.hadoop.fs.FileSystem;
@@ -113,7 +115,10 @@ public class Table implements Serializable {
113115
/**
114116
* These fields are all cached fields. The information comes from tTable.
115117
*/
116-
private List<FieldSchema> cachedPartCols;
118+
private List<FieldSchema> tablePartCols;
119+
private List<FieldSchema> tableNonPartCols;
120+
private List<FieldSchema> tableAllCols;
121+
private Map<String, Pair<Integer, FieldSchema>> inputColumnIndexByName;
117122
private transient Deserializer deserializer;
118123
private Class<? extends OutputFormat> outputFormatClass;
119124
private Class<? extends InputFormat> inputFormatClass;
@@ -198,8 +203,8 @@ public Table makeCopy() {
198203

199204
newTab.setMetaTable(this.getMetaTable());
200205
newTab.setSnapshotRef(this.getSnapshotRef());
201-
if (this.cachedPartCols != null) {
202-
newTab.cachedPartCols = new ArrayList<>(this.cachedPartCols);
206+
if (this.tablePartCols != null) {
207+
newTab.tablePartCols = new ArrayList<>(this.tablePartCols);
203208
}
204209
return newTab;
205210
}
@@ -616,15 +621,15 @@ private List<FieldSchema> getNativePartCols() {
616621
* where partition columns are not stored in the metastore.
617622
*/
618623
public List<FieldSchema> getPartCols() {
619-
if (cachedPartCols != null) {
620-
return cachedPartCols;
624+
if (tablePartCols != null) {
625+
return tablePartCols;
621626
}
622627
if (isTableTypeSet() && hasNonNativePartitionSupport()) {
623-
cachedPartCols = getStorageHandler().getPartitionKeys(this);
628+
tablePartCols = getStorageHandler().getPartitionKeys(this);
624629
} else {
625-
cachedPartCols = getNativePartCols();
630+
tablePartCols = getNativePartCols();
626631
}
627-
return cachedPartCols;
632+
return tablePartCols;
628633
}
629634

630635
private boolean isTableTypeSet() {
@@ -756,18 +761,48 @@ private boolean isField(String col) {
756761
return false;
757762
}
758763

759-
public List<FieldSchema> getCols() {
764+
private void fillColumnIndexByName() {
765+
inputColumnIndexByName = new HashMap<>();
766+
List<FieldSchema> fsList = new ArrayList<>(getColsInternal(false));
760767
if (!isNonNative()) {
761-
return getColsInternal(false);
768+
fsList.addAll(getNativePartCols());
769+
}
770+
for (int i = 0; i < fsList.size(); i++) {
771+
inputColumnIndexByName.put(fsList.get(i).getName(), Pair.of(i, fsList.get(i)));
772+
}
773+
}
774+
775+
public int getColumnIndexByName(String colName) {
776+
if (inputColumnIndexByName == null) {
777+
fillColumnIndexByName();
762778
}
763-
List<FieldSchema> nonPartFields = new ArrayList<>();
764-
Set<String> partFieldsName = getPartCols().stream().map(FieldSchema::getName).collect(Collectors.toSet());
765-
for (FieldSchema field : getColsInternal(false)) {
766-
if (!partFieldsName.contains(field.getName())) {
767-
nonPartFields.add(field);
779+
return inputColumnIndexByName.get(colName.toLowerCase()).getLeft();
780+
}
781+
782+
public FieldSchema getFieldSchemaByName(String colName) {
783+
if (inputColumnIndexByName == null) {
784+
fillColumnIndexByName();
785+
}
786+
return inputColumnIndexByName.get(colName).getRight();
787+
}
788+
789+
public List<FieldSchema> getCols() {
790+
if (tableNonPartCols != null) {
791+
return tableNonPartCols;
792+
}
793+
if (!isNonNative()) {
794+
tableNonPartCols = getColsInternal(false);
795+
} else {
796+
List<FieldSchema> nonPartFields = new ArrayList<>();
797+
Set<String> partFieldsName = getPartCols().stream().map(FieldSchema::getName).collect(Collectors.toSet());
798+
for (FieldSchema field : getColsInternal(false)) {
799+
if (!partFieldsName.contains(field.getName())) {
800+
nonPartFields.add(field);
801+
}
768802
}
803+
tableNonPartCols = nonPartFields;
769804
}
770-
return nonPartFields;
805+
return tableNonPartCols;
771806
}
772807

773808
public List<FieldSchema> getColsForMetastore() {
@@ -800,9 +835,11 @@ private List<FieldSchema> getColsInternal(boolean forMs) {
800835
* @return List&lt;FieldSchema&gt;
801836
*/
802837
public List<FieldSchema> getAllCols() {
803-
ArrayList<FieldSchema> allCols = new ArrayList<>(getCols());
804-
allCols.addAll(getPartCols());
805-
return allCols;
838+
List<FieldSchema> fsList = new ArrayList<>(getColsInternal(false));
839+
if (!isNonNative()) {
840+
fsList.addAll(getNativePartCols());
841+
}
842+
return fsList;
806843
}
807844

808845
public void setPartCols(List<FieldSchema> partCols) {

0 commit comments

Comments
 (0)