Skip to content

Commit 9917449

Browse files
committed
added more tests for strings with escape symbols. fixed test for older version
1 parent 195f3f9 commit 9917449

File tree

5 files changed

+70
-13
lines changed

5 files changed

+70
-13
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import com.clickhouse.data.ClickHouseFormat;
4242
import org.apache.hc.core5.concurrent.DefaultThreadFactory;
4343
import org.apache.hc.core5.http.ClassicHttpResponse;
44+
import org.apache.hc.core5.http.Header;
4445
import org.apache.hc.core5.http.HttpHeaders;
4546
import org.apache.hc.core5.http.HttpStatus;
4647
import org.slf4j.Logger;
@@ -1639,8 +1640,13 @@ public CompletableFuture<QueryResponse> query(String sqlQuery, Map<String, Objec
16391640
.getFirstHeader(ClickHouseHttpProto.HEADER_QUERY_ID), finalSettings.getQueryId());
16401641
metrics.setQueryId(queryId);
16411642
metrics.operationComplete();
1643+
Header formatHeader = httpResponse.getFirstHeader(ClickHouseHttpProto.HEADER_FORMAT);
1644+
ClickHouseFormat responseFormat = finalSettings.getFormat();
1645+
if (formatHeader != null) {
1646+
responseFormat = ClickHouseFormat.valueOf(formatHeader.getValue());
1647+
}
16421648

1643-
return new QueryResponse(httpResponse, finalSettings.getFormat(), finalSettings, metrics);
1649+
return new QueryResponse(httpResponse, responseFormat, finalSettings, metrics);
16441650

