Skip to content

Commit db1c498

Browse files
authored
Merge pull request #1864 from ClickHouse/v2_log_comment
[client-v2] Added setting log_comment
2 parents 3b831cd + a5b3277 commit db1c498

File tree

6 files changed

+102
-14
lines changed

6 files changed

+102
-14
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,6 @@ public static List<String> valuesFromCommaSeparated(String value) {
3535
}
3636

3737
public static final String SESSION_DB_ROLES = "session_db_roles";
38+
39+
public static final String SETTING_LOG_COMMENT = SERVER_SETTING_PREFIX + "log_comment";
3840
}

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,4 +224,23 @@ public InsertSettings setDBRoles(Collection<String> dbRoles) {
224224
public Collection<String> getDBRoles() {
225225
return (Collection<String>) rawSettings.get(ClientSettings.SESSION_DB_ROLES);
226226
}
227+
228+
/**
229+
* Sets the comment that will be added to the query log record associated with the query.
230+
* @param logComment - comment to be added to the log
231+
* @return same instance of the builder
232+
*/
233+
public InsertSettings logComment(String logComment) {
234+
this.logComment = logComment;
235+
if (logComment != null && !logComment.isEmpty()) {
236+
rawSettings.put(ClientSettings.SETTING_LOG_COMMENT, logComment);
237+
}
238+
return this;
239+
}
240+
241+
private String logComment = null;
242+
243+
public String getLogComment() {
244+
return logComment;
245+
}
227246
}

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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,29 +54,22 @@
5454

5555
import javax.net.ssl.SSLContext;
5656
import javax.net.ssl.SSLException;
57-
import java.io.BufferedReader;
58-
import java.io.ByteArrayOutputStream;
5957
import java.io.IOException;
6058
import java.io.InputStream;
61-
import java.io.InputStreamReader;
6259
import java.io.OutputStream;
6360
import java.net.ConnectException;
6461
import java.net.InetSocketAddress;
6562
import java.net.NoRouteToHostException;
6663
import java.net.URI;
6764
import java.net.URISyntaxException;
6865
import java.net.UnknownHostException;
69-
import java.nio.ByteBuffer;
7066
import java.nio.charset.StandardCharsets;
7167
import java.security.NoSuchAlgorithmException;
7268
import java.util.Base64;
7369
import java.util.Collection;
7470
import java.util.Collections;
75-
import java.util.EnumSet;
76-
import java.util.HashSet;
7771
import java.util.Map;
7872
import java.util.Set;
79-
import java.util.StringTokenizer;
8073
import java.util.concurrent.TimeUnit;
8174
import java.util.function.Function;
8275

client-v2/src/main/java/com/clickhouse/client/api/query/QuerySettings.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,4 +240,23 @@ public QuerySettings setDBRoles(Collection<String> dbRoles) {
240240
public Collection<String> getDBRoles() {
241241
return (Collection<String>) rawSettings.get(ClientSettings.SESSION_DB_ROLES);
242242
}
243+
244+
/**
245+
* Sets the comment that will be added to the query log record associated with the query.
246+
* @param logComment - comment to be added to the log
247+
* @return same instance of the builder
248+
*/
249+
public QuerySettings logComment(String logComment) {
250+
this.logComment = logComment;
251+
if (logComment != null && !logComment.isEmpty()) {
252+
rawSettings.put(ClientSettings.SETTING_LOG_COMMENT, logComment);
253+
}
254+
return this;
255+
}
256+
257+
private String logComment = null;
258+
259+
public String getLogComment() {
260+
return logComment;
261+
}
243262
}

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

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import com.clickhouse.client.ClickHouseProtocol;
1010
import com.clickhouse.client.api.Client;
1111
import com.clickhouse.client.api.ClientException;
12+
import com.clickhouse.client.api.command.CommandResponse;
1213
import com.clickhouse.client.api.data_formats.ClickHouseBinaryFormatReader;
1314
import com.clickhouse.client.api.enums.Protocol;
1415
import com.clickhouse.client.api.insert.InsertResponse;
@@ -18,17 +19,12 @@
1819
import com.clickhouse.client.api.metrics.ServerMetrics;
1920
import com.clickhouse.client.api.query.GenericRecord;
2021
import com.clickhouse.client.api.query.QueryResponse;
21-
import com.clickhouse.client.config.ClickHouseClientOption;
2222
import com.clickhouse.data.ClickHouseFormat;
23-
import com.github.tomakehurst.wiremock.WireMockServer;
24-
import com.github.tomakehurst.wiremock.client.WireMock;
25-
import com.github.tomakehurst.wiremock.common.ConsoleNotifier;
26-
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
27-
import com.github.tomakehurst.wiremock.http.Fault;
2823
import org.testcontainers.shaded.org.apache.commons.lang3.RandomStringUtils;
2924
import org.testng.Assert;
3025
import org.testng.annotations.AfterMethod;
3126
import org.testng.annotations.BeforeMethod;
27+
import org.testng.annotations.DataProvider;
3228
import org.testng.annotations.Test;
3329

