-
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 all 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 |
|---|---|---|
|
|
@@ -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 |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.spark.sql.execution.datasources.jdbc | ||
|
|
||
| import org.apache.spark.SparkFunSuite | ||
| import org.apache.spark.sql.AnalysisException | ||
| import org.apache.spark.sql.catalyst.parser.ParseException | ||
| import org.apache.spark.sql.types._ | ||
|
|
||
| class JdbcUtilsSuite extends SparkFunSuite { | ||
|
|
||
| val tableSchema = StructType(Seq( | ||
| StructField("C1", StringType, false), StructField("C2", IntegerType, false))) | ||
| val caseSensitive = org.apache.spark.sql.catalyst.analysis.caseSensitiveResolution | ||
| val caseInsensitive = org.apache.spark.sql.catalyst.analysis.caseInsensitiveResolution | ||
|
|
||
| test("Parse user specified column types") { | ||
| assert( | ||
| JdbcUtils.getCustomSchema(tableSchema, "C1 DATE, C2 STRING", caseInsensitive) === | ||
| StructType(Seq(StructField("C1", DateType, true), StructField("C2", StringType, true)))) | ||
| assert(JdbcUtils.getCustomSchema(tableSchema, "C1 DATE, C2 STRING", caseSensitive) === | ||
| StructType(Seq(StructField("C1", DateType, true), StructField("C2", StringType, true)))) | ||
| assert( | ||
| JdbcUtils.getCustomSchema(tableSchema, "c1 DATE, C2 STRING", caseInsensitive) === | ||
| StructType(Seq(StructField("c1", DateType, true), StructField("C2", StringType, true)))) | ||
| assert(JdbcUtils.getCustomSchema( | ||
| tableSchema, "c1 DECIMAL(38, 0), C2 STRING", caseInsensitive) === | ||
| StructType(Seq(StructField("c1", DecimalType(38, 0), true), | ||
| StructField("C2", StringType, true)))) | ||
|
|
||
| // Throw AnalysisException | ||
| val duplicate = intercept[AnalysisException]{ | ||
| JdbcUtils.getCustomSchema(tableSchema, "c1 DATE, c1 STRING", caseInsensitive) === | ||
| StructType(Seq(StructField("c1", DateType, true), StructField("c1", StringType, true))) | ||
| } | ||
| assert(duplicate.getMessage.contains( | ||
| "Found duplicate column(s) in the customSchema option value")) | ||
|
|
||
| val allColumns = intercept[AnalysisException]{ | ||
| JdbcUtils.getCustomSchema(tableSchema, "C1 STRING", caseSensitive) === | ||
| StructType(Seq(StructField("C1", DateType, true))) | ||
| } | ||
| assert(allColumns.getMessage.contains("Please provide all the columns,")) | ||
|
|
||
| val caseSensitiveColumnNotFound = intercept[AnalysisException]{ | ||
| JdbcUtils.getCustomSchema(tableSchema, "c1 DATE, C2 STRING", caseSensitive) === | ||
| StructType(Seq(StructField("c1", DateType, true), StructField("C2", StringType, true))) | ||
| } | ||
| assert(caseSensitiveColumnNotFound.getMessage.contains( | ||
| "Please provide all the columns, all columns are: C1,C2;")) | ||
|
|
||
| val caseInsensitiveColumnNotFound = intercept[AnalysisException]{ | ||
| JdbcUtils.getCustomSchema(tableSchema, "c3 DATE, C2 STRING", caseInsensitive) === | ||
| StructType(Seq(StructField("c3", DateType, true), StructField("C2", StringType, true))) | ||
| } | ||
| assert(caseInsensitiveColumnNotFound.getMessage.contains( | ||
| "Please provide all the columns, all columns are: C1,C2;")) | ||
|
|
||
| // Throw ParseException | ||
| val dataTypeNotSupported = intercept[ParseException]{ | ||
| JdbcUtils.getCustomSchema(tableSchema, "c3 DATEE, C2 STRING", caseInsensitive) === | ||
| StructType(Seq(StructField("c3", DateType, true), StructField("C2", StringType, true))) | ||
| } | ||
| assert(dataTypeNotSupported.getMessage.contains("DataType datee is not supported")) | ||
|
|
||
| val mismatchedInput = intercept[ParseException]{ | ||
| JdbcUtils.getCustomSchema(tableSchema, "c3 DATE. C2 STRING", caseInsensitive) === | ||
| StructType(Seq(StructField("c3", DateType, true), StructField("C2", StringType, true))) | ||
| } | ||
| assert(mismatchedInput.getMessage.contains("mismatched input '.' expecting")) | ||
| } | ||
| } |
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.
Move these newly added test cases to JDBCSuite.scala.
Only add a single test case for Oracle data type mappings requested in the JIRA tickets.