-
Notifications
You must be signed in to change notification settings - Fork 29.3k
[SPARK-38054][SQL] Supports list namespaces in JDBC v2 MySQL dialect #35355
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 9 commits
2dd6350
c6a9542
f220c56
b6efaa8
29d8da5
4978c25
826c00f
b3a1c3b
19fb6cd
d3a004d
5182cdf
daa11d2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -979,36 +979,34 @@ object JdbcUtils extends Logging with SQLConfHelper { | |
| namespace: String, | ||
| comment: String): Unit = { | ||
| val dialect = JdbcDialects.get(options.url) | ||
| executeStatement(conn, options, s"CREATE SCHEMA ${dialect.quoteIdentifier(namespace)}") | ||
| if (!comment.isEmpty) createNamespaceComment(conn, options, namespace, comment) | ||
| dialect.createSchema(conn, options, namespace, comment) | ||
| } | ||
|
|
||
| def createNamespaceComment( | ||
| def namespaceExists(conn: Connection, options: JDBCOptions, namespace: String): Boolean = { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. since we are touching this file, can we rename
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK |
||
| val dialect = JdbcDialects.get(options.url) | ||
| dialect.schemasExists(conn, options, namespace) | ||
| } | ||
|
|
||
| def listNamespaces(conn: Connection, options: JDBCOptions): Array[Array[String]] = { | ||
| val dialect = JdbcDialects.get(options.url) | ||
| dialect.listSchemas(conn, options) | ||
| } | ||
|
|
||
| def alterNamespaceComment( | ||
| conn: Connection, | ||
| options: JDBCOptions, | ||
| namespace: String, | ||
| comment: String): Unit = { | ||
| val dialect = JdbcDialects.get(options.url) | ||
| try { | ||
| executeStatement( | ||
| conn, options, dialect.getSchemaCommentQuery(namespace, comment)) | ||
| } catch { | ||
| case e: Exception => | ||
| logWarning("Cannot create JDBC catalog comment. The catalog comment will be ignored.") | ||
| } | ||
| executeStatement(conn, options, dialect.getSchemaCommentQuery(namespace, comment)) | ||
| } | ||
|
|
||
| def removeNamespaceComment( | ||
| conn: Connection, | ||
| options: JDBCOptions, | ||
| namespace: String): Unit = { | ||
| val dialect = JdbcDialects.get(options.url) | ||
| try { | ||
| executeStatement(conn, options, dialect.removeSchemaCommentQuery(namespace)) | ||
| } catch { | ||
| case e: Exception => | ||
| logWarning("Cannot drop JDBC catalog comment.") | ||
| } | ||
| executeStatement(conn, options, dialect.removeSchemaCommentQuery(namespace)) | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -1148,11 +1146,17 @@ object JdbcUtils extends Logging with SQLConfHelper { | |
| } | ||
| } | ||
|
|
||
| def executeQuery(conn: Connection, options: JDBCOptions, sql: String): ResultSet = { | ||
| def executeQuery(conn: Connection, options: JDBCOptions, sql: String)( | ||
| f: ResultSet => Unit): Unit = { | ||
| val statement = conn.createStatement | ||
| try { | ||
| statement.setQueryTimeout(options.queryTimeout) | ||
| statement.executeQuery(sql) | ||
| val rs = statement.executeQuery(sql) | ||
| try { | ||
| f(rs) | ||
| } finally { | ||
| rs.close() | ||
| } | ||
| } finally { | ||
| statement.close() | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -229,6 +229,51 @@ abstract class JdbcDialect extends Serializable with Logging{ | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * Create schema with an optional comment. Empty string means no comment. | ||
| */ | ||
| def createSchema( | ||
| conn: Connection, options: JDBCOptions, schema: String, comment: String): Unit = { | ||
| val statement = conn.createStatement | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shall we pass
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK |
||
| try { | ||
| statement.setQueryTimeout(options.queryTimeout) | ||
| val schemaCommentQuery = if (comment.nonEmpty) { | ||
| getSchemaCommentQuery(schema, comment) | ||
| } else { | ||
| comment | ||
| } | ||
| statement.executeUpdate(s"CREATE SCHEMA ${quoteIdentifier(schema)}") | ||
| if (comment.nonEmpty) { | ||
| statement.executeUpdate(schemaCommentQuery) | ||
| } | ||
| } finally { | ||
| statement.close() | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Check schema exists or not. | ||
| */ | ||
| def schemasExists(conn: Connection, options: JDBCOptions, schema: String): Boolean = { | ||
| val rs = conn.getMetaData.getSchemas(null, schema) | ||
| while (rs.next()) { | ||
| if (rs.getString(1) == schema) return true; | ||
| } | ||
| false | ||
| } | ||
|
|
||
| /** | ||
| * Lists all the schemas in this table. | ||
| */ | ||
| def listSchemas(conn: Connection, options: JDBCOptions): Array[Array[String]] = { | ||
| val schemaBuilder = ArrayBuilder.make[Array[String]] | ||
| val rs = conn.getMetaData.getSchemas() | ||
| while (rs.next()) { | ||
| schemaBuilder += Array(rs.getString(1)) | ||
| } | ||
| schemaBuilder.result | ||
| } | ||
|
|
||
| /** | ||
| * Return Some[true] iff `TRUNCATE TABLE` causes cascading default. | ||
| * Some[true] : TRUNCATE TABLE causes cascading. | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the actual change here? Get rid of the try-catch in
createNamespaceComment?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I want keep the atomic of create namespace with comment.