Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions history/latest/3212.md
Original file line number Diff line number Diff line change
@@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}