Skip to content

Commit fa6d100

Browse files
mdonkerszhicwu
authored andcommitted
[bug ClickHouse#1389] Resolve processing Nested column type in combination with AggregateFunction
1 parent a9c7ac1 commit fa6d100

File tree

5 files changed

+33
-6
lines changed

5 files changed

+33
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
### Bug Fixes
1212
* Java client threw confusing error when query is invalid.
13+
* JDBC Driver correctly processes `AggregateFunction(Nested(...))` columns
1314

1415
## 0.4.6, 2023-05-02
1516
### New Features

clickhouse-data/src/main/java/com/clickhouse/data/ClickHouseUtils.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ public static boolean isSeparator(char ch) {
440440

441441
/**
442442
* Escape quotes in given string.
443-
*
443+
*
444444
* @param str string
445445
* @param quote quote to escape
446446
* @return escaped string
@@ -466,7 +466,7 @@ public static String escape(String str, char quote) {
466466

467467
/**
468468
* Unescape quoted string.
469-
*
469+
*
470470
* @param str quoted string
471471
* @return unescaped string
472472
*/
@@ -1460,7 +1460,7 @@ public static int readValueArray(String args, int startIndex, int len, Consumer<
14601460
}
14611461

14621462
public static int readParameters(String args, int startIndex, int len, List<String> params) {
1463-
char closeBracket = ')'; // startIndex points to the openning bracket
1463+
char closeBracket = ')'; // startIndex points to the opening bracket
14641464
Deque<Character> stack = new ArrayDeque<>();
14651465
StringBuilder builder = new StringBuilder();
14661466

@@ -1489,7 +1489,7 @@ public static int readParameters(String args, int startIndex, int len, List<Stri
14891489

14901490
for (int i = startIndex; i < len; i++) {
14911491
char ch = args.charAt(i);
1492-
if (Character.isWhitespace(ch)) {
1492+
if (Character.isWhitespace(ch) && stack.isEmpty()) {
14931493
continue;
14941494
} else if (isQuote(ch)) {
14951495
builder.append(ch);

clickhouse-data/src/test/java/com/clickhouse/data/ClickHouseColumnTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ public void testAggregationFunction() {
206206
Assert.assertTrue(column.isAggregateFunction());
207207
Assert.assertEquals(column.getDataType(), ClickHouseDataType.AggregateFunction);
208208
Assert.assertEquals(column.getAggregateFunction(), ClickHouseAggregateFunction.quantiles);
209-
Assert.assertEquals(column.getFunction(), "quantiles(0.5,0.9)");
209+
Assert.assertEquals(column.getFunction(), "quantiles(0.5, 0.9)");
210210
Assert.assertEquals(column.getNestedColumns(),
211211
Collections.singletonList(ClickHouseColumn.of("", "Nullable(UInt64)")));
212212
Assert.assertFalse(column.isFixedLength(), "Should not have fixed length in byte");

clickhouse-data/src/test/java/com/clickhouse/data/ClickHouseUtilsTest.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ public void testReadParameters() {
459459
List<String> params = new LinkedList<>();
460460
Assert.assertEquals(ClickHouseUtils.readParameters(args, args.indexOf('('), args.length(), params),
461461
args.lastIndexOf(')') + 1);
462-
Assert.assertEquals(params, Arrays.asList("quantiles(0.5,'c \\'''([1],2) d',0.9)", "UInt64"));
462+
Assert.assertEquals(params, Arrays.asList("quantiles(0.5, 'c \\'''([1],2) d',0.9)", "UInt64"));
463463

464464
params.clear();
465465
args = " ('a'/* a*/, 1-- test\n, b)";
@@ -470,6 +470,12 @@ public void testReadParameters() {
470470
args = " a, b c";
471471
Assert.assertEquals(ClickHouseUtils.readParameters(args, 0, args.length(), params), args.length());
472472
Assert.assertEquals(params, Arrays.asList("a", "bc"));
473+
474+
params.clear();
475+
args = "column1 SimpleAggregateFunction(anyLast, Nested(a string, b string))";
476+
Assert.assertEquals(ClickHouseUtils.readParameters(args, args.indexOf('('), args.length(), params),
477+
args.lastIndexOf(')') + 1);
478+
Assert.assertEquals(params, Arrays.asList("anyLast", "Nested(a string, b string)"));
473479
}
474480

475481
@Test(groups = { "unit" })

clickhouse-jdbc/src/test/java/com/clickhouse/jdbc/ClickHouseResultSetTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,26 @@ public void testTuple() throws SQLException {
295295
}
296296
}
297297

298+
@Test(groups = "integration")
299+
public void testNested() throws SQLException {
300+
try (ClickHouseConnection conn = newConnection(new Properties());
301+
Statement stmt = conn.createStatement()) {
302+
303+
stmt.execute("set flatten_nested=0; "
304+
+ "drop table if exists test_simple_aggregate_nested; "
305+
+ "create table test_simple_aggregate_nested(id Int8, n0 SimpleAggregateFunction(anyLast, Nested(a String,b String))) ENGINE = AggregatingMergeTree() ORDER BY (id); "
306+
+ "insert into test_simple_aggregate_nested values(1, [tuple('foo1', 'bar1'), tuple('foo11', 'bar11')]), (2, [tuple('foo2', 'bar2'), tuple('foo22', 'bar22')])");
307+
ResultSet rs = stmt
308+
.executeQuery(
309+
"select * from test_simple_aggregate_nested");
310+
Assert.assertTrue(rs.next());
311+
Assert.assertEquals(rs.getInt(1), 1);
312+
Map<?, ?> v = rs.getObject(2, Map.class);
313+
Assert.assertEquals(v.size(), 2);
314+
Assert.assertEquals(v.get("a"), new String[]{"foo1", "foo11"});
315+
}
316+
}
317+
298318
@Test(dataProvider = "nullableTypes", groups = "integration")
299319
public void testNullableValues(ClickHouseDataType type, Object value, BiFunction<ResultSet, Integer, Object> func)
300320
throws SQLException {

0 commit comments

Comments
 (0)