Skip to content
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

Add Accurate Count-Distinct aggregator (RoaringBitmap) #493

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
Copyright 2015 Vinted.com

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package com.twitter.algebird

import org.roaringbitmap._
import org.roaringbitmap.FastAggregation

import scala.collection.JavaConversions.asJavaIterator

object ExactCountDistinct {
implicit val monoid = new RoaringBitMapMonoid()

/**
* Accurate Count-Distinct for integer values (<= 2,147,483,646)
* Internally, this uses `RoaringBitmap`, a compressed alternative to `BitSet`.
*/
def accurateDistinctAggregator: MonoidAggregator[Int, RoaringBitmap, Int] = {
Aggregator.prepareMonoid { value: Int =>
RoaringBitmap.bitmapOf(value)
}.andThenPresent(_.getCardinality)
}
}

class RoaringBitMapSemigroup extends Semigroup[RoaringBitmap] {
def plus(l: RoaringBitmap, r: RoaringBitmap): RoaringBitmap = RoaringBitmap.or(l, r)

override def sumOption(iter: TraversableOnce[RoaringBitmap]): Option[RoaringBitmap] = {
if (!iter.isEmpty){
Some(FastAggregation.or(iter.toIterator))
} else {
None
}
}
}

class RoaringBitMapMonoid extends RoaringBitMapSemigroup with Monoid[RoaringBitmap] {
def zero = new RoaringBitmap()

override def sum(vs: TraversableOnce[RoaringBitmap]): RoaringBitmap = {
FastAggregation.or(vs.toIterator)
}
}
1 change: 1 addition & 0 deletions project/Build.scala
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ object AlgebirdBuild extends Build {
""".stripMargin('|'),
libraryDependencies <++= (scalaVersion) { scalaVersion =>
Seq("com.googlecode.javaewah" % "JavaEWAH" % "0.6.6",
"org.roaringbitmap" % "RoaringBitmap" % "0.5.4",
"org.scala-lang" % "scala-reflect" % scalaVersion) ++ {
if (isScala210x(scalaVersion))
Seq("org.scalamacros" %% "quasiquotes" % quasiquotesVersion)
Expand Down