dongjoon-hyun commented on code in PR #59095:
URL: https://github.com/apache/spark/pull/59095#discussion_r4542645082
##########
core/src/main/scala/org/apache/spark/api/java/JavaSparkContext.scala:
##########
@@ -871,4 +899,31 @@ object JavaSparkContext {
*/
private[spark]
def fakeClassTag[T]: ClassTag[T] = ClassTag.AnyRef.asInstanceOf[ClassTag[T]]
+
+ private[spark] class InferredArrayClassTag[T](val underlying: ClassTag[T])
Review Comment:
**[1] `ClassTag` already implements every member overridden here.**
In Scala 2.13, `ClassTag` defines `newArray`, `wrap`, `unapply`, `canEqual`,
`equals`, `hashCode` and `toString` from `runtimeClass` alone. `ClassTag(cls)`
for an array class returns a `GenericClassTag`, which only overrides `newArray`
with the same body. So the delegation to `underlying` adds nothing, and a
marker class is enough:
```scala
private[spark] class InferredArrayClassTag[T](override val runtimeClass:
Class[_])
extends ClassTag[T]
```
L176-177 then becomes `new
JavaSparkContext.InferredArrayClassTag[K](firstArrayClass)`. `underlying` has
no other users, `ClassTag` is already `Serializable`, and equality with plain
tags stays the same, so `mergeKeyClassTags` works as before. I tried this with
items 2 and 4 on 2423810: core compiles, and `JavaAPISuite` and
`PartitioningSuite` pass. Could we simplify it this way?
##########
core/src/main/scala/org/apache/spark/api/java/JavaPairRDD.scala:
##########
@@ -511,7 +514,9 @@ class JavaPairRDD[K, V](val rdd: RDD[(K, V)])
def rightOuterJoin[W](other: JavaPairRDD[K, W], partitioner: Partitioner)
: JavaPairRDD[K, (Optional[V], W)] = {
val joinResult = rdd.rightOuterJoin(other, partitioner)
- fromRDD(joinResult.mapValues{case (v, w) =>
(JavaUtils.optionToOptional(v), w)})
+ val kTag = JavaSparkContext.mergeKeyClassTags(kClassTag, other.kClassTag)
Review Comment:
**[2] Minor: `mergeKeyClassTags` is not only for keys, and the same two
lines repeat in 18 overloads.**
`union` also uses it for the value tag (L210), and `JavaRDD.union` for the
element tag (JavaRDD.scala:178), so a neutral name such as `mergeClassTags`
would fit better. Every combining overload here also repeats
```scala
val kTag = JavaSparkContext.mergeKeyClassTags(kClassTag, other.kClassTag)
fromRDD(...)(kTag, fakeClassTag)
```
A private helper would make each one a single expression:
```scala
private def mergedKeyTag(others: JavaPairRDD[K, _]*): ClassTag[K] =
JavaSparkContext.mergeClassTags(kClassTag, others.map(_.kClassTag): _*)
```
so this one becomes `fromRDD(...)(mergedKeyTag(other), fakeClassTag)`. Could
we rename it and add such a helper?
##########
core/src/main/scala/org/apache/spark/rdd/PairRDDFunctions.scala:
##########
@@ -1100,9 +1103,18 @@ class PairRDDFunctions[K, V](self: RDD[(K, V)])
private[spark] def keyOrdering: Option[Ordering[K]] = Option(ord)
- private def failOnHashPartitionerWithArrayKey(partitioner: Partitioner):
Unit = {
+ private[spark] def failOnHashPartitionerWithArrayKey(partitioner:
Partitioner): Unit = {
Review Comment:
**[3] Nit: this method can stay `private`.**
All of its callers are in this class (L83, L525, L775, L791, L804 and L914),
and `OrderedRDDFunctions` calls the new companion method instead.
`private[spark]` is public in bytecode, so the change adds a method to a public
class with no caller outside it. Could we keep it `private`?
##########
core/src/main/scala/org/apache/spark/api/java/JavaSparkContext.scala:
##########
@@ -152,9 +152,34 @@ 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
+ val seq = list.asScala.toSeq
+ var firstArrayClass: Class[_] = null
+ var isHomogeneousArray = false
Review Comment:
**[4] Nit: `isHomogeneousArray` always equals `firstArrayClass != null`.**
It becomes `true` exactly when `firstArrayClass` is set (L167-168) and is
never reset, so L175 could test `firstArrayClass != null && !hasConflict`. The
scan could also be one iterator expression with the same result (it skips null
tuples and null keys, and stops at the first key that is not an array of the
first key's class):
```scala
val keyClasses = seq.iterator.collect { case (k, _) if k != null =>
k.getClass }
val first: Class[_] = if (keyClasses.hasNext) keyClasses.next() else null
implicit val ctagK: ClassTag[K] =
if (first != null && first.isArray && keyClasses.forall(_ == first)) {
new JavaSparkContext.InferredArrayClassTag[K](first)
} else {
fakeClassTag[K]
}
```
The constructor call assumes item 1. Could we drop the extra state?
##########
core/src/main/scala/org/apache/spark/api/java/JavaPairRDD.scala:
##########
@@ -1081,4 +1123,29 @@ object JavaPairRDD {
new JavaPairRDD[K, V](rdd.rdd)
}
+ /**
+ * Convert a JavaRDD of key-value pairs to JavaPairRDD with an explicit key
class.
+ *
+ * @param rdd an RDD of pairs
+ * @param keyClass the runtime class of the keys. When an array class is
specified,
+ * pair operations using a
[[org.apache.spark.HashPartitioner]]
+ * will fail fast with [[org.apache.spark.SparkException]]
+ * ("UNSUPPORTED_ARRAY_KEY.HASH_PARTITIONER"), and map-side
combine
+ * operations will fail fast with
+ * ("UNSUPPORTED_ARRAY_KEY.MAP_SIDE_COMBINE"). It also
determines the
+ * runtime type of arrays returned by actions on [[keys()]].
Must be a
Review Comment:
**[5] Nit: `[[keys()]]` cannot be resolved here.**
This Scaladoc belongs to `object JavaPairRDD`, but `keys` is a member of the
class, so Scaladoc finds no member to link and warns. Could it be `keys()` in
backticks, or `[[JavaPairRDD#keys]]`, as in
`[[org.apache.spark.rdd.RDD#zipWithIndex]]` elsewhere in core?
##########
core/src/test/java/test/org/apache/spark/JavaAPISuite.java:
##########
@@ -36,10 +36,13 @@
import java.util.stream.Collectors;
import java.util.stream.IntStream;
+import org.apache.spark.HashPartitioner;
import org.apache.spark.Partitioner;
import org.apache.spark.SparkConf;
+import org.apache.spark.SparkException;
import org.apache.spark.TaskContext;
import org.apache.spark.TaskContext$;
+import org.apache.spark.api.java.Optional;
Review Comment:
**[6] Nit: unrelated formatting changes.**
This moves `import org.apache.spark.api.java.Optional;` out of its sorted
`org.apache.spark.api.java.*` group into the `TaskContext`/`scala` block. In
PairRDDFunctions.scala, L911-913 re-wrap the `subtractByKey` signature, which
is exactly 100 characters on master and did not change, and L1120 adds a blank
line at the end of the file. Could we revert these so the diff only has the fix?
--
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]