Skip to content

Commit 04c443c

Browse files
committed
SPARK-5068: fix bug when partition path doesn't exists #2
1 parent 41f60ce commit 04c443c

File tree

2 files changed

+97
-1
lines changed

2 files changed

+97
-1
lines changed

sql/hive/src/main/scala/org/apache/spark/sql/hive/TableReader.scala

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,39 @@ class HadoopTableReader(
142142
partitionToDeserializer: Map[HivePartition,
143143
Class[_ <: Deserializer]],
144144
filterOpt: Option[PathFilter]): RDD[Row] = {
145-
val hivePartitionRDDs = partitionToDeserializer.map { case (partition, partDeserializer) =>
145+
// SPARK-5068:get FileStatus and do the filtering locally when the path is not exists
146+
147+
var existPathSet =collection.mutable.Set[String]()
148+
var pathPatternSet = collection.mutable.Set[String]()
149+
150+
val hivePartitionRDDs = partitionToDeserializer.filter {
151+
case (partition, partDeserializer) =>
152+
153+
def updateExistPathSetByPathPattern(pathPatternStr:String ){
154+
val pathPattern = new Path(pathPatternStr)
155+
val fs = pathPattern.getFileSystem(sc.hiveconf)
156+
val matchs = fs.globStatus(pathPattern);
157+
matchs.map( fileStatus =>(existPathSet+= fileStatus.getPath.toString))
158+
}
159+
// convert /demo/data/year/month/day to /demo/data/**/**/**/
160+
def getPathPatternByPath(parNum:Int,tpath:Path):String = {
161+
var path = tpath
162+
for (i <- (1 to parNum)) { path = path.getParent }
163+
val tails = (1 to parNum).map(_ => "*").mkString("/","/","/")
164+
path.toString + tails
165+
}
166+
167+
val partPath = HiveShim.getDataLocationPath(partition)
168+
val partNum = Utilities.getPartitionDesc(partition).getPartSpec.size();
169+
var pathPatternStr = getPathPatternByPath(partNum,partPath)
170+
if(!pathPatternSet.contains(pathPatternStr)){
171+
pathPatternSet+=pathPatternStr
172+
updateExistPathSetByPathPattern(pathPatternStr)
173+
}
174+
existPathSet.contains(partPath.toString)
175+
176+
}
177+
.map { case (partition, partDeserializer) =>
146178
val partDesc = Utilities.getPartitionDesc(partition)
147179
val partPath = HiveShim.getDataLocationPath(partition)
148180
val inputPathStr = applyFilterIfNeeded(partPath, filterOpt)
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.spark.sql.hive
19+
20+
import java.io.File
21+
22+
import com.google.common.io.Files
23+
import org.apache.spark.sql.{QueryTest, _}
24+
import org.apache.spark.sql.hive.test.TestHive
25+
import org.apache.spark.util.Utils
26+
/* Implicits */
27+
import org.apache.spark.sql.hive.test.TestHive._
28+
29+
30+
31+
class QueryPartitionSuite extends QueryTest {
32+
import org.apache.spark.sql.hive.test.TestHive.implicits._
33+
34+
test("SPARK-5068: query data when path doesn't exists"){
35+
val testData = TestHive.sparkContext.parallelize(
36+
(1 to 10).map(i => TestData(i, i.toString))).toDF()
37+
testData.registerTempTable("testData")
38+
39+
val tmpDir = Files.createTempDir()
40+
//create the table for test
41+
sql(s"CREATE TABLE table_with_partition(key int,value string) PARTITIONED by (ds string) location '${tmpDir.toURI.toString}' ")
42+
sql("INSERT OVERWRITE TABLE table_with_partition partition (ds='1') SELECT key,value FROM testData")
43+
sql("INSERT OVERWRITE TABLE table_with_partition partition (ds='2') SELECT key,value FROM testData")
44+
sql("INSERT OVERWRITE TABLE table_with_partition partition (ds='3') SELECT key,value FROM testData")
45+
sql("INSERT OVERWRITE TABLE table_with_partition partition (ds='4') SELECT key,value FROM testData")
46+
47+
//test for the exist path
48+
checkAnswer(sql("select key,value from table_with_partition"),
49+
testData.toSchemaRDD.collect ++ testData.toSchemaRDD.collect
50+
++ testData.toSchemaRDD.collect ++ testData.toSchemaRDD.collect)
51+
52+
//delect the path of one partition
53+
val folders = tmpDir.listFiles.filter(_.isDirectory)
54+
Utils.deleteRecursively(folders(0))
55+
56+
//test for affter delete the path
57+
checkAnswer(sql("select key,value from table_with_partition"),
58+
testData.toSchemaRDD.collect ++ testData.toSchemaRDD.collect
59+
++ testData.toSchemaRDD.collect)
60+
61+
sql("DROP TABLE table_with_partition")
62+
sql("DROP TABLE createAndInsertTest")
63+
}
64+
}

0 commit comments

Comments
 (0)