Skip to content

Commit 0f57171

Browse files
authored
Added column index map to speed up initial table creation (#115)
1 parent 53441c6 commit 0f57171

2 files changed

Lines changed: 19 additions & 3 deletions

File tree

src/main/java/net/tlabs/tablesaw/parquet/TablesawParquetReadOptions.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646
import org.slf4j.Logger;
4747
import org.slf4j.LoggerFactory;
4848

49+
import it.unimi.dsi.fastutil.objects.Object2IntMap;
50+
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
4951
import tech.tablesaw.api.ColumnType;
5052
import tech.tablesaw.io.ReadOptions;
5153

@@ -71,6 +73,7 @@ public enum UnnanotatedBinaryAs {
7173
private final boolean floatColumnTypeUsed;
7274
private final ManageGroupsAs manageGroupsAs;
7375
private final List<String> columns;
76+
private final Object2IntMap<String> columnIndex;
7477
private final URI inputURI;
7578
private final FileDecryptionProperties fileDecryptionProperties;
7679
private final Filter recordFilter;
@@ -81,6 +84,11 @@ protected TablesawParquetReadOptions(final Builder builder) {
8184
unnanotatedBinaryAs = builder.unnanotatedBinaryAs;
8285
manageGroupsAs = builder.manageGroupsAs;
8386
columns = Collections.unmodifiableList(Arrays.asList(builder.columns));
87+
final int nbCols = columns.size();
88+
columnIndex = new Object2IntOpenHashMap<>(nbCols);
89+
for(int i = nbCols; --i >= 0; ) {
90+
columnIndex.put(columns.get(i), i);
91+
}
8492
inputURI = builder.inputURI;
8593
shortColumnTypeUsed = this.columnTypesToDetect.contains(ColumnType.SHORT);
8694
floatColumnTypeUsed = this.columnTypesToDetect.contains(ColumnType.FLOAT);
@@ -124,7 +132,16 @@ public List<String> getColumns() {
124132
*/
125133
public boolean hasColumn(final String columnName) {
126134
if(columns.isEmpty()) return true;
127-
return columns.contains(columnName);
135+
return columnIndex.containsKey(columnName);
136+
}
137+
138+
/**
139+
* Returns the index of the given column name or -1 if not in the column list
140+
* @param columnName the column name to get the index for
141+
* @return the column index or -1 if not in the list
142+
*/
143+
public int indexOfColumn(final String columnName) {
144+
return columnIndex.getOrDefault(columnName, -1);
128145
}
129146

130147
public URI getInputURI() {

src/main/java/net/tlabs/tablesaw/parquet/TablesawReadSupport.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,7 @@ public ReadContext init(final InitContext context) {
8686
// because full mapping by idx is done on the filtered fields only
8787
// sort is stable for an empty column list
8888
.boxed()
89-
.sorted(Comparator.comparingInt(i -> options.getColumns()
90-
.indexOf(initialFields.get(i).getName())))
89+
.sorted(Comparator.comparingInt(i -> options.indexOfColumn(initialFields.get(i).getName())))
9190
.collect(Collectors.toList());
9291
// mapping by idx uses filtered column index
9392
final List<Integer> projectedFieldsIndices = IntStream.range(0, filteredFieldsIndices.size())

0 commit comments

Comments
 (0)