Skip to content

Commit e57123f

Browse files
committed
commit
1 parent f64adc6 commit e57123f

1 file changed

Lines changed: 49 additions & 18 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: 49 additions & 18 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,8 @@ 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 Map<String, Pair<Integer, FieldSchema>> inputColumnIndexByName;
117120
private transient Deserializer deserializer;
118121
private Class<? extends OutputFormat> outputFormatClass;
119122
private Class<? extends InputFormat> inputFormatClass;
@@ -198,8 +201,8 @@ public Table makeCopy() {
198201

199202
newTab.setMetaTable(this.getMetaTable());
200203
newTab.setSnapshotRef(this.getSnapshotRef());
201-
if (this.cachedPartCols != null) {
202-
newTab.cachedPartCols = new ArrayList<>(this.cachedPartCols);
204+
if (this.tablePartCols != null) {
205+
newTab.tablePartCols = new ArrayList<>(this.tablePartCols);
203206
}
204207
return newTab;
205208
}
@@ -616,15 +619,15 @@ private List<FieldSchema> getNativePartCols() {
616619
* where partition columns are not stored in the metastore.
617620
*/
618621
public List<FieldSchema> getPartCols() {
619-
if (cachedPartCols != null) {
620-
return cachedPartCols;
622+
if (tablePartCols != null) {
623+
return tablePartCols;
621624
}
622625
if (isTableTypeSet() && hasNonNativePartitionSupport()) {
623-
cachedPartCols = getStorageHandler().getPartitionKeys(this);
626+
tablePartCols = getStorageHandler().getPartitionKeys(this);
624627
} else {
625-
cachedPartCols = getNativePartCols();
628+
tablePartCols = getNativePartCols();
626629
}
627-
return cachedPartCols;
630+
return tablePartCols;
628631
}
629632

630633
private boolean isTableTypeSet() {
@@ -756,18 +759,44 @@ private boolean isField(String col) {
756759
return false;
757760
}
758761

762+
private void fillColumnIndexByName() {
763+
inputColumnIndexByName = new HashMap<>();
764+
List<FieldSchema> fsList = new ArrayList<>(getColsInternal(false));
765+
if (!isNonNative()) {
766+
fsList.addAll(getNativePartCols());
767+
}
768+
for (int i = 0; i < fsList.size(); i++) {
769+
inputColumnIndexByName.put(fsList.get(i).getName(), Pair.of(i, fsList.get(i)));
770+
}
771+
}
772+
773+
public int getColumnIndexByName(String colName) {
774+
if (inputColumnIndexByName == null) {
775+
fillColumnIndexByName();
776+
}
777+
return inputColumnIndexByName.get(colName.toLowerCase()).getLeft();
778+
}
779+
780+
public FieldSchema getFieldSchemaByName(String colName) {
781+
if (inputColumnIndexByName == null) {
782+
fillColumnIndexByName();
783+
}
784+
return inputColumnIndexByName.get(colName).getRight();
785+
}
786+
759787
public List<FieldSchema> getCols() {
760788
if (!isNonNative()) {
761789
return getColsInternal(false);
762-
}
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);
790+
} else {
791+
List<FieldSchema> nonPartFields = new ArrayList<>();
792+
Set<String> partFieldsName = getPartCols().stream().map(FieldSchema::getName).collect(Collectors.toSet());
793+
for (FieldSchema field : getColsInternal(false)) {
794+
if (!partFieldsName.contains(field.getName())) {
795+
nonPartFields.add(field);
796+
}
768797
}
798+
return nonPartFields;
769799
}
770-
return nonPartFields;
771800
}
772801

773802
public List<FieldSchema> getColsForMetastore() {
@@ -800,9 +829,11 @@ private List<FieldSchema> getColsInternal(boolean forMs) {
800829
* @return List&lt;FieldSchema&gt;
801830
*/
802831
public List<FieldSchema> getAllCols() {
803-
ArrayList<FieldSchema> allCols = new ArrayList<>(getCols());
804-
allCols.addAll(getPartCols());
805-
return allCols;
832+
List<FieldSchema> fsList = new ArrayList<>(getColsInternal(false));
833+
if (!isNonNative()) {
834+
fsList.addAll(getNativePartCols());
835+
}
836+
return fsList;
806837
}
807838

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

0 commit comments

Comments
 (0)