Skip to content

Commit 1186ae3

Browse files
authored
Merge pull request #2029 from ClickHouse/add-server-context
Initial attempt at adding some server context to the client
2 parents d458d60 + 5faebc4 commit 1186ae3

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,9 @@ public class Client implements AutoCloseable {
156156

157157
private final ColumnToMethodMatchingStrategy columnToMethodMatchingStrategy;
158158

159+
// Server context
160+
private String serverVersion;
161+
159162
private Client(Set<String> endpoints, Map<String,String> configuration, boolean useNewImplementation,
160163
ExecutorService sharedOperationExecutor, ColumnToMethodMatchingStrategy columnToMethodMatchingStrategy) {
161164
this.endpoints = endpoints;
@@ -182,8 +185,27 @@ private Client(Set<String> endpoints, Map<String,String> configuration, boolean
182185
LOG.info("Using old http client implementation");
183186
}
184187
this.columnToMethodMatchingStrategy = columnToMethodMatchingStrategy;
188+
189+
updateServerContext();
190+
}
191+
192+
private void updateServerContext() {
193+
try (QueryResponse response = this.query("SELECT currentUser() AS user, timezone() AS timezone, version() AS version LIMIT 1").get()) {
194+
try (ClickHouseBinaryFormatReader reader = this.newBinaryFormatReader(response)) {
195+
if (reader.next() != null) {
196+
this.configuration.put(ClientConfigProperties.USER.getKey(), reader.getString("user"));
197+
this.configuration.put(ClientConfigProperties.SERVER_TIMEZONE.getKey(), reader.getString("timezone"));
198+
serverVersion = reader.getString("version");
199+
}
200+
}
201+
} catch (Exception e) {
202+
LOG.error("Failed to get server info", e);
203+
}
185204
}
186205

206+
207+
208+
187209
/**
188210
* Returns default database name that will be used by operations if not specified.
189211
*
@@ -219,6 +241,7 @@ public void close() {
219241
}
220242
}
221243

244+
222245
public static class Builder {
223246
private Set<String> endpoints;
224247

@@ -1033,6 +1056,7 @@ public Client build() {
10331056
this.columnToMethodMatchingStrategy);
10341057
}
10351058

1059+
10361060
private static final int DEFAULT_NETWORK_BUFFER_SIZE = 300_000;
10371061

10381062
private void setDefaults() {
@@ -2142,6 +2166,10 @@ public String getUser() {
21422166
return this.configuration.get(ClientConfigProperties.USER.getKey());
21432167
}
21442168

2169+
public String getServerVersion() {
2170+
return this.serverVersion;
2171+
}
2172+
21452173
/**
21462174
* Sets list of DB roles that should be applied to each query.
21472175
*

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1957,4 +1957,20 @@ public void testReadingSimpleAggregateFunction() throws Exception {
19571957
Assert.assertFalse(reader.hasNext());
19581958
}
19591959
}
1960+
1961+
1962+
@Test(groups = {"integration"})
1963+
public void testServerTimezone() throws Exception {
1964+
final String sql = "SELECT now() as t, toDateTime(now(), 'UTC') as utc_time, toDateTime(now(), 'America/New_York') as est_time";
1965+
try (QueryResponse response = client.query(sql).get(1, TimeUnit.SECONDS)) {
1966+
ClickHouseBinaryFormatReader reader = client.newBinaryFormatReader(response);
1967+
Assert.assertNotNull(reader.next());
1968+
ZonedDateTime serverTime = reader.getZonedDateTime(1);
1969+
ZonedDateTime serverUtcTime = reader.getZonedDateTime(2);
1970+
ZonedDateTime serverEstTime = reader.getZonedDateTime(3);
1971+
Assert.assertEquals(serverTime.withZoneSameInstant(ZoneId.of("UTC")), serverUtcTime);
1972+
Assert.assertEquals(serverTime, serverUtcTime);
1973+
Assert.assertEquals(serverUtcTime.withZoneSameInstant(ZoneId.of("America/New_York")), serverEstTime);
1974+
}
1975+
}
19601976
}

0 commit comments

Comments
 (0)