[client-v2] Insert of a nested Map into a Dynamic column takes time that grows with the cube of the nesting depth
Los mantenedores suelen responder en 1 día
Nadie ha tomado este issue todavía.
Evaluación
- Dificultad
- 3/5
- Tiempo estimado
- 1-2 días
- Aptitud para principiantes
- 70/100
- Tipo de issue
- Error
- Claridad
- Bien especificado
- Estado de actividad
- Activo
- Stack tecnológico
- java
- Área
- backend, performance
Línea de trabajo
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.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
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.
- Lenguaje dominante
- Java
- Estrellas
- 1.6k
- Forks
- 657
- Merge medio
- 2 d 6 h
- PR fusionados (30 d)
- 36
Preparar el entorno
- Sin Dockerfile ni archivo de Docker Compose
- Tiene una plantilla de pull request
- Leer la guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de ClickHouse/clickhouse-java
-
api-diagnostics bug jdbc
Dificultad 2/5 1-3 horas Aptitud para principiantes 75/100
ClickHouse/clickhouse-java#3464 ·
Los mantenedores suelen responder en 1 día
-
tests
Dificultad 2/5 1-3 horas Aptitud para principiantes 76/100
ClickHouse/clickhouse-java#3452 ·
Los mantenedores suelen responder en 1 día
-
bug jdbc performance queries sql-parser
Dificultad 2/5 1-3 horas Aptitud para principiantes 77/100
ClickHouse/clickhouse-java#3451 ·
Los mantenedores suelen responder en 1 día
-
bug jdbc legacy queries
Dificultad 2/5 1-3 horas Aptitud para principiantes 70/100
ClickHouse/clickhouse-java#3444 ·
Los mantenedores suelen responder en 1 día
-
api-diagnostics bug client jdbc
Dificultad 2/5 1-3 horas Aptitud para principiantes 76/100
ClickHouse/clickhouse-java#3439 · 1 comentario ·
Los mantenedores suelen responder en 1 día
Todos los issues de ClickHouse/clickhouse-java
Issues similares
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 60/100
quelea-projection/Quelea#749 ·
Los mantenedores suelen responder en 1 día
-
free_port can hand one port to two callers, so the fixed-port http tests fail in parallel runsAbierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 92/100
erkamyaman/rust-anything#963 ·
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 Medio día Aptitud para principiantes 76/100
-
Deferred
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
Sanne/incus-spawn#2768 ·
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 82/100
morluto/rea#15041 · 1 comentario ·
Los mantenedores suelen responder en 1 día