Skip to content

Commit 0c21432

Browse files
committed
fix: Fix support for simpleAggregateFunction
1 parent 6788dbc commit 0c21432

File tree

2 files changed

+45
-5
lines changed

2 files changed

+45
-5
lines changed

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

+20-5
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,11 @@ protected void setSchema(TableSchema schema) {
226226

227227
for (int i = 0; i < columns.length; i++) {
228228
ClickHouseColumn column = columns[i];
229-
230-
switch (column.getDataType()) {
229+
ClickHouseDataType columnDataType = column.getDataType();
230+
if (columnDataType.equals(ClickHouseDataType.SimpleAggregateFunction)){
231+
columnDataType = column.getNestedColumns().get(0).getDataType();
232+
}
233+
switch (columnDataType) {
231234
case Int8:
232235
case Int16:
233236
case UInt8:
@@ -362,7 +365,11 @@ public BigDecimal getBigDecimal(String colName) {
362365
public Instant getInstant(String colName) {
363366
int colIndex = schema.nameToIndex(colName);
364367
ClickHouseColumn column = schema.getColumns().get(colIndex);
365-
switch (column.getDataType()) {
368+
ClickHouseDataType columnDataType = column.getDataType();
369+
if (columnDataType.equals(ClickHouseDataType.SimpleAggregateFunction)){
370+
columnDataType = column.getNestedColumns().get(0).getDataType();
371+
}
372+
switch (columnDataType) {
366373
case Date:
367374
case Date32:
368375
LocalDate data = readValue(colName);
@@ -380,7 +387,11 @@ public Instant getInstant(String colName) {
380387
public ZonedDateTime getZonedDateTime(String colName) {
381388
int colIndex = schema.nameToIndex(colName);
382389
ClickHouseColumn column = schema.getColumns().get(colIndex);
383-
switch (column.getDataType()) {
390+
ClickHouseDataType columnDataType = column.getDataType();
391+
if (columnDataType.equals(ClickHouseDataType.SimpleAggregateFunction)){
392+
columnDataType = column.getNestedColumns().get(0).getDataType();
393+
}
394+
switch (columnDataType) {
384395
case DateTime:
385396
case DateTime64:
386397
case Date:
@@ -396,8 +407,12 @@ public Duration getDuration(String colName) {
396407
int colIndex = schema.nameToIndex(colName);
397408
ClickHouseColumn column = schema.getColumns().get(colIndex);
398409
BigInteger value = readValue(colName);
410+
ClickHouseDataType columnDataType = column.getDataType();
411+
if (columnDataType.equals(ClickHouseDataType.SimpleAggregateFunction)){
412+
columnDataType = column.getNestedColumns().get(0).getDataType();
413+
}
399414
try {
400-
switch (column.getDataType()) {
415+
switch (columnDataType) {
401416
case IntervalYear:
402417
return Duration.of(value.longValue(), java.time.temporal.ChronoUnit.YEARS);
403418
case IntervalQuarter:

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

+25
Original file line numberDiff line numberDiff line change
@@ -1983,6 +1983,31 @@ public void testReadingSimpleAggregateFunction() throws Exception {
19831983
}
19841984
}
19851985

1986+
@Test(groups = {"integration"})
1987+
public void testReadingSimpleAggregateFunction2() throws Exception {
1988+
final String tableName = "simple_aggregate_function_test_table";
1989+
client.execute("DROP TABLE IF EXISTS " + tableName).get();
1990+
client.execute("CREATE TABLE `" + tableName + "` " +
1991+
"(idx UInt8, lowest_value SimpleAggregateFunction(min, UInt8), count SimpleAggregateFunction(sum, Int64), date SimpleAggregateFunction(anyLast, DateTime32)) " +
1992+
"ENGINE Memory;").get();
1993+
1994+
1995+
try (InsertResponse response = client.insert(tableName, new ByteArrayInputStream("1\t2\t3\t2024-12-22T12:00:00".getBytes(StandardCharsets.UTF_8)), ClickHouseFormat.TSV).get(30, TimeUnit.SECONDS)) {
1996+
Assert.assertEquals(response.getWrittenRows(), 1);
1997+
}
1998+
1999+
try (QueryResponse queryResponse = client.query("SELECT * FROM " + tableName + " LIMIT 1").get(30, TimeUnit.SECONDS)) {
2000+
2001+
ClickHouseBinaryFormatReader reader = client.newBinaryFormatReader(queryResponse);
2002+
Assert.assertNotNull(reader.next());
2003+
Assert.assertEquals(reader.getByte("idx"), Byte.valueOf("1"));
2004+
Assert.assertEquals((Short) reader.getShort("lowest_value"), Short.parseShort("2"));
2005+
Assert.assertEquals((Long) reader.getLong("count"), Long.parseLong("3"));
2006+
Assert.assertEquals(reader.getLocalDateTime("date"), LocalDateTime.of(2024,12,22,12,00,00));
2007+
Assert.assertFalse(reader.hasNext());
2008+
}
2009+
}
2010+
19862011
@Test(groups = {"integration"})
19872012
public void testReadingEnumsAsStrings() throws Exception {
19882013
final String tableName = "enums_as_strings_test_table";

0 commit comments

Comments
 (0)