Skip to content

Cannot write AggregateFunction(groupBitmap, UInt64) via JDBC to spark #2362

Open

Description

@hotstar-xia

Description

Hi ClickHouse team,
I'm encountering an issue when trying to write to a column of type AggregateFunction(groupBitmap, UInt64) via Spark using clickhouse-jdbc and clickhouse-data libraries. This used to work in older versions of the driver, but no longer does.
Details can be found here:
https://clickhousedb.slack.com/archives/CU478UEQZ/p1747041808183739
Details log and code files:
https://gist.github.com/hotstar-xia/e90cd64443ac5deadaf69107978295d4

Steps to reproduce

  1. Create the target table
  2. Use Spark JDBC to write a row into this table. The uv column is constructed via serialized bitmap (using Roaring64NavigableMap + ClickHouseBitmap wrapper).

Error Log or Exception StackTrace

Job aborted due to stage failure: Only singleton array is allowed, but we got: [0, 3, 2, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0]
Caused by: IllegalArgumentException: Only singleton array is allowed, but we got: [0, 3, 2, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0]
    at com.clickhouse.data.ClickHouseValue.update(ClickHouseValue.java:782)
    at com.clickhouse.jdbc.internal.InputBasedPreparedStatement.setBytes(InputBasedPreparedStatement.java:286)
...

Expected Behaviour

Writing bitmap to the AggregateFunction(groupBitmap, UInt64) column should succeed, as it did in prior driver versions.

Code Example

df.write
    .format("jdbc")
    .mode("append")
    .option("driver", "com.clickhouse.jdbc.ClickHouseDriver")
    .option("url", "jdbc:clickhouse://ip-10-10-240-59:8123")
    .option("user", "default")
    .option("password", "")
    .option("dbtable", "label_tmp_table")
    .option("batchsize", 10000)
    .option("isolationLevel", "NONE")
    // .option("createTableOptions", "ENGINE = AggregatingMergeTree ORDER BY (label_table, label_name, value_type, value_op)")
    .option("truncate", "true")
    .save()

Configuration

Client Configuration

  • ClickHouse version: 25.4.3.22

Environment

  • clickhouse-jdbc: 0.6.0-patch3
  • clickhouse-data: 0.8.5
  • Spark version: Databricks Runtime 12.2 LTS (Apache Spark 3.3.2, Scala 2.12)
  • Cluster type: Single-node
  • Target column: AggregateFunction(groupBitmap, UInt64)

ClickHouse Server

  • ClickHouse version: 25.4.3.22

  • CREATE TABLE statements for tables involved:

CREATE TABLE default.label_tmp_table
(
    `label_table` String,
    `label_name` String,
    `value_type` String,
    `value_float64` Float64,
    `value_int64` UInt64,
    `value_string` String,
    `value_datetime` DateTime,
    `value_boolean` Bool,
    `value_op` String,
    `uv` AggregateFunction(groupBitmap, UInt64)
)
ENGINE = AggregatingMergeTree
ORDER BY (label_table, label_name, value_type, value_op)

