Github user HyukjinKwon commented on a diff in the pull request:

    https://github.com/apache/spark/pull/12601#discussion_r66716162
  
    --- Diff: 
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/jdbc/JdbcRelationProvider.scala
 ---
    @@ -19,37 +19,105 @@ package org.apache.spark.sql.execution.datasources.jdbc
     
     import java.util.Properties
     
    -import org.apache.spark.sql.SQLContext
    -import org.apache.spark.sql.sources.{BaseRelation, DataSourceRegister, 
RelationProvider}
    +import org.apache.spark.sql.{DataFrame, SaveMode, SQLContext}
    +import org.apache.spark.sql.sources.{BaseRelation, 
CreatableRelationProvider, DataSourceRegister, RelationProvider, 
SchemaRelationProvider}
    +import org.apache.spark.sql.types.StructType
     
    -class JdbcRelationProvider extends RelationProvider with 
DataSourceRegister {
    +class JdbcRelationProvider extends CreatableRelationProvider
    +  with SchemaRelationProvider with RelationProvider with 
DataSourceRegister {
     
       override def shortName(): String = "jdbc"
     
    -  /** Returns a new base relation with the given parameters. */
       override def createRelation(
           sqlContext: SQLContext,
           parameters: Map[String, String]): BaseRelation = {
    -    val jdbcOptions = new JDBCOptions(parameters)
    -    if (jdbcOptions.partitionColumn != null
    -      && (jdbcOptions.lowerBound == null
    -        || jdbcOptions.upperBound == null
    -        || jdbcOptions.numPartitions == null)) {
    +    createRelation(sqlContext, parameters, null)
    +  }
    +
    +  /** Returns a new base relation with the given parameters. */
    +  override def createRelation(
    +      sqlContext: SQLContext,
    +      parameters: Map[String, String],
    +      schema: StructType): BaseRelation = {
    +    val url = parameters.getOrElse("url", sys.error("Option 'url' not 
specified"))
    +    val table = parameters.getOrElse("dbtable", sys.error("Option 
'dbtable' not specified"))
    +    val partitionColumn = parameters.getOrElse("partitionColumn", null)
    +    val lowerBound = parameters.getOrElse("lowerBound", null)
    +    val upperBound = parameters.getOrElse("upperBound", null)
    +    val numPartitions = parameters.getOrElse("numPartitions", null)
    +
    +    if (partitionColumn != null
    +      && (lowerBound == null || upperBound == null || numPartitions == 
null)) {
           sys.error("Partitioning incompletely specified")
         }
     
    -    val partitionInfo = if (jdbcOptions.partitionColumn == null) {
    -      null
    -    } else {
    +    val partitionInfo = if (partitionColumn == null) null
    +    else {
           JDBCPartitioningInfo(
    -        jdbcOptions.partitionColumn,
    -        jdbcOptions.lowerBound.toLong,
    -        jdbcOptions.upperBound.toLong,
    -        jdbcOptions.numPartitions.toInt)
    +        partitionColumn, lowerBound.toLong, upperBound.toLong, 
numPartitions.toInt)
         }
         val parts = JDBCRelation.columnPartition(partitionInfo)
         val properties = new Properties() // Additional properties that we 
will pass to getConnection
         parameters.foreach(kv => properties.setProperty(kv._1, kv._2))
    -    JDBCRelation(jdbcOptions.url, jdbcOptions.table, parts, 
properties)(sqlContext.sparkSession)
    +    JDBCRelation(url, table, parts, properties, 
Option(schema))(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
    +   */
    +  override def createRelation(
    +      sqlContext: SQLContext,
    +      mode: SaveMode,
    +      parameters: Map[String, String],
    +      data: DataFrame): BaseRelation = {
    +    val url = parameters.getOrElse("url",
    +      sys.error("Saving jdbc source requires url to be set." +
    +        " (ie. df.option(\"url\", \"ACTUAL_URL\")"))
    +    val table = parameters.getOrElse("dbtable", 
parameters.getOrElse("table",
    +      sys.error("Saving jdbc source requires dbtable to be set." +
    +        " (ie. df.option(\"dbtable\", \"ACTUAL_DB_TABLE\")")))
    +
    +    import collection.JavaConverters._
    +    val props = new Properties()
    +    props.putAll(parameters.asJava)
    +    val conn = JdbcUtils.createConnectionFactory(url, props)()
    +
    +    try {
    +      val tableExists = JdbcUtils.tableExists(conn, url, table)
    +
    +      val (doCreate, doSave) = (mode, tableExists) match {
    +        case (SaveMode.Ignore, true) => (false, false)
    +        case (SaveMode.ErrorIfExists, true) => sys.error(s"Table $table 
already exists.")
    +        case (SaveMode.Overwrite, true) =>
    +          JdbcUtils.dropTable(conn, table)
    +          (true, true)
    +        case (SaveMode.Append, true) => (false, true)
    +        case (_, true) => sys.error(s"Unexpected SaveMode, '$mode', for 
handling existing tables.")
    +        case (_, false) => (true, true)
    --- End diff --
    
    Personally, I think this combinations of booleans are confusing. It might 
be better if they have some variables so that we can understand what each 
`case` means.


---
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]

Reply via email to