Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 5 additions & 4 deletions core/src/main/scala/org/apache/spark/SparkContext.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1306,11 +1306,12 @@ class SparkContext(config: SparkConf) extends Logging {

/** Build the union of a list of RDDs. */
def union[T: ClassTag](rdds: Seq[RDD[T]]): RDD[T] = withScope {
val partitioners = rdds.flatMap(_.partitioner).toSet
if (rdds.forall(_.partitioner.isDefined) && partitioners.size == 1) {
new PartitionerAwareUnionRDD(this, rdds)
val nonEmptyRdds = rdds.filter(!_.partitions.isEmpty)
val partitioners = nonEmptyRdds.flatMap(_.partitioner).toSet
if (nonEmptyRdds.forall(_.partitioner.isDefined) && partitioners.size == 1) {
new PartitionerAwareUnionRDD(this, nonEmptyRdds)
} else {
new UnionRDD(this, rdds)
new UnionRDD(this, nonEmptyRdds)
}
}

Expand Down
14 changes: 13 additions & 1 deletion core/src/test/scala/org/apache/spark/rdd/RDDSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,16 @@ class RDDSuite extends SparkFunSuite with SharedSparkContext {
}
}

test("SPARK-23778: empty RDD in union should not produce a UnionRDD") {
Copy link
Contributor

Choose a reason for hiding this comment

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

Have we tested when all input RDDs are empty?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

When all RDDs are empty we are returning a UnionRDD. Though in this case it is not a big issue, since a shuffle of an empty RDD is not an issue.

Copy link
Contributor

Choose a reason for hiding this comment

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

can we add a test? just make sure we are safe when UnionRDD.rdds is Nil

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added, thanks.

val rddWithPartitioner = sc.parallelize(Seq(1 -> true)).partitionBy(new HashPartitioner(1))
val emptyRDD = sc.emptyRDD[(Int, Boolean)]
val unionRDD = sc.union(emptyRDD, rddWithPartitioner)
assert(unionRDD.isInstanceOf[PartitionerAwareUnionRDD[_]])
val unionAllEmptyRDD = sc.union(emptyRDD, emptyRDD)
assert(unionAllEmptyRDD.isInstanceOf[UnionRDD[_]])
assert(unionAllEmptyRDD.collect().isEmpty)
}

test("partitioner aware union") {
def makeRDDWithPartitioner(seq: Seq[Int]): RDD[Int] = {
sc.makeRDD(seq, 1)
Expand Down Expand Up @@ -1047,7 +1057,9 @@ class RDDSuite extends SparkFunSuite with SharedSparkContext {
private class CyclicalDependencyRDD[T: ClassTag] extends RDD[T](sc, Nil) {
private val mutableDependencies: ArrayBuffer[Dependency[_]] = ArrayBuffer.empty
override def compute(p: Partition, c: TaskContext): Iterator[T] = Iterator.empty
override def getPartitions: Array[Partition] = Array.empty
override def getPartitions: Array[Partition] = Array(new Partition {
override def index: Int = 0
})
override def getDependencies: Seq[Dependency[_]] = mutableDependencies
def addDependency(dep: Dependency[_]) {
mutableDependencies += dep
Expand Down