[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 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
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時間
マージ済み PR(30日)
36

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

ClickHouse/clickhouse-java のほかの issue

ClickHouse/clickhouse-java の issue をすべて見る

似ている issue

Java の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。