Skip to content

[jdbc-v2] RowBinary writer drops values set with setBytes, the stream setters and setURL (column gets NULL or its default) #3334

In review

Description

@polyglotAI-bot

Description

When the beta RowBinary writer is on (beta.row_binary_for_simple_insert=true), 14 PreparedStatement setters do not give the value to the writer: setBytes, setAsciiStream (3 overloads), setUnicodeStream, setBinaryStream (3), setCharacterStream (3), setNCharacterStream (2) and setURL. The insert then writes no value for that column:

  • Nullable column: NULL is stored. There is no error.
  • Column with a DEFAULT: the default is stored. There is no error.
  • Other columns: IllegalArgumentException: An attempt to write null into not nullable column 'v'. The message does not tell the user that the bound value was lost.

The first two cases are silent data loss. When the writer is off (PreparedStatementImpl), the same calls store the bound value. Both executeUpdate() and addBatch() + executeBatch() are affected.

The writer is opt-in now (default false). #3328 proposes to make it the default. Then every user of these setters gets this behavior.

Steps to reproduce

  1. Open a jdbc-v2 connection with beta.row_binary_for_simple_insert=true.
  2. Create t_writer_lost with a Nullable(String) column b and a String DEFAULT 'x' column d (SQL in the code example).
  3. Prepare INSERT INTO t_writer_lost VALUES (?, ?, ?). Bind column b with setBytes and column d with setBinaryStream or setCharacterStream. Run executeUpdate() and executeBatch().
  4. SELECT id, b, d FROM t_writer_lost ORDER BY id

Error Log or Exception StackTrace

Output of the code example below:

writer=false impl=PreparedStatementImpl
  (1, abc, yz)
  (2, def, uv)
writer=true impl=WriterStatementImpl
  (1, null, x)
  (2, null, x)

Each of the 14 setters was also checked alone, in a new table per row (INSERT INTO t VALUES (?, ?), value "abc", or http://example.com/a for setURL). The value was read back with SELECT v IS NULL, toString(v):

Target column Writer off (13 setters) Writer on (all 14 setters)
v Nullable(String) 'abc' NULL
v String DEFAULT 'x' 'abc' 'x'
v String 'abc' IllegalArgumentException: An attempt to write null into not nullable column 'v'

The control setString stores 'abc' in all six cells. With the writer off, setURL fails with a server syntax error (Code: 62): a different defect in the SQL-text path, not part of this issue.

Expected Behaviour

The column gets the bound value, the same as when the writer is off. For example, setBytes(2, "abc".getBytes()) into a Nullable(String) column stores abc, not NULL. setBinaryStream(3, …"yz"…) into String DEFAULT 'x' stores yz, not x.

Root cause

  • jdbc-v2/src/main/java/com/clickhouse/jdbc/WriterStatementImpl.java: these overrides only call ensureOpen(), so the row slot in the writer stays null: setBytes (:205), setAsciiStream (:226, :231, :237), setUnicodeStream (:243), setBinaryStream (:248, :254, :260), setCharacterStream (:266, :271, :276), setNCharacterStream (:281, :287), setURL (:406).
  • RowBinaryFormatSerializer.writeValuePreamble() (RowBinaryFormatSerializer.java:199) writes the NULL marker for a null in a Nullable column, the "use default" marker for a column with a default (RowBinaryWithDefaults), and throws for other columns.

The fix is in the 14 setters of WriterStatementImpl.
Polyglot AI can open a fix PR if a maintainer assigns this issue to @polyglotAI-bot.

Related gap (fails with an error, no data loss)

When the writer is on, setObject(int, Object, int), setObject(int, Object, int, int), setObject(int, Object, SQLType) and setObject(int, Object, SQLType, int) throw SQLException: This form of setObject is not supported yet (SQL state 0A000, WriterStatementImpl.java:424-451). When the writer is off, the same calls store the value (verified with setObject(2, "abc", …) and Types.VARCHAR / JDBCType.VARCHAR, with and without a scale 0). Frameworks use these overloads (for example Spring JdbcTemplate with SqlParameterValue).

Also with the writer on, setBlob(int, InputStream, long) stored the text com.clickhouse.client.api.data_formats.RowBinaryFormatWriter$InputStreamHolder@3d1cfad4 in the column. That is a separate defect in client-v2 RowBinaryFormatWriter.setInputStream() / setReader().

Code Example

Reproduction program (Java, 41 lines)
import java.io.ByteArrayInputStream;
import java.io.StringReader;
import java.sql.*;
import java.util.Properties;

public class Minimal {
    public static void main(String[] args) throws Exception {
        com.clickhouse.jdbc.Driver.load();
        for (boolean writer : new boolean[] {false, true}) {
            Properties p = new Properties();
            p.setProperty("user", "default");
            p.setProperty("password", "<password>");
            p.setProperty("beta.row_binary_for_simple_insert", String.valueOf(writer));
            try (Connection conn = DriverManager.getConnection("jdbc:clickhouse://localhost:8123/default", p)) {
                try (Statement st = conn.createStatement()) {
                    st.execute("DROP TABLE IF EXISTS t_writer_lost");
                    st.execute("CREATE TABLE t_writer_lost (id Int32, b Nullable(String), d String DEFAULT 'x') ENGINE = MergeTree ORDER BY id");
                }
                try (PreparedStatement ps = conn.prepareStatement("INSERT INTO t_writer_lost VALUES (?, ?, ?)")) {
                    ps.setInt(1, 1);
                    ps.setBytes(2, "abc".getBytes());
                    ps.setBinaryStream(3, new ByteArrayInputStream("yz".getBytes()));
                    ps.executeUpdate();

                    ps.setInt(1, 2);
                    ps.setBytes(2, "def".getBytes());
                    ps.setCharacterStream(3, new StringReader("uv"));
                    ps.addBatch();
                    ps.executeBatch();
                    System.out.println("writer=" + writer + " impl=" + ps.getClass().getSimpleName());
                }
                try (Statement st = conn.createStatement();
                     ResultSet rs = st.executeQuery("SELECT id, b, d FROM t_writer_lost ORDER BY id")) {
                    while (rs.next()) {
                        System.out.println("  (" + rs.getInt(1) + ", " + rs.getString(2) + ", " + rs.getString(3) + ")");
                    }
                }
            }
        }
    }
}

Configuration

Client Configuration

Properties p = new Properties();
p.setProperty("beta.row_binary_for_simple_insert", "true");

Environment

  • Cloud
  • Client version: jdbc-v2 0.12.0-rc1-SNAPSHOT, built from main f9586aba0
  • Language version: Java 17 (Temurin 17.0.21)
  • OS: Linux (Docker)

ClickHouse Server

  • ClickHouse Server version: 26.9.1.1629
  • ClickHouse Server non-default settings, if any: none
  • CREATE TABLE statements for tables involved: in the code example
  • Sample data for all these tables: not needed, the example inserts the rows

Found by automated analysis (Polyglot AI) while working on #3328. Verified against a live server. Shortened on 2026-11-20: the edit history has the first version.

Activity

  1. added
    jdbc-v2
    area:jdbc-insert
    on Nov 11, 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

    area:jdbc-insertbugjdbc-v2

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions