Skip to content

Commit 0ba5aed

Browse files
author
Paultagoras
committed
Server context code + test
1 parent 20babb5 commit 0ba5aed

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,10 @@ public class Client implements AutoCloseable {
154154

155155
private final ColumnToMethodMatchingStrategy columnToMethodMatchingStrategy;
156156

157+
// Server context
158+
private String serverUser;
159+
private String serverVersion;
160+
157161
private Client(Set<String> endpoints, Map<String,String> configuration, boolean useNewImplementation,
158162
ExecutorService sharedOperationExecutor, ColumnToMethodMatchingStrategy columnToMethodMatchingStrategy) {
159163
this.endpoints = endpoints;
@@ -179,8 +183,25 @@ private Client(Set<String> endpoints, Map<String,String> configuration, boolean
179183
LOG.info("Using old http client implementation");
180184
}
181185
this.columnToMethodMatchingStrategy = columnToMethodMatchingStrategy;
186+
187+
updateServerContext();
182188
}
183189

190+
private void updateServerContext() {
191+
try (QueryResponse response = this.query("SELECT currentUser() AS user, timezone() AS timezone, version() AS version LIMIT 1").get()) {
192+
try (ClickHouseBinaryFormatReader reader = this.newBinaryFormatReader(response)) {
193+
if (reader.next() != null) {
194+
serverUser = reader.getString("user");
195+
this.configuration.put(ClientConfigProperties.SERVER_TIMEZONE.getKey(), reader.getString("timezone"));
196+
serverVersion = reader.getString("version");
197+
}
198+
}
199+
} catch (Exception e) {
200+
LOG.error("Failed to get server info", e);
201+
}
202+
}
203+
204+
184205
/**
185206
* Returns default database name that will be used by operations if not specified.
186207
*
@@ -216,6 +237,7 @@ public void close() {
216237
}
217238
}
218239

240+
219241
public static class Builder {
220242
private Set<String> endpoints;
221243

@@ -1015,6 +1037,7 @@ public Client build() {
10151037
return new Client(this.endpoints, this.configuration, this.useNewImplementation, this.sharedOperationExecutor, this.columnToMethodMatchingStrategy);
10161038
}
10171039

1040+
10181041
private static final int DEFAULT_NETWORK_BUFFER_SIZE = 300_000;
10191042

10201043
private void setDefaults() {

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)