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
12 changes: 10 additions & 2 deletions python/pyspark/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from tempfile import NamedTemporaryFile

from py4j.protocol import Py4JError
from py4j.java_gateway import is_instance_of

from pyspark import accumulators
from pyspark.accumulators import Accumulator
Expand Down Expand Up @@ -864,10 +865,17 @@ def union(self, rdds):
first_jrdd_deserializer = rdds[0]._jrdd_deserializer
if any(x._jrdd_deserializer != first_jrdd_deserializer for x in rdds):
rdds = [x._reserialize() for x in rdds]
gw = SparkContext._gateway
cls = SparkContext._jvm.org.apache.spark.api.java.JavaRDD
jrdds = SparkContext._gateway.new_array(cls, len(rdds))
is_jrdd = is_instance_of(gw, rdds[0]._jrdd, cls)
jrdds = gw.new_array(cls, len(rdds))
for i in range(0, len(rdds)):
jrdds[i] = rdds[i]._jrdd
if is_jrdd:
jrdds[i] = rdds[i]._jrdd
else:
# zip could return JavaPairRDD hence we ensure `_jrdd`
# to be `JavaRDD` by wrapping it in a `map`
jrdds[i] = rdds[i].map(lambda x: x)._jrdd
return RDD(self._jsc.union(jrdds), self, rdds[0]._jrdd_deserializer)

def broadcast(self, value):
Expand Down
9 changes: 9 additions & 0 deletions python/pyspark/tests/test_rdd.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,15 @@ def test_zip_chaining(self):
set([(x, (x, x)) for x in 'abc'])
)

def test_union_pair_rdd(self):
# Regression test for SPARK-31788
rdd = self.sc.parallelize([1, 2])
pair_rdd = rdd.zip(rdd)
self.assertEqual(
self.sc.union([pair_rdd, pair_rdd]).collect(),
[((1, 1), (2, 2)), ((1, 1), (2, 2))]
)

def test_deleting_input_files(self):
# Regression test for SPARK-1025
tempFile = tempfile.NamedTemporaryFile(delete=False)
Expand Down