Skip to content

Commit 3a04972

Browse files
committed
Merge branch 'main' into fix_compress_stream
2 parents e3710c9 + 50bfa00 commit 3a04972

File tree

6 files changed

+119
-15
lines changed

6 files changed

+119
-15
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
@@ -222,8 +222,11 @@ protected void setSchema(TableSchema schema) {
222222

223223
for (int i = 0; i < columns.length; i++) {
224224
ClickHouseColumn column = columns[i];
225-
226-
switch (column.getDataType()) {
225+
ClickHouseDataType columnDataType = column.getDataType();
226+
if (columnDataType.equals(ClickHouseDataType.SimpleAggregateFunction)){
227+
columnDataType = column.getNestedColumns().get(0).getDataType();
228+
}
229+
switch (columnDataType) {
227230
case Int8:
228231
case Int16:
229232
case UInt8:
@@ -378,7 +381,11 @@ public BigDecimal getBigDecimal(String colName) {
378381
public Instant getInstant(String colName) {
379382
int colIndex = schema.nameToIndex(colName);
380383
ClickHouseColumn column = schema.getColumns().get(colIndex);
381-
switch (column.getDataType()) {
384+
ClickHouseDataType columnDataType = column.getDataType();
385+
if (columnDataType.equals(ClickHouseDataType.SimpleAggregateFunction)){
386+
columnDataType = column.getNestedColumns().get(0).getDataType();
387+
}
388+
switch (columnDataType) {
382389
case Date:
383390
case Date32:
384391
LocalDate data = readValue(colName);
@@ -402,7 +409,11 @@ public Instant getInstant(String colName) {
402409
public ZonedDateTime getZonedDateTime(String colName) {
403410
int colIndex = schema.nameToIndex(colName);
404411
ClickHouseColumn column = schema.getColumns().get(colIndex);
405-
switch (column.getDataType()) {
412+
ClickHouseDataType columnDataType = column.getDataType();
413+
if (columnDataType.equals(ClickHouseDataType.SimpleAggregateFunction)){
414+
columnDataType = column.getNestedColumns().get(0).getDataType();
415+
}
416+
switch (columnDataType) {
406417
case DateTime:
407418
case DateTime64:
408419
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";

performance/src/test/com/clickhouse/benchmark/BenchmarkRunner.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public static void main(String[] args) throws Exception {
4545
.jvmArgs("-Xms8g", "-Xmx8g")
4646
.measurementTime(TimeValue.seconds(30))
4747
.resultFormat(ResultFormatType.JSON)
48-
.output(String.format("jmh-results-%s-%s.out", isCloud() ? "cloud" : "local", System.currentTimeMillis()))
48+
// .output(String.format("jmh-results-%s-%s.out", isCloud() ? "cloud" : "local", System.currentTimeMillis()))
4949
.result(String.format("jmh-results-%s-%s.json", isCloud() ? "cloud" : "local", System.currentTimeMillis()))
5050
.build();
5151

performance/src/test/com/clickhouse/benchmark/clients/InsertClient.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,13 +140,11 @@ public void insertV1RowBinary(DataState dataState) {
140140
.data(out -> {
141141
ClickHouseDataProcessor p = dataState.dataSet.getClickHouseDataProcessor();
142142
ClickHouseSerializer[] serializers = p.getSerializers(clientV1.getConfig(), p.getColumns());
143-
144143
for (ClickHouseRecord record : dataState.dataSet.getClickHouseRecords()) {
145144
for (int i = 0; i < serializers.length; i++) {
146145
serializers[i].serialize(record.getValue(i), out);
147146
}
148147
}
149-
150148
})
151149
.executeAndWait()) {
152150
response.getSummary();
@@ -161,7 +159,6 @@ public void insertV2RowBinary(DataState dataState) {
161159
try {
162160
try (InsertResponse response = clientV2.insert(dataState.tableNameEmpty, out -> {
163161
RowBinaryFormatWriter w = new RowBinaryFormatWriter(out, dataState.dataSet.getSchema(), ClickHouseFormat.RowBinary);
164-
List<ClickHouseColumn> columns = dataState.dataSet.getSchema().getColumns();
165162
for (List<Object> row : dataState.dataSet.getRowsOrdered()) {
166163
int index = 1;
167164
for (Object value : row) {
@@ -172,7 +169,7 @@ public void insertV2RowBinary(DataState dataState) {
172169
}
173170
out.flush();
174171

175-
}, ClickHouseFormat.RowBinaryWithDefaults, new InsertSettings().compressClientRequest(true)).get()) {
172+
}, ClickHouseFormat.RowBinaryWithDefaults, new InsertSettings()).get()) {
176173
response.getWrittenRows();
177174
}
178175
} catch (Exception e) {

0 commit comments

Comments
 (0)