-
Notifications
You must be signed in to change notification settings - Fork 28.9k
[Spark-10625] [SQL] Spark SQL JDBC read/write is unable to handle JDBC Drivers that adds unserializable objects into connection properties #8785
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
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 |
|---|---|---|
|
|
@@ -38,6 +38,9 @@ private[sql] case class JDBCPartitioningInfo( | |
| numPartitions: Int) | ||
|
|
||
| private[sql] object JDBCRelation extends Logging { | ||
|
|
||
| import scala.collection.JavaConverters._ | ||
|
|
||
| /** | ||
| * Given a partitioning schematic (a column of integral type, a number of | ||
| * partitions, and upper and lower bounds on the column's value), generate | ||
|
|
@@ -99,6 +102,16 @@ private[sql] object JDBCRelation extends Logging { | |
| } | ||
| ans.toArray | ||
| } | ||
|
|
||
| def getEffectiveProperties( | ||
| connectionProperties: Properties, | ||
| extraOptions: scala.collection.Map[String, String] = Map()): Properties = { | ||
|
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. Still need not be qualified; then I think this need not wrap?
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. Qualifier removed, but if without wrap the line exceeds 100 characters. |
||
| val props = new Properties() | ||
| props.putAll(extraOptions.asJava) | ||
| // connectionProperties should override settings in extraOptions | ||
| props.putAll(connectionProperties) | ||
|
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. How does this handle inherited properties? E.g. if
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. yes they can be preserved by the deep copy, but unfortunately any changes on parent properties AFTER the deep copy won't be preserved in returned value. I expect callers of this function to use its returned value transiently. |
||
| props | ||
| } | ||
| } | ||
|
|
||
| private[sql] case class JDBCRelation( | ||
|
|
@@ -127,7 +140,7 @@ private[sql] case class JDBCRelation( | |
| sparkSession.sparkContext, | ||
| schema, | ||
| url, | ||
| properties, | ||
| JDBCRelation.getEffectiveProperties(properties), | ||
| table, | ||
| requiredColumns, | ||
| filters, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| /* | ||
| * 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.jdbc | ||
|
|
||
| import java.sql.{Connection, DriverManager} | ||
| import java.util.Properties | ||
| import java.util.logging.Logger | ||
|
|
||
| object UnserializableDriverHelper { | ||
|
|
||
| import scala.collection.JavaConverters._ | ||
|
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 imported locally in a few places, why? Below you don't import org.h2.Driver though. I'm not worried about changing it though.
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. Patches should target specific problem and minimize collateral impact. No? Then I won't import utility package in upcoming pull requests.
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 meant why is it imported inside the object scope rather than with other imports -- the use of the converters is fine and important. There are some special cases where qualified name or local imports are needed but I think by convention, the strong default is to declare all of them at the top of the compilation unit, especially fairly common ones.
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. OK, I was under the false impression that import of implicits may cause conflicts. But in a well designed library this rarely happens
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. That's a reasonable argument, though we have imported this set of implicits widely elsewhere in the code
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. Please move this import to the top; the arguments RE: scope of implicits are different on a case-by-case basis, but JavaConverters is a case that should pretty much always be safe to put in a somewhat global scope. |
||
|
|
||
| def replaceDriverDuring[T](f: => T): T = { | ||
| object UnserializableH2Driver extends org.h2.Driver { | ||
|
|
||
| override def connect(url: String, info: Properties): Connection = { | ||
|
|
||
| val result = super.connect(url, info) | ||
| info.put("unserializableDriver", this) | ||
| result | ||
| } | ||
|
|
||
| override def getParentLogger: Logger = null | ||
| } | ||
|
|
||
| val oldDrivers = DriverManager.getDrivers.asScala.toList.filter(_.acceptsURL("jdbc:h2:")) | ||
| oldDrivers.foreach(DriverManager.deregisterDriver) | ||
| DriverManager.registerDriver(UnserializableH2Driver) | ||
|
|
||
| val result = try { | ||
| f | ||
| } finally { | ||
| DriverManager.deregisterDriver(UnserializableH2Driver) | ||
| oldDrivers.foreach(DriverManager.registerDriver) | ||
| } | ||
| result | ||
| } | ||
| } | ||
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.
this indentation is still a little funky, see https://cwiki.apache.org/confluence/display/SPARK/Spark+Code+Style+Guide
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.
@tribbloid I think Holden's comment still stands -- see how other methods wrap args. I also don't think you need to fully-qualify
scala.collection.Maphere?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.
+1 on Sean's comments. Also, could you add a one- or two-line comment to explain what's going on here? Maybe give this method Scaladoc?