Github user HyukjinKwon commented on a diff in the pull request:
https://github.com/apache/spark/pull/15263#discussion_r80739029
--- Diff:
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/jdbc/JdbcRelationProvider.scala
---
@@ -50,67 +51,52 @@ class JdbcRelationProvider extends
CreatableRelationProvider
JDBCRelation(jdbcOptions.url, jdbcOptions.table, parts,
properties)(sqlContext.sparkSession)
}
- /*
- * The following structure applies to this code:
- * | tableExists | !tableExists
-
*------------------------------------------------------------------------------------
- * Ignore | BaseRelation | CreateTable, saveTable,
BaseRelation
- * ErrorIfExists | ERROR | CreateTable, saveTable,
BaseRelation
- * Overwrite* | (DropTable, CreateTable,) | CreateTable, saveTable,
BaseRelation
- * | saveTable, BaseRelation |
- * Append | saveTable, BaseRelation | CreateTable, saveTable,
BaseRelation
- *
- * *Overwrite & tableExists with truncate, will not drop & create, but
instead truncate
- */
override def createRelation(
sqlContext: SQLContext,
mode: SaveMode,
parameters: Map[String, String],
- data: DataFrame): BaseRelation = {
- val jdbcOptions = new JDBCOptions(parameters)
- val url = jdbcOptions.url
- val table = jdbcOptions.table
-
+ df: DataFrame): BaseRelation = {
+ val options = new JDBCOptions(parameters)
+ val url = options.url
+ val table = options.table
+ val createTableOptions = options.createTableOptions
+ val isTruncate = options.isTruncate
val props = new Properties()
props.putAll(parameters.asJava)
- val conn = JdbcUtils.createConnectionFactory(url, props)()
+ val conn = JdbcUtils.createConnectionFactory(url, props)()
try {
val tableExists = JdbcUtils.tableExists(conn, url, table)
+ if (tableExists) {
+ mode match {
+ case SaveMode.Overwrite =>
+ if (isTruncate &&
isCascadingTruncateTable(url).contains(false)) {
+ // In this case, we should truncate table and then load.
+ truncateTable(conn, table)
+ saveTable(df, url, table, props)
+ } else {
+ // Otherwise, do not truncate but just drop.
+ dropTable(conn, table)
+ createTable(df, url, table, createTableOptions, conn)
+ saveTable(df, url, table, props)
+ }
+ case SaveMode.Append =>
+ saveTable(df, url, table, props)
- val (doCreate, doSave) = (mode, tableExists) match {
- case (SaveMode.Ignore, true) => (false, false)
- case (SaveMode.ErrorIfExists, true) => throw new AnalysisException(
- s"Table or view '$table' already exists, and SaveMode is set to
ErrorIfExists.")
- case (SaveMode.Overwrite, true) =>
- if (jdbcOptions.isTruncate &&
JdbcUtils.isCascadingTruncateTable(url) == Some(false)) {
- JdbcUtils.truncateTable(conn, table)
- (false, true)
- } else {
- JdbcUtils.dropTable(conn, table)
- (true, true)
- }
- case (SaveMode.Append, true) => (false, true)
- case (_, true) => throw new IllegalArgumentException(s"Unexpected
SaveMode, '$mode'," +
- " for handling existing tables.")
- case (_, false) => (true, true)
- }
+ case SaveMode.ErrorIfExists =>
+ sys.error(s"Table $table already exists.")
+
+ case SaveMode.Ignore => // Just ignore this case.
+ }
+ } else {
+ mode match {
+ case SaveMode.Overwrite | SaveMode.Append |
SaveMode.ErrorIfExists =>
+ createTable(df, url, table, createTableOptions, conn)
+ saveTable(df, url, table, props)
- if (doCreate) {
- val schema = JdbcUtils.schemaString(data, url)
- // To allow certain options to append when create a new table,
which can be
- // table_options or partition_options.
- // E.g., "CREATE TABLE t (name string) ENGINE=InnoDB DEFAULT
CHARSET=utf8"
- val createtblOptions = jdbcOptions.createTableOptions
- val sql = s"CREATE TABLE $table ($schema) $createtblOptions"
- val statement = conn.createStatement
- try {
- statement.executeUpdate(sql)
- } finally {
- statement.close()
+ case SaveMode.Ignore => // Just ignore this case.
--- End diff --
Definitely. I will mark this PR as `[WIP]`. Thanks for your quick look.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]