-
Notifications
You must be signed in to change notification settings - Fork 86
Add bounded unique count aggregation #781
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
dac9f91
Add bounded unique count aggregation
jbrooks-stripe d5746da
use hash value instead
pengyu-hou 03a3a3b
scala fmt
pengyu-hou fd27c7a
fix ir type
pengyu-hou 0ca15cd
use iterator
pengyu-hou 5f1ff09
scala fmt
pengyu-hou 996d050
Optimize BoundedUniqueCount for numeric types with sentinel set pat…
pengyu-hou 941a060
Merge branch 'main' into jbrooks-bounded-unique-count
pengyu-hou File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -25,6 +25,7 @@ import com.yahoo.sketches.kll.KllFloatsSketch | |||||||||||||||
| import com.yahoo.sketches.{ArrayOfDoublesSerDe, ArrayOfItemsSerDe, ArrayOfLongsSerDe, ArrayOfStringsSerDe} | ||||||||||||||||
|
|
||||||||||||||||
| import java.io.{ByteArrayInputStream, ByteArrayOutputStream, ObjectInputStream, ObjectOutputStream} | ||||||||||||||||
| import java.security.MessageDigest | ||||||||||||||||
| import java.util | ||||||||||||||||
| import scala.collection.mutable | ||||||||||||||||
| import scala.jdk.CollectionConverters._ | ||||||||||||||||
|
|
@@ -599,6 +600,113 @@ class ApproxHistogram[T: FrequentItemsFriendly](mapSize: Int, errorType: ErrorTy | |||||||||||||||
| } | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| object BoundedUniqueCount { | ||||||||||||||||
| private val SentinelSet: util.Set[Any] = new util.HashSet[Any]() | ||||||||||||||||
| private val SentinelMarker: String = "__SENTINEL__" | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| class BoundedUniqueCount[T](inputType: DataType, k: Int = 8) extends SimpleAggregator[T, util.Set[Any], Long] { | ||||||||||||||||
| private def toBytes(input: T): Array[Byte] = { | ||||||||||||||||
| val bos = new ByteArrayOutputStream() | ||||||||||||||||
| val out = new ObjectOutputStream(bos) | ||||||||||||||||
| out.writeObject(input) | ||||||||||||||||
| out.flush() | ||||||||||||||||
| bos.toByteArray | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| private def md5Hex(bytes: Array[Byte]): String = | ||||||||||||||||
| MessageDigest.getInstance("MD5").digest(bytes).map("%02x".format(_)).mkString | ||||||||||||||||
|
|
||||||||||||||||
| private def processInput(input: T): Any = { | ||||||||||||||||
| inputType match { | ||||||||||||||||
| case IntType | LongType | DoubleType | FloatType | ShortType | BinaryType => | ||||||||||||||||
| input | ||||||||||||||||
| case _ => | ||||||||||||||||
| md5Hex(toBytes(input)) | ||||||||||||||||
| } | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| override def prepare(input: T): util.Set[Any] = { | ||||||||||||||||
| val result = new util.HashSet[Any](k) | ||||||||||||||||
| result.add(processInput(input)) | ||||||||||||||||
| result | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| override def update(ir: util.Set[Any], input: T): util.Set[Any] = { | ||||||||||||||||
| if (ir == BoundedUniqueCount.SentinelSet || ir.size() >= k) { | ||||||||||||||||
| return BoundedUniqueCount.SentinelSet | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| ir.add(processInput(input)) | ||||||||||||||||
| ir | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| override def outputType: DataType = LongType | ||||||||||||||||
|
|
||||||||||||||||
| override def irType: DataType = | ||||||||||||||||
| inputType match { | ||||||||||||||||
| case IntType | LongType | DoubleType | FloatType | ShortType | BinaryType => | ||||||||||||||||
| ListType(inputType) | ||||||||||||||||
| case _ => | ||||||||||||||||
| ListType(StringType) | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| override def merge(ir1: util.Set[Any], ir2: util.Set[Any]): util.Set[Any] = { | ||||||||||||||||
| if (ir1 == BoundedUniqueCount.SentinelSet || ir2 == BoundedUniqueCount.SentinelSet) { | ||||||||||||||||
| return BoundedUniqueCount.SentinelSet | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| ir2 | ||||||||||||||||
| .iterator() | ||||||||||||||||
| .asScala | ||||||||||||||||
| .foreach(v => | ||||||||||||||||
| if (ir1.size() < k) { | ||||||||||||||||
| ir1.add(v) | ||||||||||||||||
| }) | ||||||||||||||||
|
|
||||||||||||||||
| if (ir1.size() >= k) { | ||||||||||||||||
| BoundedUniqueCount.SentinelSet | ||||||||||||||||
| } else { | ||||||||||||||||
| ir1 | ||||||||||||||||
| } | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| override def finalize(ir: util.Set[Any]): Long = { | ||||||||||||||||
| if (ir == BoundedUniqueCount.SentinelSet) { | ||||||||||||||||
| k | ||||||||||||||||
| } else { | ||||||||||||||||
| ir.size() | ||||||||||||||||
| } | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| override def clone(ir: util.Set[Any]): util.Set[Any] = { | ||||||||||||||||
| if (ir == BoundedUniqueCount.SentinelSet) { | ||||||||||||||||
| BoundedUniqueCount.SentinelSet | ||||||||||||||||
| } else { | ||||||||||||||||
| new util.HashSet[Any](ir) | ||||||||||||||||
| } | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| override def normalize(ir: util.Set[Any]): Any = { | ||||||||||||||||
| if (ir == BoundedUniqueCount.SentinelSet) { | ||||||||||||||||
| val list = new util.ArrayList[Any]() | ||||||||||||||||
| list.add(BoundedUniqueCount.SentinelMarker) | ||||||||||||||||
| list | ||||||||||||||||
| } else { | ||||||||||||||||
| new util.ArrayList[Any](ir) | ||||||||||||||||
|
Comment on lines
+691
to
+696
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. aren't both of these same - can we just do
Suggested change
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi @nikhil-zlai , this is used to differentiate the empty Sentinel Set and the actual empty set. |
||||||||||||||||
| } | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| override def denormalize(ir: Any): util.Set[Any] = { | ||||||||||||||||
| val list = ir.asInstanceOf[util.ArrayList[Any]] | ||||||||||||||||
| if (list.size() == 1 && list.get(0) == BoundedUniqueCount.SentinelMarker) { | ||||||||||||||||
| BoundedUniqueCount.SentinelSet | ||||||||||||||||
| } else { | ||||||||||||||||
| new util.HashSet[Any](list) | ||||||||||||||||
| } | ||||||||||||||||
| } | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| // Based on CPC sketch (a faster, smaller and more accurate version of HLL) | ||||||||||||||||
| // See: Back to the future: an even more nearly optimal cardinality estimation algorithm, 2017 | ||||||||||||||||
| // https://arxiv.org/abs/1708.06839 | ||||||||||||||||
|
|
||||||||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
207 changes: 207 additions & 0 deletions
207
aggregator/src/test/scala/ai/chronon/aggregator/test/BoundedUniqueCountTest.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,207 @@ | ||
| package ai.chronon.aggregator.test | ||
|
|
||
| import ai.chronon.aggregator.base.BoundedUniqueCount | ||
| import ai.chronon.api.{StringType, IntType, LongType, DoubleType, FloatType, BinaryType} | ||
| import junit.framework.TestCase | ||
| import org.junit.Assert._ | ||
|
|
||
| import java.util | ||
| import scala.jdk.CollectionConverters._ | ||
|
|
||
| class BoundedUniqueCountTest extends TestCase { | ||
| def testHappyCase(): Unit = { | ||
| val boundedDistinctCount = new BoundedUniqueCount[String](StringType, 5) | ||
| var ir = boundedDistinctCount.prepare("1") | ||
| ir = boundedDistinctCount.update(ir, "1") | ||
| ir = boundedDistinctCount.update(ir, "2") | ||
|
|
||
| val result = boundedDistinctCount.finalize(ir) | ||
| assertEquals(2, result) | ||
| } | ||
|
|
||
| def testExceedSize(): Unit = { | ||
| val boundedDistinctCount = new BoundedUniqueCount[String](StringType, 5) | ||
| var ir = boundedDistinctCount.prepare("1") | ||
| ir = boundedDistinctCount.update(ir, "2") | ||
| ir = boundedDistinctCount.update(ir, "3") | ||
| ir = boundedDistinctCount.update(ir, "4") | ||
| ir = boundedDistinctCount.update(ir, "5") | ||
| ir = boundedDistinctCount.update(ir, "6") | ||
| ir = boundedDistinctCount.update(ir, "7") | ||
|
|
||
| val result = boundedDistinctCount.finalize(ir) | ||
| assertEquals(5, result) | ||
| } | ||
|
|
||
| def testMerge(): Unit = { | ||
| val boundedDistinctCount = new BoundedUniqueCount[String](StringType, 5) | ||
| val ir1 = new util.HashSet[Any](Seq("1", "2", "3").asJava) | ||
| val ir2 = new util.HashSet[Any](Seq("4", "5", "6").asJava) | ||
|
|
||
| val merged = boundedDistinctCount.merge(ir1, ir2) | ||
| val result = boundedDistinctCount.finalize(merged) | ||
| assertEquals(5, result) // Should return k=5 when exceeding the limit | ||
| } | ||
|
|
||
| def testIntTypeHappyCase(): Unit = { | ||
| val boundedDistinctCount = new BoundedUniqueCount[Int](IntType, 5) | ||
| var ir = boundedDistinctCount.prepare(1) | ||
| ir = boundedDistinctCount.update(ir, 1) | ||
| ir = boundedDistinctCount.update(ir, 2) | ||
|
|
||
| val result = boundedDistinctCount.finalize(ir) | ||
| assertEquals(2, result) | ||
| } | ||
|
|
||
| def testIntTypeExceedSize(): Unit = { | ||
| val boundedDistinctCount = new BoundedUniqueCount[Int](IntType, 5) | ||
| var ir = boundedDistinctCount.prepare(1) | ||
| ir = boundedDistinctCount.update(ir, 2) | ||
| ir = boundedDistinctCount.update(ir, 3) | ||
| ir = boundedDistinctCount.update(ir, 4) | ||
| ir = boundedDistinctCount.update(ir, 5) | ||
| ir = boundedDistinctCount.update(ir, 6) | ||
| ir = boundedDistinctCount.update(ir, 7) | ||
|
|
||
| val result = boundedDistinctCount.finalize(ir) | ||
| assertEquals(5, result) | ||
| } | ||
|
|
||
| def testIntTypeMerge(): Unit = { | ||
| val boundedDistinctCount = new BoundedUniqueCount[Int](IntType, 5) | ||
| val ir1 = new util.HashSet[Any](Seq(1, 2, 3).asJava) | ||
| val ir2 = new util.HashSet[Any](Seq(4, 5, 6).asJava) | ||
|
|
||
| val merged = boundedDistinctCount.merge(ir1, ir2) | ||
| val result = boundedDistinctCount.finalize(merged) | ||
| assertEquals(5, result) // Should return k=5 when exceeding the limit | ||
| } | ||
|
|
||
| def testLongTypeHappyCase(): Unit = { | ||
| val boundedDistinctCount = new BoundedUniqueCount[Long](LongType, 5) | ||
| var ir = boundedDistinctCount.prepare(1L) | ||
| ir = boundedDistinctCount.update(ir, 1L) | ||
| ir = boundedDistinctCount.update(ir, 2L) | ||
|
|
||
| val result = boundedDistinctCount.finalize(ir) | ||
| assertEquals(2, result) | ||
| } | ||
|
|
||
| def testLongTypeExceedSize(): Unit = { | ||
| val boundedDistinctCount = new BoundedUniqueCount[Long](LongType, 5) | ||
| var ir = boundedDistinctCount.prepare(1L) | ||
| ir = boundedDistinctCount.update(ir, 2L) | ||
| ir = boundedDistinctCount.update(ir, 3L) | ||
| ir = boundedDistinctCount.update(ir, 4L) | ||
| ir = boundedDistinctCount.update(ir, 5L) | ||
| ir = boundedDistinctCount.update(ir, 6L) | ||
| ir = boundedDistinctCount.update(ir, 7L) | ||
|
|
||
| val result = boundedDistinctCount.finalize(ir) | ||
| assertEquals(5, result) | ||
| } | ||
|
|
||
| def testDoubleTypeHappyCase(): Unit = { | ||
| val boundedDistinctCount = new BoundedUniqueCount[Double](DoubleType, 5) | ||
| var ir = boundedDistinctCount.prepare(1.0) | ||
| ir = boundedDistinctCount.update(ir, 1.0) | ||
| ir = boundedDistinctCount.update(ir, 2.5) | ||
|
|
||
| val result = boundedDistinctCount.finalize(ir) | ||
| assertEquals(2, result) | ||
| } | ||
|
|
||
| def testDoubleTypeExceedSize(): Unit = { | ||
| val boundedDistinctCount = new BoundedUniqueCount[Double](DoubleType, 5) | ||
| var ir = boundedDistinctCount.prepare(1.0) | ||
| ir = boundedDistinctCount.update(ir, 2.5) | ||
| ir = boundedDistinctCount.update(ir, 3.7) | ||
| ir = boundedDistinctCount.update(ir, 4.2) | ||
| ir = boundedDistinctCount.update(ir, 5.8) | ||
| ir = boundedDistinctCount.update(ir, 6.3) | ||
| ir = boundedDistinctCount.update(ir, 7.9) | ||
|
|
||
| val result = boundedDistinctCount.finalize(ir) | ||
| assertEquals(5, result) | ||
| } | ||
|
|
||
| def testFloatTypeHappyCase(): Unit = { | ||
| val boundedDistinctCount = new BoundedUniqueCount[Float](FloatType, 5) | ||
| var ir = boundedDistinctCount.prepare(1.0f) | ||
| ir = boundedDistinctCount.update(ir, 1.0f) | ||
| ir = boundedDistinctCount.update(ir, 2.5f) | ||
|
|
||
| val result = boundedDistinctCount.finalize(ir) | ||
| assertEquals(2, result) | ||
| } | ||
|
|
||
| def testFloatTypeExceedSize(): Unit = { | ||
| val boundedDistinctCount = new BoundedUniqueCount[Float](FloatType, 5) | ||
| var ir = boundedDistinctCount.prepare(1.0f) | ||
| ir = boundedDistinctCount.update(ir, 2.5f) | ||
| ir = boundedDistinctCount.update(ir, 3.7f) | ||
| ir = boundedDistinctCount.update(ir, 4.2f) | ||
| ir = boundedDistinctCount.update(ir, 5.8f) | ||
| ir = boundedDistinctCount.update(ir, 6.3f) | ||
| ir = boundedDistinctCount.update(ir, 7.9f) | ||
|
|
||
| val result = boundedDistinctCount.finalize(ir) | ||
| assertEquals(5, result) | ||
| } | ||
|
|
||
| def testBinaryTypeHappyCase(): Unit = { | ||
| val boundedDistinctCount = new BoundedUniqueCount[Array[Byte]](BinaryType, 5) | ||
| val bytes1 = Array[Byte](1, 2, 3) | ||
| val bytes2 = Array[Byte](4, 5, 6) | ||
|
|
||
| var ir = boundedDistinctCount.prepare(bytes1) | ||
| ir = boundedDistinctCount.update(ir, bytes1) | ||
| ir = boundedDistinctCount.update(ir, bytes2) | ||
|
|
||
| val result = boundedDistinctCount.finalize(ir) | ||
| assertEquals(2, result) | ||
| } | ||
|
|
||
| def testBinaryTypeExceedSize(): Unit = { | ||
| val boundedDistinctCount = new BoundedUniqueCount[Array[Byte]](BinaryType, 5) | ||
| var ir = boundedDistinctCount.prepare(Array[Byte](1)) | ||
| ir = boundedDistinctCount.update(ir, Array[Byte](2)) | ||
| ir = boundedDistinctCount.update(ir, Array[Byte](3)) | ||
| ir = boundedDistinctCount.update(ir, Array[Byte](4)) | ||
| ir = boundedDistinctCount.update(ir, Array[Byte](5)) | ||
| ir = boundedDistinctCount.update(ir, Array[Byte](6)) | ||
| ir = boundedDistinctCount.update(ir, Array[Byte](7)) | ||
|
|
||
| val result = boundedDistinctCount.finalize(ir) | ||
| assertEquals(5, result) | ||
| } | ||
|
|
||
| def testBinaryTypeMerge(): Unit = { | ||
| val boundedDistinctCount = new BoundedUniqueCount[Array[Byte]](BinaryType, 5) | ||
| val ir1 = new util.HashSet[Any](Seq(Array[Byte](1), Array[Byte](2), Array[Byte](3)).asJava) | ||
| val ir2 = new util.HashSet[Any](Seq(Array[Byte](4), Array[Byte](5), Array[Byte](6)).asJava) | ||
|
|
||
| val merged = boundedDistinctCount.merge(ir1, ir2) | ||
| val result = boundedDistinctCount.finalize(merged) | ||
| assertEquals(5, result) // Should return k=5 when exceeding the limit | ||
| } | ||
|
|
||
| def testNumericTypeIrType(): Unit = { | ||
| val intBoundedDistinctCount = new BoundedUniqueCount[Int](IntType, 5) | ||
| val longBoundedDistinctCount = new BoundedUniqueCount[Long](LongType, 5) | ||
| val doubleBoundedDistinctCount = new BoundedUniqueCount[Double](DoubleType, 5) | ||
| val floatBoundedDistinctCount = new BoundedUniqueCount[Float](FloatType, 5) | ||
| val binaryBoundedDistinctCount = new BoundedUniqueCount[Array[Byte]](BinaryType, 5) | ||
| val stringBoundedDistinctCount = new BoundedUniqueCount[String](StringType, 5) | ||
|
|
||
| // For numeric and binary types, irType should be ListType(inputType) | ||
| assertEquals(ai.chronon.api.ListType(IntType), intBoundedDistinctCount.irType) | ||
| assertEquals(ai.chronon.api.ListType(LongType), longBoundedDistinctCount.irType) | ||
| assertEquals(ai.chronon.api.ListType(DoubleType), doubleBoundedDistinctCount.irType) | ||
| assertEquals(ai.chronon.api.ListType(FloatType), floatBoundedDistinctCount.irType) | ||
| assertEquals(ai.chronon.api.ListType(BinaryType), binaryBoundedDistinctCount.irType) | ||
|
|
||
| // For non-numeric types, irType should be ListType(StringType) | ||
| assertEquals(ai.chronon.api.ListType(StringType), stringBoundedDistinctCount.irType) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lets say i want to unique count a bunch of user / merchant ids (long values) - won't this be less efficient than simply keeping the set of longs?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
made the code change to keep the numeric type as is