You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Connection.prepareStatement executes all but the last statement of multi-statement SQL #1004
java.sql.Connection.prepareStatement(String) is expected to only parse and plan. With multi-statement text, duckdb-java executes every statement except the last one during prepare. The side effects of those statements happen before execute() is called.
This is a security problem for applications that use prepare to inspect the shape of user-supplied SQL, for example calling getMetaData() to learn the result columns. A "metadata only" call performs writes.
Reproduction (duckdb_jdbc 1.5.6.0)
try (Connectionc = DriverManager.getConnection("jdbc:duckdb:")) {
try (PreparedStatementp = c.prepareStatement("CREATE TABLE z(i int); SELECT 1 AS x")) {
System.out.println(p.getMetaData().getColumnCount()); // 1// execute() was never calledtry (Statements = c.createStatement();
ResultSetr = s.executeQuery("SELECT count(*) FROM duckdb_tables() WHERE table_name = 'z'")) {
r.next();
System.out.println(r.getInt(1)); // 1: table z was created by prepare
}
}
}
Expected
prepareStatement has no side effects. It either rejects multi-statement text or prepares without executing. The count of z should be 0.
These multi-statement support is a UX feature, it was there from the beginning, before the prepared and non-prepared statements got separate implementation.
I agree with the reasoning and filed #1008 to reject multi-statements on prepare.
Side note:
The following:
prepareStatement has no side effects
is correct for the core DuckDB, but not necessary correct for queries that go to extensions. Getting columns metadata ("binding" step) may or may not have side-effects in extensions (in core extensions we are striving for it to have no side effects). For example when odbc_scanner extension is used, preparing the query which contains odbc_query() table function will call "prepare" on a third-party ODBC driver, and some drivers will just run the query in remote DB (and discard the result) to get the column types.
Activity
staticlibs commentedon Nov 25, 2026
Hi, thanks for the report!
These multi-statement support is a UX feature, it was there from the beginning, before the prepared and non-prepared statements got separate implementation.
I agree with the reasoning and filed #1008 to reject multi-statements on prepare.
Side note:
The following:
is correct for the core DuckDB, but not necessary correct for queries that go to extensions. Getting columns metadata ("binding" step) may or may not have side-effects in extensions (in core extensions we are striving for it to have no side effects). For example when
odbc_scannerextension is used, preparing the query which containsodbc_query()table function will call "prepare" on a third-party ODBC driver, and some drivers will just run the query in remote DB (and discard the result) to get the column types.Disallow multiple statements in prepare (2.0)
staticlibs commentedon Nov 25, 2026
Just FYI, the patched version (compatible with v2.0.1 extensions) is available in this repo:
Disallow multiple concatenated statements in prepare