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
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import com.clickhouse.client.api.data_formats.RowBinaryFormatReader;
import com.clickhouse.client.api.data_formats.RowBinaryWithNamesAndTypesFormatReader;
import com.clickhouse.client.api.data_formats.RowBinaryWithNamesFormatReader;
import com.clickhouse.client.api.data_formats.internal.AbstractBinaryFormatReader;
import com.clickhouse.client.api.data_formats.internal.BinaryStreamReader;
import com.clickhouse.client.api.data_formats.internal.MapBackedRecord;
import com.clickhouse.client.api.data_formats.internal.ProcessParser;
Expand Down Expand Up @@ -37,7 +36,6 @@
import com.clickhouse.client.api.transport.Endpoint;
import com.clickhouse.client.api.transport.HttpEndpoint;
import com.clickhouse.client.config.ClickHouseClientOption;
import com.clickhouse.config.ClickHouseOption;
import com.clickhouse.data.ClickHouseColumn;
import com.clickhouse.data.ClickHouseDataType;
import com.clickhouse.data.ClickHouseFormat;
Expand Down Expand Up @@ -1575,7 +1573,7 @@ public CompletableFuture<QueryResponse> query(String sqlQuery, Map<String, Objec
Supplier<QueryResponse> responseSupplier;

if (queryParams != null) {
settings.setOption("statement_params", queryParams);
settings.setOption(HttpAPIClientHelper.KEY_STATEMENT_PARAMS, queryParams);
}
final QuerySettings finalSettings = new QuerySettings(buildRequestSettings(settings.getAllSettings()));
responseSupplier = () -> {
Expand Down Expand Up @@ -2027,6 +2025,7 @@ protected int getOperationTimeout() {
* @return - set of endpoints
* @deprecated
*/
@Deprecated
public Set<String> getEndpoints() {
return endpoints.stream().map(Endpoint::getBaseURL).collect(Collectors.toSet());
}
Expand Down Expand Up @@ -2100,4 +2099,5 @@ private Map<String, Object> buildRequestSettings(Map<String, Object> opSettings)
requestSettings.putAll(opSettings);
return requestSettings;
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
package com.clickhouse.client.api;

import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoField;
import java.util.Objects;

import com.clickhouse.data.ClickHouseDataType;

public class DataTypeUtils {

Expand All @@ -19,4 +26,91 @@ public class DataTypeUtils {
*/
public static DateTimeFormatter DATETIME_WITH_NANOS_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.nnnnnnnnn");

private static final DateTimeFormatter INSTANT_FORMATTER = new DateTimeFormatterBuilder()
.appendValue(ChronoField.INSTANT_SECONDS)
.appendFraction(ChronoField.NANO_OF_SECOND, 9, 9, true)
.toFormatter();

/**
* Formats an {@link Instant} object for use in SQL statements or as query
* parameter.
*
* @param instant
* the Java object to format
* @return a suitable String representation of {@code instant}
* @throws NullPointerException
* if {@code instant} is null
*/
public static String formatInstant(Instant instant) {
return formatInstant(instant, null);
}

/**
* Formats an {@link Instant} object for use in SQL statements or as query
* parameter.
*
* This method uses the {@code dataTypeHint} parameter to find the best
* suitable format for the instant.
*
* @param instant
* the Java object to format
* @param dataTypeHint
* the ClickHouse data type {@code instant} should be used for
* @return a suitable String representation of {@code instant}
* @throws NullPointerException
* if {@code instant} is null
*/
public static String formatInstant(Instant instant, ClickHouseDataType dataTypeHint) {
return formatInstant(instant, dataTypeHint, null);
}

/**
* Formats an {@link Instant} object for use in SQL statements or as query
* parameter.
*
* This method uses the {@code dataTypeHint} parameter to find the best
* suitable format for the instant.
*
* For <em>some</em> formatting operations, providing a {@code timeZone} is
* mandatory, e.g. for {@link ClickHouseDataType#Date}.
*
* @param instant
* the Java object to format
* @param dataTypeHint
* the ClickHouse data type {@code object} should be used for
* @param timeZone
* the time zone to be used when formatting the instant for use
* in non-time-zone-based ClickHouse data types
* @return a suitable String representation of {@code object}, or the empty
* String for {@code null} objects
* @throws NullPointerException
* if {@code instant} is null
*/
public static String formatInstant(Instant instant, ClickHouseDataType dataTypeHint,
ZoneId timeZone)
{
Objects.requireNonNull(instant, "Instant required for formatInstant");
if (dataTypeHint == null) {
return formatInstantDefault(instant);
}
switch (dataTypeHint) {
case Date:
case Date32:
Objects.requireNonNull(
timeZone,
"TimeZone required for formatting Instant for '" + dataTypeHint + "' use");
return DATE_FORMATTER.format(
instant.atZone(timeZone).toLocalDate());
case DateTime:
case DateTime32:
return String.valueOf(instant.getEpochSecond());
default:
return formatInstantDefault(instant);
}
}

private static String formatInstantDefault(Instant instant) {
return INSTANT_FORMATTER.format(instant);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.clickhouse.client.api.data_formats.internal.SerializerUtils;
import com.clickhouse.data.ClickHouseColumn;
import com.clickhouse.data.ClickHouseDataType;
import com.clickhouse.data.ClickHouseFormat;
import com.clickhouse.data.format.BinaryStreamUtils;

import java.io.IOException;
Expand Down Expand Up @@ -132,7 +131,7 @@ public void writeFixedString(String value, int len) throws IOException {
}

public void writeDate(ZonedDateTime value) throws IOException {
SerializerUtils.writeDate(out, value, ZoneId.of("UTC"));
SerializerUtils.writeDate(out, value, value.getZone());
}

public void writeDate32(ZonedDateTime value, ZoneId targetTz) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public <T> T readValue(ClickHouseColumn column, Class<?> typeHint) throws IOExce
if (column.isNullable()) {
int isNull = readByteOrEOF(input);
if (isNull == 1) { // is Null?
return (T) null;
return null;
}
}

Expand Down Expand Up @@ -588,7 +588,7 @@ public static byte[] readNBytesLE(InputStream input, byte[] buffer, int offset,

return bytes;
}

/**
* Reads a array into an ArrayValue object.
* @param column - column information
Expand Down Expand Up @@ -964,7 +964,7 @@ private ZonedDateTime readDateTime32(TimeZone tz) throws IOException {
*/
public static ZonedDateTime readDateTime32(InputStream input, byte[] buff, TimeZone tz) throws IOException {
long time = readUnsignedIntLE(input, buff);
return LocalDateTime.ofInstant(Instant.ofEpochSecond(Math.max(time, 0L)), tz.toZoneId()).atZone(tz.toZoneId());
return Instant.ofEpochSecond(Math.max(time, 0L)).atZone(tz.toZoneId());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Array;
Expand All @@ -29,7 +26,14 @@
import java.net.Inet4Address;
import java.net.Inet6Address;
import java.sql.Timestamp;
import java.time.*;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.Period;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
Expand All @@ -54,8 +58,6 @@

public class SerializerUtils {

private static final Logger LOG = LoggerFactory.getLogger(SerializerUtils.class);

public static void serializeData(OutputStream stream, Object value, ClickHouseColumn column) throws IOException {
//Serialize the value to the stream based on the data type
switch (column.getDataType()) {
Expand Down Expand Up @@ -1070,6 +1072,9 @@ public static void writeDate(OutputStream output, Object value, ZoneId targetTz)
} else if (value instanceof ZonedDateTime) {
ZonedDateTime dt = (ZonedDateTime) value;
epochDays = (int)dt.withZoneSameInstant(targetTz).toLocalDate().toEpochDay();
} else if (value instanceof OffsetDateTime) {
OffsetDateTime dt = (OffsetDateTime) value;
epochDays = (int) dt.atZoneSameInstant(targetTz).toLocalDate().toEpochDay();
} else {
throw new IllegalArgumentException("Cannot convert " + value + " to Long");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,10 @@
import java.util.regex.Pattern;

public class HttpAPIClientHelper {
private static final Logger LOG = LoggerFactory.getLogger(Client.class);

public static final String KEY_STATEMENT_PARAMS = "statement_params";

private static final Logger LOG = LoggerFactory.getLogger(HttpAPIClientHelper.class);

private static final int ERROR_BODY_BUFFER_SIZE = 1024; // Error messages are usually small

Expand Down Expand Up @@ -567,11 +570,9 @@ private void addQueryParams(URIBuilder req, Map<String, Object> requestConfig) {
if (requestConfig.containsKey(ClientConfigProperties.QUERY_ID.getKey())) {
req.addParameter(ClickHouseHttpProto.QPARAM_QUERY_ID, requestConfig.get(ClientConfigProperties.QUERY_ID.getKey()).toString());
}
if (requestConfig.containsKey("statement_params")) {
Map<String, Object> params = (Map<String, Object>) requestConfig.get("statement_params");
for (Map.Entry<String, Object> entry : params.entrySet()) {
req.addParameter("param_" + entry.getKey(), String.valueOf(entry.getValue()));
}
if (requestConfig.containsKey(KEY_STATEMENT_PARAMS)) {
Map<?, ?> params = (Map<?, ?>) requestConfig.get(KEY_STATEMENT_PARAMS);
params.forEach((k, v) -> req.addParameter("param_" + k, String.valueOf(v)));
}

boolean clientCompression = ClientConfigProperties.COMPRESS_CLIENT_REQUEST.getOrDefault(requestConfig);
Expand Down
Loading
Loading