Skip to content

Commit 4facc05

Browse files
committed
Merge branch 'main' into v2_fix_aggregate_function_serde
2 parents daedbda + 65bc28c commit 4facc05

File tree

30 files changed

+873
-223
lines changed

30 files changed

+873
-223
lines changed

README.md

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ Nightly Builds: https://s01.oss.sonatype.org/content/repositories/snapshots/com/
6868

6969
### Artifacts
7070

71-
| Component | Maven Central Link |
72-
|---------------------------|--------------------|
73-
| ClickHouse Java Client V2 | [![Maven Central](https://img.shields.io/maven-central/v/com.clickhouse/client-v2)](https://mvnrepository.com/artifact/com.clickhouse/client-v2) |
71+
| Component | Maven Central Link | Javadoc Link |
72+
|---------------------------|--------------------|--------------|
73+
| ClickHouse Java Client V2 | [![Maven Central](https://img.shields.io/maven-central/v/com.clickhouse/client-v2)](https://mvnrepository.com/artifact/com.clickhouse/client-v2) | [![javadoc](https://javadoc.io/badge2/com.clickhouse/client-v2/javadoc.svg)](https://javadoc.io/doc/com.clickhouse/client-v2) |
7474

7575
### Compatibility
7676

@@ -194,10 +194,13 @@ Client client = new Client.Builder()
194194

195195
### Artifacts
196196

197-
| Component | Maven Central Link |
198-
|-----------|--------------------|
199-
| ClickHouse Java HTTP Client | [![Maven Central](https://img.shields.io/maven-central/v/com.clickhouse/clickhouse-client)](https://mvnrepository.com/artifact/com.clickhouse/clickhouse-http-client) |
200-
| ClickHouse JDBC Driver | [![Maven Central](https://img.shields.io/maven-central/v/com.clickhouse/clickhouse-jdbc)](https://mvnrepository.com/artifact/com.clickhouse/clickhouse-jdbc) |
197+
| Component | Maven Central Link | Javadoc Link |
198+
|-----------|--------------------|--------------|
199+
| ClickHouse Java Unified Client | [![Maven Central](https://img.shields.io/maven-central/v/com.clickhouse/clickhouse-client)](https://mvnrepository.com/artifact/com.clickhouse/clickhouse-client) | [![javadoc](https://javadoc.io/badge2/com.clickhouse/clickhouse-client/javadoc.svg)](https://javadoc.io/doc/com.clickhouse/clickhouse-client) |
200+
| ClickHouse Java HTTP Client | [![Maven Central](https://img.shields.io/maven-central/v/com.clickhouse/clickhouse-http-client)](https://mvnrepository.com/artifact/com.clickhouse/clickhouse-http-client) | [![javadoc](https://javadoc.io/badge2/com.clickhouse/clickhouse-http-client/javadoc.svg)](https://javadoc.io/doc/com.clickhouse/clickhouse-http-client) |
201+
| ClickHouse JDBC Driver | [![Maven Central](https://img.shields.io/maven-central/v/com.clickhouse/clickhouse-jdbc)](https://mvnrepository.com/artifact/com.clickhouse/clickhouse-jdbc) | [![javadoc](https://javadoc.io/badge2/com.clickhouse/clickhouse-jdbc/javadoc.svg)](https://javadoc.io/doc/com.clickhouse/clickhouse-jdbc) |
202+
| ClickHouse R2DBC Driver | [![Maven Central](https://img.shields.io/maven-central/v/com.clickhouse/clickhouse-r2dbc)](https://mvnrepository.com/artifact/com.clickhouse/clickhouse-r2dbc) | [![javadoc](https://javadoc.io/badge2/com.clickhouse/clickhouse-r2dbc/javadoc.svg)](https://javadoc.io/doc/com.clickhouse/clickhouse-r2dbc) |
203+
| ClickHouse gRPC Driver | [![Maven Central](https://img.shields.io/maven-central/v/com.clickhouse/clickhouse-grpc-client)](https://mvnrepository.com/artifact/com.clickhouse/clickhouse-grpc-client) | [![javadoc](https://javadoc.io/badge2/com.clickhouse/clickhouse-jdbc/javadoc.svg)](https://javadoc.io/doc/com.clickhouse/clickhouse-grpc-client) | [![javadoc](https://javadoc.io/badge2/com.clickhouse/clickhouse-grpc-client/javadoc.svg)](https://javadoc.io/doc/com.clickhouse/clickhouse-grpc-client) |
201204

202205

203206
### Features

clickhouse-client/src/main/java/com/clickhouse/client/ClickHouseClient.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
import com.clickhouse.data.ClickHouseValue;
4141
import com.clickhouse.data.ClickHouseValues;
4242
import com.clickhouse.data.ClickHouseWriter;
43+
import com.clickhouse.logging.Logger;
44+
import com.clickhouse.logging.LoggerFactory;
4345

4446
/**
4547
* A unified interface defines Java client for ClickHouse. A client can only
@@ -56,6 +58,7 @@
5658
* implementation properly in runtime.
5759
*/
5860
public interface ClickHouseClient extends AutoCloseable {
61+
Logger LOG = LoggerFactory.getLogger(ClickHouseClient.class);
5962

6063
/**
6164
* Returns a builder for creating a new client.
@@ -959,8 +962,8 @@ default boolean ping(ClickHouseNode server, int timeout) {
959962
.get(timeout, TimeUnit.MILLISECONDS)) {
960963
return resp != null;
961964
} catch (Exception e) {
962-
// ignore
963-
e.printStackTrace();
965+
LOG.debug("Failed to connect to the server", e);
966+
return false;
964967
}
965968
}
966969

clickhouse-http-client/pom.xml

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,14 @@
4040
<dependency>
4141
<groupId>org.apache.httpcomponents.client5</groupId>
4242
<artifactId>httpclient5</artifactId>
43-
<exclusions>
44-
<exclusion>
45-
<groupId>org.apache.httpcomponents.core5</groupId>
46-
<artifactId>httpcore5</artifactId>
47-
</exclusion>
48-
</exclusions>
49-
<optional>true</optional>
43+
<version>${apache.httpclient.version}</version>
5044
</dependency>
5145
<dependency>
5246
<groupId>org.apache.httpcomponents.core5</groupId>
5347
<artifactId>httpcore5</artifactId>
54-
<optional>true</optional>
48+
<version>${apache.httpclient.version}</version>
5549
</dependency>
50+
5651
<dependency>
5752
<groupId>org.brotli</groupId>
5853
<artifactId>dec</artifactId>

clickhouse-http-client/src/main/java/com/clickhouse/client/http/ApacheHttpConnectionImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,11 +194,11 @@ private void setHeaders(HttpRequest request, Map<String, String> headers) {
194194
}
195195

196196
private void checkResponse(ClickHouseConfig config, CloseableHttpResponse response) throws IOException {
197-
if (response.getCode() == HttpURLConnection.HTTP_OK) {
197+
final Header errorCode = response.getFirstHeader(ClickHouseHttpProto.HEADER_EXCEPTION_CODE);
198+
if (response.getCode() == HttpURLConnection.HTTP_OK && errorCode == null) {
198199
return;
199200
}
200201

201-
final Header errorCode = response.getFirstHeader(ClickHouseHttpProto.HEADER_EXCEPTION_CODE);
202202
final Header serverName = response.getFirstHeader(ClickHouseHttpProto.HEADER_SRV_DISPLAY_NAME);
203203
if (response.getEntity() == null) {
204204
throw new ConnectException(

clickhouse-http-client/src/main/java/com/clickhouse/client/http/ClickHouseHttpProto.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,5 @@ public class ClickHouseHttpProto {
5353
*/
5454
public static final String QPARAM_QUERY_ID = "query_id";
5555

56+
public static final String QPARAM_ROLE = "role";
5657
}

clickhouse-jdbc/pom.xml

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -47,26 +47,7 @@
4747
<artifactId>commons-compress</artifactId>
4848
<optional>true</optional>
4949
</dependency>
50-
<dependency>
51-
<groupId>org.apache.httpcomponents.client5</groupId>
52-
<artifactId>httpclient5</artifactId>
53-
<exclusions>
54-
<exclusion>
55-
<groupId>org.apache.httpcomponents.core5</groupId>
56-
<artifactId>httpcore5</artifactId>
57-
</exclusion>
58-
<exclusion>
59-
<groupId>org.slf4j</groupId>
60-
<artifactId>slf4j-api</artifactId>
61-
</exclusion>
62-
</exclusions>
63-
<optional>true</optional>
64-
</dependency>
65-
<dependency>
66-
<groupId>org.apache.httpcomponents.core5</groupId>
67-
<artifactId>httpcore5</artifactId>
68-
<optional>true</optional>
69-
</dependency>
50+
7051
<dependency>
7152
<groupId>org.lz4</groupId>
7253
<artifactId>lz4-pure-java</artifactId>
@@ -145,7 +126,7 @@
145126
<dependency>
146127
<groupId>com.clickhouse</groupId>
147128
<artifactId>clickhouse-http-client</artifactId>
148-
<version>0.7.0-SNAPSHOT</version>
129+
<version>${revision}</version>
149130
<scope>compile</scope>
150131
</dependency>
151132
</dependencies>

client-v2/pom.xml

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
<description>New client api for ClickHouse</description>
1717
<url>https://github.com/ClickHouse/clickhouse-java/tree/main/clickhouse-client-api</url>
1818

19+
<properties>
20+
<apache.httpclient.version>5.3.1</apache.httpclient.version>
21+
</properties>
22+
1923
<dependencies>
2024
<dependency>
2125
<groupId>${project.parent.groupId}</groupId>
@@ -26,6 +30,7 @@
2630
<groupId>${project.parent.groupId}</groupId>
2731
<artifactId>clickhouse-http-client</artifactId>
2832
<version>${revision}</version>
33+
<optional>true</optional>
2934
</dependency>
3035
<dependency>
3136
<groupId>org.slf4j</groupId>
@@ -35,7 +40,7 @@
3540
<dependency>
3641
<groupId>org.apache.httpcomponents.client5</groupId>
3742
<artifactId>httpclient5</artifactId>
38-
<version>5.3.1</version>
43+
<version>${apache.httpclient.version}</version>
3944
</dependency>
4045
<dependency>
4146
<groupId>com.github.luben</groupId>
@@ -45,20 +50,23 @@
4550
<dependency>
4651
<groupId>org.lz4</groupId>
4752
<artifactId>lz4-pure-java</artifactId>
48-
<optional>true</optional>
53+
<version>${lz4.version}</version>
54+
</dependency>
55+
<dependency>
56+
<groupId>org.apache.commons</groupId>
57+
<artifactId>commons-compress</artifactId>
58+
<version>${compress.version}</version>
4959
</dependency>
5060
<dependency>
51-
<groupId>${project.groupId}</groupId>
52-
<artifactId>org.apache.commons.compress</artifactId>
53-
<version>${repackaged.version}</version>
61+
<groupId>org.ow2.asm</groupId>
62+
<artifactId>asm</artifactId>
63+
<version>9.7</version>
5464
</dependency>
55-
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
5665
<dependency>
5766
<groupId>com.fasterxml.jackson.core</groupId>
5867
<artifactId>jackson-core</artifactId>
5968
<version>2.17.2</version>
6069
</dependency>
61-
6270
<!-- Test Dependencies -->
6371
<dependency>
6472
<groupId>com.google.code.gson</groupId>
@@ -73,26 +81,13 @@
7381
<scope>test</scope>
7482
<version>2.17.2</version>
7583
</dependency>
76-
<dependency>
77-
<groupId>org.apache.commons</groupId>
78-
<artifactId>commons-compress</artifactId>
79-
<optional>true</optional>
80-
</dependency>
8184
<!-- necessary for Java 9+ -->
8285
<dependency>
8386
<groupId>org.apache.tomcat</groupId>
8487
<artifactId>annotations-api</artifactId>
8588
<scope>provided</scope>
8689
</dependency>
8790

88-
<!-- https://mvnrepository.com/artifact/org.ow2.asm/asm -->
89-
<dependency>
90-
<groupId>org.ow2.asm</groupId>
91-
<artifactId>asm</artifactId>
92-
<version>9.7</version>
93-
</dependency>
94-
95-
9691
<!-- Test dependencies -->
9792
<dependency>
9893
<groupId>${project.parent.groupId}</groupId>

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

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.clickhouse.client.api;
22

33
import com.clickhouse.client.ClickHouseClient;
4+
import com.clickhouse.client.ClickHouseException;
45
import com.clickhouse.client.ClickHouseNode;
56
import com.clickhouse.client.ClickHouseRequest;
67
import com.clickhouse.client.ClickHouseResponse;
@@ -79,6 +80,7 @@
7980
import java.util.TimeZone;
8081
import java.util.UUID;
8182
import java.util.concurrent.CompletableFuture;
83+
import java.util.concurrent.CompletionException;
8284
import java.util.concurrent.ConcurrentHashMap;
8385
import java.util.concurrent.ExecutionException;
8486
import java.util.concurrent.ExecutorService;
@@ -1034,6 +1036,7 @@ public boolean ping(long timeout) {
10341036
try (QueryResponse response = query("SELECT 1 FORMAT TabSeparated").get(timeout, TimeUnit.MILLISECONDS)) {
10351037
return true;
10361038
} catch (Exception e) {
1039+
LOG.debug("Failed to connect to the server", e);
10371040
return false;
10381041
}
10391042
} else {
@@ -1454,6 +1457,11 @@ public CompletableFuture<InsertResponse> insert(String tableName,
14541457
return response;
14551458
} catch (ExecutionException e) {
14561459
throw new ClientException("Failed to get insert response", e.getCause());
1460+
} catch (CompletionException e) {
1461+
if (e.getCause() instanceof ClickHouseException) {
1462+
throw new ServerException(((ClickHouseException)e.getCause()).getErrorCode(), e.getCause().getMessage().trim());
1463+
}
1464+
throw new ClientException("Failed to get query response", e.getCause());
14571465
} catch (InterruptedException | TimeoutException e) {
14581466
throw new ClientException("Operation has likely timed out.", e);
14591467
}
@@ -1574,7 +1582,7 @@ public CompletableFuture<QueryResponse> query(String sqlQuery, Map<String, Objec
15741582
} else {
15751583
throw lastException;
15761584
}
1577-
} catch (ClientException e) {
1585+
} catch (ClientException | ServerException e) {
15781586
throw e;
15791587
} catch (Exception e) {
15801588
throw new ClientException("Query request failed", e);
@@ -1608,6 +1616,11 @@ public CompletableFuture<QueryResponse> query(String sqlQuery, Map<String, Objec
16081616
return new QueryResponse(clickHouseResponse, format, clientStats, finalSettings);
16091617
} catch (ClientException e) {
16101618
throw e;
1619+
} catch (CompletionException e) {
1620+
if (e.getCause() instanceof ClickHouseException) {
1621+
throw new ServerException(((ClickHouseException)e.getCause()).getErrorCode(), e.getCause().getMessage().trim());
1622+
}
1623+
throw new ClientException("Failed to get query response", e.getCause());
16111624
} catch (Exception e) {
16121625
throw new ClientException("Failed to get query response", e);
16131626
}
@@ -1662,10 +1675,10 @@ public CompletableFuture<Records> queryRecords(String sqlQuery, QuerySettings se
16621675
* @param sqlQuery - SQL query
16631676
* @return - complete list of records
16641677
*/
1665-
public List<GenericRecord> queryAll(String sqlQuery) {
1678+
public List<GenericRecord> queryAll(String sqlQuery, QuerySettings settings) {
16661679
try {
16671680
int operationTimeout = getOperationTimeout();
1668-
QuerySettings settings = new QuerySettings().setFormat(ClickHouseFormat.RowBinaryWithNamesAndTypes)
1681+
settings.setFormat(ClickHouseFormat.RowBinaryWithNamesAndTypes)
16691682
.waitEndOfQuery(true);
16701683
try (QueryResponse response = operationTimeout == 0 ? query(sqlQuery, settings).get() :
16711684
query(sqlQuery, settings).get(operationTimeout, TimeUnit.MILLISECONDS)) {
@@ -1688,6 +1701,10 @@ public List<GenericRecord> queryAll(String sqlQuery) {
16881701
}
16891702
}
16901703

1704+
public List<GenericRecord> queryAll(String sqlQuery) {
1705+
return queryAll(sqlQuery, new QuerySettings());
1706+
}
1707+
16911708
public <T> List<T> queryAll(String sqlQuery, Class<T> clazz, TableSchema schema) {
16921709
return queryAll(sqlQuery, clazz, schema, null);
16931710
}
@@ -1794,6 +1811,8 @@ private TableSchema getTableSchemaImpl(String describeQuery, String name, String
17941811
throw new ClientException("Operation has likely timed out after " + getOperationTimeout() + " seconds.", e);
17951812
} catch (ExecutionException e) {
17961813
throw new ClientException("Failed to get table schema", e.getCause());
1814+
} catch (ServerException e) {
1815+
throw e;
17971816
} catch (Exception e) {
17981817
throw new ClientException("Failed to get table schema", e);
17991818
}
@@ -1936,6 +1955,28 @@ public Set<String> getEndpoints() {
19361955
return Collections.unmodifiableSet(endpoints);
19371956
}
19381957

1958+
/**
1959+
* Sets list of DB roles that should be applied to each query.
1960+
*
1961+
* @param dbRoles
1962+
*/
1963+
public void setDBRoles(Collection<String> dbRoles) {
1964+
this.configuration.put(ClientSettings.SESSION_DB_ROLES, ClientSettings.commaSeparated(dbRoles));
1965+
this.unmodifiableDbRolesView =
1966+
Collections.unmodifiableCollection(ClientSettings.valuesFromCommaSeparated(
1967+
this.configuration.get(ClientSettings.SESSION_DB_ROLES)));
1968+
}
1969+
1970+
private Collection<String> unmodifiableDbRolesView = Collections.emptyList();
1971+
1972+
/**
1973+
* Returns list of DB roles that should be applied to each query.
1974+
*
1975+
* @return List of DB roles
1976+
*/
1977+
public Collection<String> getDBRoles() {
1978+
return unmodifiableDbRolesView;
1979+
}
19391980

19401981
private ClickHouseNode getNextAliveNode() {
19411982
return serverNodes.get(0);

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

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

33
import java.util.Arrays;
44
import java.util.Collection;
5+
import java.util.Collections;
56
import java.util.List;
67
import java.util.stream.Collectors;
78

@@ -18,14 +19,22 @@ public class ClientSettings {
1819
public static String commaSeparated(Collection<?> values) {
1920
StringBuilder sb = new StringBuilder();
2021
for (Object value : values) {
21-
sb.append(value.toString().replaceAll(",", "\\,")).append(",");
22+
sb.append(value.toString().replaceAll(",", "\\\\,")).append(",");
2223
}
2324
sb.setLength(sb.length() - 1);
2425
return sb.toString();
2526
}
2627

2728
public static List<String> valuesFromCommaSeparated(String value) {
28-
return Arrays.stream(value.split(",")).map(s -> s.replaceAll("\\\\,", ","))
29+
if (value == null || value.isEmpty()) {
30+
return Collections.emptyList();
31+
}
32+
33+
return Arrays.stream(value.split("(?<!\\\\),")).map(s -> s.replaceAll("\\\\,", ","))
2934
.collect(Collectors.toList());
3035
}
36+
37+
public static final String SESSION_DB_ROLES = "session_db_roles";
38+
39+
public static final String SETTING_LOG_COMMENT = SERVER_SETTING_PREFIX + "log_comment";
3140
}

0 commit comments

Comments
 (0)