Skip to content

Commit 05ff371

Browse files
committed
fixed hasValue to not throw exception
1 parent bb676e6 commit 05ff371

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -569,12 +569,19 @@ 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+
try {
581+
return hasValue(schema.nameToColumnIndex(colName));
582+
} catch (NoSuchColumnException e) {
583+
return false;
584+
}
578585
}
579586

580587
@Override

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

Lines changed: 6 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;
@@ -310,7 +311,11 @@ public String[] getStringArray(String colName) {
310311

311312
@Override
312313
public boolean hasValue(int colIndex) {
313-
return hasValue(schema.columnIndexToName(colIndex));
314+
try {
315+
return hasValue(schema.columnIndexToName(colIndex));
316+
} catch (NoSuchColumnException e) {
317+
return false;
318+
}
314319
}
315320

316321
@Override

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)