Skip to content

Commit a487d97

Browse files
[KYUUBI #7305][FOLLOWUP] Mirror JDBC standard setSchema in JdbcDialect
Per review feedback, replicate the JDBC `Connection#setSchema(String)` API on JdbcDialect with a leading `conn` parameter. The default forwards to the standard API; dialects whose driver does not support it can override to a no-op. Drop the boolean return value — callers treat the call as if it always takes effect. JdbcSessionImpl now records `effectiveDatabase` unconditionally after the call, decoupling "backend connection switched" from "engine metadata scope".
1 parent 5c616a0 commit a487d97

3 files changed

Lines changed: 17 additions & 22 deletions

File tree

externals/kyuubi-jdbc-engine/src/main/scala/org/apache/kyuubi/engine/jdbc/dialect/JdbcDialect.scala

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -83,18 +83,15 @@ abstract class JdbcDialect extends SupportServiceLoader with Logging {
8383
def getSchemaHelper(): SchemaHelper
8484

8585
/**
86-
* Switch the current database on the backend connection.
87-
*
88-
* Called during session open when the client specified a database in the connection URL
89-
* (populated by the Hive JDBC driver into `USE_DATABASE`). Dialects that support an
90-
* in-session database context switch should override this and return `true` on success;
91-
* the default is a no-op that returns `false`, meaning the backend has no notion of
92-
* "current database" that applies to this session.
93-
*
94-
* The return value is used by `JdbcOperationManager` to decide whether the requested
95-
* database can be treated as the effective schema filter for metadata operations.
86+
* Mirrors the standard `Connection#setSchema(String)` JDBC API with a leading
87+
* `conn` parameter, allowing dialects to customize the call. The default forwards
88+
* to the standard API; dialects whose driver does not support it (e.g. Phoenix,
89+
* Impala) should override this to a no-op. Callers should treat this as if it
90+
* always takes effect.
9691
*/
97-
def setCurrentDatabase(connection: Connection, database: String): Boolean = false
92+
def setSchema(conn: Connection, schema: String): Unit = {
93+
conn.setSchema(schema)
94+
}
9895

9996
def cancelStatement(jdbcStatement: Statement): Unit = {
10097
if (jdbcStatement != null) {

externals/kyuubi-jdbc-engine/src/main/scala/org/apache/kyuubi/engine/jdbc/dialect/MySQLDialect.scala

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,15 +150,14 @@ class MySQLDialect extends JdbcDialect {
150150
query.toString()
151151
}
152152

153-
override def setCurrentDatabase(connection: Connection, database: String): Boolean = {
153+
override def setSchema(conn: Connection, schema: String): Unit = {
154154
// mysql-connector-j makes setCatalog/setSchema mutually exclusive based on the
155155
// `databaseTerm` connection property: with the default CATALOG, setCatalog issues
156156
// COM_INIT_DB and setSchema is a silent no-op; with SCHEMA it is the inverse.
157157
// Calling both means exactly one fires regardless of how the user configured the
158158
// URL. StarRocksDialect / DorisDialect inherit this via MySQLDialect.
159-
connection.setCatalog(database)
160-
connection.setSchema(database)
161-
true
159+
conn.setCatalog(schema)
160+
conn.setSchema(schema)
162161
}
163162

164163
override def getTRowSetGenerator(): JdbcTRowSetGenerator = new MySQLTRowSetGenerator

externals/kyuubi-jdbc-engine/src/main/scala/org/apache/kyuubi/engine/jdbc/session/JdbcSessionImpl.scala

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,10 @@ class JdbcSessionImpl(
4848
private var databaseMetaData: DatabaseMetaData = _
4949

5050
/**
51-
* The database that was successfully applied to the backend connection during session open.
52-
* `None` when no `USE_DATABASE` was requested, or when the request was for "default"
53-
* (the Hive JDBC driver's fallback) and the backend had no such database.
51+
* The database the client requested via `USE_DATABASE` at session open. Used by
52+
* `JdbcOperationManager` as the metadata schema filter. `None` when no `USE_DATABASE`
53+
* was requested, or when the request was for "default" (the Hive JDBC driver's fallback)
54+
* and the dialect's `setSchema` call failed.
5455
*/
5556
private[jdbc] var effectiveDatabase: Option[String] = None
5657

@@ -78,10 +79,8 @@ class JdbcSessionImpl(
7879
sessionConf.get(ENGINE_JDBC_SESSION_INITIALIZE_SQL))
7980
conf.get(USE_DATABASE).foreach { database =>
8081
try {
81-
if (JdbcDialects.get(sessionConf).setCurrentDatabase(sessionConnection, database)) {
82-
effectiveDatabase = Some(database)
83-
info(s"Switched to database: $database")
84-
}
82+
JdbcDialects.get(sessionConf).setSchema(sessionConnection, database)
83+
effectiveDatabase = Some(database)
8584
} catch {
8685
case NonFatal(e) if database == "default" =>
8786
// The Hive JDBC driver sends "default" when the user didn't specify a database

0 commit comments

Comments
 (0)