3430
import java.io.ByteArrayInputStream;
@@ -41,7 +37,6 @@
4137
import java.util.UUID;
4238
import java.util.concurrent.TimeUnit;
4339

44-
import static com.github.tomakehurst.wiremock.stubbing.Scenario.STARTED;
4540
import static org.testng.Assert.assertEquals;
4641
import static org.testng.Assert.assertTrue;
4742
import static org.testng.Assert.fail;
@@ -294,4 +289,44 @@ public void testInsertSettingsAddDatabase() throws Exception {
294289
List<GenericRecord> records = client.queryAll("SELECT * FROM " + new_database + "." + tableName);
295290
assertEquals(records.size(), 1000);
296291
}
292+
293+
@Test(groups = {"integration"}, dataProviderClass = InsertTests.class, dataProvider = "logCommentDataProvider")
294+
public void testLogComment(String logComment) throws Exception {
295+
296+
InsertSettings settings = new InsertSettings()
297+
.setQueryId(UUID.randomUUID().toString())
298+
.logComment(logComment);
299+
300+
final String tableName = "single_pojo_table";
301+
final String createSQL = SamplePOJO.generateTableCreateSQL(tableName);
302+
final SamplePOJO pojo = new SamplePOJO();
303+
304+
dropTable(tableName);
305+
createTable(createSQL);
306+
client.register(SamplePOJO.class, client.getTableSchema(tableName, "default"));
307+
308+
try (InsertResponse response = client.insert(tableName, Collections.singletonList(pojo), settings).get(30, TimeUnit.SECONDS)) {
309+
Assert.assertEquals(response.getWrittenRows(), 1);
310+
}
311+
312+
try (CommandResponse resp = client.execute("SYSTEM FLUSH LOGS").get()) {
313+
}
314+
315+
List<GenericRecord> logRecords = client.queryAll("SELECT query_id, log_comment FROM system.query_log WHERE query_id = '" + settings.getQueryId() + "'");
316+
Assert.assertEquals(logRecords.get(0).getString("query_id"), settings.getQueryId());
317+
Assert.assertEquals(logRecords.get(0).getString("log_comment"), logComment == null ? "" : logComment);
318+
}
319+
320+
@DataProvider( name = "logCommentDataProvider")
321+
public static Object[] logCommentDataProvider() {
322+
return new Object[][] {
323+
{ "Test log comment" },
324+
{ "Another log comment?" },
325+
{ "Log comment with special characters: !@#$%^&*()" },
326+
{ "Log comment with unicode: 你好" },
327+
{ "", },
328+
{ " "},
329+
{ null }
330+
};
331+
}
297332
}

client-v2/src/test/java/com/clickhouse/client/query/QueryTests.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1743,6 +1743,26 @@ public void testClientCustomRoles(String[] roles) throws Exception {
17431743
}
17441744

17451745

1746+
@Test(groups = {"integration"})
1747+
public void testLogComment() throws Exception {
1748+
1749+
String logComment = "Test log comment";
1750+
QuerySettings settings = new QuerySettings()
1751+
.setQueryId(UUID.randomUUID().toString())
1752+
.logComment(logComment);
1753+
try (QueryResponse response = client.query("SELECT 1", settings).get()) {
1754+
Assert.assertNotNull(response.getQueryId());
1755+
Assert.assertTrue(response.getQueryId().startsWith(settings.getQueryId()));
1756+
}
1757+
1758+
try (CommandResponse resp = client.execute("SYSTEM FLUSH LOGS").get()) {
1759+
}
1760+
1761+
List<GenericRecord> logRecords = client.queryAll("SELECT query_id, log_comment FROM system.query_log WHERE query_id = '" + settings.getQueryId() + "'");
1762+
Assert.assertEquals(logRecords.get(0).getString("query_id"), settings.getQueryId());
1763+
Assert.assertEquals(logRecords.get(0).getString("log_comment"), logComment);
1764+
}
1765+
17461766
protected Client.Builder newClient() {
17471767
ClickHouseNode node = getServer(ClickHouseProtocol.HTTP);
17481768
return new Client.Builder()

0 commit comments

Comments
 (0)