-
Notifications
You must be signed in to change notification settings - Fork 28.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
591 additions
and
328 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
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
81 changes: 81 additions & 0 deletions
81
sql/core/src/main/scala/org/apache/spark/sql/test/SQLTestUtils.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,81 @@ | ||
/* | ||
* 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.test | ||
|
||
import java.io.File | ||
|
||
import scala.util.Try | ||
|
||
import org.apache.spark.sql.SQLContext | ||
import org.apache.spark.util.Utils | ||
|
||
trait SQLTestUtils { | ||
val sqlContext: SQLContext | ||
|
||
import sqlContext.{conf, sparkContext} | ||
|
||
protected def configuration = sparkContext.hadoopConfiguration | ||
|
||
/** | ||
* Sets all SQL configurations specified in `pairs`, calls `f`, and then restore all SQL | ||
* configurations. | ||
* | ||
* @todo Probably this method should be moved to a more general place | ||
*/ | ||
protected def withSQLConf(pairs: (String, String)*)(f: => Unit): Unit = { | ||
val (keys, values) = pairs.unzip | ||
val currentValues = keys.map(key => Try(conf.getConf(key)).toOption) | ||
(keys, values).zipped.foreach(conf.setConf) | ||
try f finally { | ||
keys.zip(currentValues).foreach { | ||
case (key, Some(value)) => conf.setConf(key, value) | ||
case (key, None) => conf.unsetConf(key) | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Generates a temporary path without creating the actual file/directory, then pass it to `f`. If | ||
* a file/directory is created there by `f`, it will be delete after `f` returns. | ||
* | ||
* @todo Probably this method should be moved to a more general place | ||
*/ | ||
protected def withTempPath(f: File => Unit): Unit = { | ||
val path = Utils.createTempDir() | ||
path.delete() | ||
try f(path) finally Utils.deleteRecursively(path) | ||
} | ||
|
||
/** | ||
* Creates a temporary directory, which is then passed to `f` and will be deleted after `f` | ||
* returns. | ||
* | ||
* @todo Probably this method should be moved to a more general place | ||
*/ | ||
protected def withTempDir(f: File => Unit): Unit = { | ||
val dir = Utils.createTempDir().getCanonicalFile | ||
try f(dir) finally Utils.deleteRecursively(dir) | ||
} | ||
|
||
/** | ||
* Drops temporary table `tableName` after calling `f`. | ||
*/ | ||
protected def withTempTable(tableName: String)(f: => Unit): Unit = { | ||
try f finally sqlContext.dropTempTable(tableName) | ||
} | ||
} |
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
Oops, something went wrong.