Skip to content

[client-v2] POJO serialization: Skipped fields still consume memory #2928

Open

Description

@chernser

Description

Method com.clickhouse.client.api.data_formats.internal.BinaryStreamReader#skipValue internally calls readValue and does complete read and object creation. This may be critical when many BigInteger fields are skipped but still consume memory.

Expected that next evolution of the reader will have option to read or skip bytes from stream entirely.

Activity

  1. added
    client
    on Jul 15, 2026
  2. added theissue type on Jul 15, 2026
  3. polyglotAI-bot commented on Nov 27, 2026

    @polyglotAI-bot
    Collaborator

    Recommendation: Keep open — fix needed
    The run confirms that the reader fully decodes each skipped POJO column: skipping costs almost as much memory as reading. No open PR changes skipValue.

    • Category: Performance
    • Classification: impact: Performance · area: Data types & formats (RowBinaryWithNamesAndTypes, POJO)
    • Status on main: Reproduced on main — 4 skipped UInt256 columns add about 460 bytes per row; 4 read columns add about 515.
    • Breaking change: No — BinaryStreamReader is in an internal package.
    • Duplicates / related: None found
    • Workaround: Select only the columns that the POJO maps (the first case of the run).

    Analysis: Client.queryAll(sql, Class, schema) reads each row with readToPOJO (Client.java:2316), which calls skipValue for each column with no setter (AbstractBinaryFormatReader.java:133). skipValue (BinaryStreamReader.java:852-854) only calls readValue(column, null), so it decodes the value and creates its object (a BigInteger for UInt256) and then drops it.

    Reproduction: complete program and output
    import com.clickhouse.client.api.Client;
    import com.clickhouse.client.api.metadata.TableSchema;
    import java.lang.management.ManagementFactory;
    import java.math.BigInteger;
    import java.util.List;
    
    public class Repro {
        static final String URL = System.getenv().getOrDefault("CLICKHOUSE_URL", "http://localhost:8123");
        static final String PASSWORD = System.getenv().getOrDefault("CLICKHOUSE_PASSWORD", "");
        static final int ROWS = 200_000;
    
        public static class Small {
            private long id;
            public Small() {}
            public long getId() { return id; }
            public void setId(long id) { this.id = id; }
        }
    
        public static class Full {
            private long id;
            private BigInteger a1, a2, a3, a4;
            public Full() {}
            public long getId() { return id; }
            public void setId(long id) { this.id = id; }
            public BigInteger getA1() { return a1; }
            public void setA1(BigInteger v) { a1 = v; }
            public BigInteger getA2() { return a2; }
            public void setA2(BigInteger v) { a2 = v; }
            public BigInteger getA3() { return a3; }
            public void setA3(BigInteger v) { a3 = v; }
            public BigInteger getA4() { return a4; }
            public void setA4(BigInteger v) { a4 = v; }
        }
    
        static long allocated() {
            return ((com.sun.management.ThreadMXBean) ManagementFactory.getThreadMXBean())
                    .getThreadAllocatedBytes(Thread.currentThread().getId());
        }
    
        static void measure(String name, Client client, String sql, Class<?> clazz, TableSchema schema) {
            client.queryAll(sql, clazz, schema); // warm-up
            long before = allocated();
            long t0 = System.nanoTime();
            List<?> rows = client.queryAll(sql, clazz, schema);
            long ms = (System.nanoTime() - t0) / 1_000_000;
            long bytes = allocated() - before;
            System.out.printf("%s: rows=%d, allocated=%d bytes/row, %d ms%n", name, rows.size(), bytes / rows.size(), ms);
        }
    
        public static void main(String[] args) throws Exception {
            try (Client client = new Client.Builder().addEndpoint(URL)
                    .setUsername("default").setPassword(PASSWORD).build()) {
                client.execute("DROP TABLE IF EXISTS t2928").get().close();
                client.execute("CREATE TABLE t2928 (id UInt64, a1 UInt256, a2 UInt256, a3 UInt256, a4 UInt256) ENGINE = MergeTree ORDER BY id").get().close();
                client.execute("INSERT INTO t2928 SELECT number, number * 1000000000000, number + 1, number + 2, number + 3 FROM numbers(" + ROWS + ")").get().close();
    
                TableSchema all = client.getTableSchema("t2928");
                TableSchema idOnly = client.getTableSchemaFromQuery("SELECT id FROM t2928");
                client.register(Small.class, all);
                client.register(Small.class, idOnly);
                client.register(Full.class, all);
    
                measure("Small POJO, SELECT id (no column skipped)          ", client, "SELECT id FROM t2928", Small.class, idOnly);
                measure("Small POJO, SELECT * (4 UInt256 columns skipped)   ", client, "SELECT * FROM t2928", Small.class, all);
                measure("Full POJO,  SELECT * (4 UInt256 columns read)      ", client, "SELECT * FROM t2928", Full.class, all);
    
                client.execute("DROP TABLE t2928").get().close();
            }
        }
    }

    Output on main (8480ca001), ClickHouse 26.11.2.4:

    Small POJO, SELECT id (no column skipped)          : rows=200000, allocated=223 bytes/row, 39 ms
    Small POJO, SELECT * (4 UInt256 columns skipped)   : rows=200000, allocated=682 bytes/row, 80 ms
    Full POJO,  SELECT * (4 UInt256 columns read)      : rows=200000, allocated=738 bytes/row, 111 ms
    

    Automated triage by Polyglot AI. A maintainer makes the final decision.

  4. added
    data-types
    on Nov 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    clientdata-typesperformance

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions