Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[client-v2] Attach TimeZone to a QueryResponse & use TimeZone settings in binary readers #1763

Merged
merged 8 commits into from
Aug 6, 2024
Prev Previous commit
Next Next commit
propagation of timezone settings to request settings
  • Loading branch information
chernser committed Aug 5, 2024
commit e1eb107b5ceb25a4f4be4dc2f94216c24afdb82b
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
package com.clickhouse.client;

import java.sql.Time;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.TimeZone;

import com.clickhouse.data.ClickHouseColumn;
import com.clickhouse.data.ClickHouseInputStream;
import com.clickhouse.data.ClickHouseRecord;
Expand All @@ -15,6 +8,12 @@
import com.clickhouse.data.ClickHouseSimpleRecord;
import com.clickhouse.data.ClickHouseValue;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.TimeZone;

/**
* A simple response built on top of two lists: columns and records.
*/
Expand All @@ -31,7 +30,7 @@ public class ClickHouseSimpleResponse implements ClickHouseResponse {
* @return response object
*/
public static ClickHouseResponse of(ClickHouseConfig config, List<ClickHouseColumn> columns, Object[][] values) {
return of(config, columns, values, null);
return of(config, columns, values, null, null);
}

/**
Expand Down Expand Up @@ -136,6 +135,7 @@ protected ClickHouseSimpleResponse(List<ClickHouseColumn> columns, List<ClickHou
this.columns = columns;
this.records = Collections.unmodifiableList(records);
this.summary = summary != null ? summary : ClickHouseResponseSummary.EMPTY;
this.timeZone = timeZone;
}

protected ClickHouseSimpleResponse(List<ClickHouseColumn> columns, ClickHouseValue[][] values,
Expand All @@ -151,6 +151,7 @@ protected ClickHouseSimpleResponse(List<ClickHouseColumn> columns, ClickHouseVal
this.records = Collections.unmodifiableList(list);

this.summary = summary != null ? summary : ClickHouseResponseSummary.EMPTY;
this.timeZone = timeZone;
}

@Override
Expand Down
29 changes: 17 additions & 12 deletions client-v2/src/main/java/com/clickhouse/client/api/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -1051,17 +1051,7 @@ public CompletableFuture<QueryResponse> query(String sqlQuery, Map<String, Objec
clientStats.start(ClientMetrics.OP_DURATION);

if (useNewImplementation) {
// merge settings
if (!settings.getAllSettings().containsKey(ClickHouseClientOption.USE_TIME_ZONE.getKey()) &&
configuration.containsKey(ClickHouseClientOption.USE_TIME_ZONE.getKey())) {
settings.setOption(ClickHouseClientOption.USE_TIME_ZONE.getKey(),
configuration.get(ClickHouseClientOption.USE_TIME_ZONE.getKey()));
}
if (!settings.getAllSettings().containsKey(ClickHouseClientOption.USE_SERVER_TIME_ZONE.getKey()) &&
configuration.containsKey(ClickHouseClientOption.USE_SERVER_TIME_ZONE.getKey())) {
settings.setOption(ClickHouseClientOption.USE_SERVER_TIME_ZONE.getKey(),
MapUtils.getFlag(configuration, ClickHouseClientOption.USE_SERVER_TIME_ZONE.getKey()));
}
applyDefaults(settings);
//
String retry = configuration.get(ClickHouseClientOption.RETRY.getKey());
final int maxRetries = retry == null ? (int) ClickHouseClientOption.RETRY.getDefaultValue() : Integer.parseInt(retry);
Expand Down Expand Up @@ -1194,7 +1184,8 @@ public List<GenericRecord> queryAll(String sqlQuery) {
query(sqlQuery, settings).get(operationTimeout, TimeUnit.MILLISECONDS)) {
List<GenericRecord> records = new ArrayList<>();
if (response.getResultRows() > 0) {
ClickHouseBinaryFormatReader reader = new RowBinaryWithNamesAndTypesFormatReader(response.getInputStream());
ClickHouseBinaryFormatReader reader =
new RowBinaryWithNamesAndTypesFormatReader(response.getInputStream(), settings);
Map<String, Object> record;
while ((record = reader.next()) != null) {
records.add(new MapBackedRecord(record, reader.getSchema()));
Expand Down Expand Up @@ -1292,6 +1283,20 @@ private String startOperation() {
return operationId;
}

private void applyDefaults(QuerySettings settings) {
Map<String, Object> settingsMap = settings.getAllSettings();

String key = ClickHouseClientOption.USE_SERVER_TIME_ZONE.getKey();
if (!settingsMap.containsKey(key) && configuration.containsKey(key)) {
settings.setOption(key, MapUtils.getFlag(configuration, key));
}

key = ClickHouseClientOption.USE_TIME_ZONE.getKey();
if ( !settings.getUseServerTimeZone() && !settingsMap.containsKey(key) && configuration.containsKey(key)) {
settings.setOption(key, TimeZone.getTimeZone(configuration.get(key)));
}
}

public String toString() {
return "Client{" +
"endpoints=" + endpoints +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@

public class RowBinaryWithNamesAndTypesFormatReader extends AbstractBinaryFormatReader implements Iterator<Map<String, Object>> {

public RowBinaryWithNamesAndTypesFormatReader(InputStream inputStream) {
this(inputStream, null);
}

public RowBinaryWithNamesAndTypesFormatReader(InputStream inputStream, QuerySettings querySettings) {
super(inputStream, querySettings, null);
readSchema();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ protected AbstractBinaryFormatReader(InputStream inputStream, QuerySettings quer
TimeZone timeZone = useServerTimeZone ? querySettings.getServerTimeZone() :
(TimeZone) this.settings.get(ClickHouseClientOption.USE_TIME_ZONE.getKey());
if (timeZone == null) {
throw new ClientException("Time zone is not set");
throw new ClientException("Time zone is not set. (useServerTimezone:" + useServerTimeZone + ")");
}
this.binaryStreamReader = new BinaryStreamReader(inputStream, timeZone, LOG);
setSchema(schema);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,13 @@ public QueryResponse(ClassicHttpResponse response, QuerySettings settings, Opera
this.operationMetrics = operationMetrics;

Header tzHeader = response.getFirstHeader(ClickHouseHttpProto.HEADER_TIMEZONE);
settings.setOption("server_timezone", tzHeader);
if (tzHeader != null) {
try {
settings.setOption("server_timezone", TimeZone.getTimeZone(tzHeader.getValue()));
} catch (Exception e) {
throw new ClientException("Failed to parse server timezone", e);
}
}
}

public InputStream getInputStream() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public Records(QueryResponse response, QuerySettings finalSettings) {
throw new ClientException("Unsupported format: " + finalSettings.getFormat());
}

this.reader = new RowBinaryWithNamesAndTypesFormatReader(response.getInputStream());
this.reader = new RowBinaryWithNamesAndTypesFormatReader(response.getInputStream(), finalSettings);
this.empty = !reader.hasNext();
}

Expand Down