Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions core/src/test/scala/org/apache/spark/SparkFunSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,18 @@ abstract class SparkFunSuite
}
}

/**
* Creates a temporary directory with the provided prefix, which is then passed to `f` and will
* be deleted after `f` returns.
*/
protected def withTempDir(prefix: String)(f: File => Unit): Unit = {
val dir = Utils.createTempDir(namePrefix = prefix)
try f(dir) finally {
Utils.deleteRecursively(dir)
}
}


/**
* Creates a temporary directory containing a secret file, which is then passed to `f` and
* will be deleted after `f` returns.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import org.apache.spark.sql.catalyst.expressions.{Expression, Literal}
import org.apache.spark.sql.catalyst.parser.ParseException
import org.apache.spark.sql.catalyst.plans.logical.{ColumnDefinition, CreateTable, LocalRelation, LogicalPlan, OptionList, RecoverPartitions, ShowFunctions, ShowNamespaces, ShowTables, UnresolvedTableSpec, View}
import org.apache.spark.sql.catalyst.types.DataTypeUtils
import org.apache.spark.sql.catalyst.util.CaseInsensitiveMap
import org.apache.spark.sql.connector.catalog.{CatalogManager, SupportsNamespaces, TableCatalog}
import org.apache.spark.sql.connector.catalog.CatalogV2Implicits.{CatalogHelper, MultipartIdentifierHelper, NamespaceHelper, TransformHelper}
import org.apache.spark.sql.errors.QueryCompilationErrors
Expand Down Expand Up @@ -671,12 +672,9 @@ class CatalogImpl(sparkSession: SparkSession) extends Catalog {
} else {
CatalogTableType.MANAGED
}
val location = if (storage.locationUri.isDefined) {
val locationStr = storage.locationUri.get.toString
Some(locationStr)
} else {
None
}

// The location in UnresolvedTableSpec should be the original user-provided path string.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just in case, shall we add a legacy config to restore to the old behavior?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about this. I did not add a config because the change in behaviour is only for new tables. Existing tables should continue working. I am also suspecting that there is no real usage of this API for tables with special chars. But I don't have a strong opinion. WDYT?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK let's keep it simple. double escaping is definitely a bug and no one should rely on it (existing tables won't be affected)

val location = CaseInsensitiveMap(options).get("path")

val newOptions = OptionList(options.map { case (key, value) =>
(key, Literal(value).asInstanceOf[Expression])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,7 @@ class CatalogSuite extends SharedSparkSession with AnalysisTest with BeforeAndAf
val description = "this is a test table"

withTable("t") {
withTempDir { dir =>
withTempDir(prefix = "test%prefix") { dir =>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure we need this new util function with prefix parameter, we can just do

withTempDir { baseDir =>
  val dir = new File(baseDir, "test%prefix")

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would also work, but I think is useful to have it since we should expand more on testing of paths with special chars. In the future we could also change the default value, so this helper will be needed to avoid the special chars.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's orthogonal from this bug fix, isn't it? I'd recommend to proceed that test utility contribution as an independent PR, @cstavr . We can review separately.

That would also work, but I think is useful to have it since we should expand more on testing of paths with special chars. In the future we could also change the default value, so this helper will be needed to avoid the special chars.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK. I removed the test util.

spark.catalog.createTable(
tableName = "t",
source = "json",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1162,7 +1162,7 @@ class FileStreamSourceSuite extends FileStreamSourceTest {
}

private def testThresholdLogic(option: String): Unit = {
withTempDir { case src =>
withTempDir { src =>
var lastFileModTime: Option[Long] = None

/** Create a text file with a single data item */
Expand Down Expand Up @@ -1258,7 +1258,7 @@ class FileStreamSourceSuite extends FileStreamSourceTest {
private def testIncorrectThresholdValues(option: String): Unit = {
val testTable = s"${option}_test"
withTable(testTable) {
withTempDir { case src =>
withTempDir { src =>
def testIncorrectValue(value: String): Unit = {
val df = spark.readStream.option(option, value).text(src.getCanonicalPath)
val e = intercept[StreamingQueryException] {
Expand Down Expand Up @@ -1287,7 +1287,7 @@ class FileStreamSourceSuite extends FileStreamSourceTest {
testQuietly("SPARK-46641: max bytes per trigger & max files per trigger - both set") {
val testTable = "maxBytesPerTrigger_maxFilesPerTrigger_test"
withTable(testTable) {
withTempDir { case src =>
withTempDir { src =>
val df = spark.readStream
.option("maxBytesPerTrigger", "1")
.option("maxFilesPerTrigger", "1")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,17 @@ private[sql] trait SQLTestUtils extends SparkFunSuite with SQLTestUtilsBase with
}
}

/**
* Creates a temporary directory with the provided prefix, which is then passed to `f` and will
* be deleted after `f` returns.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In a new test utility PR, could you revise this PR description? The AS-IS description looks like a copy of the description of super.withTempDir. However, this has more features, doesn't it?

*/
protected override def withTempDir(prefix: String)(f: File => Unit): Unit = {
super.withTempDir(prefix) { dir =>
f(dir)
waitForTasksToFinish()
}
}

/**
* A helper function for turning off/on codegen.
*/
Expand Down