Repository navigation
Description
Description
Client.insert with POJOs is very slow when a Dynamic column gets a nested java.util.Map: the client CPU time per row grows with the cube of the nesting depth. The stored data is correct. A List of the same depth is fast, and so is the same Map value in a column with a declared Map(String, Map(...)) type.
- 10,000 rows with a Map nested 50 levels: 11.5 s. A List nested 50 levels: 0.45 s. The same Map in a declared
Mapcolumn: 0.13 s. - One row with a Map nested 499 levels (the deepest that the default
input_format_binary_max_type_complexity = 1000accepts): 0.86 s. - With
input_format_binary_max_type_complexity = 0(no server limit), one row with 2000 levels fails withNoHttpResponseExceptionafter about 40 s. The client has not written the body when the server'shttp_receive_timeout(30 s) ends.
Every Java value that the client writes into a Dynamic column goes through this code (SerializerUtils.java:121), also from RowBinaryFormatWriter.
Root cause: for each nested Map level, SerializerUtils.valueToColumnForDynamicType parses the full type name of that level again with ClickHouseColumn.of (SerializerUtils.java:282), and one parse of a k-level Map type name is O(k²) (ClickHouseColumn.readColumn calls skipBrackets and substring over the rest of the name at each level, ClickHouseColumn.java:514 and :528). The List path (listValue2Column) builds the name once and parses it once.
The fix is in the Map branch of valueToColumnForDynamicType. Polyglot AI can open a fix PR if a maintainer assigns this issue to @polyglotAI-bot.
Related: open PR #3377 changes the same Map branch (self-reference check); it keeps the per-level parse.
Steps to reproduce
- Run
Repro.java(below) with client-v2 on the class path,CLICKHOUSE_URLandCLICKHOUSE_PASSWORDset. - It inserts nested Maps and one nested List into
t_map_depth (id Int32, v Dynamic), and prints the time of each insert.
Error Log or Exception StackTrace
Map 1 levels, 1 rows: 63 ms, ok
Map 25 levels, 10000 rows: 2297 ms, ok
Map 50 levels, 10000 rows: 11521 ms, ok
List 50 levels, 10000 rows: 453 ms, ok
Map 499 levels, 1 rows: 857 ms, ok
Map 2000 levels, 1 rows: 40323 ms, NoHttpResponseException: clickhouse:8123 failed to respond
The type inference alone (valueToColumnForDynamicType, one value) takes 5.4 s for a Map of 1000 levels and 42.8 s for 2000 levels (8 times for 2 times the depth). A List of 2000 levels takes 44 ms.
Expected Behaviour
The time per row grows about linearly with the depth, as it does for a List. The server is not the slow part: it stores the 50-level Map rows of a declared Map column in 129 ms for 10,000 rows.
Code Example
Reproduction program (Java, 55 lines)
import com.clickhouse.client.api.Client;
import java.util.*;
public class Repro {
public static class Row {
private int id; private Object v;
public Row() {}
public Row(int id, Object v) { this.id = id; this.v = v; }
public int getId() { return id; } public void setId(int id) { this.id = id; }
public Object getV() { return v; } public void setV(Object v) { this.v = v; }
}
static Object nest(boolean map, int depth) {
Object v = "leaf";
for (int i = 0; i < depth; i++) {
if (map) { Map<String, Object> m = new HashMap<>(); m.put("k", v); v = m; }
else { List<Object> l = new ArrayList<>(); l.add(v); v = l; }
}
return v;
}
static void run(Client c, boolean map, int depth, int rows) {
List<Row> data = new ArrayList<>();
Object v = nest(map, depth);
for (int i = 0; i < rows; i++) data.add(new Row(i, v));
long t0 = System.nanoTime();
String res;
try { c.insert("t_map_depth", data).join(); res = "ok"; }
catch (Throwable t) { Throwable r = t; while (r.getCause() != null) r = r.getCause(); res = r.getClass().getSimpleName() + ": " + r.getMessage(); }
System.out.printf("%s %4d levels, %5d rows: %6d ms, %s%n", map ? "Map " : "List", depth, rows, (System.nanoTime() - t0) / 1_000_000, res);
}
public static void main(String[] args) {
String url = System.getenv().getOrDefault("CLICKHOUSE_URL", "http://localhost:8123");
String password = System.getenv().getOrDefault("CLICKHOUSE_PASSWORD", "");
try (Client c = new Client.Builder().addEndpoint(url).setUsername("default").setPassword(password)
.setMaxRetries(0).build()) {
c.queryAll("DROP TABLE IF EXISTS t_map_depth");
c.queryAll("CREATE TABLE t_map_depth (id Int32, v Dynamic) ENGINE = MergeTree ORDER BY id");
c.register(Row.class, c.getTableSchema("t_map_depth"));
run(c, true, 1, 1); // warm-up
run(c, true, 25, 10_000);
run(c, true, 50, 10_000);
run(c, false, 50, 10_000);
run(c, true, 499, 1); // deepest Map that the default server limit accepts
}
// input_format_binary_max_type_complexity = 0: the server accepts any depth
try (Client c = new Client.Builder().addEndpoint(url).setUsername("default").setPassword(password)
.setMaxRetries(0).serverSetting("input_format_binary_max_type_complexity", "0").build()) {
c.register(Row.class, c.getTableSchema("t_map_depth"));
run(c, true, 2000, 1);
c.queryAll("DROP TABLE t_map_depth");
}
}
}Configuration
Client Configuration
new Client.Builder().addEndpoint(url).setUsername("default").setPassword(password).setMaxRetries(0).build()
// the 2000-level case adds .serverSetting("input_format_binary_max_type_complexity", "0")Environment
- Cloud
- Client version: client-v2
main(8480ca0, 0.12.0-rc1-SNAPSHOT) - Language version: Java 17
- OS: Linux (Docker)
ClickHouse Server
- ClickHouse Server version: 26.11.2.4
- ClickHouse Server non-default settings, if any: none (the 2000-level case sets
input_format_binary_max_type_complexity = 0for its requests) CREATE TABLEstatements for tables involved: in the program- Sample data for all these tables: made by the program
Classification: impact: Performance · area: Data types & formats (Dynamic, Map)
Found by automated analysis (Polyglot AI) while working on #3453. Verified against a live server.
Activity