zhengruifeng commented on code in PR #47904:
URL: https://github.com/apache/spark/pull/47904#discussion_r1735781173
##########
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:
in the benchmark, removing it won't hurt the performance:
```
Elapsed time: 10.855109542s
```
--
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]