Activity

  1. self-assigned this
    on May 15, 2025
  2. added
    jdbc
    data-types
    on Jul 12, 2025
  3. mshustov commented on Sep 27, 2025

    @mshustov
    Member

    depends on #2599

  4. removed
    jdbc
    data-types
    on Jun 3, 2026
  5. polyglotAI-bot commented on Nov 28, 2026

    @polyglotAI-bot
    Collaborator

    Recommendation: Close — already fixed
    The issue's driver class ClickHouseDriver runs jdbc-v2 since 0.8.0, and jdbc-v2 writes the groupBitmap state with setBytes since #2734, first released in v0.9.7. Only the opt-in v1 driver (clickhouse.jdbc.v1=true) still rejects it.

    • Category: Bug
    • Classification: impact: none · area: Data types & formats (AggregateFunction(groupBitmap, UInt64))
    • Status on main: Fixed on main — with jdbc-v2, the JDBC calls of Spark's writer store the issue's state as [2,4,7].
    • Breaking change: No
    • Duplicates / related: Related: [Client, JDBC] Support Aggregate Functions #2599 — support for the other aggregate functions; this groupBitmap write does not need it.
    • Workaround: With the v1 driver, setObject(ClickHouseBitmap.wrap(bitmap, ClickHouseDataType.UInt64)) stores the value.

    Analysis: jdbc-v2 writes a byte[] parameter as unhex('…') (jdbc-v2/src/main/java/com/clickhouse/jdbc/PreparedStatementImpl.java:794-795), which the server reads as the aggregate state; before #2734 it sent the bytes as an Array. The v1 driver passes the bytes to ClickHouseValue.update(byte[]) (clickhouse-jdbc/src/main/java/com/clickhouse/jdbc/internal/InputBasedPreparedStatement.java:287), which accepts only one byte (clickhouse-data/src/main/java/com/clickhouse/data/ClickHouseValue.java:782), because ClickHouseBitmapValue does not override it.

    Reproduction: complete program and output

    Changed from the issue's steps: the JDBC calls that Spark's writer makes (setBytes, addBatch, executeBatch) in place of Spark, a table with only a key and the uv column, and the state from the issue's error message.

    import com.clickhouse.data.ClickHouseDataType;
    import com.clickhouse.data.value.ClickHouseBitmap;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.Statement;
    import java.util.Properties;
    import org.roaringbitmap.longlong.Roaring64NavigableMap;
    
    public class Repro {
        static final String URL = System.getenv().getOrDefault("CLICKHOUSE_URL", "http://localhost:8123");
        static final String PASSWORD = System.getenv().getOrDefault("CLICKHOUSE_PASSWORD", "");
        // The serialized groupBitmap state {2, 4, 7} from the issue's error message.
        static final byte[] STATE = {0, 3, 2, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0};
    
        static Connection connect(String extra) throws Exception {
            Properties props = new Properties();
            props.setProperty("user", "default");
            props.setProperty("password", PASSWORD);
            String jdbcUrl = "jdbc:clickhouse:" + URL.replaceFirst("^https?:", "") + "/default" + extra;
            return DriverManager.getConnection(jdbcUrl, props);
        }
    
        static void run(String name, String extra, Object value) {
            try (Connection conn = connect(extra); Statement s = conn.createStatement()) {
                s.execute("DROP TABLE IF EXISTS issue2362");
                s.execute("CREATE TABLE issue2362 (id Int32, uv AggregateFunction(groupBitmap, UInt64)) ENGINE = AggregatingMergeTree ORDER BY id");
                // The statement and the calls that Spark's JDBC writer makes (setBytes for a BinaryType column, batches)
                try (PreparedStatement ps = conn.prepareStatement("INSERT INTO issue2362 (\"id\",\"uv\") VALUES (?,?)")) {
                    ps.setInt(1, 1);
                    if (value instanceof byte[]) ps.setBytes(2, (byte[]) value); else ps.setObject(2, value);
                    ps.addBatch();
                    ps.executeBatch();
                }
                try (ResultSet rs = s.executeQuery("SELECT toString(bitmapToArray(uv)) FROM issue2362")) {
                    rs.next();
                    System.out.println(name + ": OK, stored " + rs.getString(1));
                }
            } catch (Throwable e) {
                Throwable c = e;
                while (c.getCause() != null) c = c.getCause();
                System.out.println(name + ": " + e.getClass().getName() + ": " + e.getMessage()
                    + (c != e ? " | cause " + c.getClass().getName() + ": " + c.getMessage() : ""));
            }
        }
    
        public static void main(String[] args) throws Exception {
            run("jdbc-v2 setBytes(state)", "", STATE);
            run("v1 driver setBytes(state)", "?clickhouse.jdbc.v1=true&compress=0", STATE);
            Roaring64NavigableMap bitmap = Roaring64NavigableMap.bitmapOf(2L, 4L, 7L);
            run("v1 driver setObject(ClickHouseBitmap)", "?clickhouse.jdbc.v1=true&compress=0",
                ClickHouseBitmap.wrap(bitmap, ClickHouseDataType.UInt64));
        }
    }

    Output on main (8480ca001), ClickHouse 26.11.4.11:

    jdbc-v2 setBytes(state): OK, stored [2,4,7]
    v1 driver setBytes(state): java.lang.IllegalArgumentException: Only singleton array is allowed, but we got: [0, 3, 2, 0, 0, 0, 0, 0, 0, 0, 4, 0, …]
    v1 driver setObject(ClickHouseBitmap): OK, stored [2,4,7]
    

    Output of the jdbc-v2 case on the releases v0.9.6 and v0.9.7 (Maven Central), ClickHouse 26.11.4.11, with ?compress=0 in the jdbc-v2 URL (with compression, these releases cannot read the responses of this server):

    == 0.9.6
    jdbc-v2 setBytes(state) compress=0: java.sql.SQLException: Code: 53. DB::Exception: Cannot convert Array to AggregateFunction(groupBitmap, UInt64): While executing ValuesBlockInputFormat. (TYPE_MISMATCH) …
    == 0.9.7
    jdbc-v2 setBytes(state) compress=0: OK, stored [2,4,7]
    

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

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

Metadata

Metadata

Assignees

Labels

bugdata-typesjdbc

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions