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 8c6e58803..06f7935c6 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 @@ -192,16 +192,24 @@ private Client(Set endpoints, Map configuration, boolean * */ public void loadServerInfo() { - 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"); + // only if 2 properties are set disable retrieval from server + if (!this.configuration.containsKey(ClientConfigProperties.SERVER_TIMEZONE.getKey()) && !this.configuration.containsKey(ClientConfigProperties.SERVER_VERSION.getKey())) { + 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) { + throw new ClientException("Failed to get server info", e); + } + } else { + LOG.info("Using server version " + this.configuration.get(ClientConfigProperties.SERVER_VERSION.getKey()) + " and timezone " + this.configuration.get(ClientConfigProperties.SERVER_TIMEZONE.getKey()) ); + if (this.configuration.containsKey(ClientConfigProperties.SERVER_VERSION.getKey())) { + serverVersion = this.configuration.get(ClientConfigProperties.SERVER_VERSION.getKey()); } - } catch (Exception e) { - throw new ClientException("Failed to get server info", e); } } @@ -991,6 +999,17 @@ public Builder registerClientMetrics(Object registry, String name) { return this; } + /** + * Sets server version that the client is interacting with. + * + * @param serverVersion - ClickHouse server version + * @return same instance of the builder + */ + public Builder setServerVersion(String serverVersion) { + this.configuration.put(ClientConfigProperties.SERVER_VERSION.getKey(), serverVersion); + return this; + } + public Client build() { setDefaults(); @@ -2174,6 +2193,10 @@ public String getServerVersion() { return this.serverVersion; } + public String getServerTimeZone() { + return this.configuration.get(ClientConfigProperties.SERVER_TIMEZONE.getKey()); + } + public String getClientVersion() { return clientVersion; } diff --git a/client-v2/src/main/java/com/clickhouse/client/api/ClientConfigProperties.java b/client-v2/src/main/java/com/clickhouse/client/api/ClientConfigProperties.java index c2137b736..0327e1e80 100644 --- a/client-v2/src/main/java/com/clickhouse/client/api/ClientConfigProperties.java +++ b/client-v2/src/main/java/com/clickhouse/client/api/ClientConfigProperties.java @@ -36,6 +36,8 @@ public enum ClientConfigProperties { USE_TIMEZONE("use_time_zone"), + SERVER_VERSION("server_version"), + SERVER_TIMEZONE("server_time_zone"), ASYNC_OPERATIONS("async"), diff --git a/jdbc-v2/src/test/java/com/clickhouse/jdbc/ConnectionTest.java b/jdbc-v2/src/test/java/com/clickhouse/jdbc/ConnectionTest.java index d8901dd22..324c57698 100644 --- a/jdbc-v2/src/test/java/com/clickhouse/jdbc/ConnectionTest.java +++ b/jdbc-v2/src/test/java/com/clickhouse/jdbc/ConnectionTest.java @@ -2,12 +2,9 @@ import java.nio.charset.StandardCharsets; import java.sql.*; -import java.util.Arrays; -import java.util.Base64; -import java.util.Properties; +import java.util.*; import java.util.Properties; -import java.util.UUID; import com.clickhouse.client.ClickHouseNode; import com.clickhouse.client.ClickHouseProtocol; @@ -553,5 +550,21 @@ public void testJWTWithCloud() throws Exception { Assert.assertTrue(rs.next()); } } + @Test(groups = { "integration" }) + public void testDisableExtraCallToServer() throws Exception { + Properties properties = new Properties(); + properties.put(ClientConfigProperties.SERVER_TIMEZONE.getKey(), "GMT"); + properties.put(ClientConfigProperties.SERVER_VERSION.getKey(), "1.0.0"); + try (Connection conn = getJdbcConnection(properties); + Statement stmt = conn.createStatement(); + ResultSet rs = stmt.executeQuery("SELECT 1")) { + Assert.assertTrue(rs.next()); + ConnectionImpl connImpl = (ConnectionImpl) conn; + + Assert.assertEquals(connImpl.getClient().getServerVersion(), "1.0.0"); + Assert.assertEquals(connImpl.getClient().getServerTimeZone(), "GMT"); + } + + } }