|
80 | 80 | import java.util.concurrent.Executors; |
81 | 81 | import java.util.concurrent.Future; |
82 | 82 | import java.util.concurrent.TimeUnit; |
| 83 | +import java.util.concurrent.atomic.AtomicInteger; |
83 | 84 | import java.util.function.Consumer; |
84 | 85 | import java.util.function.Function; |
85 | 86 | import java.util.function.Supplier; |
| 87 | +import java.util.regex.Matcher; |
86 | 88 | import java.util.stream.BaseStream; |
87 | 89 | import java.util.stream.Collectors; |
88 | 90 | import java.util.stream.IntStream; |
@@ -1420,7 +1422,10 @@ private List<Map<String, Object>> prepareDataSet(String table, List<String> colu |
1420 | 1422 | } |
1421 | 1423 | createStmtBuilder.setLength(createStmtBuilder.length() - 2); |
1422 | 1424 | createStmtBuilder.append(") ENGINE = MergeTree ORDER BY tuple()"); |
1423 | | - client.execute(createStmtBuilder.toString()).get(10, TimeUnit.SECONDS); |
| 1425 | + client.execute(createStmtBuilder.toString(), (CommandSettings) |
| 1426 | + new CommandSettings().serverSetting("enable_dynamic_type", "1") |
| 1427 | + .serverSetting("allow_experimental_json_type", "1")) |
| 1428 | + .get(10, TimeUnit.SECONDS); |
1424 | 1429 |
|
1425 | 1430 | // Insert data |
1426 | 1431 | StringBuilder insertStmtBuilder = new StringBuilder(); |
@@ -2058,17 +2063,59 @@ public void testGettingRowsBeforeLimit() throws Exception { |
2058 | 2063 | @Test(groups = {"integration"}) |
2059 | 2064 | public void testGetDynamicValue() throws Exception { |
2060 | 2065 | String table = "test_get_dynamic_values"; |
2061 | | - client.execute("DROP TABLE IF EXISTS " + table); |
2062 | | - client.execute("CREATE TABLE " + table + " (rowId Int32, v Dynamic) Engine MergeTree ORDER BY ()", (CommandSettings) new CommandSettings().serverSetting("enable_dynamic_type", "1")); |
2063 | 2066 |
|
2064 | | - client.execute("INSERT INTO " + table + " VALUES (0, 'string'), (1, 2222222)"); |
| 2067 | + final AtomicInteger rowId = new AtomicInteger(-1); |
| 2068 | + final Random rnd = new Random(); |
| 2069 | + |
| 2070 | + List<Map<String,Object>> dataset = prepareDataSet(table, Arrays.asList("rowId Int32", "v Dynamic"), |
| 2071 | + Arrays.asList(s -> rowId.incrementAndGet(), s-> { |
| 2072 | + int decision = rnd.nextInt(3); |
| 2073 | + if (decision == 0) { |
| 2074 | + return RandomStringUtils.randomAlphanumeric(3, 10); |
| 2075 | + } else if (decision == 1) { |
| 2076 | + return rnd.nextInt(); |
| 2077 | + } else { |
| 2078 | + return rnd.nextDouble(); |
| 2079 | + } |
| 2080 | + }), 1000); |
2065 | 2081 |
|
2066 | 2082 | try (QueryResponse response = client.query("SELECT * FROM " + table).get()) { |
2067 | 2083 | ClickHouseBinaryFormatReader reader = client.newBinaryFormatReader(response); |
2068 | | - reader.next(); |
2069 | | - System.out.println(reader.getString("v")); |
2070 | | - reader.next(); |
2071 | | - System.out.println(reader.getString("v")); |
| 2084 | + while (reader.next() != null) { |
| 2085 | + int rowIndex = reader.getInteger("rowId"); |
| 2086 | + Assert.assertEquals(reader.getString("v"), dataset.get(rowIndex).get("v").toString()); |
| 2087 | + } |
| 2088 | + } |
| 2089 | + } |
| 2090 | + |
| 2091 | + @Test(groups = {"integration"}) |
| 2092 | + public void testGetJSON() throws Exception { |
| 2093 | + String table = "test_get_json_values"; |
| 2094 | + |
| 2095 | + final AtomicInteger rowId = new AtomicInteger(-1); |
| 2096 | + final Random rnd = new Random(); |
| 2097 | + |
| 2098 | + List<Map<String,Object>> dataset = prepareDataSet(table, Arrays.asList("rowId Int32", "v1 JSON"), |
| 2099 | + Arrays.asList(s -> rowId.incrementAndGet(), |
| 2100 | + s-> { |
| 2101 | + String a = "{'a': '" + RandomStringUtils.randomAlphabetic(20) + "', 'b': { 'c': 'test1', 'd': " + rnd |
| 2102 | + .nextInt(1000) + "}}"; |
| 2103 | + return a.replaceAll("'", "\""); |
| 2104 | + }), 1); |
| 2105 | + |
| 2106 | + System.out.println(dataset); |
| 2107 | + ObjectMapper jackson = new ObjectMapper(); |
| 2108 | + try (QueryResponse response = client.query("SELECT * FROM " + table).get()) { |
| 2109 | + ClickHouseBinaryFormatReader reader = client.newBinaryFormatReader(response); |
| 2110 | + while (reader.next() != null) { |
| 2111 | + int rowIndex = reader.getInteger("rowId"); |
| 2112 | + JsonNode expected = jackson.readValue(dataset.get(rowIndex).get("v1").toString(), JsonNode.class); |
| 2113 | + Map<String, Object> v1 = reader.readValue("v1"); |
| 2114 | + for (Map.Entry<String, Object> e : v1.entrySet()) { |
| 2115 | + String pointer = "/" + e.getKey().replaceAll("\\.", "/"); |
| 2116 | + Assert.assertEquals(e.getValue().toString(), expected.at(pointer).asText()); |
| 2117 | + } |
| 2118 | + } |
2072 | 2119 | } |
2073 | 2120 | } |
2074 | 2121 | } |
0 commit comments