diff --git a/client-v2/src/main/java/com/clickhouse/client/api/Client.java b/client-v2/src/main/java/com/clickhouse/client/api/Client.java index 608fb8071..884d8545a 100644 --- a/client-v2/src/main/java/com/clickhouse/client/api/Client.java +++ b/client-v2/src/main/java/com/clickhouse/client/api/Client.java @@ -154,6 +154,9 @@ public class Client implements AutoCloseable { private final ColumnToMethodMatchingStrategy columnToMethodMatchingStrategy; + // Server context + private String serverVersion; + private Client(Set endpoints, Map configuration, boolean useNewImplementation, ExecutorService sharedOperationExecutor, ColumnToMethodMatchingStrategy columnToMethodMatchingStrategy) { this.endpoints = endpoints; @@ -179,8 +182,27 @@ private Client(Set endpoints, Map configuration, boolean LOG.info("Using old http client implementation"); } this.columnToMethodMatchingStrategy = columnToMethodMatchingStrategy; + + updateServerContext(); + } + + private void updateServerContext() { + try (QueryResponse response = this.query("SELECT currentUser() AS user, timezone() AS timezone, version() AS version LIMIT 1").get()) { + try (ClickHouseBinaryFormatReader reader = this.newBinaryFormatReader(response)) { + if (reader.next() != null) { + this.configuration.put(ClientConfigProperties.USER.getKey(), reader.getString("user")); + this.configuration.put(ClientConfigProperties.SERVER_TIMEZONE.getKey(), reader.getString("timezone")); + serverVersion = reader.getString("version"); + } + } + } catch (Exception e) { + LOG.error("Failed to get server info", e); + } } + + + /** * Returns default database name that will be used by operations if not specified. * @@ -216,6 +238,7 @@ public void close() { } } + public static class Builder { private Set endpoints; @@ -1015,6 +1038,7 @@ public Client build() { return new Client(this.endpoints, this.configuration, this.useNewImplementation, this.sharedOperationExecutor, this.columnToMethodMatchingStrategy); } + private static final int DEFAULT_NETWORK_BUFFER_SIZE = 300_000; private void setDefaults() { @@ -2124,6 +2148,10 @@ public String getUser() { return this.configuration.get(ClientConfigProperties.USER.getKey()); } + public String getServerVersion() { + return this.serverVersion; + } + /** * Sets list of DB roles that should be applied to each query. * diff --git a/client-v2/src/test/java/com/clickhouse/client/query/QueryTests.java b/client-v2/src/test/java/com/clickhouse/client/query/QueryTests.java index d77056483..fc646891b 100644 --- a/client-v2/src/test/java/com/clickhouse/client/query/QueryTests.java +++ b/client-v2/src/test/java/com/clickhouse/client/query/QueryTests.java @@ -1957,4 +1957,20 @@ public void testReadingSimpleAggregateFunction() throws Exception { Assert.assertFalse(reader.hasNext()); } } + + + @Test(groups = {"integration"}) + public void testServerTimezone() throws Exception { + final String sql = "SELECT now() as t, toDateTime(now(), 'UTC') as utc_time, toDateTime(now(), 'America/New_York') as est_time"; + try (QueryResponse response = client.query(sql).get(1, TimeUnit.SECONDS)) { + ClickHouseBinaryFormatReader reader = client.newBinaryFormatReader(response); + Assert.assertNotNull(reader.next()); + ZonedDateTime serverTime = reader.getZonedDateTime(1); + ZonedDateTime serverUtcTime = reader.getZonedDateTime(2); + ZonedDateTime serverEstTime = reader.getZonedDateTime(3); + Assert.assertEquals(serverTime.withZoneSameInstant(ZoneId.of("UTC")), serverUtcTime); + Assert.assertEquals(serverTime, serverUtcTime); + Assert.assertEquals(serverUtcTime.withZoneSameInstant(ZoneId.of("America/New_York")), serverEstTime); + } + } }