Repository navigation
Description
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 (aDoublebound forNullable(Float64)columnvwas stored as'2.5'inStringcolumnname). - List that leaves out columns, parsers
ANTLR4andANTLR4_PARAMS_PARSER: the insert fails withCANNOT_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 callssetInsertColumns(only the ANTLR4 listener does,:480), soWriterStatementImpl(WriterStatementImpl.java:61) writes with the full table schema. executeLargeUpdatesendsINSERT INTO <table> FORMAT RowBinarywith no column list (WriterStatementImpl.java:127-128callinsert(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:
- [jdbc-v2] Make Insert with binary default for simple #3328 (open): makes this writer the default, so each simple INSERT with a column list gets this behavior.
- [jdbc-v2] Send the writer INSERT to the database selected with setSchema #3398 (open PR): changes the same
insert(...)call inexecuteLargeUpdate.
Steps to reproduce
- Connect with
beta.row_binary_for_simple_insert=true. - For
t (k Int32, a Nullable(Int32), b Nullable(Int32)), prepareINSERT INTO t (k, b) VALUES (?, ?), bind1and42, and callexecuteUpdate():a=42,b=NULL. - For
t (a Int32, b Int32), prepareINSERT INTO t (b, a) VALUES (?, ?), bind1and2, and callexecuteUpdate():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_PARSEREnvironment
- Cloud
- Client version: jdbc-v2 0.12.0-rc1-SNAPSHOT (
mainat 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 TABLEstatements 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