Skip to content

Commit c0324b8

Browse files
committed
optimized code
1 parent f467f42 commit c0324b8

File tree

3 files changed

+25
-13
lines changed

3 files changed

+25
-13
lines changed

client-v2/src/main/java/com/clickhouse/client/api/data_formats/internal/AbstractBinaryFormatReader.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -577,11 +577,7 @@ public boolean hasValue(int colIndex) {
577577

578578
@Override
579579
public boolean hasValue(String colName) {
580-
try {
581-
return hasValue(schema.nameToColumnIndex(colName));
582-
} catch (NoSuchColumnException e) {
583-
return false;
584-
}
580+
return hasValue(schema.findColumnIndex(colName));
585581
}
586582

587583
@Override

client-v2/src/main/java/com/clickhouse/client/api/data_formats/internal/MapBackedRecord.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import com.clickhouse.data.value.ClickHouseGeoPointValue;
1313
import com.clickhouse.data.value.ClickHouseGeoPolygonValue;
1414
import com.clickhouse.data.value.ClickHouseGeoRingValue;
15+
import com.google.common.collect.ImmutableList;
1516

1617
import java.math.BigDecimal;
1718
import java.math.BigInteger;
@@ -311,11 +312,8 @@ public String[] getStringArray(String colName) {
311312

312313
@Override
313314
public boolean hasValue(int colIndex) {
314-
try {
315-
return hasValue(schema.columnIndexToName(colIndex));
316-
} catch (NoSuchColumnException e) {
317-
return false;
318-
}
315+
String columnName = schema.findColumnName(colIndex);
316+
return columnName != null && hasValue(columnName);
319317
}
320318

321319
@Override

client-v2/src/main/java/com/clickhouse/client/api/metadata/TableSchema.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,10 @@ public ClickHouseColumn getColumnByIndex(int colIndex) {
8686
* @return - column name
8787
*/
8888
public String indexToName(int index) {
89-
try {
90-
return columns.get(index).getColumnName();
91-
} catch (IndexOutOfBoundsException e) {
89+
if (index < 0 || index >= columns.size()) {
9290
throw new NoSuchColumnException("Result has no column with index = " + index);
9391
}
92+
return columns.get(index).getColumnName();
9493
}
9594

9695
/**
@@ -129,6 +128,25 @@ public int nameToIndex(String name) {
129128
return index;
130129
}
131130

131+
/**
132+
* Looks up for column 1-based index for a column name.
133+
* @param columnName - name of column to search
134+
* @return column 1-based index of column or -1 if not found
135+
*/
136+
public int findColumnIndex(String columnName) {
137+
Integer index = colIndex.get(columnName);
138+
return index == null ? -1 : index + 1;
139+
}
140+
141+
public String findColumnName(int colIndex) {
142+
int lookupIndex = colIndex - 1;
143+
if (lookupIndex < 0 || lookupIndex >= columns.size()) {
144+
return null;
145+
}
146+
147+
return columns.get(lookupIndex).getColumnName();
148+
}
149+
132150
@Override
133151
public String toString() {
134152
return "TableSchema{" +

0 commit comments

Comments
 (0)