Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions client-v2/src/main/java/com/clickhouse/client/api/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@ public class Client implements AutoCloseable {

private final ColumnToMethodMatchingStrategy columnToMethodMatchingStrategy;

// Server context
private String serverVersion;

private Client(Set<String> endpoints, Map<String,String> configuration, boolean useNewImplementation,
ExecutorService sharedOperationExecutor, ColumnToMethodMatchingStrategy columnToMethodMatchingStrategy) {
this.endpoints = endpoints;
Expand All @@ -179,8 +182,27 @@ private Client(Set<String> endpoints, Map<String,String> 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.
*
Expand Down Expand Up @@ -216,6 +238,7 @@ public void close() {
}
}


public static class Builder {
private Set<String> endpoints;

Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Loading