Github user gatorsmile commented on a diff in the pull request:
https://github.com/apache/spark/pull/15263#discussion_r80845381
--- Diff:
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/jdbc/JdbcRelationProvider.scala
---
@@ -50,67 +51,48 @@ 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.
--- End diff --
This comment looks weird. In RDBMS, users might do `drop and recreate` a
table, instead of truncating it. Thus, please change it to
`// Otherwise, do not truncate but just drop and recreate`
---
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]