Repository navigation
Description
Description
After an application calls Client.loadServerInfo(), every call to Client.getServerTimeZone() throws ClassCastException: class java.lang.String cannot be cast to class java.util.TimeZone.
docs/integration-client.md (Tools summary) documents this sequence: "client.loadServerInfo() ... Refreshes cached server info; then read getServerVersion(), getServerTimeZone(), getUser()". getServerVersion() and getUser() work. getServerTimeZone() fails.
Root cause. The parsed client configuration keeps server_time_zone as a java.util.TimeZone (ClientConfigProperties.SERVER_TIMEZONE has value type TimeZone.class, and parseValue returns TimeZone.getTimeZone(value)).
Client.loadServerInfo()(client-v2/src/main/java/com/clickhouse/client/api/Client.java:261) writes aStringto the same key:this.configuration.put(ClientConfigProperties.SERVER_TIMEZONE.getKey(), reader.getString("timezone"));Client.getServerTimeZone()(Client.java:2633) casts the value:TimeZone tz = (TimeZone) this.configuration.get(ClientConfigProperties.SERVER_TIMEZONE.getKey());
The cast came in 813df63 ("made client settings super stream lined…", first released in v0.9.1). Before that commit, getServerTimeZone() returned the value as a String, so the String from loadServerInfo() was valid.
Queries that run after loadServerInfo() are not affected in my tests: the String also goes into the per-request settings (buildRequestSettings copies the client configuration), but QueryResponse replaces it with a TimeZone built from the X-ClickHouse-Timezone response header, which the server sent on every response.
ClientTests#testLoadingServerContext calls loadServerInfo() but asserts only getServerVersion(), so the test suite does not catch this.
Steps to reproduce
- Create a
Clientwith any time-zone configuration (the defaults are enough). - Call
client.getServerTimeZone(). It returnsUTC. - Call
client.loadServerInfo(), then callclient.getServerTimeZone()again. It throws.
I got the same result with 4 builder configurations: defaults, setServerTimeZone("Europe/Berlin"), useTimeZone("Asia/Tokyo"), and useServerTimeZone(true) + serverSetting("session_timezone", "Asia/Tokyo"). In each case getServerTimeZone() worked before loadServerInfo() and threw after it.
Error Log or Exception StackTrace
Exception in thread "main" java.lang.ClassCastException: class java.lang.String cannot be cast to class java.util.TimeZone (java.lang.String and java.util.TimeZone are in module java.base of loader 'bootstrap')
at com.clickhouse.client.api.Client.getServerTimeZone(Client.java:2633)
at Min.main(Min.java:13)
Expected Behaviour
getServerTimeZone() returns the server time zone that loadServerInfo() read (the server's SELECT timezone(), UTC on the test server), as it does before loadServerInfo() is called.
Code Example
import com.clickhouse.client.api.Client;
public class Min {
public static void main(String[] args) {
try (Client client = new Client.Builder()
.addEndpoint("http://localhost:8123/")
.setUsername("default")
.setPassword("")
.build()) {
System.out.println(client.getServerTimeZone()); // UTC
client.loadServerInfo();
System.out.println(client.getServerVersion()); // 26.10.4.207
System.out.println(client.getServerTimeZone()); // ClassCastException
}
}
}Suggested fix. In loadServerInfo(), store the same type as the parsed configuration, for example ClientConfigProperties.SERVER_TIMEZONE.parseValue(reader.getString("timezone")) (equivalent to TimeZone.getTimeZone(...)). A regression test can extend ClientTests#testLoadingServerContext to assert getServerTimeZone() against the server's timezone() after loadServerInfo(). Contrast case that must keep its current behavior: getServerTimeZone() before loadServerInfo() returns the configured value (UTC by default, or the value from setServerTimeZone(...)).
Configuration
Client Configuration
new Client.Builder()
.addEndpoint("http://localhost:8123/")
.setUsername("default")
.setPassword("")
.build();Environment
- Cloud
- Client version:
client-v20.12.0-rc1-SNAPSHOT, built frommainat 57261ad - Language version: OpenJDK 17.0.21
- OS: Linux (Docker)
ClickHouse Server
- ClickHouse Server version: 26.10.4.207
- ClickHouse Server non-default settings, if any: none (server
timezone()=UTC) CREATE TABLEstatements for tables involved: none- Sample data for all these tables: none
Found by automated analysis of the client (Polyglot AI) while working on #3286. Verified against a live ClickHouse server, not only by reading the code.
Activity