-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-18538] [SQL] Fix Concurrent Table Fetching Using DataFrameReader JDBC APIs #15975
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 5 commits
bcc86c0
5c5b3ca
483c2a4
fdb7392
1b0caea
c29199c
57e72f5
404aa22
1987e77
728c103
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 |
|---|---|---|
|
|
@@ -159,7 +159,11 @@ class DataFrameReader private[sql](sparkSession: SparkSession) extends Logging { | |
| * @since 1.4.0 | ||
| */ | ||
| def jdbc(url: String, table: String, properties: Properties): DataFrame = { | ||
| jdbc(url, table, JDBCRelation.columnPartition(null), properties) | ||
| // properties should override settings in extraOptions. | ||
| this.extraOptions = this.extraOptions ++ properties.asScala | ||
| // explicit url and dbtable should override all | ||
| this.extraOptions += ("url" -> url, "dbtable" -> table) | ||
| format("jdbc").load() | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -192,9 +196,13 @@ class DataFrameReader private[sql](sparkSession: SparkSession) extends Logging { | |
| upperBound: Long, | ||
| numPartitions: Int, | ||
| connectionProperties: Properties): DataFrame = { | ||
| val partitioning = JDBCPartitioningInfo(columnName, lowerBound, upperBound, numPartitions) | ||
| val parts = JDBCRelation.columnPartition(partitioning) | ||
| jdbc(url, table, parts, connectionProperties) | ||
| // columnName, lowerBound, upperBound and numPartitions override settings in extraOptions. | ||
| this.extraOptions ++= Map( | ||
| JDBCOptions.JDBC_PARTITION_COLUMN -> columnName, | ||
| JDBCOptions.JDBC_LOWER_BOUND -> lowerBound.toString, | ||
| JDBCOptions.JDBC_UPPER_BOUND -> upperBound.toString, | ||
| JDBCOptions.JDBC_NUM_PARTITIONS -> numPartitions.toString) | ||
| jdbc(url, table, connectionProperties) | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -220,22 +228,14 @@ class DataFrameReader private[sql](sparkSession: SparkSession) extends Logging { | |
| table: String, | ||
| predicates: Array[String], | ||
| connectionProperties: Properties): DataFrame = { | ||
| // connectionProperties should override settings in extraOptions. | ||
| val params = extraOptions.toMap ++ connectionProperties.asScala.toMap | ||
|
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. this parameter is never used, when did we introduce it?
Member
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. Introduced in the PR #15499 which was merged to 2.1 only |
||
| val options = new JDBCOptions(url, table, params) | ||
| val parts: Array[Partition] = predicates.zipWithIndex.map { case (part, i) => | ||
| JDBCPartition(part, i) : Partition | ||
| } | ||
| jdbc(url, table, parts, connectionProperties) | ||
| } | ||
|
|
||
| private def jdbc( | ||
| url: String, | ||
| table: String, | ||
| parts: Array[Partition], | ||
| connectionProperties: Properties): DataFrame = { | ||
| // connectionProperties should override settings in extraOptions. | ||
| this.extraOptions = this.extraOptions ++ connectionProperties.asScala | ||
| // explicit url and dbtable should override all | ||
| this.extraOptions += ("url" -> url, "dbtable" -> table) | ||
| format("jdbc").load() | ||
| val relation = JDBCRelation(parts, options)(sparkSession) | ||
| sparkSession.baseRelationToDataFrame(relation) | ||
|
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. so we have 2 code path for jdbc? The API with
Member
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. Yeah. The predicate-based API is very useful for the advanced JDBC users. |
||
| } | ||
|
|
||
| /** | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -137,7 +137,8 @@ private[sql] case class JDBCRelation( | |
| } | ||
|
|
||
| override def toString: String = { | ||
| val partitioningInfo = if (parts.nonEmpty) s" [numPartitions=${parts.length}]" else "" | ||
| // credentials should not be included in the plan output, table information is sufficient. | ||
| s"JDBCRelation(${jdbcOptions.table})" | ||
| s"JDBCRelation(${jdbcOptions.table})" + partitioningInfo | ||
|
Member
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. If parts is empty, this string looks weird like "JDBCRelation(.....)()" as I tried locally.
Member
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. Fixed. Thanks! |
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -209,6 +209,14 @@ class JDBCSuite extends SparkFunSuite | |
| conn.close() | ||
| } | ||
|
|
||
| // Check whether the tables are fetched in the expected degree of parallelism | ||
| def checkNumPartitions(df: DataFrame, expectedNumPartitions: Int): Unit = { | ||
| val explain = ExplainCommand(df.queryExecution.logical, extended = true) | ||
| val plans = spark.sessionState.executePlan(explain).executedPlan | ||
| val expectedMsg = s"${JDBCOptions.JDBC_NUM_PARTITIONS}=$expectedNumPartitions" | ||
|
||
| assert(plans.executeCollect().map(_.toString).mkString.contains(expectedMsg)) | ||
| } | ||
|
|
||
| test("SELECT *") { | ||
| assert(sql("SELECT * FROM foobar").collect().size === 3) | ||
| } | ||
|
|
@@ -313,13 +321,23 @@ class JDBCSuite extends SparkFunSuite | |
| } | ||
|
|
||
| test("SELECT * partitioned") { | ||
| assert(sql("SELECT * FROM parts").collect().size == 3) | ||
| val df = sql("SELECT * FROM parts") | ||
| checkNumPartitions(df, expectedNumPartitions = 3) | ||
| assert(df.collect().length == 3) | ||
| } | ||
|
|
||
| test("SELECT WHERE (simple predicates) partitioned") { | ||
| assert(sql("SELECT * FROM parts WHERE THEID < 1").collect().size === 0) | ||
| assert(sql("SELECT * FROM parts WHERE THEID != 2").collect().size === 2) | ||
| assert(sql("SELECT THEID FROM parts WHERE THEID = 1").collect().size === 1) | ||
| val df1 = sql("SELECT * FROM parts WHERE THEID < 1") | ||
| checkNumPartitions(df1, expectedNumPartitions = 3) | ||
| assert(df1.collect().length === 0) | ||
|
|
||
| val df2 = sql("SELECT * FROM parts WHERE THEID != 2") | ||
| checkNumPartitions(df2, expectedNumPartitions = 3) | ||
| assert(df2.collect().length === 2) | ||
|
|
||
| val df3 = sql("SELECT THEID FROM parts WHERE THEID = 1") | ||
| checkNumPartitions(df3, expectedNumPartitions = 3) | ||
| assert(df3.collect().length === 1) | ||
| } | ||
|
|
||
| test("SELECT second field partitioned") { | ||
|
|
@@ -370,41 +388,46 @@ class JDBCSuite extends SparkFunSuite | |
| } | ||
|
|
||
| test("Partitioning via JDBCPartitioningInfo API") { | ||
| assert( | ||
| spark.read.jdbc(urlWithUserAndPass, "TEST.PEOPLE", "THEID", 0, 4, 3, new Properties()) | ||
| .collect().length === 3) | ||
| val df = spark.read.jdbc(urlWithUserAndPass, "TEST.PEOPLE", "THEID", 0, 4, 3, new Properties()) | ||
| checkNumPartitions(df, expectedNumPartitions = 3) | ||
| assert(df.collect().length === 3) | ||
| } | ||
|
|
||
| test("Partitioning via list-of-where-clauses API") { | ||
| val parts = Array[String]("THEID < 2", "THEID >= 2") | ||
| assert(spark.read.jdbc(urlWithUserAndPass, "TEST.PEOPLE", parts, new Properties()) | ||
| .collect().length === 3) | ||
| val df = spark.read.jdbc(urlWithUserAndPass, "TEST.PEOPLE", parts, new Properties()) | ||
| checkNumPartitions(df, expectedNumPartitions = 2) | ||
| assert(df.collect().length === 3) | ||
| } | ||
|
|
||
| test("Partitioning on column that might have null values.") { | ||
| assert( | ||
| spark.read.jdbc(urlWithUserAndPass, "TEST.EMP", "theid", 0, 4, 3, new Properties()) | ||
| .collect().length === 4) | ||
| assert( | ||
| spark.read.jdbc(urlWithUserAndPass, "TEST.EMP", "THEID", 0, 4, 3, new Properties()) | ||
| .collect().length === 4) | ||
| val df = spark.read.jdbc(urlWithUserAndPass, "TEST.EMP", "theid", 0, 4, 3, new Properties()) | ||
| checkNumPartitions(df, expectedNumPartitions = 3) | ||
| assert(df.collect().length === 4) | ||
|
|
||
| val df2 = spark.read.jdbc(urlWithUserAndPass, "TEST.EMP", "THEID", 0, 4, 3, new Properties()) | ||
| checkNumPartitions(df2, expectedNumPartitions = 3) | ||
| assert(df2.collect().length === 4) | ||
|
|
||
| // partitioning on a nullable quoted column | ||
| assert( | ||
| spark.read.jdbc(urlWithUserAndPass, "TEST.EMP", """"Dept"""", 0, 4, 3, new Properties()) | ||
| .collect().length === 4) | ||
| } | ||
|
|
||
| test("Partitioning on column where numPartitions is zero") { | ||
| val res = spark.read.jdbc( | ||
| url = urlWithUserAndPass, | ||
| table = "TEST.seq", | ||
| columnName = "id", | ||
| lowerBound = 0, | ||
| upperBound = 4, | ||
| numPartitions = 0, | ||
| connectionProperties = new Properties() | ||
| ) | ||
| assert(res.count() === 8) | ||
| val e = intercept[IllegalArgumentException] { | ||
| spark.read.jdbc( | ||
| url = urlWithUserAndPass, | ||
| table = "TEST.seq", | ||
| columnName = "id", | ||
| lowerBound = 0, | ||
| upperBound = 4, | ||
| numPartitions = 0, | ||
| connectionProperties = new Properties() | ||
| ) | ||
| }.getMessage | ||
| assert(e.contains("Invalid value `0` for parameter `numPartitions`. The minimum value is 1.")) | ||
| } | ||
|
|
||
| test("Partitioning on column where numPartitions are more than the number of total rows") { | ||
|
|
@@ -417,6 +440,7 @@ class JDBCSuite extends SparkFunSuite | |
| numPartitions = 10, | ||
| connectionProperties = new Properties() | ||
| ) | ||
| checkNumPartitions(res, expectedNumPartitions = 4) | ||
| assert(res.count() === 8) | ||
| } | ||
|
|
||
|
|
@@ -430,6 +454,7 @@ class JDBCSuite extends SparkFunSuite | |
| numPartitions = 4, | ||
| connectionProperties = new Properties() | ||
| ) | ||
| checkNumPartitions(res, expectedNumPartitions = 1) | ||
| assert(res.count() === 8) | ||
| } | ||
|
|
||
|
|
@@ -450,7 +475,9 @@ class JDBCSuite extends SparkFunSuite | |
| } | ||
|
|
||
| test("SELECT * on partitioned table with a nullable partition column") { | ||
| assert(sql("SELECT * FROM nullparts").collect().size == 4) | ||
| val df = sql("SELECT * FROM nullparts") | ||
| checkNumPartitions(df, expectedNumPartitions = 3) | ||
| assert(df.collect().length == 4) | ||
| } | ||
|
|
||
| test("H2 integral types") { | ||
|
|
@@ -722,7 +749,8 @@ class JDBCSuite extends SparkFunSuite | |
| } | ||
| // test the JdbcRelation toString output | ||
| df.queryExecution.analyzed.collect { | ||
| case r: LogicalRelation => assert(r.relation.toString == "JDBCRelation(TEST.PEOPLE)") | ||
| case r: LogicalRelation => | ||
| assert(r.relation.toString == "JDBCRelation(TEST.PEOPLE) [numPartitions=3]") | ||
| } | ||
| } | ||
|
|
||
|
|
||
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.
please use the constant
JDBCOptions.xxx