16451651
} catch (Exception e) {
16461652
lastException = httpClientHelper.wrapException(String.format("Query request failed (Attempt: %s/%s - Duration: %s)",

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

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import com.fasterxml.jackson.databind.JsonNode;
3535
import com.fasterxml.jackson.databind.MappingIterator;
3636
import com.fasterxml.jackson.databind.ObjectMapper;
37+
import com.google.common.io.BaseEncoding;
3738
import org.apache.commons.lang3.RandomStringUtils;
3839
import org.apache.commons.lang3.StringEscapeUtils;
3940
import org.testng.Assert;
@@ -55,6 +56,7 @@
5556
import java.net.Inet4Address;
5657
import java.net.Inet6Address;
5758
import java.net.InetAddress;
59+
import java.nio.ByteBuffer;
5860
import java.nio.charset.StandardCharsets;
5961
import java.time.LocalDate;
6062
import java.time.LocalDateTime;
@@ -487,7 +489,8 @@ record = reader.next();
487489
"col1 Array(UInt32)",
488490
"col2 Array(Array(Int32))",
489491
"col3 Array(UInt64)",
490-
"col4 Array(Bool)"
492+
"col4 Array(Bool)",
493+
"col5 Array(String)"
491494
);
492495

493496
private final static List<Function<String, Object>> ARRAY_VALUE_GENERATORS = Arrays.asList(
@@ -506,8 +509,17 @@ record = reader.next();
506509
RANDOM.longs(10, 0, Long.MAX_VALUE)
507510
.mapToObj(BigInteger::valueOf).collect(Collectors.toList()),
508511
c -> RANDOM.ints(10, 0, 1)
509-
.mapToObj(i -> i == 0 ).collect(Collectors.toList())
510-
512+
.mapToObj(i -> i == 0 ).collect(Collectors.toList()),
513+
c -> {
514+
UUID uuid = UUID.randomUUID();
515+
byte[] bts = ByteBuffer.allocate(16)
516+
.putLong(uuid.getMostSignificantBits())
517+
.putLong(uuid.getLeastSignificantBits())
518+
.array();
519+
String sep = "\\x";
520+
String hex = sep + BaseEncoding.base16().withSeparator(sep, 2).encode(bts);
521+
return Arrays.asList(hex);
522+
}
511523
);
512524

513525
@Test(groups = {"integration"})
@@ -547,6 +559,7 @@ public void testArrayValues() throws Exception {
547559
Assert.assertEquals(col4Values, data.get(0).get("col4"));
548560
boolean[] col4Array = reader.getBooleanArray("col4");
549561
Assert.assertEquals(col4Array, ((List)data.get(0).get("col4")).toArray());
562+
Assert.assertEquals(reader.getList("col5"), ((List)data.get(0).get("col5")));
550563
}
551564

552565
@Test
@@ -1453,6 +1466,13 @@ private Map<String, Object> writeValuesRow(StringBuilder insertStmtBuilder, List
14531466
}
14541467
insertStmtBuilder.setLength(insertStmtBuilder.length() - 2);
14551468
insertStmtBuilder.append("}, ");
1469+
} else if (value instanceof List) {
1470+
insertStmtBuilder.append("[");
1471+
for (Object item : (List)value) {
1472+
insertStmtBuilder.append(quoteValue(item)).append(", ");
1473+
}
1474+
insertStmtBuilder.setLength(insertStmtBuilder.length() - 2);
1475+
insertStmtBuilder.append("], ");
14561476
} else {
14571477
insertStmtBuilder.append(value).append(", ");
14581478
}
@@ -1464,7 +1484,9 @@ private Map<String, Object> writeValuesRow(StringBuilder insertStmtBuilder, List
14641484

14651485
private String quoteValue(Object value) {
14661486
if (value instanceof String) {
1467-
return '\'' + value.toString() + '\'';
1487+
String strVal = (String)value;
1488+
1489+
return '\'' + strVal.replaceAll("\\\\", "\\\\\\\\") + '\'';
14681490
}
14691491
return value.toString();
14701492
}
@@ -2021,9 +2043,17 @@ public void testLowCardinalityValues() throws Exception {
20212043

20222044
@Test(groups = {"integration"})
20232045
public void testGettingRowsBeforeLimit() throws Exception {
2046+
int expectedTotalRowsToRead = 100;
2047+
List<GenericRecord> serverVersion = client.queryAll("SELECT version()");
2048+
if (ClickHouseVersion.of(serverVersion.get(0).getString(1)).check("(,24.8]")) {
2049+
// issue in prev. release.
2050+
expectedTotalRowsToRead = 0;
2051+
}
2052+
20242053
try (QueryResponse response = client.query("SELECT number FROM system.numbers LIMIT 100").get()) {
20252054
Assert.assertTrue(response.getResultRows() < 1000);
2026-
Assert.assertEquals(response.getTotalRowsToRead(), 100);
2055+
2056+
Assert.assertEquals(response.getTotalRowsToRead(), expectedTotalRowsToRead);
20272057
}
20282058
}
20292059
}

jdbc-v2/src/main/java/com/clickhouse/jdbc/StatementImpl.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,11 @@ public ResultSetImpl executeQuery(String sql, QuerySettings settings) throws SQL
182182
} else {
183183
response = connection.client.query(lastSql, mergedSettings).get(queryTimeout, TimeUnit.SECONDS);
184184
}
185+
186+
if (response.getFormat().isText()) {
187+
throw new SQLException("Only RowBinaryWithNameAndTypes is supported for output format. Please check your query.",
188+
ExceptionUtils.SQL_STATE_CLIENT_ERROR);
189+
}
185190
ClickHouseBinaryFormatReader reader = connection.client.newBinaryFormatReader(response);
186191

187192
currentResultSet = new ResultSetImpl(this, response, reader);
@@ -601,4 +606,12 @@ public String enquoteNCharLiteral(String val) throws SQLException {
601606
checkClosed();
602607
return Statement.super.enquoteNCharLiteral(val);
603608
}
609+
610+
/**
611+
* Return query ID of last executed statement. It is not guaranteed when statements is used concurrently.
612+
* @return query ID
613+
*/
614+
public String getLastQueryId() {
615+
return lastQueryId;
616+
}
604617
}

jdbc-v2/src/test/java/com/clickhouse/jdbc/DataTypeTests.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.clickhouse.jdbc;
22

