srowen commented on code in PR #47904: URL: https://github.com/apache/spark/pull/47904#discussion_r1735503480
########## 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: Out of curiosity, why 8 here? can we avoid the repartition()? I don't immediately see why we need it -- 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-unsubscr...@spark.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org