Skip to content

Commit 09e89bc

Browse files
committed
Fixed handling datetime and queryID
1 parent 6ef50f8 commit 09e89bc

File tree

14 files changed

+53
-78
lines changed

14 files changed

+53
-78
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1160,7 +1160,7 @@ public CompletableFuture<InsertResponse> insert(String tableName, List<?> data,
11601160
OperationMetrics metrics = new OperationMetrics(clientStats);
11611161
String summary = HttpAPIClientHelper.getHeaderVal(httpResponse.getFirstHeader(ClickHouseHttpProto.HEADER_SRV_SUMMARY), "{}");
11621162
ProcessParser.parseSummary(summary, metrics);
1163-
String queryId = HttpAPIClientHelper.getHeaderVal(httpResponse.getFirstHeader(ClickHouseHttpProto.QPARAM_QUERY_ID), finalSettings.getQueryId(), String::valueOf);
1163+
String queryId = HttpAPIClientHelper.getHeaderVal(httpResponse.getFirstHeader(ClickHouseHttpProto.HEADER_QUERY_ID), finalSettings.getQueryId(), String::valueOf);
11641164
metrics.operationComplete();
11651165
metrics.setQueryId(queryId);
11661166
return new InsertResponse(metrics);
@@ -1283,7 +1283,7 @@ public CompletableFuture<InsertResponse> insert(String tableName,
12831283
OperationMetrics metrics = new OperationMetrics(clientStats);
12841284
String summary = HttpAPIClientHelper.getHeaderVal(httpResponse.getFirstHeader(ClickHouseHttpProto.HEADER_SRV_SUMMARY), "{}");
12851285
ProcessParser.parseSummary(summary, metrics);
1286-
String queryId = HttpAPIClientHelper.getHeaderVal(httpResponse.getFirstHeader(ClickHouseHttpProto.QPARAM_QUERY_ID), finalSettings.getQueryId(), String::valueOf);
1286+
String queryId = HttpAPIClientHelper.getHeaderVal(httpResponse.getFirstHeader(ClickHouseHttpProto.HEADER_QUERY_ID), finalSettings.getQueryId(), String::valueOf);
12871287
metrics.operationComplete();
12881288
metrics.setQueryId(queryId);
12891289
return new InsertResponse(metrics);

client-v2/src/main/java/com/clickhouse/client/api/data_formats/internal/AbstractBinaryFormatReader.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -319,12 +319,9 @@ public ZonedDateTime getZonedDateTime(String colName) {
319319
switch (column.getDataType()) {
320320
case DateTime:
321321
case DateTime64:
322-
LocalDateTime dateTime = readValue(colName);
323-
return dateTime.atZone(column.getTimeZone().toZoneId());
324322
case Date:
325323
case Date32:
326-
LocalDate data = readValue(colName);
327-
return data.atStartOfDay(column.getTimeZone().toZoneId());
324+
return readValue(colName);
328325
}
329326

330327
throw new ClientException("Column of type " + column.getDataType() + " cannot be converted to Instant");

client-v2/src/main/java/com/clickhouse/client/api/data_formats/internal/BinaryStreamReader.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ private <T> T readValueImpl(ClickHouseColumn column) throws IOException {
203203
throw new IllegalArgumentException("Unsupported data type: " + column.getDataType());
204204
}
205205
} catch (EOFException e) {
206+
log.info("End of stream reached before reading all data for column " + column.getColumnName());
206207
throw e;
207208
} catch (Exception e) {
208209
throw new ClientException("Failed to read value for column " + column.getColumnName(), e);

client-v2/src/main/java/com/clickhouse/client/api/data_formats/internal/MapBackedRecord.java

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -159,12 +159,9 @@ public ZonedDateTime getZonedDateTime(String colName) {
159159
switch (column.getDataType()) {
160160
case DateTime:
161161
case DateTime64:
162-
LocalDateTime dateTime = readValue(colName);
163-
return dateTime.atZone(column.getTimeZone().toZoneId());
164162
case Date:
165163
case Date32:
166-
LocalDate data = readValue(colName);
167-
return data.atStartOfDay(column.getTimeZone().toZoneId());
164+
return readValue(colName);
168165
}
169166

170167
throw new ClientException("Column of type " + column.getDataType() + " cannot be converted to Instant");
@@ -449,36 +446,40 @@ public short getEnum16(int index) {
449446
}
450447

451448
@Override
452-
public LocalDate getLocalDate(String colName) {
453-
Object value = readValue(colName);
454-
if (value instanceof LocalDateTime) {
455-
return ((LocalDateTime) value).toLocalDate();
449+
public LocalDate getLocalDate(int index) {
450+
Object value = readValue(index);
451+
if (value instanceof ZonedDateTime) {
452+
return ((ZonedDateTime) value).toLocalDate();
456453
}
457454
return (LocalDate) value;
458-
459455
}
460456

461457
@Override
462-
public LocalDate getLocalDate(int index) {
463-
Object value = readValue(index);
464-
if (value instanceof LocalDateTime) {
465-
return ((LocalDateTime) value).toLocalDate();
458+
public LocalDate getLocalDate(String colName) {
459+
Object value = readValue(colName);
460+
if (value instanceof ZonedDateTime) {
461+
return ((ZonedDateTime) value).toLocalDate();
466462
}
467463
return (LocalDate) value;
464+
468465
}
469466

470467
@Override
471468
public LocalDateTime getLocalDateTime(String colName) {
472469
Object value = readValue(colName);
473-
if (value instanceof LocalDate) {
474-
return ((LocalDate) value).atStartOfDay();
470+
if (value instanceof ZonedDateTime) {
471+
return ((ZonedDateTime) value).toLocalDateTime();
475472
}
476473
return (LocalDateTime) value;
477474
}
478475

479476
@Override
480477
public LocalDateTime getLocalDateTime(int index) {
481-
return readValue(index);
478+
Object value = readValue(index);
479+
if (value instanceof ZonedDateTime) {
480+
return ((ZonedDateTime) value).toLocalDateTime();
481+
}
482+
return (LocalDateTime) value;
482483
}
483484

484485
@Override

client-v2/src/main/java/com/clickhouse/client/api/insert/InsertSettings.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
package com.clickhouse.client.api.insert;
22

33
import com.clickhouse.client.api.internal.ValidationUtils;
4+
import com.clickhouse.client.config.ClickHouseClientOption;
45

5-
import java.util.Collections;
66
import java.util.HashMap;
77
import java.util.Map;
88

99
public class InsertSettings {
1010
private static final int DEFAULT_INPUT_STREAM_BATCH_SIZE = 8196;
1111

12-
private String queryId;
1312
private int inputStreamCopyBufferSize;
1413
private String operationId;
1514
Map<String, Object> rawSettings;
@@ -27,7 +26,6 @@ public InsertSettings(Map<String, Object> settings) {
2726

2827
private void setDefaults() {// Default settings, for now a very small list
2928
this.setInputStreamCopyBufferSize(DEFAULT_INPUT_STREAM_BATCH_SIZE);
30-
this.queryId = null;
3129
}
3230

3331
/**
@@ -57,7 +55,7 @@ public void setOption(String option, Object value) {
5755
* @return all settings
5856
*/
5957
public Map<String, Object> getAllSettings() {
60-
return Collections.unmodifiableMap(rawSettings);
58+
return rawSettings;
6159
}
6260

6361
/**
@@ -72,14 +70,14 @@ public InsertSettings setDeduplicationToken(String token) {
7270
}
7371

7472
public String getQueryId() {
75-
return this.queryId;
73+
return (String) rawSettings.get(ClickHouseClientOption.QUERY_ID.getKey());
7674
}
7775

7876
/**
7977
* Sets the query id. This id will be sent to the server and can be used to identify the query.
8078
*/
8179
public InsertSettings setQueryId(String queryId) {
82-
this.queryId = queryId;
80+
rawSettings.put(ClickHouseClientOption.QUERY_ID.getKey(), queryId);
8381
return this;
8482
}
8583

client-v2/src/main/java/com/clickhouse/client/api/internal/HttpAPIClientHelper.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,9 @@ private void addHeaders(HttpPost req, Map<String, String> chConfig, Map<String,
387387
if (requestConfig.containsKey(ClickHouseClientOption.FORMAT.getKey())) {
388388
req.addHeader(ClickHouseHttpProto.HEADER_FORMAT, requestConfig.get(ClickHouseClientOption.FORMAT.getKey()));
389389
}
390+
if (requestConfig.containsKey(ClickHouseClientOption.QUERY_ID.getKey())) {
391+
req.addHeader(ClickHouseHttpProto.HEADER_QUERY_ID, requestConfig.get(ClickHouseClientOption.QUERY_ID.getKey()).toString());
392+
}
390393
}
391394
req.addHeader(ClickHouseHttpProto.HEADER_DATABASE, chConfig.get(ClickHouseClientOption.DATABASE.getKey()));
392395
req.addHeader(ClickHouseHttpProto.HEADER_DB_USER, chConfig.get(ClickHouseDefaults.USER.getKey()));

client-v2/src/main/java/com/clickhouse/client/api/internal/SerializerUtils.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,10 @@ private static void serializePrimitiveData(OutputStream stream, Object value, Cl
164164
BinaryStreamUtils.writeBoolean(stream, (Boolean) value);
165165
break;
166166
case String:
167-
BinaryStreamUtils.writeString(stream, (String) value);
167+
BinaryStreamUtils.writeString(stream, convertToString(value));
168168
break;
169169
case FixedString:
170-
BinaryStreamUtils.writeFixedString(stream, (String) value, column.getPrecision());
170+
BinaryStreamUtils.writeFixedString(stream, convertToString(value), column.getPrecision());
171171
break;
172172
case Date:
173173
BinaryStreamUtils.writeDate(stream, (LocalDate) value);
@@ -249,6 +249,10 @@ public static BigInteger convertToBigInteger(Object value) {
249249
}
250250
}
251251

252+
public static String convertToString(Object value) {
253+
return java.lang.String.valueOf(value);
254+
}
255+
252256
public static <T extends Enum<T>> Set<T> parseEnumList(String value, Class<T> enumType) {
253257
Set<T> values = new HashSet<>();
254258
for (StringTokenizer causes = new StringTokenizer(value, Client.VALUES_LIST_DELIMITER); causes.hasMoreTokens(); ) {

client-v2/src/test/java/com/clickhouse/client/ClientTests.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,6 @@
2121
public class ClientTests extends BaseIntegrationTest {
2222
private static final Logger LOGGER = LoggerFactory.getLogger(ClientTests.class);
2323

24-
// static {
25-
// System.setProperty("org.slf4j.simpleLogger.defaultLogLevel", "DEBUG");
26-
// }
27-
2824
@Test(dataProvider = "clientProvider")
2925
public void testAddSecureEndpoint(Client client) {
3026
try {

client-v2/src/test/java/com/clickhouse/client/HttpTransportTests.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import com.clickhouse.client.api.ConnectionInitiationException;
77
import com.clickhouse.client.api.ConnectionReuseStrategy;
88
import com.clickhouse.client.api.ServerException;
9-
import com.clickhouse.client.api.data_formats.ClickHouseBinaryFormatReader;
109
import com.clickhouse.client.api.enums.Protocol;
1110
import com.clickhouse.client.api.enums.ProxyType;
1211
import com.clickhouse.client.api.insert.InsertResponse;
@@ -38,18 +37,12 @@
3837
import java.util.Random;
3938
import java.util.concurrent.CompletableFuture;
4039
import java.util.concurrent.ExecutionException;
41-
import java.util.concurrent.Future;
4240
import java.util.concurrent.TimeUnit;
4341
import java.util.concurrent.atomic.AtomicInteger;
44-
import java.util.function.Consumer;
4542

4643
import static com.github.tomakehurst.wiremock.stubbing.Scenario.STARTED;
4744

48-
public class HttpTransportTests extends BaseIntegrationTest{
49-
50-
static {
51-
System.setProperty("org.slf4j.simpleLogger.defaultLogLevel", "DEBUG");
52-
}
45+
public class HttpTransportTests extends BaseIntegrationTest {
5346

5447
@Test(groups = {"integration"},dataProvider = "testConnectionTTLProvider")
5548
@SuppressWarnings("java:S2925")

client-v2/src/test/java/com/clickhouse/client/insert/InsertClientContentCompressionTests.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
package com.clickhouse.client.insert;
22

33
public class InsertClientContentCompressionTests extends InsertTests {
4-
5-
static {
6-
System.setProperty("org.slf4j.simpleLogger.defaultLogLevel", "DEBUG");
7-
}
8-
94
public InsertClientContentCompressionTests() {
105
super(true, false);
116
}

0 commit comments

Comments
 (0)