uros-b commented on code in PR #59095:
URL: https://github.com/apache/spark/pull/59095#discussion_r4122311861
##########
core/src/main/scala/org/apache/spark/api/java/JavaSparkContext.scala:
##########
@@ -152,9 +152,18 @@ class JavaSparkContext(val sc: SparkContext) extends
Closeable {
/** Distribute a local Scala collection to form an RDD. */
def parallelizePairs[K, V](list: java.util.List[Tuple2[K, V]], numSlices:
Int)
: JavaPairRDD[K, V] = {
- implicit val ctagK: ClassTag[K] = fakeClassTag
- implicit val ctagV: ClassTag[V] = fakeClassTag
- JavaPairRDD.fromRDD(sc.parallelize(list.asScala.toSeq, numSlices))
+ val seq = list.asScala.toSeq
+ implicit val ctagK: ClassTag[K] =
+ seq.iterator.filter(_ != null).map(_._1).find(_ != null) match {
+ case Some(k) => ClassTag(k.getClass).asInstanceOf[ClassTag[K]]
+ case None => fakeClassTag[K]
+ }
+ implicit val ctagV: ClassTag[V] =
+ seq.iterator.filter(_ != null).map(_._2).find(_ != null) match {
+ case Some(v) => ClassTag(v.getClass).asInstanceOf[ClassTag[V]]
+ case None => fakeClassTag[V]
+ }
+ JavaPairRDD.fromRDD(sc.parallelize(seq, numSlices))
}
Review Comment:
Resolving ClassTag[K] AND ClassTag[V] from the first non-null element's
runtime class (vs the prior fakeClassTag -> Object) stores a leaf ClassTag. For
a heterogeneous collection (e.g. List<Tuple2<Number, V>> with keys [Integer(1),
Double(2.0)], legal Java) a later keys().collect() / values().collect()
allocates Array[K]/Array[V] via that specific tag (rdd.map(_._1) ->
iter.toArray) and can throw ArrayStoreException where Object[] previously
succeeded - a newly introduced behavior change in a widely used public API,
uncovered by the tests.
--
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]