[client-v2] Insert of a nested Map into a Dynamic column takes time that grows with the cube of the nesting depth

未关闭
#3,454 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
70/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
java

调研方向

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.

由索引模型根据 Issue 内容生成。

描述

client data-types performance

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 Map column: 0.13 s.
  • One row with a Map nested 499 levels (the deepest that the default input_format_binary_max_type_complexity = 1000 accepts): 0.86 s.
  • With input_format_binary_max_type_complexity = 0 (no server limit), one row with 2000 levels fails with NoHttpResponseException after about 40 s. The client has not written the body when the server's http_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
  1. Run Repro.java (below) with client-v2 on the class path, CLICKHOUSE_URL and CLICKHOUSE_PASSWORD set.
  2. 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 = 0 for its requests)
  • CREATE TABLE statements 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.

主要语言
Java
星标
1.6k
派生
657
平均合并
2 天 6 小时
30 天内合并 PR
36

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

ClickHouse/clickhouse-java 的其他 Issue

查看 ClickHouse/clickhouse-java 的全部 Issue

相似的 Issue

更多 Java Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。