-
Notifications
You must be signed in to change notification settings - Fork 28.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SPARK-22387][SQL] Propagate session configs to data source read/writ…
…e options ## What changes were proposed in this pull request? Introduce a new interface `SessionConfigSupport` for `DataSourceV2`, it can help to propagate session configs with the specified key-prefix to all data source operations in this session. ## How was this patch tested? Add new test suite `DataSourceV2UtilsSuite`. Author: Xingbo Jiang <xingbo.jiang@databricks.com> Closes #19861 from jiangxb1987/datasource-configs.
- Loading branch information
1 parent
fb0562f
commit 9c289a5
Showing
5 changed files
with
164 additions
and
8 deletions.
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 | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
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,49 @@ | ||
/* | ||
* 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") | ||
conf.setConfString(s"spark.datasource.$keyPrefix.", "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" | ||
} |