forked from apache/spark
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SPARK-42680][CONNECT][TESTS] Create the helper function withSQLConf …
…for connect test framework ### What changes were proposed in this pull request? Spark SQL have the helper function `withSQLConf` that is easy to change SQL config and make test easy. ### Why are the changes needed? Make the connect test cases easy to implement. ### Does this PR introduce _any_ user-facing change? No, it is a test only change. ### How was this patch tested? Test case updated. Closes apache#40296 from beliefer/SPARK-42680. Authored-by: Jiaan Geng <[email protected]> Signed-off-by: Hyukjin Kwon <[email protected]>
- Loading branch information
1 parent
dfdc4a1
commit 201e08c
Showing
2 changed files
with
54 additions
and
5 deletions.
There are no files selected for viewing
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
52 changes: 52 additions & 0 deletions
52
connector/connect/client/jvm/src/test/scala/org/apache/spark/sql/SQLHelper.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,52 @@ | ||
/* | ||
* 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 | ||
|
||
trait SQLHelper { | ||
|
||
def spark: SparkSession | ||
|
||
/** | ||
* Sets all SQL configurations specified in `pairs`, calls `f`, and then restores all SQL | ||
* configurations. | ||
*/ | ||
protected def withSQLConf(pairs: (String, String)*)(f: => Unit): Unit = { | ||
val (keys, values) = pairs.unzip | ||
val currentValues = keys.map { key => | ||
if (spark.conf.getOption(key).isDefined) { | ||
Some(spark.conf.get(key)) | ||
} else { | ||
None | ||
} | ||
} | ||
(keys, values).zipped.foreach { (k, v) => | ||
if (spark.conf.isModifiable(k)) { | ||
spark.conf.set(k, v) | ||
} else { | ||
throw new AnalysisException(s"Cannot modify the value of a static config: $k") | ||
} | ||
|
||
} | ||
try f | ||
finally { | ||
keys.zip(currentValues).foreach { | ||
case (key, Some(value)) => spark.conf.set(key, value) | ||
case (key, None) => spark.conf.unset(key) | ||
} | ||
} | ||
} | ||
} |