Skip to content

Commit 1e4fddd

Browse files
authored
Merge pull request #2758 from ClickHouse/02/24/26/fix_hasValue_for_no_column
[client-v2] fixed hasValue to not throw exception
2 parents 0d0b9bb + c0324b8 commit 1e4fddd

File tree

4 files changed

+64
-5
lines changed

4 files changed

+64
-5
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -569,12 +569,15 @@ public Object[] getObjectArray(String colName) {
569569

570570
@Override
571571
public boolean hasValue(int colIndex) {
572+
if (colIndex < 1 || colIndex > currentRecord.length) {
573+
return false;
574+
}
572575
return currentRecord[colIndex - 1] != null;
573576
}
574577

575578
@Override
576579
public boolean hasValue(String colName) {
577-
return hasValue(schema.nameToColumnIndex(colName));
580+
return hasValue(schema.findColumnIndex(colName));
578581
}
579582

580583
@Override

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.clickhouse.client.api.ClientException;
44
import com.clickhouse.client.api.internal.DataTypeConverter;
5+
import com.clickhouse.client.api.metadata.NoSuchColumnException;
56
import com.clickhouse.client.api.metadata.TableSchema;
67
import com.clickhouse.client.api.query.GenericRecord;
78
import com.clickhouse.client.api.query.NullValueException;
@@ -11,6 +12,7 @@
1112
import com.clickhouse.data.value.ClickHouseGeoPointValue;
1213
import com.clickhouse.data.value.ClickHouseGeoPolygonValue;
1314
import com.clickhouse.data.value.ClickHouseGeoRingValue;
15+
import com.google.common.collect.ImmutableList;
1416

1517
import java.math.BigDecimal;
1618
import java.math.BigInteger;
@@ -310,7 +312,8 @@ public String[] getStringArray(String colName) {
310312

311313
@Override
312314
public boolean hasValue(int colIndex) {
313-
return hasValue(schema.columnIndexToName(colIndex));
315+
String columnName = schema.findColumnName(colIndex);
316+
return columnName != null && hasValue(columnName);
314317
}
315318

316319
@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{" +

client-v2/src/test/java/com/clickhouse/client/query/QueryTests.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,41 @@ public void testQueryRecordsReadsAllValues() throws Exception {
694694
}
695695
}
696696

697+
@Test(groups = {"integration"})
698+
public void testSimpleResultSetReadWithBinaryReader() throws Exception {
699+
QuerySettings settings = new QuerySettings().setFormat(ClickHouseFormat.RowBinaryWithNamesAndTypes);
700+
701+
try (QueryResponse response = client.query("SELECT 1 a, null::Nullable(Int32) b", settings).get(3, TimeUnit.SECONDS)) {
702+
ClickHouseBinaryFormatReader reader = client.newBinaryFormatReader(response);
703+
704+
Assert.assertTrue(reader.hasNext());
705+
reader.next();
706+
Assert.assertEquals(reader.getInteger(1), 1);
707+
708+
Assert.assertTrue(reader.hasValue(1));
709+
Assert.assertFalse(reader.hasValue(2));
710+
Assert.assertFalse(reader.hasValue(3));
711+
712+
Assert.assertTrue(reader.hasValue("a"));
713+
Assert.assertFalse(reader.hasValue("b"));
714+
Assert.assertFalse(reader.hasValue("c"));
715+
}
716+
717+
List<GenericRecord> records = client.queryAll("SELECT 1 a, null::Nullable(Int32) b", settings);
718+
719+
GenericRecord record = records.get(0);
720+
721+
Assert.assertEquals(record.getInteger(1), 1);
722+
723+
Assert.assertTrue(record.hasValue(1));
724+
Assert.assertFalse(record.hasValue(2));
725+
Assert.assertFalse(record.hasValue(3));
726+
727+
Assert.assertTrue(record.hasValue("a"));
728+
Assert.assertFalse(record.hasValue("b"));
729+
Assert.assertFalse(record.hasValue("c"));
730+
}
731+
697732
private final static List<String> NULL_DATASET_COLUMNS = Arrays.asList(
698733
"id UInt32",
699734
"col1 UInt32 NULL",

0 commit comments

Comments
 (0)