You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "LuciferYang (via GitHub)" <gi...@apache.org> on 2024/02/04 04:51:43 UTC

Re: [PR] [SPARK-46970][CORE] Rewrite `OpenHashSet#hasher` with `pattern matching` [spark]

LuciferYang commented on code in PR #44998:
URL: https://github.com/apache/spark/pull/44998#discussion_r1477177975


##########
core/src/main/scala/org/apache/spark/util/collection/OpenHashSet.scala:
##########
@@ -62,28 +62,12 @@ class OpenHashSet[@specialized(Long, Int, Double, Float) T: ClassTag](
   // specialization to work (specialized class extends the non-specialized one and needs access
   // to the "private" variables).
 
-  protected val hasher: Hasher[T] = {
-    // It would've been more natural to write the following using pattern matching. But Scala 2.9.x
-    // compiler has a bug when specialization is used together with this pattern matching, and
-    // throws:
-    // scala.tools.nsc.symtab.Types$TypeError: type mismatch;
-    //  found   : scala.reflect.AnyValManifest[Long]
-    //  required: scala.reflect.ClassTag[Int]
-    //         at scala.tools.nsc.typechecker.Contexts$Context.error(Contexts.scala:298)
-    //         at scala.tools.nsc.typechecker.Infer$Inferencer.error(Infer.scala:207)
-    //         ...
-    val mt = classTag[T]
-    if (mt == ClassTag.Long) {
-      (new LongHasher).asInstanceOf[Hasher[T]]
-    } else if (mt == ClassTag.Int) {
-      (new IntHasher).asInstanceOf[Hasher[T]]
-    } else if (mt == ClassTag.Double) {
-      (new DoubleHasher).asInstanceOf[Hasher[T]]
-    } else if (mt == ClassTag.Float) {
-      (new FloatHasher).asInstanceOf[Hasher[T]]
-    } else {
-      new Hasher[T]
-    }
+  protected val hasher: Hasher[T] = classTag[T] match {
+    case ClassTag.Long => new LongHasher().asInstanceOf[Hasher[T]]
+    case ClassTag.Int => new IntHasher().asInstanceOf[Hasher[T]]
+    case ClassTag.Double => new DoubleHasher().asInstanceOf[Hasher[T]]
+    case ClassTag.Float => new FloatHasher().asInstanceOf[Hasher[T]]
+    case _ => new Hasher[T]

Review Comment:
   cc @cloud-fan @dongjoon-hyun @srowen 
   
   All test passed. Can we make this change?
   



-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org