From 0896eaccdac51532883efdbf8e4a006e3bb7f0d0 Mon Sep 17 00:00:00 2001 From: Sergey Chernov Date: Mon, 7 Jul 2025 09:22:24 -0700 Subject: [PATCH 1/2] rewrote a bit to use older API --- .../client/api/internal/HttpAPIClientHelper.java | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/client-v2/src/main/java/com/clickhouse/client/api/internal/HttpAPIClientHelper.java b/client-v2/src/main/java/com/clickhouse/client/api/internal/HttpAPIClientHelper.java index 94af7a11e..475b8be33 100644 --- a/client-v2/src/main/java/com/clickhouse/client/api/internal/HttpAPIClientHelper.java +++ b/client-v2/src/main/java/com/clickhouse/client/api/internal/HttpAPIClientHelper.java @@ -64,6 +64,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.io.UnsupportedEncodingException; import java.lang.reflect.Method; import java.net.ConnectException; import java.net.InetSocketAddress; @@ -763,15 +764,19 @@ private static void addHeader(HttpRequest req, String headerName, return; } String tString = value.toString(); - if (tString.isBlank()) { + if (tString == null || tString.trim().isEmpty()) { return; } if (PATTERN_HEADER_VALUE_ASCII.matcher(tString).matches()) { req.addHeader(headerName, tString); } else { - req.addHeader( - headerName + "*", - "UTF-8''" + URLEncoder.encode(tString, StandardCharsets.UTF_8)); + try { + req.addHeader( + headerName + "*", + "UTF-8''" + URLEncoder.encode(tString, StandardCharsets.UTF_8.name())); + } catch (UnsupportedEncodingException e) { + throw new ClientException("Failed to convert string to UTF8" , e); + } } } From 064f3cc3a5b2be7d54ed6189f19708a67dd81fda Mon Sep 17 00:00:00 2001 From: Sergey Chernov Date: Mon, 7 Jul 2025 09:33:40 -0700 Subject: [PATCH 2/2] fixed potential NPE and class cast exception --- .../java/com/clickhouse/client/api/Client.java | 4 ++-- .../api/internal/HttpAPIClientHelper.java | 17 +++++++++-------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/client-v2/src/main/java/com/clickhouse/client/api/Client.java b/client-v2/src/main/java/com/clickhouse/client/api/Client.java index c747904f6..3edf74ba8 100644 --- a/client-v2/src/main/java/com/clickhouse/client/api/Client.java +++ b/client-v2/src/main/java/com/clickhouse/client/api/Client.java @@ -1197,7 +1197,7 @@ public CompletableFuture insert(String tableName, List data, Integer retry = (Integer) configuration.get(ClientConfigProperties.RETRY_ON_FAILURE.getKey()); final int maxRetries = retry == null ? 0 : retry; - settings.setOption(ClientConfigProperties.INPUT_OUTPUT_FORMAT.getKey(), format.name()); + settings.setOption(ClientConfigProperties.INPUT_OUTPUT_FORMAT.getKey(), format); final InsertSettings finalSettings = new InsertSettings(buildRequestSettings(settings.getAllSettings())); Supplier supplier = () -> { long startTime = System.nanoTime(); @@ -1400,7 +1400,7 @@ public CompletableFuture insert(String tableName, throw new IllegalArgumentException("Buffer size must be greater than 0"); } - settings.setOption(ClientConfigProperties.INPUT_OUTPUT_FORMAT.getKey(), format.name()); + settings.setOption(ClientConfigProperties.INPUT_OUTPUT_FORMAT.getKey(), format); final InsertSettings finalSettings = new InsertSettings(buildRequestSettings(settings.getAllSettings())); StringBuilder sqlStmt = new StringBuilder("INSERT INTO ").append(tableName); diff --git a/client-v2/src/main/java/com/clickhouse/client/api/internal/HttpAPIClientHelper.java b/client-v2/src/main/java/com/clickhouse/client/api/internal/HttpAPIClientHelper.java index 475b8be33..7f6f442f5 100644 --- a/client-v2/src/main/java/com/clickhouse/client/api/internal/HttpAPIClientHelper.java +++ b/client-v2/src/main/java/com/clickhouse/client/api/internal/HttpAPIClientHelper.java @@ -14,6 +14,7 @@ import com.clickhouse.client.api.enums.ProxyType; import com.clickhouse.client.api.http.ClickHouseHttpProto; import com.clickhouse.client.api.transport.Endpoint; +import com.clickhouse.data.ClickHouseFormat; import net.jpountz.lz4.LZ4Factory; import org.apache.hc.client5.http.ConnectTimeoutException; import org.apache.hc.client5.http.classic.methods.HttpPost; @@ -450,13 +451,13 @@ private void addHeaders(HttpPost req, Map requestConfig) { addHeader( req, ClickHouseHttpProto.HEADER_FORMAT, - requestConfig.get(ClientConfigProperties.INPUT_OUTPUT_FORMAT.getKey())); + ((ClickHouseFormat) requestConfig.get(ClientConfigProperties.INPUT_OUTPUT_FORMAT.getKey())).name()); } if (requestConfig.containsKey(ClientConfigProperties.QUERY_ID.getKey())) { addHeader( req, ClickHouseHttpProto.HEADER_QUERY_ID, - requestConfig.get(ClientConfigProperties.QUERY_ID.getKey())); + (String) requestConfig.get(ClientConfigProperties.QUERY_ID.getKey())); } addHeader( req, @@ -758,22 +759,22 @@ public void close() { } private static void addHeader(HttpRequest req, String headerName, - T value) + String value) { if (value == null) { return; } - String tString = value.toString(); - if (tString == null || tString.trim().isEmpty()) { + + if (value.trim().isEmpty()) { return; } - if (PATTERN_HEADER_VALUE_ASCII.matcher(tString).matches()) { - req.addHeader(headerName, tString); + if (PATTERN_HEADER_VALUE_ASCII.matcher(value).matches()) { + req.addHeader(headerName, value); } else { try { req.addHeader( headerName + "*", - "UTF-8''" + URLEncoder.encode(tString, StandardCharsets.UTF_8.name())); + "UTF-8''" + URLEncoder.encode(value, StandardCharsets.UTF_8.name())); } catch (UnsupportedEncodingException e) { throw new ClientException("Failed to convert string to UTF8" , e); }