steveloughran commented on a change in pull request #33828:
URL: https://github.com/apache/spark/pull/33828#discussion_r823138978



##########
File path: 
core/src/main/scala/org/apache/spark/internal/io/FileCommitProtocol.scala
##########
@@ -232,6 +253,89 @@ object FileCommitProtocol extends Logging {
   def getStagingDir(path: String, jobId: String): Path = {
     new Path(path, ".spark-staging-" + jobId)
   }
+
+  def externalTempPath(
+      path: Path,
+      hadoopConf: Configuration,
+      stagingDir: String,
+      engineType: String,
+      jobId: String): Path = {
+    val extURI = path.toUri
+    if (extURI.getScheme == "viewfs") {

Review comment:
       always a bit brittle using the uri scheme. any way to avoid?

##########
File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/internal/SQLConf.scala
##########
@@ -3542,6 +3542,17 @@ object SQLConf {
     .booleanConf
     .createWithDefault(false)
 
+  val EXEC_STAGING_DIR = buildConf("spark.sql.exec.stagingDir")
+      .doc("The staging directory of Spark job. Spark uses it to deal with 
files with " +
+        "absolute output path, or writing data into partitioned directory " +
+        "when dynamic partition overwrite mode is on. " +
+        "Default value means staging directory is under table path.")
+      .version("3.3.0")
+      .internal()
+      .stringConf
+      .checkValue(!_.isEmpty, "Should not pass an empty string as staging 
diretory.")

Review comment:
       typo

##########
File path: 
sql/core/src/test/scala/org/apache/spark/sql/sources/StagingInsertSuite.scala
##########
@@ -0,0 +1,70 @@
+/*
+ * 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
+
+import org.apache.spark.SparkConf
+import org.apache.spark.sql.{QueryTest, Row}
+import org.apache.spark.sql.internal.SQLConf
+import org.apache.spark.sql.test.SharedSparkSession
+import org.apache.spark.util.Utils
+
+class StagingInsertSuite extends QueryTest with SharedSparkSession {
+  import testImplicits._
+
+  val stagingDir = Utils.createTempDir()

Review comment:
       be nice if this could be subclassed so we could add tests which tried 
this on, say abfs or gcs

##########
File path: 
core/src/main/scala/org/apache/spark/internal/io/FileCommitProtocol.scala
##########
@@ -232,6 +253,89 @@ object FileCommitProtocol extends Logging {
   def getStagingDir(path: String, jobId: String): Path = {
     new Path(path, ".spark-staging-" + jobId)
   }
+
+  def externalTempPath(
+      path: Path,
+      hadoopConf: Configuration,
+      stagingDir: String,
+      engineType: String,
+      jobId: String): Path = {
+    val extURI = path.toUri
+    if (extURI.getScheme == "viewfs") {
+      getExtTmpPathRelTo(path.getParent, hadoopConf, stagingDir, engineType, 
jobId)
+    } else {
+      new Path(getExternalScratchDir(extURI, hadoopConf, stagingDir, 
engineType, jobId),
+        "-ext-10000")
+    }
+  }
+
+  private def getExtTmpPathRelTo(
+      path: Path,
+      hadoopConf: Configuration,
+      stagingDir: String,
+      engineType: String,
+      jobId: String): Path = {
+    // Hive uses 10000
+    new Path(getStagingDir(path, hadoopConf, stagingDir, engineType, jobId), 
"-ext-10000")
+  }
+
+  private def getExternalScratchDir(
+      extURI: URI,
+      hadoopConf: Configuration,
+      stagingDir: String,
+      engineType: String,
+      jobId: String): Path = {
+    getStagingDir(
+      new Path(extURI.getScheme, extURI.getAuthority, extURI.getPath),
+      hadoopConf,
+      stagingDir,
+      engineType,
+      jobId)
+  }
+
+  def getStagingDir(
+      inputPath: Path,
+      hadoopConf: Configuration,
+      stagingDir: String,
+      engineType: String,
+      jobId: String): Path = {
+    val inputPathName: String = inputPath.toString
+    val fs: FileSystem = inputPath.getFileSystem(hadoopConf)
+    var stagingPathName: String =
+      if (inputPathName.indexOf(stagingDir) == -1) {
+        new Path(inputPathName, stagingDir).toString
+      } else {
+        inputPathName.substring(0, inputPathName.indexOf(stagingDir) + 
stagingDir.length)
+      }
+
+    // SPARK-20594: This is a walk-around fix to resolve a Hive bug. Hive 
requires that the
+    // staging directory needs to avoid being deleted when users set 
hive.exec.stagingdir
+    // under the table directory.
+    if (isSubDir(new Path(stagingPathName), inputPath, fs) &&
+      
!stagingPathName.stripPrefix(inputPathName).stripPrefix("/").startsWith(".")) {
+      logDebug(s"The staging dir '$stagingPathName' should be a child 
directory starts " +
+        "with '.' to avoid being deleted if we set hive.exec.stagingdir under 
the table " +
+        "directory.")
+      stagingPathName = new Path(inputPathName, ".hive-staging").toString
+    }
+
+    val dir = fs.makeQualified(
+      new Path(stagingPathName + "_" + executionId(engineType) + "-" + jobId))
+    logDebug("Created staging dir = " + dir + " for path = " + inputPath)

Review comment:
       s" $expansion strings"?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to