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

    https://github.com/apache/spark/pull/22560#discussion_r220756677
  
    --- Diff: 
sql/core/src/test/scala/org/apache/spark/sql/execution/datasources/jdbc/JdbcUtilsSuite.scala
 ---
    @@ -65,4 +67,60 @@ class JdbcUtilsSuite extends SparkFunSuite {
         }
         assert(mismatchedInput.getMessage.contains("mismatched input '.' 
expecting"))
       }
    +
    +  test("Custom ConnectionFactory falls back to default when no factory 
specified") {
    +    val options = new JDBCOptions(Map[String, String](
    +      "url" -> "jdbc:mysql://foo.com/bar",
    +      "dbtable" -> "table")
    +    )
    +    assert(options.connectionFactoryProvider eq 
DefaultConnectionFactoryProvider)
    +  }
    +
    +  test("JdbcUtils uses the specified connection factory") {
    +    val options = new JDBCOptions(Map[String, String](
    +      "url" -> "jdbc:mysql://foo.com/bar",
    +      "dbtable" -> "table",
    +      JDBCOptions.JDBC_CONNECTION_FACTORY_PROVIDER ->
    +        "org.apache.spark.sql.execution.datasources.jdbc.TestFactory")
    +    )
    +    val x = intercept[RuntimeException] {
    +      JdbcUtils.createConnectionFactory(options)()
    +    }
    +    assert(x.getMessage == "This message will be tested in test")
    +  }
    +
    +  test("invalid connection factory throws IllegalArgumentException") {
    +
    +    val nonexisting = intercept[IllegalArgumentException] {
    +      new JDBCOptions(Map[String, String](
    +        "url" -> "jdbc:mysql://foo.com/bar",
    +        "dbtable" -> "table",
    +        JDBCOptions.JDBC_CONNECTION_FACTORY_PROVIDER -> "notexistingclass")
    +      )
    +    }
    +    assert(nonexisting.getMessage == "notexistingclass is not a valid 
ConnectionFactoryProvider")
    +
    +    val missingTrait = intercept[IllegalArgumentException] {
    +      new JDBCOptions(Map[String, String](
    +        "url" -> "jdbc:mysql://foo.com/bar",
    +        "dbtable" -> "table",
    +        JDBCOptions.JDBC_CONNECTION_FACTORY_PROVIDER ->
    +          "org.apache.spark.sql.execution.datasources.jdbc.BadFactory")
    +      )
    +    }
    +    assert(missingTrait.getMessage ==
    +      "org.apache.spark.sql.execution.datasources.jdbc.BadFactory" +
    +        " is not a valid ConnectionFactoryProvider")
    +
    +  }
    +}
    +
    +// this one does not implement ConnectionFactoryProvider
    +class BadFactory {
    +
    +}
    +
    +class TestFactory extends ConnectionFactoryProvider {
    +  override def createConnectionFactory(options: JDBCOptions): () => 
Connection =
    --- End diff --
    
    The intent is for the spark jdbc package to allow for custom connection 
factories by specifying a classname in the options to spark.sql.read. This 
class has to implement the trait ConnectionFactoryProvider which in this new 
setup would also be provided by spark jdbc. The only reason we had to copy code 
into our code base was to make changes to JdbcUtils to allow this and for those 
classes in spark jdbc that use JdbcUtils.createConnectionFactory to use ours 
instead. This PR would allow for this behavior out of the box but use the 
default factory when no className for a ConnectionFactoryProvider is specified.


---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to