srowen commented on code in PR #47904:
URL: https://github.com/apache/spark/pull/47904#discussion_r1736106080


##########
mllib/src/main/scala/org/apache/spark/ml/feature/StringIndexer.scala:
##########
@@ -197,53 +196,43 @@ class StringIndexer @Since("1.4.0") (
     }
   }
 
-  private def countByValue(
-      dataset: Dataset[_],
-      inputCols: Array[String]): Array[OpenHashMap[String, Long]] = {
+  private def sortByFreq(dataset: Dataset[_], ascending: Boolean): 
Array[Array[String]] = {
+    val (inputCols, _) = getInOutCols()
     val selectedCols = getSelectedCols(dataset, inputCols.toImmutableArraySeq)
-    val results = Array.fill(selectedCols.size)(new OpenHashMap[String, 
Long]())
+    val numCols = inputCols.length
+
+    // In the case of equal frequency, always sorts strings by alphabet 
(ascending).
+    val countCol = if (ascending) count(lit(1)) else negate(count(lit(1)))
+
+    val result = Array.fill(numCols)(Array.empty[String])
     dataset.select(posexplode(array(selectedCols: _*)).as(Seq("index", 
"value")))
       .where(col("value").isNotNull)
       .groupBy("index", "value")
-      .agg(count(lit(1)).as("count"))
+      .agg(countCol.as("count"))
       .groupBy("index")
-      .agg(collect_list(struct("value", "count")))
+      .agg(collect_list(struct("count", "value")).alias("array"))
+      .repartition(8, col("index"))

Review Comment:
   I get it, just seems like a repartition() is potentially expensive. I don't 
know if there was much significance to 8, other than it being a bit parallel. 
Here we already have parallelism in place and it's a small operation. I'd 
imagine you can remove it for simplicity, even if it isn't faster at small 
scale.



-- 
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]

Reply via email to