Skip to content

Commit c02e8ba

Browse files
committed
fix
1 parent bae5baa commit c02e8ba

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

sql/hive/src/main/scala/org/apache/spark/sql/hive/execution/SaveAsHiveFile.scala

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,12 +210,11 @@ private[hive] trait SaveAsHiveFile extends DataWritingCommand {
210210
stagingDir)
211211
}
212212

213-
private def getStagingDir(
213+
private[hive] def getStagingDir(
214214
inputPath: Path,
215215
hadoopConf: Configuration,
216216
stagingDir: String): Path = {
217-
val inputPathUri: URI = inputPath.toUri
218-
val inputPathName: String = inputPathUri.getPath
217+
val inputPathName: String = inputPath.toString
219218
val fs: FileSystem = inputPath.getFileSystem(hadoopConf)
220219
var stagingPathName: String =
221220
if (inputPathName.indexOf(stagingDir) == -1) {
@@ -228,7 +227,7 @@ private[hive] trait SaveAsHiveFile extends DataWritingCommand {
228227
// staging directory needs to avoid being deleted when users set hive.exec.stagingdir
229228
// under the table directory.
230229
if (isSubDir(new Path(stagingPathName), inputPath, fs) &&
231-
!stagingPathName.stripPrefix(inputPathName).stripPrefix(File.separator).startsWith(".")) {
230+
!stagingPathName.stripPrefix(inputPathName).stripPrefix("/").startsWith(".")) {
232231
logDebug(s"The staging dir '$stagingPathName' should be a child directory starts " +
233232
"with '.' to avoid being deleted if we set hive.exec.stagingdir under the table " +
234233
"directory.")

sql/hive/src/test/scala/org/apache/spark/sql/hive/InsertSuite.scala

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@ package org.apache.spark.sql.hive
1919

2020
import java.io.File
2121

22-
import org.scalatest.BeforeAndAfter
22+
import org.apache.hadoop.fs.Path
23+
import org.scalatest.{BeforeAndAfter, PrivateMethodTester}
2324

2425
import org.apache.spark.SparkException
2526
import org.apache.spark.sql.{QueryTest, _}
2627
import org.apache.spark.sql.catalyst.parser.ParseException
2728
import org.apache.spark.sql.catalyst.plans.logical.InsertIntoTable
29+
import org.apache.spark.sql.hive.execution.InsertIntoHiveTable
2830
import org.apache.spark.sql.hive.test.TestHiveSingleton
2931
import org.apache.spark.sql.internal.SQLConf
3032
import org.apache.spark.sql.test.SQLTestUtils
@@ -36,7 +38,7 @@ case class TestData(key: Int, value: String)
3638
case class ThreeCloumntable(key: Int, value: String, key1: String)
3739

3840
class InsertSuite extends QueryTest with TestHiveSingleton with BeforeAndAfter
39-
with SQLTestUtils {
41+
with SQLTestUtils with PrivateMethodTester {
4042
import spark.implicits._
4143

4244
override lazy val testData = spark.sparkContext.parallelize(
@@ -550,6 +552,32 @@ class InsertSuite extends QueryTest with TestHiveSingleton with BeforeAndAfter
550552
}
551553
}
552554

555+
test("SPARK-27552: hive.exec.stagingdir is invalid on Windows OS") {
556+
val conf = spark.sessionState.newHadoopConf()
557+
val inputPath = new Path("/tmp/b/c")
558+
var stagingDir = "tmp/b"
559+
val saveHiveFile = InsertIntoHiveTable(null, Map.empty, null, false, false, null)
560+
val getStagingDir = PrivateMethod[Path]('getStagingDir)
561+
var path = saveHiveFile invokePrivate getStagingDir(inputPath, conf, stagingDir)
562+
assert(path.toString.indexOf("/tmp/b_hive_") != -1)
563+
564+
stagingDir = "tmp/b/c"
565+
path = saveHiveFile invokePrivate getStagingDir(inputPath, conf, stagingDir)
566+
assert(path.toString.indexOf("/tmp/b/c/.hive-staging_hive_") != -1)
567+
568+
stagingDir = "d/e"
569+
path = saveHiveFile invokePrivate getStagingDir(inputPath, conf, stagingDir)
570+
assert(path.toString.indexOf("/tmp/b/c/.hive-staging_hive_") != -1)
571+
572+
stagingDir = ".d/e"
573+
path = saveHiveFile invokePrivate getStagingDir(inputPath, conf, stagingDir)
574+
assert(path.toString.indexOf("/tmp/b/c/.d/e_hive_") != -1)
575+
576+
stagingDir = "/tmp/c/"
577+
path = saveHiveFile invokePrivate getStagingDir(inputPath, conf, stagingDir)
578+
assert(path.toString.indexOf("/tmp/c_hive_") != -1)
579+
}
580+
553581
test("insert overwrite to dir from hive metastore table") {
554582
withTempDir { dir =>
555583
val path = dir.toURI.getPath

0 commit comments

Comments
 (0)