3-
import com.clickhouse.client.api.ClientConfigProperties;
43
import com.clickhouse.data.Tuple;
54
import org.slf4j.Logger;
65
import org.slf4j.LoggerFactory;
@@ -11,7 +10,6 @@
1110
import java.math.BigInteger;
1211
import java.sql.Connection;
1312
import java.sql.Date;
14-
import java.sql.DriverManager;
1513
import java.sql.JDBCType;
1614
import java.sql.PreparedStatement;
1715
import java.sql.ResultSet;
@@ -26,7 +24,6 @@
2624
import java.util.GregorianCalendar;
2725
import java.util.HashMap;
2826
import java.util.Map;
29-
import java.util.Properties;
3027
import java.util.Random;
3128
import java.util.TimeZone;
3229
import java.util.UUID;
@@ -318,7 +315,8 @@ public void testStringTypes() throws SQLException {
318315
runQuery("CREATE TABLE test_strings (order Int8, "
319316
+ "str String, fixed FixedString(6), "
320317
+ "enum Enum8('a' = 6, 'b' = 7, 'c' = 8), enum8 Enum8('a' = 1, 'b' = 2, 'c' = 3), enum16 Enum16('a' = 1, 'b' = 2, 'c' = 3), "
321-
+ "uuid UUID, ipv4 IPv4, ipv6 IPv6"
318+
+ "uuid UUID, ipv4 IPv4, ipv6 IPv6, "
319+
+ "escaped String "
322320
+ ") ENGINE = MergeTree ORDER BY ()");
323321

324322
// Insert random (valid) values
@@ -333,10 +331,10 @@ public void testStringTypes() throws SQLException {
333331
String uuid = UUID.randomUUID().toString();
334332
String ipv4 = rand.nextInt(256) + "." + rand.nextInt(256) + "." + rand.nextInt(256) + "." + rand.nextInt(256);
335333
String ipv6 = "2001:adb8:85a3:1:2:8a2e:370:7334";
336-
334+
String escaped = "\\xA3\\xA3\\x12\\xA0\\xDF\\x13\\x4E\\x8C\\x87\\x74\\xD4\\x53\\xDB\\xFC\\x34\\x95";
337335

338336
try (Connection conn = getConnection()) {
339-
try (PreparedStatement stmt = conn.prepareStatement("INSERT INTO test_strings VALUES ( 1, ?, ?, ?, ?, ?, ?, ?, ? )")) {
337+
try (PreparedStatement stmt = conn.prepareStatement("INSERT INTO test_strings VALUES ( 1, ?, ?, ?, ?, ?, ?, ?, ?, ? )")) {
340338
stmt.setString(1, str);
341339
stmt.setString(2, fixed);
342340
stmt.setString(3, enum8);
@@ -345,6 +343,7 @@ public void testStringTypes() throws SQLException {
345343
stmt.setString(6, uuid);
346344
stmt.setString(7, ipv4);
347345
stmt.setString(8, ipv6);
346+
stmt.setString(9, escaped);
348347
stmt.executeUpdate();
349348
}
350349
}
@@ -365,7 +364,7 @@ public void testStringTypes() throws SQLException {
365364
assertEquals(rs.getString("uuid"), uuid);
366365
assertEquals(rs.getString("ipv4"), "/" + ipv4);
367366
assertEquals(rs.getString("ipv6"), "/" + ipv6);
368-
367+
assertEquals(rs.getString("escaped"), escaped);
369368
assertFalse(rs.next());
370369
}
371370
}

jdbc-v2/src/test/java/com/clickhouse/jdbc/StatementTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public void testExecuteQuerySimpleNumbers() throws Exception {
5757
assertEquals(rs.getLong("num"), 1);
5858
assertFalse(rs.next());
5959
}
60+
Assert.assertFalse(((StatementImpl)stmt).getLastQueryId().isEmpty());
6061
}
6162
}
6263
}
@@ -530,4 +531,12 @@ public void testConcurrentCancel() throws Exception {
530531
}
531532
}
532533
}
534+
535+
@Test(groups = {"integration"})
536+
public void testTextFormatInResponse() throws Exception {
537+
try (Connection conn = getJdbcConnection();
538+
Statement stmt = conn.createStatement()) {
539+
Assert.expectThrows(SQLException.class, () ->stmt.executeQuery("SELECT 1 FORMAT JSON"));
540+
}
541+
}
533542
}

0 commit comments

Comments
 (0)