From c0e095a195689199251b788dec4d3f0286324246 Mon Sep 17 00:00:00 2001 From: piyush15102003 Date: Fri, 20 Nov 2026 21:27:28 +0530 Subject: [PATCH] [jdbc-v2] Send the writer INSERT to the database selected with setSchema The RowBinary writer built its InsertSettings without a database, so the INSERT went to the database from the connection configuration even though the table schema was resolved in the one set by Connection.setSchema. Closes #3212 --- history/latest/3212.md | 4 ++ .../clickhouse/jdbc/WriterStatementImpl.java | 2 + .../jdbc/WriterStatementImplTest.java | 48 +++++++++++++++++++ 3 files changed, 54 insertions(+) create mode 100644 history/latest/3212.md diff --git a/history/latest/3212.md b/history/latest/3212.md new file mode 100644 index 0000000000..f2a9e3c856 --- /dev/null +++ b/history/latest/3212.md @@ -0,0 +1,4 @@ +- **[jdbc-v2]** With `beta.row_binary_for_simple_insert=true`, an `INSERT` prepared after `Connection.setSchema(db)` + now writes to `db`. Before, the RowBinary writer sent the rows to the database from the connection configuration: + they landed in the wrong table when a table with the same name existed there, or failed with `UNKNOWN_TABLE`. + (https://github.com/ClickHouse/clickhouse-java/issues/3212) diff --git a/jdbc-v2/src/main/java/com/clickhouse/jdbc/WriterStatementImpl.java b/jdbc-v2/src/main/java/com/clickhouse/jdbc/WriterStatementImpl.java index a01944f5db..ed4d0eb235 100644 --- a/jdbc-v2/src/main/java/com/clickhouse/jdbc/WriterStatementImpl.java +++ b/jdbc-v2/src/main/java/com/clickhouse/jdbc/WriterStatementImpl.java @@ -122,6 +122,8 @@ public long executeLargeUpdate() throws SQLException { InputStream in = new ByteArrayInputStream(out.toByteArray()); InsertSettings settings = new InsertSettings(); settings.setQueryId(setLastQueryID(settings.getQueryId())); + // Use the database the table schema was resolved in (the connection schema at prepare time). + settings.setDatabase(tableSchema.getDatabaseName()); try (InsertResponse response = queryTimeout == 0 ? connection.getClient().insert(tableSchema.getTableName(),in, writer.getFormat(), settings).get() diff --git a/jdbc-v2/src/test/java/com/clickhouse/jdbc/WriterStatementImplTest.java b/jdbc-v2/src/test/java/com/clickhouse/jdbc/WriterStatementImplTest.java index d6c12f96e9..9017441c94 100644 --- a/jdbc-v2/src/test/java/com/clickhouse/jdbc/WriterStatementImplTest.java +++ b/jdbc-v2/src/test/java/com/clickhouse/jdbc/WriterStatementImplTest.java @@ -435,4 +435,52 @@ private static boolean hasInjectedCause(Throwable t) { } return false; } + + @Test(groups = {"integration"}) + public void testWriterInsertsIntoDatabaseSelectedWithSetSchema() throws Exception { + String otherDb = getDatabase() + "_set_schema_" + UUID.randomUUID().toString().replace('-', '_').substring(0, 8); + String table = "writer_set_schema"; + String onlyInOther = "writer_set_schema_only"; + runQuery("CREATE DATABASE IF NOT EXISTS " + otherDb); + try { + runQuery("CREATE TABLE " + otherDb + "." + table + " (x UInt32) ENGINE = Memory"); + runQuery("CREATE TABLE " + otherDb + "." + onlyInOther + " (x UInt32) ENGINE = Memory"); + runQuery("DROP TABLE IF EXISTS " + getDatabase() + "." + table); + runQuery("CREATE TABLE " + getDatabase() + "." + table + " (x UInt32) ENGINE = Memory"); + + Properties properties = new Properties(); + properties.setProperty(DriverProperties.BETA_ROW_BINARY_WRITER.getKey(), "true"); + try (Connection connection = getJdbcConnection(properties)) { + connection.setSchema(otherDb); + // same table name in both databases: the row must go to the selected one + try (PreparedStatement stmt = connection.prepareStatement("INSERT INTO " + table + " VALUES (?)")) { + Assert.assertTrue(stmt instanceof WriterStatementImpl); + stmt.setInt(1, 1); + Assert.assertEquals(stmt.executeUpdate(), 1); + } + // table exists only in the selected database + try (PreparedStatement stmt = connection.prepareStatement("INSERT INTO " + onlyInOther + " VALUES (?)")) { + Assert.assertTrue(stmt instanceof WriterStatementImpl); + stmt.setInt(1, 2); + Assert.assertEquals(stmt.executeUpdate(), 1); + } + } + + try (Connection connection = getJdbcConnection(); Statement stmt = connection.createStatement()) { + Assert.assertEquals(countRows(stmt, otherDb + "." + table), 1); + Assert.assertEquals(countRows(stmt, getDatabase() + "." + table), 0); + Assert.assertEquals(countRows(stmt, otherDb + "." + onlyInOther), 1); + } + } finally { + runQuery("DROP DATABASE IF EXISTS " + otherDb); + runQuery("DROP TABLE IF EXISTS " + getDatabase() + "." + table); + } + } + + private static long countRows(Statement stmt, String table) throws SQLException { + try (ResultSet rs = stmt.executeQuery("SELECT count() FROM " + table)) { + Assert.assertTrue(rs.next()); + return rs.getLong(1); + } + } }