-
Notifications
You must be signed in to change notification settings - Fork 28.4k
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
[SPARK-22387][SQL] Propagate session configs to data source read/write options #19861
Closed
Closed
Changes from 13 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
356c55e
propagate session configs to data source options.
jiangxb1987 b076a69
fix scalastyle
jiangxb1987 eaa6cae
style fix
jiangxb1987 ec5723c
move around function withSessionConfig.
jiangxb1987 84df37e
update ConfigSupport
jiangxb1987 0dd7f2e
add method getValidOptions
jiangxb1987 8329a6b
update comments.
jiangxb1987 6b4fcab
update comments
jiangxb1987 ec9a717
simplify ConfigSupport interface.
jiangxb1987 ebb8d86
refactor
jiangxb1987 52aaf51
refactor
jiangxb1987 d964158
refactor DataSourceV2Utils
jiangxb1987 f7d5a4d
update DataSourceV2UtilsSuite
jiangxb1987 5292329
update regex pattern
jiangxb1987 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
sql/core/src/main/java/org/apache/spark/sql/sources/v2/SessionConfigSupport.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* 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.sources.v2; | ||
|
||
import org.apache.spark.annotation.InterfaceStability; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* A mix-in interface for {@link DataSourceV2}. Data sources can implement this interface to | ||
* propagate session configs with the specified key-prefix to all data source operations in this | ||
* session. | ||
*/ | ||
@InterfaceStability.Evolving | ||
public interface SessionConfigSupport { | ||
|
||
/** | ||
* Key prefix of the session configs to propagate. Spark will extract all session configs that | ||
* starts with `spark.datasource.$keyPrefix`, turn `spark.datasource.$keyPrefix.xxx -> yyy` | ||
* into `xxx -> yyy`, and propagate them to all data source operations in this session. | ||
*/ | ||
String keyPrefix(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
...core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/DataSourceV2Utils.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* 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.v2 | ||
|
||
import java.util.regex.Pattern | ||
|
||
import org.apache.spark.internal.Logging | ||
import org.apache.spark.sql.internal.SQLConf | ||
import org.apache.spark.sql.sources.v2.{DataSourceV2, SessionConfigSupport} | ||
|
||
private[sql] object DataSourceV2Utils extends Logging { | ||
|
||
/** | ||
* Helper method that extracts and transforms session configs into k/v pairs, the k/v pairs will | ||
* be used to create data source options. | ||
* Only extract when `ds` implements [[SessionConfigSupport]], in this case we may fetch the | ||
* specified key-prefix from `ds`, and extract session configs with config keys that start with | ||
* `spark.datasource.$keyPrefix`. A session config `spark.datasource.$keyPrefix.xxx -> yyy` will | ||
* be transformed into `xxx -> yyy`. | ||
* | ||
* @param ds a [[DataSourceV2]] object | ||
* @param conf the session conf | ||
* @return an immutable map that contains all the extracted and transformed k/v pairs. | ||
*/ | ||
def extractSessionConfigs(ds: DataSourceV2, conf: SQLConf): Map[String, String] = ds match { | ||
case cs: SessionConfigSupport => | ||
val keyPrefix = cs.keyPrefix() | ||
require(keyPrefix != null, "The data source config key prefix can't be null.") | ||
|
||
val pattern = Pattern.compile(s"^spark\\.datasource\\.$keyPrefix\\.(.*)") | ||
|
||
conf.getAllConfs.flatMap { case (key, value) => | ||
val m = pattern.matcher(key) | ||
if (m.matches() && m.groupCount() > 0) { | ||
Seq((m.group(1), value)) | ||
} else { | ||
Seq.empty | ||
} | ||
} | ||
|
||
case _ => Map.empty | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
sql/core/src/test/scala/org/apache/spark/sql/sources/v2/DataSourceV2UtilsSuite.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* 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.sources.v2 | ||
|
||
import org.apache.spark.SparkFunSuite | ||
import org.apache.spark.sql.execution.datasources.v2.DataSourceV2Utils | ||
import org.apache.spark.sql.internal.SQLConf | ||
|
||
class DataSourceV2UtilsSuite extends SparkFunSuite { | ||
|
||
private val keyPrefix = new DataSourceV2WithSessionConfig().keyPrefix | ||
|
||
test("method withSessionConfig() should propagate session configs correctly") { | ||
// Only match configs with keys start with "spark.datasource.${keyPrefix}". | ||
val conf = new SQLConf | ||
conf.setConfString(s"spark.datasource.$keyPrefix.foo.bar", "false") | ||
conf.setConfString(s"spark.datasource.$keyPrefix.whateverConfigName", "123") | ||
conf.setConfString(s"spark.sql.$keyPrefix.config.name", "false") | ||
conf.setConfString("spark.datasource.another.config.name", "123") | ||
val cs = classOf[DataSourceV2WithSessionConfig].newInstance() | ||
val confs = DataSourceV2Utils.extractSessionConfigs(cs.asInstanceOf[DataSourceV2], conf) | ||
assert(confs.size == 2) | ||
assert(confs.keySet.filter(_.startsWith("spark.datasource")).size == 0) | ||
assert(confs.keySet.filter(_.startsWith("not.exist.prefix")).size == 0) | ||
assert(confs.keySet.contains("foo.bar")) | ||
assert(confs.keySet.contains("whateverConfigName")) | ||
} | ||
} | ||
|
||
class DataSourceV2WithSessionConfig extends SimpleDataSourceV2 with SessionConfigSupport { | ||
|
||
override def keyPrefix: String = "userDefinedDataSource" | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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:
(.*)
->(.+)
. Just to forbid some corner case likespark.datasource.$keyPrefix.