wangzhigang1999 commented on code in PR #7419:
URL: https://github.com/apache/kyuubi/pull/7419#discussion_r3154003255
##########
externals/kyuubi-jdbc-engine/src/main/scala/org/apache/kyuubi/engine/jdbc/dialect/MySQLDialect.scala:
##########
@@ -122,6 +150,16 @@ class MySQLDialect extends JdbcDialect {
query.toString()
}
+ override def setSchema(conn: Connection, schema: String): Unit = {
+ // mysql-connector-j makes setCatalog/setSchema mutually exclusive based
on the
+ // `databaseTerm` connection property: with the default CATALOG,
setCatalog issues
+ // COM_INIT_DB and setSchema is a silent no-op; with SCHEMA it is the
inverse.
+ // Calling both means exactly one fires regardless of how the user
configured the
+ // URL. StarRocksDialect / DorisDialect inherit this via MySQLDialect.
+ conn.setCatalog(schema)
+ conn.setSchema(schema)
Review Comment:
Good point to think through. The dual `setCatalog` + `setSchema` is safe
because mysql-connector-j picks one of the two to actually send and silently
swallows the other inside the driver, based on its `databaseTerm` connection
property: with the default `CATALOG`, `setCatalog` sends a "switch database"
command and `setSchema` does nothing; with `SCHEMA` it's the reverse. So no
matter how the user configured the URL, the StarRocks/Doris server only ever
receives one "switch database" command, never two.
Confirmed by reading the driver source — both methods funnel into the same
`setDatabase(...)` call after the `databaseTerm` check, see
[`ConnectionImpl.java#L2145`](https://github.com/mysql/mysql-connector-j/blob/release/9.x/src/main/user-impl/java/com/mysql/cj/jdbc/ConnectionImpl.java#L2145).
`setCatalog` is literally `if (databaseTerm == CATALOG) setDatabase(catalog);`
and `setSchema` is the symmetric `if (databaseTerm == SCHEMA)
setDatabase(schema);` — exactly one branch fires per call site, neither throws
when its branch is skipped.
The server treats that command as "switch the current database" — it does
**not** treat it as "switch catalog" (catalog switching on StarRocks/Doris is a
separate `SET CATALOG` SQL statement, not something the MySQL protocol can do).
The name we pass in comes from `USE_DATABASE`, which the Hive JDBC driver
parses out of the URL path. The Hive URL syntax has no concept of catalog, so
this name is always a database in the current (default) catalog.
--
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]