Skip to content

Commit 21891de

Browse files
authored
fix response with no result (#1679)
1 parent d23be8b commit 21891de

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

client-v2/src/main/java/com/clickhouse/client/api/Client.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -788,9 +788,11 @@ public List<GenericRecord> queryAll(String sqlQuery) {
788788
try {
789789
try (QueryResponse response = query(sqlQuery).get(TIMEOUT, TimeUnit.MILLISECONDS)) {
790790
List<GenericRecord> records = new ArrayList<>();
791-
ClickHouseBinaryFormatReader reader = new RowBinaryWithNamesAndTypesFormatReader(response.getInputStream());
792-
while (reader.hasNext()) {
793-
records.add(new MapBackedRecord(reader.next(), reader.getSchema()));
791+
if (response.getResultRows() > 0) {
792+
ClickHouseBinaryFormatReader reader = new RowBinaryWithNamesAndTypesFormatReader(response.getInputStream());
793+
while (reader.hasNext()) {
794+
records.add(new MapBackedRecord(reader.next(), reader.getSchema()));
795+
}
794796
}
795797
return records;
796798
}

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,14 @@ public RowBinaryWithNamesAndTypesFormatReader(InputStream inputStream) {
2323

2424
public RowBinaryWithNamesAndTypesFormatReader(InputStream inputStream, QuerySettings querySettings) {
2525
super(inputStream, querySettings, null);
26-
setSchema(readSchema());
26+
readSchema();
2727
}
2828

29-
private TableSchema readSchema() {
29+
private void readSchema() {
3030
try {
31+
if (inputStream.available() < 1) {
32+
return;
33+
}
3134
TableSchema headerSchema = new TableSchema();
3235
List<String> columns = new ArrayList<>();
3336
int nCol = chInputStream.readVarInt();
@@ -39,7 +42,7 @@ private TableSchema readSchema() {
3942
headerSchema.addColumn(columns.get(i), chInputStream.readUnicodeString());
4043
}
4144

42-
return headerSchema;
45+
setSchema(headerSchema);
4346
} catch (IOException e) {
4447
throw new ClientException("Failed to read header", e);
4548
}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,13 +135,27 @@ public void testReadRecordsGetFirstRecord() throws Exception {
135135
Assert.assertFalse(iter.hasNext());
136136
}
137137

138+
@Test(groups = {"integration"})
139+
public void testReadRecordsNoResult() throws Exception {
140+
Records records = client.queryRecords("CREATE DATABASE IF NOT EXISTS test_db").get(3, TimeUnit.SECONDS);
141+
142+
Iterator<GenericRecord> iter = records.iterator();
143+
Assert.assertFalse(iter.hasNext());
144+
}
145+
138146
@Test(groups = {"integration"})
139147
public void testQueryAll() throws Exception {
140148
prepareDataSet(DATASET_TABLE, DATASET_COLUMNS, DATASET_VALUE_GENERATORS, 10);
141149
GenericRecord hostnameRecord = client.queryAll("SELECT hostname()").stream().findFirst().get();
142150
Assert.assertNotNull(hostnameRecord);
143151
}
144152

153+
@Test(groups = {"integration"})
154+
public void testQueryAllNoResult() throws Exception {
155+
List<GenericRecord> records = client.queryAll("CREATE DATABASE IF NOT EXISTS test_db");
156+
Assert.assertTrue(records.isEmpty());
157+
}
158+
145159
@Test(groups = {"integration"}, enabled = false)
146160
public void testQueryJSONWith64BitIntegers() throws ExecutionException, InterruptedException {
147161
// won't work because format settings are set thru separate statement.

0 commit comments

Comments
 (0)