-
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 5 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 |
|---|---|---|
|
|
@@ -70,10 +70,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( | ||
|
|
@@ -198,4 +205,36 @@ class OracleIntegrationSuite extends DockerJDBCIntegrationSuite with SharedSQLCo | |
| val types = rows(0).toSeq.map(x => x.getClass.toString) | ||
| assert(types(1).equals("class java.sql.Timestamp")) | ||
| } | ||
|
|
||
| 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)) | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -120,6 +120,7 @@ class JDBCOptions( | |
| // TODO: to reuse the existing partition parameters for those partition specific options | ||
| val createTableOptions = parameters.getOrElse(JDBC_CREATE_TABLE_OPTIONS, "") | ||
| val createTableColumnTypes = parameters.get(JDBC_CREATE_TABLE_COLUMN_TYPES) | ||
| val customSchema = parameters.get(JDBC_CUSTOM_SCHEMA) | ||
|
||
| val batchSize = { | ||
| val size = parameters.getOrElse(JDBC_BATCH_INSERT_SIZE, "1000").toInt | ||
| require(size >= 1, | ||
|
|
@@ -156,6 +157,7 @@ object JDBCOptions { | |
| val JDBC_TRUNCATE = newOption("truncate") | ||
| val JDBC_CREATE_TABLE_OPTIONS = newOption("createTableOptions") | ||
| val JDBC_CREATE_TABLE_COLUMN_TYPES = newOption("createTableColumnTypes") | ||
| val JDBC_CUSTOM_SCHEMA = newOption("customSchema") | ||
| val JDBC_BATCH_INSERT_SIZE = newOption("batchsize") | ||
| val JDBC_TXN_ISOLATION_LEVEL = newOption("isolationLevel") | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -82,7 +82,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 |
|---|---|---|
|
|
@@ -907,7 +907,7 @@ 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? |
||
| ignore("SPARK-16848: jdbc API throws an exception for user specified schema") { | ||
|
||
| val schema = StructType(Seq( | ||
| StructField("name", StringType, false), StructField("theid", IntegerType, false))) | ||
| val parts = Array[String]("THEID < 2", "THEID >= 2") | ||
|
|
||
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.