wangzhigang1999 commented on code in PR #7419:
URL: https://github.com/apache/kyuubi/pull/7419#discussion_r3151638435


##########
externals/kyuubi-jdbc-engine/src/main/scala/org/apache/kyuubi/engine/jdbc/dialect/JdbcDialect.scala:
##########
@@ -82,6 +82,20 @@ abstract class JdbcDialect extends SupportServiceLoader with 
Logging {
 
   def getSchemaHelper(): SchemaHelper
 
+  /**
+   * Switch the current database on the backend connection.
+   *
+   * Called during session open when the client specified a database in the 
connection URL
+   * (populated by the Hive JDBC driver into `USE_DATABASE`). Dialects that 
support an
+   * in-session database context switch (e.g. MySQL's `USE db`) should 
override this and
+   * return `true` on success; the default is a no-op that returns `false`, 
meaning the
+   * backend has no notion of "current database" that applies to this session.
+   *
+   * The return value is used by `JdbcOperationManager` to decide whether the 
requested
+   * database can be treated as the effective schema filter for metadata 
operations.
+   */
+  def setCurrentDatabase(connection: Connection, database: String): Boolean = 
false

Review Comment:
   Good question — I looked into this and it's a bit subtle. `setSchema` 
doesn't reliably work for the MySQL family in mysql-connector-j's default 
config. From 
[`ConnectionImpl.java`](https://github.com/mysql/mysql-connector-j/blob/release/9.x/src/main/user-impl/java/com/mysql/cj/jdbc/ConnectionImpl.java#L2626)
 (9.x):
   
   ```java
   // line 2625
   public void setSchema(String schema) throws SQLException {
       checkClosed();
       if 
(this.propertySet.<DatabaseTerm>getEnumProperty(PropertyKey.databaseTerm).getValue()
 == DatabaseTerm.SCHEMA) {
           setDatabase(schema);
       }
   }
   
   // line 2143
   public void setCatalog(final String catalog) throws SQLException {
       if 
(this.propertySet.<DatabaseTerm>getEnumProperty(PropertyKey.databaseTerm).getValue()
 == DatabaseTerm.CATALOG) {
           setDatabase(catalog);
       }
   }
   ```
   
   The driver gates both methods on the `databaseTerm` connection property:
   
   | `databaseTerm` | `setCatalog(x)` | `setSchema(x)` |
   |---|---|---|
   | `CATALOG` (default) | sends `COM_INIT_DB` | silent no-op |
   | `SCHEMA` | silent no-op | sends `COM_INIT_DB` |
   
   So calling `setSchema` alone would silently do nothing under the default 
config (and reproduce the original bug). Note this also affects 
`StarRocksDialect` / `DorisDialect` since they extend `MySQLDialect` and use 
the same driver — `databaseTerm` is a driver-side property, not a server 
feature.
   
   Two options I'm considering, happy to take direction:
   
   **Option A — keep `USE db` as-is** in `MySQLDialect` (current PR). Simple, 
always works regardless of driver config, but it's hand-rolled SQL.
   
   **Option B — go through JDBC standard API, MySQL-family only**:
   
   Keep `JdbcDialect.setCurrentDatabase` as the default no-op returning `false` 
(PG / Oracle / etc. unchanged from this PR). Only `MySQLDialect` overrides it:
   
   ```scala
   // MySQLDialect: call both — under mysql-connector-j, exactly one is a real 
op
   // for either databaseTerm value, the other is a guaranteed no-op.
   // Inherited by StarRocksDialect / DorisDialect.
   override def setCurrentDatabase(connection: Connection, database: String): 
Boolean = {
     connection.setCatalog(database)
     connection.setSchema(database)
     true
   }
   ```
   
   This still avoids the hand-rolled `USE` SQL and gets the driver hooks 
(`useLocalSessionState`, `ConnectionLifecycleInterceptor`), but doesn't touch 
any non-MySQL dialect's behavior. 
   
   WDYT?
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to