Skip to content

Commit ad5694e

Browse files
committed
Merge branch 'main' into perf_deserializer_tests
2 parents 2e4f509 + 50bfa00 commit ad5694e

File tree

4 files changed

+117
-10
lines changed

4 files changed

+117
-10
lines changed

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1916,6 +1916,45 @@ public CompletableFuture<CommandResponse> execute(String sql, CommandSettings se
19161916
});
19171917
}
19181918

1919+
/**
1920+
* <p>Executes a SQL command and doesn't care response. Useful for DDL statements, like `CREATE`, `DROP`, `ALTER`.
1921+
* Method however returns execution errors from a server or summary in case of successful execution. </p>
1922+
*
1923+
* @param sql - SQL command
1924+
* @param params - query parameters
1925+
* @return {@code CompletableFuture<CommandResponse>} - a promise to command response
1926+
*/
1927+
public CompletableFuture<CommandResponse> execute(String sql, Map<String, Object> params){
1928+
return query(sql, params)
1929+
.thenApplyAsync(response -> {
1930+
try {
1931+
return new CommandResponse(response);
1932+
} catch (Exception e) {
1933+
throw new ClientException("Failed to get command response", e);
1934+
}
1935+
});
1936+
}
1937+
1938+
/**
1939+
* <p>Executes a SQL command and doesn't care response. Useful for DDL statements, like `CREATE`, `DROP`, `ALTER`.
1940+
* Method however returns execution errors from a server or summary in case of successful execution. </p>
1941+
*
1942+
* @param sql - SQL command
1943+
* @param params - query parameters
1944+
* @param settings - execution settings
1945+
* @return {@code CompletableFuture<CommandResponse>} - a promise to command response
1946+
*/
1947+
public CompletableFuture<CommandResponse> execute(String sql, Map<String, Object> params, CommandSettings settings){
1948+
return query(sql, params, settings)
1949+
.thenApplyAsync(response -> {
1950+
try {
1951+
return new CommandResponse(response);
1952+
} catch (Exception e) {
1953+
throw new ClientException("Failed to get command response", e);
1954+
}
1955+
});
1956+
}
1957+
19191958
/**
19201959
* <p>Executes a SQL command and doesn't care response. Useful for DDL statements, like `CREATE`, `DROP`, `ALTER`.
19211960
* Method however returns execution errors from a server or summary in case of successful execution. </p>

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,11 @@ protected void setSchema(TableSchema schema) {
228228

229229
for (int i = 0; i < columns.length; i++) {
230230
ClickHouseColumn column = columns[i];
231-
232-
switch (column.getDataType()) {
231+
ClickHouseDataType columnDataType = column.getDataType();
232+
if (columnDataType.equals(ClickHouseDataType.SimpleAggregateFunction)){
233+
columnDataType = column.getNestedColumns().get(0).getDataType();
234+
}
235+
switch (columnDataType) {
233236
case Int8:
234237
case Int16:
235238
case UInt8:
@@ -384,7 +387,11 @@ public BigDecimal getBigDecimal(String colName) {
384387
public Instant getInstant(String colName) {
385388
int colIndex = schema.nameToIndex(colName);
386389
ClickHouseColumn column = schema.getColumns().get(colIndex);
387-
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) {
388395
case Date:
389396
case Date32:
390397
LocalDate data = readValue(colName);
@@ -408,7 +415,11 @@ public Instant getInstant(String colName) {
408415
public ZonedDateTime getZonedDateTime(String colName) {
409416
int colIndex = schema.nameToIndex(colName);
410417
ClickHouseColumn column = schema.getColumns().get(colIndex);
411-
switch (column.getDataType()) {
418+
ClickHouseDataType columnDataType = column.getDataType();
419+
if (columnDataType.equals(ClickHouseDataType.SimpleAggregateFunction)){
420+
columnDataType = column.getNestedColumns().get(0).getDataType();
421+
}
422+
switch (columnDataType) {
412423
case DateTime:
413424
case DateTime64:
414425
case Date:

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,11 @@ public float floatValue() {
728728
public double doubleValue() {
729729
return value;
730730
}
731+
732+
@Override
733+
public String toString() {
734+
return name;
735+
}
731736
}
732737

733738
/**

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

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,7 @@
7474
import java.util.Random;
7575
import java.util.Set;
7676
import java.util.UUID;
77-
import java.util.concurrent.CountDownLatch;
78-
import java.util.concurrent.ExecutionException;
79-
import java.util.concurrent.ExecutorService;
80-
import java.util.concurrent.Executors;
81-
import java.util.concurrent.Future;
82-
import java.util.concurrent.TimeUnit;
77+
import java.util.concurrent.*;
8378
import java.util.concurrent.atomic.AtomicInteger;
8479
import java.util.function.Consumer;
8580
import java.util.function.Function;
@@ -1562,6 +1557,38 @@ public void testQueryParams() throws Exception {
15621557
Assert.assertEquals(allRecords.size(), 2);
15631558
}
15641559

1560+
@Test(groups = {"integration"})
1561+
public void testExecuteQueryParam() throws ExecutionException, InterruptedException, TimeoutException {
1562+
1563+
final String table = "execute_query_test";
1564+
Map<String, Object> query_param = new HashMap<>();
1565+
query_param.put("table_name",table);
1566+
query_param.put("engine","MergeTree");
1567+
client.execute("DROP TABLE IF EXISTS " + table).get(10, TimeUnit.SECONDS);
1568+
client.execute("CREATE TABLE {table_name:Identifier} ( id UInt32, name String, created_at DateTime) ENGINE = MergeTree ORDER BY tuple()", query_param)
1569+
.get(10, TimeUnit.SECONDS);
1570+
1571+
TableSchema schema = client.getTableSchema(table);
1572+
Assert.assertNotNull(schema);
1573+
}
1574+
1575+
@Test(groups = {"integration"})
1576+
public void testExecuteQueryParamCommandSettings() throws ExecutionException, InterruptedException, TimeoutException {
1577+
1578+
final String table = "execute_query_test";
1579+
String q1Id = UUID.randomUUID().toString();
1580+
Map<String, Object> query_param = new HashMap<>();
1581+
query_param.put("table_name",table);
1582+
query_param.put("engine","MergeTree");
1583+
client.execute("DROP TABLE IF EXISTS " + table).get(10, TimeUnit.SECONDS);
1584+
client.execute("CREATE TABLE {table_name:Identifier} ( id UInt32, name String, created_at DateTime) ENGINE = MergeTree ORDER BY tuple()",
1585+
query_param, (CommandSettings) new CommandSettings().setQueryId(q1Id))
1586+
.get(10, TimeUnit.SECONDS);
1587+
1588+
TableSchema schema = client.getTableSchema(table);
1589+
Assert.assertNotNull(schema);
1590+
}
1591+
15651592
@Test(groups = {"integration"})
15661593
public void testGetTableSchema() throws Exception {
15671594

@@ -1985,6 +2012,31 @@ public void testReadingSimpleAggregateFunction() throws Exception {
19852012
}
19862013
}
19872014

2015+
@Test(groups = {"integration"})
2016+
public void testReadingSimpleAggregateFunction2() throws Exception {
2017+
final String tableName = "simple_aggregate_function_test_table";
2018+
client.execute("DROP TABLE IF EXISTS " + tableName).get();
2019+
client.execute("CREATE TABLE `" + tableName + "` " +
2020+
"(idx UInt8, lowest_value SimpleAggregateFunction(min, UInt8), count SimpleAggregateFunction(sum, Int64), date SimpleAggregateFunction(anyLast, DateTime32)) " +
2021+
"ENGINE Memory;").get();
2022+
2023+
2024+
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)) {
2025+
Assert.assertEquals(response.getWrittenRows(), 1);
2026+
}
2027+
2028+
try (QueryResponse queryResponse = client.query("SELECT * FROM " + tableName + " LIMIT 1").get(30, TimeUnit.SECONDS)) {
2029+
2030+
ClickHouseBinaryFormatReader reader = client.newBinaryFormatReader(queryResponse);
2031+
Assert.assertNotNull(reader.next());
2032+
Assert.assertEquals(reader.getByte("idx"), Byte.valueOf("1"));
2033+
Assert.assertEquals((Short) reader.getShort("lowest_value"), Short.parseShort("2"));
2034+
Assert.assertEquals((Long) reader.getLong("count"), Long.parseLong("3"));
2035+
Assert.assertEquals(reader.getLocalDateTime("date"), LocalDateTime.of(2024,12,22,12,00,00));
2036+
Assert.assertFalse(reader.hasNext());
2037+
}
2038+
}
2039+
19882040
@Test(groups = {"integration"})
19892041
public void testReadingEnumsAsStrings() throws Exception {
19902042
final String tableName = "enums_as_strings_test_table";

0 commit comments

Comments
 (0)