Skip to content

Commit 5fc99cd

Browse files
[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
1 parent cde1947 commit 5fc99cd

3 files changed

Lines changed: 54 additions & 0 deletions

File tree

‎history/latest/3212.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
- **[jdbc-v2]** With `beta.row_binary_for_simple_insert=true`, an `INSERT` prepared after `Connection.setSchema(db)`
2+
now writes to `db`. Before, the RowBinary writer sent the rows to the database from the connection configuration:
3+
they landed in the wrong table when a table with the same name existed there, or failed with `UNKNOWN_TABLE`.
4+
(https://github.com/ClickHouse/clickhouse-java/issues/3212)

‎jdbc-v2/src/main/java/com/clickhouse/jdbc/WriterStatementImpl.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ public long executeLargeUpdate() throws SQLException {
122122
InputStream in = new ByteArrayInputStream(out.toByteArray());
123123
InsertSettings settings = new InsertSettings();
124124
settings.setQueryId(setLastQueryID(settings.getQueryId()));
125+
// Use the database the table schema was resolved in (the connection schema at prepare time).
126+
settings.setDatabase(tableSchema.getDatabaseName());
125127

126128
try (InsertResponse response = queryTimeout == 0 ?
127129
connection.getClient().insert(tableSchema.getTableName(),in, writer.getFormat(), settings).get()

‎jdbc-v2/src/test/java/com/clickhouse/jdbc/WriterStatementImplTest.java‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,4 +502,52 @@ private static boolean hasInjectedCause(Throwable t) {
502502
}
503503
return false;
504504
}
505+
506+
@Test(groups = {"integration"})
507+
public void testWriterInsertsIntoDatabaseSelectedWithSetSchema() throws Exception {
508+
String otherDb = getDatabase() + "_set_schema_" + UUID.randomUUID().toString().replace('-', '_').substring(0, 8);
509+
String table = "writer_set_schema";
510+
String onlyInOther = "writer_set_schema_only";
511+
runQuery("CREATE DATABASE IF NOT EXISTS " + otherDb);
512+
try {
513+
runQuery("CREATE TABLE " + otherDb + "." + table + " (x UInt32) ENGINE = Memory");
514+
runQuery("CREATE TABLE " + otherDb + "." + onlyInOther + " (x UInt32) ENGINE = Memory");
515+
runQuery("DROP TABLE IF EXISTS " + getDatabase() + "." + table);
516+
runQuery("CREATE TABLE " + getDatabase() + "." + table + " (x UInt32) ENGINE = Memory");
517+
518+
Properties properties = new Properties();
519+
properties.setProperty(DriverProperties.BETA_ROW_BINARY_WRITER.getKey(), "true");
520+
try (Connection connection = getJdbcConnection(properties)) {
521+
connection.setSchema(otherDb);
522+
// same table name in both databases: the row must go to the selected one
523+
try (PreparedStatement stmt = connection.prepareStatement("INSERT INTO " + table + " VALUES (?)")) {
524+
Assert.assertTrue(stmt instanceof WriterStatementImpl);
525+
stmt.setInt(1, 1);
526+
Assert.assertEquals(stmt.executeUpdate(), 1);
527+
}
528+
// table exists only in the selected database
529+
try (PreparedStatement stmt = connection.prepareStatement("INSERT INTO " + onlyInOther + " VALUES (?)")) {
530+
Assert.assertTrue(stmt instanceof WriterStatementImpl);
531+
stmt.setInt(1, 2);
532+
Assert.assertEquals(stmt.executeUpdate(), 1);
533+
}
534+
}
535+
536+
try (Connection connection = getJdbcConnection(); Statement stmt = connection.createStatement()) {
537+
Assert.assertEquals(countRows(stmt, otherDb + "." + table), 1);
538+
Assert.assertEquals(countRows(stmt, getDatabase() + "." + table), 0);
539+
Assert.assertEquals(countRows(stmt, otherDb + "." + onlyInOther), 1);
540+
}
541+
} finally {
542+
runQuery("DROP DATABASE IF EXISTS " + otherDb);
543+
runQuery("DROP TABLE IF EXISTS " + getDatabase() + "." + table);
544+
}
545+
}
546+
547+
private static long countRows(Statement stmt, String table) throws SQLException {
548+
try (ResultSet rs = stmt.executeQuery("SELECT count() FROM " + table)) {
549+
Assert.assertTrue(rs.next());
550+
return rs.getLong(1);
551+
}
552+
}
505553
}

0 commit comments

Comments
 (0)