|
32 | 32 | import java.util.Objects; |
33 | 33 | import java.util.Properties; |
34 | 34 | import java.util.Set; |
| 35 | +import java.util.TreeMap; |
35 | 36 | import java.util.stream.Collectors; |
36 | 37 |
|
37 | 38 |
|
38 | 39 | import org.apache.commons.collections4.CollectionUtils; |
39 | 40 | import org.apache.commons.lang3.StringUtils; |
| 41 | +import org.apache.commons.lang3.tuple.Pair; |
40 | 42 | import org.apache.hadoop.conf.Configuration; |
41 | 43 | import org.apache.hadoop.fs.FileStatus; |
42 | 44 | import org.apache.hadoop.fs.FileSystem; |
@@ -113,7 +115,8 @@ public class Table implements Serializable { |
113 | 115 | /** |
114 | 116 | * These fields are all cached fields. The information comes from tTable. |
115 | 117 | */ |
116 | | - private List<FieldSchema> cachedPartCols; |
| 118 | + private List<FieldSchema> tablePartCols; |
| 119 | + private Map<String, Pair<Integer, FieldSchema>> inputColumnIndexByName; |
117 | 120 | private transient Deserializer deserializer; |
118 | 121 | private Class<? extends OutputFormat> outputFormatClass; |
119 | 122 | private Class<? extends InputFormat> inputFormatClass; |
@@ -198,8 +201,8 @@ public Table makeCopy() { |
198 | 201 |
|
199 | 202 | newTab.setMetaTable(this.getMetaTable()); |
200 | 203 | 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); |
203 | 206 | } |
204 | 207 | return newTab; |
205 | 208 | } |
@@ -616,15 +619,15 @@ private List<FieldSchema> getNativePartCols() { |
616 | 619 | * where partition columns are not stored in the metastore. |
617 | 620 | */ |
618 | 621 | public List<FieldSchema> getPartCols() { |
619 | | - if (cachedPartCols != null) { |
620 | | - return cachedPartCols; |
| 622 | + if (tablePartCols != null) { |
| 623 | + return tablePartCols; |
621 | 624 | } |
622 | 625 | if (isTableTypeSet() && hasNonNativePartitionSupport()) { |
623 | | - cachedPartCols = getStorageHandler().getPartitionKeys(this); |
| 626 | + tablePartCols = getStorageHandler().getPartitionKeys(this); |
624 | 627 | } else { |
625 | | - cachedPartCols = getNativePartCols(); |
| 628 | + tablePartCols = getNativePartCols(); |
626 | 629 | } |
627 | | - return cachedPartCols; |
| 630 | + return tablePartCols; |
628 | 631 | } |
629 | 632 |
|
630 | 633 | private boolean isTableTypeSet() { |
@@ -756,18 +759,44 @@ private boolean isField(String col) { |
756 | 759 | return false; |
757 | 760 | } |
758 | 761 |
|
| 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 | + |
759 | 787 | public List<FieldSchema> getCols() { |
760 | 788 | if (!isNonNative()) { |
761 | 789 | 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 | + } |
768 | 797 | } |
| 798 | + return nonPartFields; |
769 | 799 | } |
770 | | - return nonPartFields; |
771 | 800 | } |
772 | 801 |
|
773 | 802 | public List<FieldSchema> getColsForMetastore() { |
@@ -800,9 +829,11 @@ private List<FieldSchema> getColsInternal(boolean forMs) { |
800 | 829 | * @return List<FieldSchema> |
801 | 830 | */ |
802 | 831 | 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; |
806 | 837 | } |
807 | 838 |
|
808 | 839 | public void setPartCols(List<FieldSchema> partCols) { |
|
0 commit comments