[client-v2] Insert of a nested Map into a Dynamic column takes time that grows with the cube of the nesting depth
Mantenedores costumam responder em até 1 dia
Ninguém assumiu esta issue ainda.
Avaliação
- Dificuldade
- 3/5
- Tempo estimado
- 1-2 dias
- Facilidade para iniciantes
- 70/100
- Tipo de issue
- Bug
- Clareza
- Claramente especificada
- Status de atividade
- Ativa
- Stack de tecnologia
- java
- Domínio
- backend, performance
Direção de pesquisa
Start with SerializerUtils.valueToColumnForDynamicType in SerializerUtils.java, especially the Map branch around line 282, and compare it with the List path. The issue identifies repeated parsing through ClickHouseColumn.of as the source of the slowdown. Check the existing RowBinaryFormatWriter path too; done means deeply nested Maps in Dynamic columns scale roughly linearly with depth, like Lists, while preserving correct stored data.
Escrita pelo modelo de indexação a partir do texto da issue.
Descrição
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(8480ca001, 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.
- Linguagem predominante
- Java
- Estrelas
- 1.6k
- Forks
- 657
- Merge médio
- 2d 6h
- PRs com merge (30d)
- 36
Preparar o ambiente
- Sem Dockerfile nem arquivo Docker Compose
- Tem um modelo de pull request
- Ler o guia de contribuição
Primeiros passos
- Leia a issue inteira e depois o guia de contribuição do projeto.
- Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
- Faça um fork do repositório e trabalhe em uma branch.
- Abra um pull request que referencie o número da issue.
Mais de ClickHouse/clickhouse-java
-
api-diagnostics bug jdbc
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 75/100
ClickHouse/clickhouse-java#3464 ·
Mantenedores costumam responder em até 1 dia
-
tests
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 76/100
ClickHouse/clickhouse-java#3452 ·
Mantenedores costumam responder em até 1 dia
-
bug jdbc performance queries sql-parser
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 77/100
ClickHouse/clickhouse-java#3451 ·
Mantenedores costumam responder em até 1 dia
-
bug jdbc legacy queries
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 70/100
ClickHouse/clickhouse-java#3444 ·
Mantenedores costumam responder em até 1 dia
-
api-diagnostics bug client jdbc
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 76/100
ClickHouse/clickhouse-java#3439 · 1 comentário ·
Mantenedores costumam responder em até 1 dia
Todas as issues de ClickHouse/clickhouse-java
Issues semelhantes
-
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 60/100
quelea-projection/Quelea#749 ·
Mantenedores costumam responder em até 1 dia
-
free_port can hand one port to two callers, so the fixed-port http tests fail in parallel runsAberta
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 92/100
erkamyaman/rust-anything#963 ·
Mantenedores costumam responder em até 1 dia
-
Dificuldade 2/5 Meio dia Facilidade para iniciantes 76/100
-
Deferred
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 78/100
Sanne/incus-spawn#2768 ·
Mantenedores costumam responder em até 1 dia
-
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 82/100
morluto/rea#15041 · 1 comentário ·
Mantenedores costumam responder em até 1 dia