Skip to content

Commit 038c202

Browse files
committed
Add tests for unusual String parameters
1 parent 8783918 commit 038c202

File tree

3 files changed

+54
-8
lines changed

3 files changed

+54
-8
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2027,6 +2027,7 @@ protected int getOperationTimeout() {
20272027
* @return - set of endpoints
20282028
* @deprecated
20292029
*/
2030+
@Deprecated
20302031
public Set<String> getEndpoints() {
20312032
return endpoints.stream().map(Endpoint::getBaseURL).collect(Collectors.toSet());
20322033
}
@@ -2108,4 +2109,5 @@ private Map<String, String> formatQueryParameters(Map<String, Object> queryParam
21082109
}
21092110
return newMap;
21102111
}
2112+
21112113
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public static String format(Object object, ClickHouseDataType dataTypeHint,
103103
if (object instanceof Instant) {
104104
return formatInstant((Instant) object, dataTypeHint, timeZone);
105105
}
106-
return String.valueOf(object);
106+
return escapeString(String.valueOf(object));
107107
}
108108

109109
private static String formatInstant(Instant instant, ClickHouseDataType dataTypeHint,
@@ -137,4 +137,8 @@ private static String formatInstantDefault(Instant instant) {
137137
return String.valueOf(instant.getEpochSecond()) + "." + new String(n);
138138
}
139139

140+
private static String escapeString(String x) {
141+
return x.replace("\\", "\\\\").replace("'", "\\'");
142+
}
143+
140144
}

client-v2/src/test/java/com/clickhouse/client/e2e/ParameterizedQueryTest.java

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.testng.Assert;
2424
import org.testng.annotations.AfterMethod;
2525
import org.testng.annotations.BeforeMethod;
26+
import org.testng.annotations.DataProvider;
2627
import org.testng.annotations.Test;
2728

2829
import com.clickhouse.client.BaseIntegrationTest;
@@ -232,6 +233,31 @@ void testParamsWithInstant() throws Exception {
232233
}
233234
}
234235

236+
@Test(dataProvider = "stringParameters")
237+
void testStringParams(String paramValue) throws Exception {
238+
String table = "test_params_unicode";
239+
String column = "val";
240+
client.execute("DROP TABLE IF EXISTS " + table).get();
241+
client.execute("CREATE TABLE " + table + "(" + column + " String) Engine = Memory").get();
242+
client.query(
243+
"INSERT INTO " + table + "(" + column + ") VALUES ('" + paramValue + "')").get();
244+
try (QueryResponse r = client.query(
245+
"SELECT " + column + " FROM " + table + " WHERE " + column + "='" + paramValue + "'").get();
246+
ClickHouseBinaryFormatReader reader = client.newBinaryFormatReader(r))
247+
{
248+
reader.next();
249+
Assert.assertEquals(reader.getString(1), paramValue);
250+
}
251+
try (QueryResponse r = client.query(
252+
"SELECT " + column + " FROM " + table + " WHERE " + column + "={x:String}",
253+
Collections.singletonMap("x", paramValue)).get();
254+
ClickHouseBinaryFormatReader reader = client.newBinaryFormatReader(r))
255+
{
256+
reader.next();
257+
Assert.assertEquals(reader.getString(1), paramValue);
258+
}
259+
}
260+
235261
private int[] queryInstant(String tableName, String fieldName, String operator,
236262
Instant param, int scale) throws InterruptedException, ExecutionException, Exception
237263
{
@@ -255,13 +281,13 @@ private Client.Builder newClient() {
255281
ClickHouseNode node = getServer(ClickHouseProtocol.HTTP);
256282
boolean isSecure = isCloud();
257283
return new Client.Builder()
258-
.addEndpoint(Protocol.HTTP, node.getHost(), node.getPort(), isSecure)
259-
.setUsername("default")
260-
.setPassword(ClickHouseServerForTest.getPassword())
261-
.compressClientRequest(false)
262-
.setDefaultDatabase(ClickHouseServerForTest.getDatabase())
263-
.serverSetting(ServerSettings.WAIT_ASYNC_INSERT, "1")
264-
.serverSetting(ServerSettings.ASYNC_INSERT, "0");
284+
.addEndpoint(Protocol.HTTP, node.getHost(), node.getPort(), isSecure)
285+
.setUsername("default")
286+
.setPassword(ClickHouseServerForTest.getPassword())
287+
.compressClientRequest(false)
288+
.setDefaultDatabase(ClickHouseServerForTest.getDatabase())
289+
.serverSetting(ServerSettings.WAIT_ASYNC_INSERT, "1")
290+
.serverSetting(ServerSettings.ASYNC_INSERT, "0");
265291
}
266292

267293
private void prepareDataSet(String table, List<String> columns, List<Supplier<Object>> valueGenerators,
@@ -319,4 +345,18 @@ private Map<String, Object> writeValuesRow(StringBuilder insertStmtBuilder, List
319345
return values;
320346
}
321347

348+
@DataProvider(name = "stringParameters")
349+
private static Object[][] createStringParameterValues() {
350+
return new Object[][] {
351+
{ "foo" },
352+
{ "with-dashes" },
353+
{ "☺" },
354+
{ "foo/bar" },
355+
{ "foobar 20" },
356+
{ " leading_and_trailing_spaces " },
357+
{ "multi\nline\r\ndos" },
358+
{ "nicely\"quoted\'string\'" },
359+
};
360+
}
361+
322362
}

0 commit comments

Comments
 (0)