@@ -129,17 +129,26 @@ case class InsertIntoHiveTable(
129129 import org .apache .spark .sql .hive .client .hive ._
130130
131131 val hiveVersion = externalCatalog.asInstanceOf [HiveExternalCatalog ].client.version
132+ // Before Hive 1.1, when inserting into a table, Hive will create the staging directory under
133+ // a common scratch directory. After the writing is finished, Hive will simply empty the table
134+ // directory and move the staging directory to it.
135+ // After Hive 1.1, Hive will create the staging directory under the table directory, and when
136+ // moving staging directory to table directory, Hive will still empty the table directory, but
137+ // will exclude the staging directory there.
138+ // We have to follow the Hive behavior here, to avoid troubles. For example, if we create
139+ // staging directory under the table director for Hive prior to 1.1, the staging directory will
140+ // be removed by Hive when Hive is trying to empty the table directory.
132141 if (hiveVersion == v12 || hiveVersion == v13 || hiveVersion == v14 || hiveVersion == v1_0) {
133- oldStyleExternalTempPath (path)
142+ oldVersionExternalTempPath (path)
134143 } else if (hiveVersion == v1_1 || hiveVersion == v1_2) {
135- newStyleExternalTempPath (path)
144+ newVersionExternalTempPath (path)
136145 } else {
137146 throw new IllegalStateException (" Unsupported hive version: " + hiveVersion.fullVersion)
138147 }
139148 }
140149
141150 // Mostly copied from Context.java#getExternalTmpPath of Hive 0.13
142- def oldStyleExternalTempPath (path : Path ): Path = {
151+ def oldVersionExternalTempPath (path : Path ): Path = {
143152 val extURI : URI = path.toUri
144153 val scratchPath = new Path (scratchDir, executionId)
145154 var dirPath = new Path (
@@ -164,7 +173,7 @@ case class InsertIntoHiveTable(
164173 }
165174
166175 // Mostly copied from Context.java#getExternalTmpPath of Hive 1.2
167- def newStyleExternalTempPath (path : Path ): Path = {
176+ def newVersionExternalTempPath (path : Path ): Path = {
168177 val extURI : URI = path.toUri
169178 if (extURI.getScheme == " viewfs" ) {
170179 getExtTmpPathRelTo(path.getParent)
0 commit comments