Skip to content

[jdbc-v2] RowBinary writer ignores the INSERT column list and stores values in the wrong columns #3422

Backlog

Description

@polyglotAI-bot

Description

With beta.row_binary_for_simple_insert=true, a PreparedStatement for INSERT INTO t (<columns>) VALUES (?, ...) binds parameter N to column N of the table, not to column N of the list:

  • List in another order than the table: the values go into the wrong columns, with no error. All three parsers (JAVACC, ANTLR4, ANTLR4_PARAMS_PARSER).
  • List that leaves out columns, default parser JAVACC: the values go into the first table columns, and the last columns get NULL or their default, with no error. When the types do not agree, the result is a client error that names the wrong column (An attempt to write null into not nullable column 'b'), or a converted value (a Double bound for Nullable(Float64) column v was stored as '2.5' in String column name).
  • List that leaves out columns, parsers ANTLR4 and ANTLR4_PARAMS_PARSER: the insert fails with CANNOT_READ_ALL_DATA.

executeBatch() does the same. A list of all columns in table order, and INSERT INTO t VALUES (...), work. With the writer off, all cases store the values in the listed columns.

Root cause:

  • The default parser does not record the column list: JavaCCParser.parsePreparedStatement (SqlParserFacade.java:85) never calls setInsertColumns (only the ANTLR4 listener does, :480), so WriterStatementImpl (WriterStatementImpl.java:61) writes with the full table schema.
  • executeLargeUpdate sends INSERT INTO <table> FORMAT RowBinary with no column list (WriterStatementImpl.java:127-128 call insert(tableName, in, format, settings)), so the server reads each row in table order.

Both causes must be fixed: a fix of only the first one changes the silent wrong data of JAVACC into the CANNOT_READ_ALL_DATA error of the ANTLR4 parsers.

Polyglot AI can open a fix PR if a maintainer assigns this issue to @polyglotAI-bot.

Related:

Steps to reproduce

  1. Connect with beta.row_binary_for_simple_insert=true.
  2. For t (k Int32, a Nullable(Int32), b Nullable(Int32)), prepare INSERT INTO t (k, b) VALUES (?, ?), bind 1 and 42, and call executeUpdate(): a=42, b=NULL.
  3. For t (a Int32, b Int32), prepare INSERT INTO t (b, a) VALUES (?, ?), bind 1 and 2, and call executeUpdate(): a=1, b=2.

Error Log or Exception StackTrace

INSERT INTO t (k, b) VALUES (?, ?) JAVACC               writer=true  a=42 b=null
INSERT INTO t (k, b) VALUES (?, ?) JAVACC               writer=false a=null b=42
INSERT INTO t (k, b) VALUES (?, ?) ANTLR4               writer=true  FAIL Code: 33. DB::Exception: Cannot read all data. Bytes read: 0. Bytes expected: 1: (at row 1) : While executing WaitForAsyncInsert. (CANNOT_READ_ALL_DATA)
INSERT INTO t (k, b) VALUES (?, ?) ANTLR4               writer=false a=null b=42
INSERT INTO t (b, a) VALUES (?, ?) JAVACC               writer=true  a=1 b=2
INSERT INTO t (b, a) VALUES (?, ?) JAVACC               writer=false a=2 b=1
INSERT INTO t (b, a) VALUES (?, ?) ANTLR4               writer=true  a=1 b=2
INSERT INTO t (b, a) VALUES (?, ?) ANTLR4               writer=false a=2 b=1
INSERT INTO t (b, a) VALUES (?, ?) ANTLR4_PARAMS_PARSER writer=true  a=1 b=2
INSERT INTO t (b, a) VALUES (?, ?) ANTLR4_PARAMS_PARSER writer=false a=2 b=1

Expected Behaviour

Each value goes into the column that the list names. The server, for the same statement as SQL:

INSERT INTO t2 (b, a) VALUES (1, 2);  SELECT a, b FROM t2  ->  a=2  b=1

Code Example

Reproduction program (Java, 42 lines)
import java.sql.*;
import java.util.Properties;

public class Repro {
    static void run(String parser, boolean writer, String ddl, String insert, int v1, int v2) throws SQLException {
        Properties p = new Properties();
        p.setProperty("user", "default");
        p.setProperty("password", System.getenv("CLICKHOUSE_PASSWORD"));
        p.setProperty("beta.row_binary_for_simple_insert", String.valueOf(writer));
        p.setProperty("jdbc_sql_parser", parser);
        try (Connection c = new com.clickhouse.jdbc.Driver().connect("jdbc:clickhouse://clickhouse:8123/default", p);
             Statement s = c.createStatement()) {
            s.execute("CREATE OR REPLACE TABLE t " + ddl + " ENGINE = Memory");
            String result;
            try (PreparedStatement ps = c.prepareStatement(insert)) {
                ps.setInt(1, v1);
                ps.setInt(2, v2);
                ps.executeUpdate();
                ResultSet rs = s.executeQuery("SELECT * FROM t FORMAT TSV");
                rs.next();
                result = "a=" + rs.getString("a") + " b=" + rs.getString("b");
            } catch (SQLException e) {
                result = "FAIL " + e.getMessage().split("\\(version")[0];
            }
            System.out.printf("%-34s %-20s writer=%-5s %s%n", insert, parser, writer, result);
        }
    }

    public static void main(String[] args) throws SQLException {
        String subset = "(k Int32, a Nullable(Int32), b Nullable(Int32))";
        for (String parser : new String[]{"JAVACC", "ANTLR4"}) {
            for (boolean writer : new boolean[]{true, false}) {
                run(parser, writer, subset, "INSERT INTO t (k, b) VALUES (?, ?)", 1, 42);
            }
        }
        for (String parser : new String[]{"JAVACC", "ANTLR4", "ANTLR4_PARAMS_PARSER"}) {
            for (boolean writer : new boolean[]{true, false}) {
                run(parser, writer, "(a Int32, b Int32)", "INSERT INTO t (b, a) VALUES (?, ?)", 1, 2);
            }
        }
    }
}

Configuration

Client Configuration

p.setProperty("beta.row_binary_for_simple_insert", "true");
p.setProperty("jdbc_sql_parser", "JAVACC"); // the default; also ANTLR4, ANTLR4_PARAMS_PARSER

Environment

  • Cloud
  • Client version: jdbc-v2 0.12.0-rc1-SNAPSHOT (main at 051a7ec)
  • Language version: Java 17 (OpenJDK 17.0.21.1)
  • OS: Linux (Docker)

ClickHouse Server

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

Classification: impact: Data integrity · area: Queries (INSERT column list)


Found by automated analysis (Polyglot AI) while working on ClickHouse/clickhouse-cs#767. Verified against a live server.

Activity

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-insertarea:sql-parserbugjdbc-v2

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions