Skip to content

Commit 771441e

Browse files
committed
added sub-column logic to JSON columns. added test for JSON with different definition
1 parent f156664 commit 771441e

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ public final class ClickHouseColumn implements Serializable {
7272
private static final String KEYWORD_MAP = ClickHouseDataType.Map.name();
7373
private static final String KEYWORD_NESTED = ClickHouseDataType.Nested.name();
7474
private static final String KEYWORD_VARIANT = ClickHouseDataType.Variant.name();
75+
private static final String KEYWORD_JSON = ClickHouseDataType.JSON.name();
7576

7677
private int columnCount;
7778
private int columnIndex;
@@ -504,6 +505,21 @@ protected static int readColumn(String args, int startIndex, int len, String nam
504505
}
505506
}
506507
}
508+
} else if (args.startsWith(KEYWORD_JSON, i)) {
509+
int index = args.indexOf('(', i + KEYWORD_JSON.length());
510+
if (index < i) {
511+
throw new IllegalArgumentException(ERROR_MISSING_NESTED_TYPE);
512+
}
513+
i = ClickHouseUtils.skipBrackets(args, index, len, '(');
514+
String originalTypeName = args.substring(startIndex, i);
515+
List<ClickHouseColumn> nestedColumns = parse(args.substring(index + 1, i - 1));
516+
if (nestedColumns.isEmpty()) {
517+
throw new IllegalArgumentException("Nested should have at least one nested column");
518+
}
519+
column = new ClickHouseColumn(ClickHouseDataType.JSON, name, originalTypeName, nullable, lowCardinality,
520+
null, nestedColumns);
521+
fixedLength = false;
522+
estimatedLength++;
507523
}
508524

509525
if (column == null) {

client-v2/src/test/java/com/clickhouse/client/datatypes/DataTypeTests.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@
77
import com.clickhouse.client.api.Client;
88
import com.clickhouse.client.api.DataTypeUtils;
99
import com.clickhouse.client.api.command.CommandSettings;
10+
import com.clickhouse.client.api.data_formats.ClickHouseBinaryFormatReader;
11+
import com.clickhouse.client.api.data_formats.internal.SerializerUtils;
1012
import com.clickhouse.client.api.enums.Protocol;
1113
import com.clickhouse.client.api.insert.InsertSettings;
1214
import com.clickhouse.client.api.metadata.TableSchema;
1315
import com.clickhouse.client.api.query.GenericRecord;
1416
import com.clickhouse.client.api.query.QueryResponse;
17+
import com.clickhouse.client.api.sql.SQLUtils;
1518
import com.clickhouse.data.ClickHouseDataType;
1619
import com.clickhouse.data.ClickHouseVersion;
1720
import lombok.AllArgsConstructor;
@@ -874,6 +877,38 @@ private void testVariantWith(String withWhat, String[] fields, Object[] values,
874877
}
875878
}
876879

880+
@Test(groups = {"integration"}, dataProvider = "testJSONBinaryFormat_dp")
881+
public void testJSONBinaryFormat(String jsonDef) throws Exception {
882+
if (isVersionMatch("(,24.8]")) {
883+
return;
884+
}
885+
886+
final String table = "test_json_binary_format";
887+
final String jsonCol = "value " + jsonDef;
888+
final String jsonValue = "{\"count\": 1000, \"stat\": {\"float\": 0.999, \"name\": \"temp\" }}";
889+
890+
client.execute("DROP TABLE IF EXISTS " + table).get().close();
891+
client.execute(tableDefinition(table, jsonCol)).get().close();
892+
client.execute("INSERT INTO " + table + " VALUES (" + SQLUtils.enquoteLiteral(jsonValue) + ")").get().close();
893+
894+
try (QueryResponse queryResponse = client.query("SELECT * FROM " + table + " LIMIT 1").get()) {
895+
ClickHouseBinaryFormatReader reader = client.newBinaryFormatReader(queryResponse);
896+
Map<String, Object> row = reader.next();
897+
Object value = row.get("value");
898+
System.out.println(value);
899+
}
900+
}
901+
902+
@DataProvider
903+
public Object[][] testJSONBinaryFormat_dp() {
904+
905+
return new Object[][] {
906+
{"JSON"},
907+
// {"JSON(a Int32, d String)"},
908+
{"JSON(stat.name String, count Int32)"},
909+
};
910+
}
911+
877912
public static String tableDefinition(String table, String... columns) {
878913
StringBuilder sb = new StringBuilder();
879914
sb.append("CREATE TABLE " + table + " ( ");

0 commit comments

Comments
 (0)