Github user mengxr commented on a diff in the pull request:
https://github.com/apache/spark/pull/7945#discussion_r36312205
--- Diff:
sql/core/src/main/scala/org/apache/spark/sql/execution/stat/FrequentItems.scala
---
@@ -38,13 +37,22 @@ private[sql] object FrequentItems extends Logging {
def add(key: Any, count: Long): this.type = {
if (baseMap.contains(key)) {
baseMap(key) += count
+ if (baseMap(key) < minCount) minCount = baseMap(key)
} else {
if (baseMap.size < size) {
baseMap += key -> count
+ if (count < minCount) minCount = count
} else {
- // TODO: Make this more efficient... A flatMap?
- baseMap.retain((k, v) => v > count)
- baseMap.transform((k, v) => v - count)
+ val remainder = count - minCount
+ if (remainder >= 0) {
+ baseMap += key -> count // something will get kicked out, so
we can add this
+ baseMap.retain((k, v) => v > minCount)
+ baseMap.transform((k, v) => v - minCount)
+ minCount = baseMap.foldLeft(Long.MaxValue)((a, b) =>
math.min(a, b._2))
--- End diff --
Since we still need to recompute `minCount` after each update, I think the
code is simpler without tracking `minCount` in all cases. We can compute
`minCount` directly in this branch.
~~~scala
if (baseMap.contains(key)) {
baseMap(key) += count
} else {
baseMap(key) = count
if (baseMap.size > size) {
val minCount = baseMap.values.min
baseMap.retain((_, v) => v > minCount)
baseMap.transform((_, v) => v - minCount)
}
}
~~~
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]