Skip to content

Commit 5ae9b83

Browse files
authored
Solves issue-2191 (#2337)
1 parent ab1917b commit 5ae9b83

File tree

3 files changed

+50
-12
lines changed

3 files changed

+50
-12
lines changed

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

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -192,16 +192,24 @@ private Client(Set<String> endpoints, Map<String,String> configuration, boolean
192192
*
193193
*/
194194
public void loadServerInfo() {
195-
try (QueryResponse response = this.query("SELECT currentUser() AS user, timezone() AS timezone, version() AS version LIMIT 1").get()) {
196-
try (ClickHouseBinaryFormatReader reader = this.newBinaryFormatReader(response)) {
197-
if (reader.next() != null) {
198-
this.configuration.put(ClientConfigProperties.USER.getKey(), reader.getString("user"));
199-
this.configuration.put(ClientConfigProperties.SERVER_TIMEZONE.getKey(), reader.getString("timezone"));
200-
serverVersion = reader.getString("version");
195+
// only if 2 properties are set disable retrieval from server
196+
if (!this.configuration.containsKey(ClientConfigProperties.SERVER_TIMEZONE.getKey()) && !this.configuration.containsKey(ClientConfigProperties.SERVER_VERSION.getKey())) {
197+
try (QueryResponse response = this.query("SELECT currentUser() AS user, timezone() AS timezone, version() AS version LIMIT 1").get()) {
198+
try (ClickHouseBinaryFormatReader reader = this.newBinaryFormatReader(response)) {
199+
if (reader.next() != null) {
200+
this.configuration.put(ClientConfigProperties.USER.getKey(), reader.getString("user"));
201+
this.configuration.put(ClientConfigProperties.SERVER_TIMEZONE.getKey(), reader.getString("timezone"));
202+
serverVersion = reader.getString("version");
203+
}
201204
}
205+
} catch (Exception e) {
206+
throw new ClientException("Failed to get server info", e);
207+
}
208+
} else {
209+
LOG.info("Using server version " + this.configuration.get(ClientConfigProperties.SERVER_VERSION.getKey()) + " and timezone " + this.configuration.get(ClientConfigProperties.SERVER_TIMEZONE.getKey()) );
210+
if (this.configuration.containsKey(ClientConfigProperties.SERVER_VERSION.getKey())) {
211+
serverVersion = this.configuration.get(ClientConfigProperties.SERVER_VERSION.getKey());
202212
}
203-
} catch (Exception e) {
204-
throw new ClientException("Failed to get server info", e);
205213
}
206214
}
207215

@@ -991,6 +999,17 @@ public Builder registerClientMetrics(Object registry, String name) {
991999
return this;
9921000
}
9931001

1002+
/**
1003+
* Sets server version that the client is interacting with.
1004+
*
1005+
* @param serverVersion - ClickHouse server version
1006+
* @return same instance of the builder
1007+
*/
1008+
public Builder setServerVersion(String serverVersion) {
1009+
this.configuration.put(ClientConfigProperties.SERVER_VERSION.getKey(), serverVersion);
1010+
return this;
1011+
}
1012+
9941013
public Client build() {
9951014
setDefaults();
9961015

@@ -2174,6 +2193,10 @@ public String getServerVersion() {
21742193
return this.serverVersion;
21752194
}
21762195

2196+
public String getServerTimeZone() {
2197+
return this.configuration.get(ClientConfigProperties.SERVER_TIMEZONE.getKey());
2198+
}
2199+
21772200
public String getClientVersion() {
21782201
return clientVersion;
21792202
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ public enum ClientConfigProperties {
3636

3737
USE_TIMEZONE("use_time_zone"),
3838

39+
SERVER_VERSION("server_version"),
40+
3941
SERVER_TIMEZONE("server_time_zone"),
4042

4143
ASYNC_OPERATIONS("async"),

jdbc-v2/src/test/java/com/clickhouse/jdbc/ConnectionTest.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,9 @@
22

33
import java.nio.charset.StandardCharsets;
44
import java.sql.*;
5-
import java.util.Arrays;
6-
import java.util.Base64;
7-
import java.util.Properties;
5+
import java.util.*;
86

97
import java.util.Properties;
10-
import java.util.UUID;
118

129
import com.clickhouse.client.ClickHouseNode;
1310
import com.clickhouse.client.ClickHouseProtocol;
@@ -553,5 +550,21 @@ public void testJWTWithCloud() throws Exception {
553550
Assert.assertTrue(rs.next());
554551
}
555552
}
553+
@Test(groups = { "integration" })
554+
public void testDisableExtraCallToServer() throws Exception {
555+
Properties properties = new Properties();
556+
properties.put(ClientConfigProperties.SERVER_TIMEZONE.getKey(), "GMT");
557+
properties.put(ClientConfigProperties.SERVER_VERSION.getKey(), "1.0.0");
558+
try (Connection conn = getJdbcConnection(properties);
559+
Statement stmt = conn.createStatement();
560+
ResultSet rs = stmt.executeQuery("SELECT 1")) {
561+
Assert.assertTrue(rs.next());
562+
ConnectionImpl connImpl = (ConnectionImpl) conn;
563+
564+
Assert.assertEquals(connImpl.getClient().getServerVersion(), "1.0.0");
565+
Assert.assertEquals(connImpl.getClient().getServerTimeZone(), "GMT");
566+
}
567+
568+
}
556569

557570
}

0 commit comments

Comments
 (0)