Skip to content

Commit df9692f

Browse files
authored
Merge pull request #2777 from PierceAndy/feature/records-get-schema
Add getSchema() to Records for schema access with empty results
2 parents b5d55e3 + ab709af commit df9692f

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

client-v2/src/main/java/com/clickhouse/client/api/query/Records.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.clickhouse.client.api.data_formats.ClickHouseBinaryFormatReader;
44
import com.clickhouse.client.api.data_formats.internal.BinaryReaderBackedRecord;
5+
import com.clickhouse.client.api.metadata.TableSchema;
56
import com.clickhouse.client.api.metrics.OperationMetrics;
67
import com.clickhouse.client.api.metrics.ServerMetrics;
78

@@ -64,6 +65,15 @@ public boolean isEmpty() {
6465
return empty;
6566
}
6667

68+
/**
69+
* Returns the schema of the collection.
70+
*
71+
* @return table schema with column definitions
72+
*/
73+
public TableSchema getSchema() {
74+
return reader.getSchema();
75+
}
76+
6777
Stream<GenericRecord> stream() {
6878
return StreamSupport.stream(spliterator(), false);
6979
}

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,36 @@ public void testQueryRecordsEmptyResult() throws Exception {
678678
}
679679
}
680680

681+
@Test(groups = {"integration"})
682+
public void testGetSchemaWithEmptyResult() throws Exception {
683+
try (Records records = client.queryRecords("SELECT 1 as id, 'test' as name LIMIT 0").get(3, TimeUnit.SECONDS)) {
684+
Assert.assertTrue(records.isEmpty());
685+
686+
TableSchema schema = records.getSchema();
687+
Assert.assertNotNull(schema);
688+
Assert.assertEquals(schema.getColumns().size(), 2);
689+
Assert.assertEquals(schema.getColumnByIndex(1).getColumnName(), "id");
690+
Assert.assertEquals(schema.getColumnByIndex(2).getColumnName(), "name");
691+
}
692+
}
693+
694+
@Test(groups = {"integration"})
695+
public void testGetSchemaWithResults() throws Exception {
696+
try (Records records = client.queryRecords("SELECT 1 as id, 'test' as name").get(3, TimeUnit.SECONDS)) {
697+
Assert.assertFalse(records.isEmpty());
698+
699+
TableSchema recordsSchema = records.getSchema();
700+
Assert.assertNotNull(recordsSchema);
701+
Assert.assertEquals(recordsSchema.getColumns().size(), 2);
702+
703+
for (GenericRecord record : records) {
704+
Assert.assertEquals(record.getInteger("id"), Integer.valueOf(1));
705+
Assert.assertEquals(record.getString("name"), "test");
706+
Assert.assertEquals(record.getSchema(), recordsSchema);
707+
}
708+
}
709+
}
710+
681711
@Test(description = "Verifies that queryRecords reads all values from the response", groups = {"integration"})
682712
public void testQueryRecordsReadsAllValues() throws Exception {
683713
try (Records records = client.queryRecords("SELECT toInt32(number) FROM system.numbers LIMIT 3").get(3, TimeUnit.SECONDS)) {

0 commit comments

Comments
 (0)