From e33d4662c06195d40a2cce8b883a74982df7aa04 Mon Sep 17 00:00:00 2001 From: Sergey Chernov Date: Thu, 10 Oct 2024 07:38:10 -0700 Subject: [PATCH 1/4] added setting log_comment --- .../client/api/command/CommandResponse.java | 7 ++++- .../client/api/insert/InsertSettings.java | 19 ++++++++++++ .../client/api/query/QuerySettings.java | 19 ++++++++++++ .../clickhouse/client/insert/InsertTests.java | 30 +++++++++++++++++++ .../clickhouse/client/query/QueryTests.java | 21 +++++++++++++ 5 files changed, 95 insertions(+), 1 deletion(-) diff --git a/client-v2/src/main/java/com/clickhouse/client/api/command/CommandResponse.java b/client-v2/src/main/java/com/clickhouse/client/api/command/CommandResponse.java index 90c3c1655..481edac4b 100644 --- a/client-v2/src/main/java/com/clickhouse/client/api/command/CommandResponse.java +++ b/client-v2/src/main/java/com/clickhouse/client/api/command/CommandResponse.java @@ -5,7 +5,7 @@ import com.clickhouse.client.api.metrics.ServerMetrics; import com.clickhouse.client.api.query.QueryResponse; -public class CommandResponse{ +public class CommandResponse implements AutoCloseable { private final QueryResponse response; @@ -71,4 +71,9 @@ public long getWrittenBytes() { public long getServerTime() { return response.getServerTime(); } + + @Override + public void close() throws Exception { + response.close(); + } } diff --git a/client-v2/src/main/java/com/clickhouse/client/api/insert/InsertSettings.java b/client-v2/src/main/java/com/clickhouse/client/api/insert/InsertSettings.java index d2cf84a50..f8605d334 100644 --- a/client-v2/src/main/java/com/clickhouse/client/api/insert/InsertSettings.java +++ b/client-v2/src/main/java/com/clickhouse/client/api/insert/InsertSettings.java @@ -205,4 +205,23 @@ public InsertSettings serverSetting(String name, Collection values) { rawSettings.put(ClientSettings.SERVER_SETTING_PREFIX + name, ClientSettings.commaSeparated(values)); return this; } + + /** + * Sets the comment that will be added to the query log record associated with the query. + * @param logComment - comment to be added to the log + * @return same instance of the builder + */ + public InsertSettings logComment(String logComment) { + this.logComment = logComment; + if (logComment != null && !logComment.isEmpty()) { + rawSettings.put(ClientSettings.SERVER_SETTING_PREFIX + "log_comment", logComment); + } + return this; + } + + private String logComment = null; + + public String getLogComment() { + return logComment; + } } diff --git a/client-v2/src/main/java/com/clickhouse/client/api/query/QuerySettings.java b/client-v2/src/main/java/com/clickhouse/client/api/query/QuerySettings.java index 82471a548..01c68185d 100644 --- a/client-v2/src/main/java/com/clickhouse/client/api/query/QuerySettings.java +++ b/client-v2/src/main/java/com/clickhouse/client/api/query/QuerySettings.java @@ -221,4 +221,23 @@ public QuerySettings serverSetting(String name, Collection values) { rawSettings.put(ClientSettings.SERVER_SETTING_PREFIX + name, ClientSettings.commaSeparated(values)); return this; } + + /** + * Sets the comment that will be added to the query log record associated with the query. + * @param logComment - comment to be added to the log + * @return same instance of the builder + */ + public QuerySettings logComment(String logComment) { + this.logComment = logComment; + if (logComment != null && !logComment.isEmpty()) { + rawSettings.put(ClientSettings.SERVER_SETTING_PREFIX + "log_comment", logComment); + } + return this; + } + + private String logComment = null; + + public String getLogComment() { + return logComment; + } } diff --git a/client-v2/src/test/java/com/clickhouse/client/insert/InsertTests.java b/client-v2/src/test/java/com/clickhouse/client/insert/InsertTests.java index ae46a97ee..d2a2e1f9c 100644 --- a/client-v2/src/test/java/com/clickhouse/client/insert/InsertTests.java +++ b/client-v2/src/test/java/com/clickhouse/client/insert/InsertTests.java @@ -9,6 +9,7 @@ import com.clickhouse.client.ClickHouseProtocol; import com.clickhouse.client.api.Client; import com.clickhouse.client.api.ClientException; +import com.clickhouse.client.api.command.CommandResponse; import com.clickhouse.client.api.data_formats.ClickHouseBinaryFormatReader; import com.clickhouse.client.api.enums.Protocol; import com.clickhouse.client.api.insert.InsertResponse; @@ -18,6 +19,7 @@ import com.clickhouse.client.api.metrics.ServerMetrics; import com.clickhouse.client.api.query.GenericRecord; import com.clickhouse.client.api.query.QueryResponse; +import com.clickhouse.client.api.query.QuerySettings; import com.clickhouse.client.config.ClickHouseClientOption; import com.clickhouse.data.ClickHouseFormat; import com.github.tomakehurst.wiremock.WireMockServer; @@ -257,4 +259,32 @@ public void testInsertMetricsOperationId() throws Exception { assertEquals(metrics.getQueryId(), settings.getQueryId()); assertTrue(metrics.getMetric(ClientMetrics.OP_DURATION).getLong() > 0); } + + @Test(groups = {"integration"}) + public void testLogComment() throws Exception { + + String logComment = "Test log comment"; + InsertSettings settings = new InsertSettings() + .setQueryId(UUID.randomUUID().toString()) + .logComment(logComment); + + final String tableName = "single_pojo_table"; + final String createSQL = SamplePOJO.generateTableCreateSQL(tableName); + final SamplePOJO pojo = new SamplePOJO(); + + dropTable(tableName); + createTable(createSQL); + client.register(SamplePOJO.class, client.getTableSchema(tableName, "default")); + + try (InsertResponse response = client.insert(tableName, Collections.singletonList(pojo), settings).get(30, TimeUnit.SECONDS)) { + Assert.assertEquals(response.getWrittenRows(), 1); + } + + try (CommandResponse resp = client.execute("SYSTEM FLUSH LOGS").get()) { + } + + List logRecords = client.queryAll("SELECT query_id, log_comment FROM system.query_log WHERE query_id = '" + settings.getQueryId() + "'"); + Assert.assertEquals(logRecords.get(0).getString("query_id"), settings.getQueryId()); + Assert.assertEquals(logRecords.get(0).getString("log_comment"), logComment); + } } diff --git a/client-v2/src/test/java/com/clickhouse/client/query/QueryTests.java b/client-v2/src/test/java/com/clickhouse/client/query/QueryTests.java index ae8baf129..f8cc18a72 100644 --- a/client-v2/src/test/java/com/clickhouse/client/query/QueryTests.java +++ b/client-v2/src/test/java/com/clickhouse/client/query/QueryTests.java @@ -14,6 +14,7 @@ import com.clickhouse.client.api.ClientException; import com.clickhouse.client.api.DataTypeUtils; import com.clickhouse.client.api.ServerException; +import com.clickhouse.client.api.command.CommandResponse; import com.clickhouse.client.api.data_formats.ClickHouseBinaryFormatReader; import com.clickhouse.client.api.enums.Protocol; import com.clickhouse.client.api.insert.InsertSettings; @@ -1571,6 +1572,26 @@ public static BigDecimal cropDecimal(BigDecimal value, int scale) { } + @Test(groups = {"integration"}) + public void testLogComment() throws Exception { + + String logComment = "Test log comment"; + QuerySettings settings = new QuerySettings() + .setQueryId(UUID.randomUUID().toString()) + .logComment(logComment); + try (QueryResponse response = client.query("SELECT 1", settings).get()) { + Assert.assertNotNull(response.getQueryId()); + Assert.assertTrue(response.getQueryId().startsWith(settings.getQueryId())); + } + + try (CommandResponse resp = client.execute("SYSTEM FLUSH LOGS").get()) { + } + + List logRecords = client.queryAll("SELECT query_id, log_comment FROM system.query_log WHERE query_id = '" + settings.getQueryId() + "'"); + Assert.assertEquals(logRecords.get(0).getString("query_id"), settings.getQueryId()); + Assert.assertEquals(logRecords.get(0).getString("log_comment"), logComment); + } + protected Client.Builder newClient() { ClickHouseNode node = getServer(ClickHouseProtocol.HTTP); return new Client.Builder() From 082ae714514be04ad31745cf4f39a5431df51e6e Mon Sep 17 00:00:00 2001 From: Sergey Chernov Date: Mon, 28 Oct 2024 15:50:20 -0700 Subject: [PATCH 2/4] added test data provider for insert test --- .../api/internal/HttpAPIClientHelper.java | 7 ----- .../clickhouse/client/insert/InsertTests.java | 29 +++++++++++-------- 2 files changed, 17 insertions(+), 19 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 cf7162eb4..1dcdf6fe7 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 @@ -54,11 +54,8 @@ import javax.net.ssl.SSLContext; import javax.net.ssl.SSLException; -import java.io.BufferedReader; -import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; -import java.io.InputStreamReader; import java.io.OutputStream; import java.net.ConnectException; import java.net.InetSocketAddress; @@ -66,15 +63,11 @@ import java.net.URI; import java.net.URISyntaxException; import java.net.UnknownHostException; -import java.nio.ByteBuffer; import java.nio.charset.StandardCharsets; import java.security.NoSuchAlgorithmException; import java.util.Base64; -import java.util.EnumSet; -import java.util.HashSet; import java.util.Map; import java.util.Set; -import java.util.StringTokenizer; import java.util.concurrent.TimeUnit; import java.util.function.Function; diff --git a/client-v2/src/test/java/com/clickhouse/client/insert/InsertTests.java b/client-v2/src/test/java/com/clickhouse/client/insert/InsertTests.java index d2a2e1f9c..3f5f59b24 100644 --- a/client-v2/src/test/java/com/clickhouse/client/insert/InsertTests.java +++ b/client-v2/src/test/java/com/clickhouse/client/insert/InsertTests.java @@ -19,18 +19,12 @@ import com.clickhouse.client.api.metrics.ServerMetrics; import com.clickhouse.client.api.query.GenericRecord; import com.clickhouse.client.api.query.QueryResponse; -import com.clickhouse.client.api.query.QuerySettings; -import com.clickhouse.client.config.ClickHouseClientOption; import com.clickhouse.data.ClickHouseFormat; -import com.github.tomakehurst.wiremock.WireMockServer; -import com.github.tomakehurst.wiremock.client.WireMock; -import com.github.tomakehurst.wiremock.common.ConsoleNotifier; -import com.github.tomakehurst.wiremock.core.WireMockConfiguration; -import com.github.tomakehurst.wiremock.http.Fault; import org.testcontainers.shaded.org.apache.commons.lang3.RandomStringUtils; import org.testng.Assert; import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeMethod; +import org.testng.annotations.DataProvider; import org.testng.annotations.Test; import java.io.ByteArrayInputStream; @@ -43,7 +37,6 @@ import java.util.UUID; import java.util.concurrent.TimeUnit; -import static com.github.tomakehurst.wiremock.stubbing.Scenario.STARTED; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertTrue; import static org.testng.Assert.fail; @@ -260,10 +253,9 @@ public void testInsertMetricsOperationId() throws Exception { assertTrue(metrics.getMetric(ClientMetrics.OP_DURATION).getLong() > 0); } - @Test(groups = {"integration"}) - public void testLogComment() throws Exception { + @Test(groups = {"integration"}, dataProviderClass = InsertTests.class, dataProvider = "logCommentDataProvider") + public void testLogComment(String logComment) throws Exception { - String logComment = "Test log comment"; InsertSettings settings = new InsertSettings() .setQueryId(UUID.randomUUID().toString()) .logComment(logComment); @@ -285,6 +277,19 @@ public void testLogComment() throws Exception { List logRecords = client.queryAll("SELECT query_id, log_comment FROM system.query_log WHERE query_id = '" + settings.getQueryId() + "'"); Assert.assertEquals(logRecords.get(0).getString("query_id"), settings.getQueryId()); - Assert.assertEquals(logRecords.get(0).getString("log_comment"), logComment); + Assert.assertEquals(logRecords.get(0).getString("log_comment"), logComment == null ? "" : logComment); + } + + @DataProvider( name = "logCommentDataProvider") + public static Object[] logCommentDataProvider() { + return new Object[][] { + { "Test log comment" }, + { "Another log comment?" }, + { "Log comment with special characters: !@#$%^&*()" }, + { "Log comment with unicode: 你好" }, + { "", }, + { " "}, + { null } + }; } } From 1d5ad3bd238a325d815e64d18823fb0f83861357 Mon Sep 17 00:00:00 2001 From: Sergey Chernov Date: Mon, 28 Oct 2024 16:01:23 -0700 Subject: [PATCH 3/4] a small fix --- .../com/clickhouse/client/api/internal/HttpAPIClientHelper.java | 2 -- 1 file changed, 2 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 63ee75d3c..fee496199 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 @@ -68,8 +68,6 @@ import java.util.Base64; import java.util.Collection; import java.util.Collections; -import java.util.EnumSet; -import java.util.HashSet; import java.util.Map; import java.util.Set; import java.util.concurrent.TimeUnit; From a5b32774f62ca66e31d21686e2e6a11f7ca1348f Mon Sep 17 00:00:00 2001 From: Sergey Chernov Date: Tue, 29 Oct 2024 09:24:36 -0700 Subject: [PATCH 4/4] used same constant --- .../src/main/java/com/clickhouse/client/api/ClientSettings.java | 2 ++ .../java/com/clickhouse/client/api/insert/InsertSettings.java | 2 +- .../java/com/clickhouse/client/api/query/QuerySettings.java | 2 +- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/client-v2/src/main/java/com/clickhouse/client/api/ClientSettings.java b/client-v2/src/main/java/com/clickhouse/client/api/ClientSettings.java index ea30b74b6..6728b8278 100644 --- a/client-v2/src/main/java/com/clickhouse/client/api/ClientSettings.java +++ b/client-v2/src/main/java/com/clickhouse/client/api/ClientSettings.java @@ -35,4 +35,6 @@ public static List valuesFromCommaSeparated(String value) { } public static final String SESSION_DB_ROLES = "session_db_roles"; + + public static final String SETTING_LOG_COMMENT = SERVER_SETTING_PREFIX + "log_comment"; } diff --git a/client-v2/src/main/java/com/clickhouse/client/api/insert/InsertSettings.java b/client-v2/src/main/java/com/clickhouse/client/api/insert/InsertSettings.java index 34ab82a28..ba1b036f3 100644 --- a/client-v2/src/main/java/com/clickhouse/client/api/insert/InsertSettings.java +++ b/client-v2/src/main/java/com/clickhouse/client/api/insert/InsertSettings.java @@ -233,7 +233,7 @@ public Collection getDBRoles() { public InsertSettings logComment(String logComment) { this.logComment = logComment; if (logComment != null && !logComment.isEmpty()) { - rawSettings.put(ClientSettings.SERVER_SETTING_PREFIX + "log_comment", logComment); + rawSettings.put(ClientSettings.SETTING_LOG_COMMENT, logComment); } return this; } diff --git a/client-v2/src/main/java/com/clickhouse/client/api/query/QuerySettings.java b/client-v2/src/main/java/com/clickhouse/client/api/query/QuerySettings.java index 7c925e8fe..22b62cfd6 100644 --- a/client-v2/src/main/java/com/clickhouse/client/api/query/QuerySettings.java +++ b/client-v2/src/main/java/com/clickhouse/client/api/query/QuerySettings.java @@ -249,7 +249,7 @@ public Collection getDBRoles() { public QuerySettings logComment(String logComment) { this.logComment = logComment; if (logComment != null && !logComment.isEmpty()) { - rawSettings.put(ClientSettings.SERVER_SETTING_PREFIX + "log_comment", logComment); + rawSettings.put(ClientSettings.SETTING_LOG_COMMENT, logComment); } return this; }