-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-20427][SQL] Read JDBC table use custom schema #18266
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 8 commits
871c303
0444c4d
06881e8
ffaee42
a984f3b
e0fc6b4
9e6f7cf
5fdd2bb
87df014
63d3244
e08ccbb
247fc78
b8b03e2
b040c72
2ea56fc
1e2c1d9
b38a1a8
0b67f0f
7fc97b4
1fdf002
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 |
|---|---|---|
|
|
@@ -71,10 +71,17 @@ class OracleIntegrationSuite extends DockerJDBCIntegrationSuite with SharedSQLCo | |
| """.stripMargin.replaceAll("\n", " ")).executeUpdate() | ||
| conn.commit() | ||
|
|
||
| conn.prepareStatement("CREATE TABLE ts_with_timezone (id NUMBER(10), t TIMESTAMP WITH TIME ZONE)") | ||
| .executeUpdate() | ||
| conn.prepareStatement("INSERT INTO ts_with_timezone VALUES (1, to_timestamp_tz('1999-12-01 11:00:00 UTC','YYYY-MM-DD HH:MI:SS TZR'))") | ||
| .executeUpdate() | ||
| conn.prepareStatement( | ||
| "CREATE TABLE ts_with_timezone (id NUMBER(10), t TIMESTAMP WITH TIME ZONE)").executeUpdate() | ||
| conn.prepareStatement( | ||
| "INSERT INTO ts_with_timezone VALUES " + | ||
| "(1, to_timestamp_tz('1999-12-01 11:00:00 UTC','YYYY-MM-DD HH:MI:SS TZR'))").executeUpdate() | ||
| conn.commit() | ||
|
|
||
| conn.prepareStatement( | ||
| "CREATE TABLE custom_column_types (id NUMBER, n1 number(1), n2 number(1))").executeUpdate() | ||
| conn.prepareStatement( | ||
| "INSERT INTO custom_column_types values(12312321321321312312312312123, 1, 0)").executeUpdate() | ||
| conn.commit() | ||
|
|
||
| sql( | ||
|
|
@@ -268,4 +275,44 @@ class OracleIntegrationSuite extends DockerJDBCIntegrationSuite with SharedSQLCo | |
| assert(row.getDate(0).equals(dateVal)) | ||
| assert(row.getTimestamp(1).equals(timestampVal)) | ||
| } | ||
|
|
||
| test("SPARK-20427/SPARK-20921: read table use custom schema") { | ||
|
|
||
| // default will throw IllegalArgumentException | ||
| val e = intercept[org.apache.spark.SparkException] { | ||
| spark.read.jdbc(jdbcUrl, "custom_column_types", new Properties()).collect() | ||
| } | ||
| assert(e.getMessage.contains( | ||
| "requirement failed: Decimal precision 39 exceeds max precision 38")) | ||
|
|
||
| // custom schema can read data | ||
| val schema = StructType(Seq( | ||
| StructField("ID", DecimalType(DecimalType.MAX_PRECISION, 0)), | ||
| StructField("N1", IntegerType, true), | ||
| StructField("N2", BooleanType, true))) | ||
|
|
||
| val dfRead = spark.read.schema(schema).jdbc(jdbcUrl, "custom_column_types", new Properties()) | ||
|
||
| val rows = dfRead.collect() | ||
|
|
||
| // verify the data type inserted | ||
| val types = rows(0).toSeq.map(x => x.getClass.toString) | ||
| assert(types(0).equals("class java.math.BigDecimal")) | ||
| assert(types(1).equals("class java.lang.Integer")) | ||
| assert(types(2).equals("class java.lang.Boolean")) | ||
|
|
||
| // verify the value inserted | ||
| val values = rows(0) | ||
| assert(values.getDecimal(0).equals(new java.math.BigDecimal("12312321321321312312312312123"))) | ||
| assert(values.getInt(1).equals(1)) | ||
| assert(values.getBoolean(2).equals(false)) | ||
|
|
||
| // throw exception if custom schema field names does not match table column names | ||
| val wrongSchema = StructType(Seq( | ||
| StructField("ID", DecimalType(DecimalType.MAX_PRECISION, 0)), | ||
| StructField("N2", BooleanType, true))) | ||
|
|
||
| intercept[IllegalArgumentException] { | ||
| spark.read.schema(wrongSchema).jdbc(jdbcUrl, "CUSTOM_COLUMN_TYPES", new Properties()).count() | ||
| }.getMessage.contains("Field ID,N2 does not match ID,N1,N2.") | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -197,11 +197,13 @@ class DataFrameReader private[sql](sparkSession: SparkSession) extends Logging { | |
| * @since 1.4.0 | ||
| */ | ||
| def jdbc(url: String, table: String, properties: Properties): DataFrame = { | ||
| assertNoSpecifiedSchema("jdbc") | ||
| // properties should override settings in extraOptions. | ||
| this.extraOptions ++= properties.asScala | ||
| // explicit url and dbtable should override all | ||
| this.extraOptions += (JDBCOptions.JDBC_URL -> url, JDBCOptions.JDBC_TABLE_NAME -> table) | ||
| if (userSpecifiedSchema.isDefined) { | ||
|
||
| this.extraOptions += (JDBCOptions.JDBC_CUSTOM_SCHEMA -> userSpecifiedSchema.get.json) | ||
| } | ||
| format("jdbc").load() | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -80,7 +80,7 @@ object JDBCRDD extends Logging { | |
| * @return A Catalyst schema corresponding to columns in the given order. | ||
| */ | ||
| private def pruneSchema(schema: StructType, columns: Array[String]): StructType = { | ||
| val fieldMap = Map(schema.fields.map(x => x.metadata.getString("name") -> x): _*) | ||
| val fieldMap = Map(schema.fields.map(x => x.name -> x): _*) | ||
|
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.
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. This is not a related change. Could you revert it back?
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.
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. Sorry, I did not get your point. Could you show me an example? Is it a behavior breaking change?
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.
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. I see. Could we just get rid of the line where we put
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. It seems safe to remove this line. |
||
| new StructType(columns.map(name => fieldMap(name))) | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -111,7 +111,22 @@ private[sql] case class JDBCRelation( | |
|
|
||
| override val needConversion: Boolean = false | ||
|
|
||
| override val schema: StructType = JDBCRDD.resolveTable(jdbcOptions) | ||
| override val schema: StructType = { | ||
| val schema = JDBCRDD.resolveTable(jdbcOptions) | ||
| val customSchema = jdbcOptions.customSchema | ||
| if (customSchema.isDefined) { | ||
| val schemaFieldNames = schema.fieldNames.mkString(",") | ||
| val customSchemaFieldNames = customSchema.get.fieldNames.mkString(",") | ||
| if(schemaFieldNames.equals(customSchemaFieldNames)) { | ||
|
||
| customSchema.get | ||
| } else { | ||
| throw new IllegalArgumentException( | ||
| s"Field ${customSchemaFieldNames} does not match ${schemaFieldNames}.") | ||
| } | ||
| } else { | ||
| schema | ||
| } | ||
|
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. val tableSchema = JDBCRDD.resolveTable(jdbcOptions)
jdbcOptions.customSchema match {
case Some(customSchema) => JdbcUtils.parseUserSpecifiedColumnTypes(
tableSchema, customSchema, sparkSession.sessionState.conf.resolver)
case None => tableSchema
} |
||
| } | ||
|
|
||
| // Check if JDBCRDD.compileFilter can accept input filters | ||
| override def unhandledFilters(filters: Array[Filter]): Array[Filter] = { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -916,21 +916,6 @@ class JDBCSuite extends SparkFunSuite | |
| assert(new JDBCOptions(CaseInsensitiveMap(parameters)).asConnectionProperties.isEmpty) | ||
| } | ||
|
|
||
| test("SPARK-16848: jdbc API throws an exception for user specified schema") { | ||
|
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. Also add test cases in JDBCSuite.scala? |
||
| val schema = StructType(Seq( | ||
| StructField("name", StringType, false), StructField("theid", IntegerType, false))) | ||
| val parts = Array[String]("THEID < 2", "THEID >= 2") | ||
| val e1 = intercept[AnalysisException] { | ||
| spark.read.schema(schema).jdbc(urlWithUserAndPass, "TEST.PEOPLE", parts, new Properties()) | ||
| }.getMessage | ||
| assert(e1.contains("User specified schema not supported with `jdbc`")) | ||
|
|
||
| val e2 = intercept[AnalysisException] { | ||
| spark.read.schema(schema).jdbc(urlWithUserAndPass, "TEST.PEOPLE", new Properties()) | ||
| }.getMessage | ||
| assert(e2.contains("User specified schema not supported with `jdbc`")) | ||
| } | ||
|
|
||
| test("SPARK-15648: teradataDialect StringType data mapping") { | ||
| val teradataDialect = JdbcDialects.get("jdbc:teradata://127.0.0.1/db") | ||
| assert(teradataDialect.getJDBCType(StringType). | ||
|
|
||
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.
Nit: Change the table names in all the test cases.