{"action":"created","comment":{"url":"https://api.github.com/repos/druid-io/druid/pulls/comments/56930658","id":56930658,"diff_hunk":"@@ -0,0 +1,214 @@\n+/*\n+ * Licensed to Metamarkets Group Inc. (Metamarkets) under one\n+ * or more contributor license agreements. See the NOTICE file\n+ * distributed with this work for additional information\n+ * regarding copyright ownership. Metamarkets licenses this file\n+ * to you under the Apache License, Version 2.0 (the\n+ * \"License\"); you may not use this file except in compliance\n+ * with the License. You may obtain a copy of the License at\n+ *\n+ * http://www.apache.org/licenses/LICENSE-2.0\n+ *\n+ * Unless required by applicable law or agreed to in writing,\n+ * software distributed under the License is distributed on an\n+ * \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY\n+ * KIND, either express or implied. See the License for the\n+ * specific language governing permissions and limitations\n+ * under the License.\n+ */\n+\n+package io.druid.query.aggregation.distinctcount;\n+\n+import com.fasterxml.jackson.annotation.JsonCreator;\n+import com.fasterxml.jackson.annotation.JsonProperty;\n+import com.google.common.primitives.Longs;\n+import com.metamx.common.StringUtils;\n+import com.metamx.common.logger.Logger;\n+import io.druid.query.aggregation.Aggregator;\n+import io.druid.query.aggregation.AggregatorFactory;\n+import io.druid.query.aggregation.BufferAggregator;\n+import io.druid.query.aggregation.LongSumAggregatorFactory;\n+import io.druid.query.dimension.DefaultDimensionSpec;\n+import io.druid.segment.ColumnSelectorFactory;\n+import io.druid.segment.DimensionSelector;\n+\n+import java.nio.ByteBuffer;\n+import java.util.Arrays;\n+import java.util.Comparator;\n+import java.util.List;\n+\n+public class DistinctCountAggregatorFactory extends AggregatorFactory\n+{\n+  private static final Logger log = new Logger(DistinctCountAggregatorFactory.class);\n+  private static final byte CACHE_TYPE_ID = 20;\n+  private static final BitMapFactory DEFAULT_BITMAP_FACTORY = new RoaringBitMapFactory();\n+\n+  private final String name;\n+  private final String fieldName;\n+  private final BitMapFactory bitMapFactory;\n+\n+  @JsonCreator\n+  public DistinctCountAggregatorFactory(\n+      @JsonProperty(\"name\") String name,\n+      @JsonProperty(\"fieldName\") String fieldName,\n+      @JsonProperty(\"bitmap\") BitMapFactory bitMapFactory\n+  )\n+  {\n+    this.name = name;\n+    this.fieldName = fieldName;\n+    this.bitMapFactory = bitMapFactory == null ? DEFAULT_BITMAP_FACTORY : bitMapFactory;\n+  }\n+\n+  @Override\n+  public Aggregator factorize(ColumnSelectorFactory columnFactory)\n+  {\n+    return new DistinctCountAggregator(\n+        name,\n+        makeDimensionSelector(columnFactory),\n+        bitMapFactory.makeEmptyMutableBitmap()\n+    );\n+  }\n+\n+  @Override\n+  public BufferAggregator factorizeBuffered(ColumnSelectorFactory columnFactory)\n+  {\n+    return new DistinctCountBufferAggregator(makeDimensionSelector(columnFactory));\n+  }\n+\n+  private DimensionSelector makeDimensionSelector(final ColumnSelectorFactory columnFactory)\n+  {\n+    return columnFactory.makeDimensionSelector(new DefaultDimensionSpec(fieldName, fieldName));\n+  }\n+\n+  @Override\n+  public Comparator getComparator()\n+  {\n+    return new Comparator()\n+    {\n+      @Override\n+      public int compare(Object o, Object o1)\n+      {\n+        return Longs.compare(((Number) o).longValue(), ((Number) o1).longValue());\n+      }\n+    };\n+  }\n+\n+  @Override\n+  public Object combine(Object lhs, Object rhs)\n+  {\n+    return ((Number) lhs).longValue() + ((Number) rhs).longValue();","path":"extensions-contrib/distinctcount/src/main/java/io/druid/query/aggregation/distinctcount/DistinctCountAggregatorFactory.java","position":99,"original_position":99,"commit_id":"688f6f128d9b5845bd9d67ba59bd59e1e003b1b0","original_commit_id":"688f6f128d9b5845bd9d67ba59bd59e1e003b1b0","user":{"login":"binlijin","id":1531353,"avatar_url":"https://avatars.githubusercontent.com/u/1531353?v=3","gravatar_id":"","url":"https://api.github.com/users/binlijin","html_url":"https://github.com/binlijin","followers_url":"https://api.github.com/users/binlijin/followers","following_url":"https://api.github.com/users/binlijin/following{/other_user}","gists_url":"https://api.github.com/users/binlijin/gists{/gist_id}","starred_url":"https://api.github.com/users/binlijin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/binlijin/subscriptions","organizations_url":"https://api.github.com/users/binlijin/orgs","repos_url":"https://api.github.com/users/binlijin/repos","events_url":"https://api.github.com/users/binlijin/events{/privacy}","received_events_url":"https://api.github.com/users/binlijin/received_events","type":"User","site_admin":false},"body":"ok","created_at":"2016-03-22T02:52:46Z","updated_at":"2016-03-22T02:52:46Z","html_url":"https://github.com/druid-io/druid/pull/2602#discussion_r56930658","pull_request_url":"https://api.github.com/repos/druid-io/druid/pulls/2602","_links":{"self":{"href":"https://api.github.com/repos/druid-io/druid/pulls/comments/56930658"},"html":{"href":"https://github.com/druid-io/druid/pull/2602#discussion_r56930658"},"pull_request":{"href":"https://api.github.com/repos/druid-io/druid/pulls/2602"}}},"pull_request":{"url":"https://api.github.com/repos/druid-io/druid/pulls/2602","id":61882060,"html_url":"https://github.com/druid-io/druid/pull/2602","diff_url":"https://github.com/druid-io/druid/pull/2602.diff","patch_url":"https://github.com/druid-io/druid/pull/2602.patch","issue_url":"https://api.github.com/repos/druid-io/druid/issues/2602","number":2602,"state":"open","locked":false,"title":"implement special distinctcount","user":{"login":"binlijin","id":1531353,"avatar_url":"https://avatars.githubusercontent.com/u/1531353?v=3","gravatar_id":"","url":"https://api.github.com/users/binlijin","html_url":"https://github.com/binlijin","followers_url":"https://api.github.com/users/binlijin/followers","following_url":"https://api.github.com/users/binlijin/following{/other_user}","gists_url":"https://api.github.com/users/binlijin/gists{/gist_id}","starred_url":"https://api.github.com/users/binlijin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/binlijin/subscriptions","organizations_url":"https://api.github.com/users/binlijin/orgs","repos_url":"https://api.github.com/users/binlijin/repos","events_url":"https://api.github.com/users/binlijin/events{/privacy}","received_events_url":"https://api.github.com/users/binlijin/received_events","type":"User","site_admin":false},"body":"Discuss at https://groups.google.com/forum/#!topic/druid-development/GXRpXfBzfJs\r\n(1) First use https://github.com/druid-io/druid/pull/2570 to partition data by visitor_id.\r\n(2) Second use distinctcount to calculate exact UV.\r\nThere is some limitations, when use with groupBy, the groupBy keys' numbers should not exceed maxIntermediateRows in every segment, if exceed the result will wrong. And when use with topN, numValuesPerPass should not too big, if too big the distinctcount will use many memory and cause the JVM out of service.","created_at":"2016-03-07T07:37:51Z","updated_at":"2016-03-22T02:52:46Z","closed_at":null,"merged_at":null,"merge_commit_sha":"102aa72d15f3a62b4a3c4fa1ee678db5df5fc742","assignee":null,"milestone":{"url":"https://api.github.com/repos/druid-io/druid/milestones/20","html_url":"https://github.com/druid-io/druid/milestones/0.9.2","labels_url":"https://api.github.com/repos/druid-io/druid/milestones/20/labels","id":1571409,"number":20,"title":"0.9.2","description":"","creator":{"login":"fjy","id":428325,"avatar_url":"https://avatars.githubusercontent.com/u/428325?v=3","gravatar_id":"","url":"https://api.github.com/users/fjy","html_url":"https://github.com/fjy","followers_url":"https://api.github.com/users/fjy/followers","following_url":"https://api.github.com/users/fjy/following{/other_user}","gists_url":"https://api.github.com/users/fjy/gists{/gist_id}","starred_url":"https://api.github.com/users/fjy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/fjy/subscriptions","organizations_url":"https://api.github.com/users/fjy/orgs","repos_url":"https://api.github.com/users/fjy/repos","events_url":"https://api.github.com/users/fjy/events{/privacy}","received_events_url":"https://api.github.com/users/fjy/received_events","type":"User","site_admin":false},"open_issues":11,"closed_issues":2,"state":"open","created_at":"2016-02-08T22:01:21Z","updated_at":"2016-03-18T08:33:09Z","due_on":null,"closed_at":null},"commits_url":"https://api.github.com/repos/druid-io/druid/pulls/2602/commits","review_comments_url":"https://api.github.com/repos/druid-io/druid/pulls/2602/comments","review_comment_url":"https://api.github.com/repos/druid-io/druid/pulls/comments{/number}","comments_url":"https://api.github.com/repos/druid-io/druid/issues/2602/comments","statuses_url":"https://api.github.com/repos/druid-io/druid/statuses/688f6f128d9b5845bd9d67ba59bd59e1e003b1b0","head":{"label":"binlijin:distinctcount","ref":"distinctcount","sha":"688f6f128d9b5845bd9d67ba59bd59e1e003b1b0","user":{"login":"binlijin","id":1531353,"avatar_url":"https://avatars.githubusercontent.com/u/1531353?v=3","gravatar_id":"","url":"https://api.github.com/users/binlijin","html_url":"https://github.com/binlijin","followers_url":"https://api.github.com/users/binlijin/followers","following_url":"https://api.github.com/users/binlijin/following{/other_user}","gists_url":"https://api.github.com/users/binlijin/gists{/gist_id}","starred_url":"https://api.github.com/users/binlijin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/binlijin/subscriptions","organizations_url":"https://api.github.com/users/binlijin/orgs","repos_url":"https://api.github.com/users/binlijin/repos","events_url":"https://api.github.com/users/binlijin/events{/privacy}","received_events_url":"https://api.github.com/users/binlijin/received_events","type":"User","site_admin":false},"repo":{"id":45811047,"name":"druid","full_name":"binlijin/druid","owner":{"login":"binlijin","id":1531353,"avatar_url":"https://avatars.githubusercontent.com/u/1531353?v=3","gravatar_id":"","url":"https://api.github.com/users/binlijin","html_url":"https://github.com/binlijin","followers_url":"https://api.github.com/users/binlijin/followers","following_url":"https://api.github.com/users/binlijin/following{/other_user}","gists_url":"https://api.github.com/users/binlijin/gists{/gist_id}","starred_url":"https://api.github.com/users/binlijin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/binlijin/subscriptions","organizations_url":"https://api.github.com/users/binlijin/orgs","repos_url":"https://api.github.com/users/binlijin/repos","events_url":"https://api.github.com/users/binlijin/events{/privacy}","received_events_url":"https://api.github.com/users/binlijin/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/binlijin/druid","description":"Column oriented distributed data store ideal for powering interactive applications","fork":true,"url":"https://api.github.com/repos/binlijin/druid","forks_url":"https://api.github.com/repos/binlijin/druid/forks","keys_url":"https://api.github.com/repos/binlijin/druid/keys{/key_id}","collaborators_url":"https://api.github.com/repos/binlijin/druid/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/binlijin/druid/teams","hooks_url":"https://api.github.com/repos/binlijin/druid/hooks","issue_events_url":"https://api.github.com/repos/binlijin/druid/issues/events{/number}","events_url":"https://api.github.com/repos/binlijin/druid/events","assignees_url":"https://api.github.com/repos/binlijin/druid/assignees{/user}","branches_url":"https://api.github.com/repos/binlijin/druid/branches{/branch}","tags_url":"https://api.github.com/repos/binlijin/druid/tags","blobs_url":"https://api.github.com/repos/binlijin/druid/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/binlijin/druid/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/binlijin/druid/git/refs{/sha}","trees_url":"https://api.github.com/repos/binlijin/druid/git/trees{/sha}","statuses_url":"https://api.github.com/repos/binlijin/druid/statuses/{sha}","languages_url":"https://api.github.com/repos/binlijin/druid/languages","stargazers_url":"https://api.github.com/repos/binlijin/druid/stargazers","contributors_url":"https://api.github.com/repos/binlijin/druid/contributors","subscribers_url":"https://api.github.com/repos/binlijin/druid/subscribers","subscription_url":"https://api.github.com/repos/binlijin/druid/subscription","commits_url":"https://api.github.com/repos/binlijin/druid/commits{/sha}","git_commits_url":"https://api.github.com/repos/binlijin/druid/git/commits{/sha}","comments_url":"https://api.github.com/repos/binlijin/druid/comments{/number}","issue_comment_url":"https://api.github.com/repos/binlijin/druid/issues/comments{/number}","contents_url":"https://api.github.com/repos/binlijin/druid/contents/{+path}","compare_url":"https://api.github.com/repos/binlijin/druid/compare/{base}...{head}","merges_url":"https://api.github.com/repos/binlijin/druid/merges","archive_url":"https://api.github.com/repos/binlijin/druid/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/binlijin/druid/downloads","issues_url":"https://api.github.com/repos/binlijin/druid/issues{/number}","pulls_url":"https://api.github.com/repos/binlijin/druid/pulls{/number}","milestones_url":"https://api.github.com/repos/binlijin/druid/milestones{/number}","notifications_url":"https://api.github.com/repos/binlijin/druid/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/binlijin/druid/labels{/name}","releases_url":"https://api.github.com/repos/binlijin/druid/releases{/id}","deployments_url":"https://api.github.com/repos/binlijin/druid/deployments","created_at":"2015-11-09T02:48:36Z","updated_at":"2016-01-14T02:57:00Z","pushed_at":"2016-03-21T06:11:45Z","git_url":"git://github.com/binlijin/druid.git","ssh_url":"git@github.com:binlijin/druid.git","clone_url":"https://github.com/binlijin/druid.git","svn_url":"https://github.com/binlijin/druid","homepage":"http://druid.io","size":104186,"stargazers_count":0,"watchers_count":0,"language":"Java","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"druid-io:master","ref":"master","sha":"45c413af7e4449153869eee889e1fb30e8817828","user":{"login":"druid-io","id":4203474,"avatar_url":"https://avatars.githubusercontent.com/u/4203474?v=3","gravatar_id":"","url":"https://api.github.com/users/druid-io","html_url":"https://github.com/druid-io","followers_url":"https://api.github.com/users/druid-io/followers","following_url":"https://api.github.com/users/druid-io/following{/other_user}","gists_url":"https://api.github.com/users/druid-io/gists{/gist_id}","starred_url":"https://api.github.com/users/druid-io/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/druid-io/subscriptions","organizations_url":"https://api.github.com/users/druid-io/orgs","repos_url":"https://api.github.com/users/druid-io/repos","events_url":"https://api.github.com/users/druid-io/events{/privacy}","received_events_url":"https://api.github.com/users/druid-io/received_events","type":"Organization","site_admin":false},"repo":{"id":6358188,"name":"druid","full_name":"druid-io/druid","owner":{"login":"druid-io","id":4203474,"avatar_url":"https://avatars.githubusercontent.com/u/4203474?v=3","gravatar_id":"","url":"https://api.github.com/users/druid-io","html_url":"https://github.com/druid-io","followers_url":"https://api.github.com/users/druid-io/followers","following_url":"https://api.github.com/users/druid-io/following{/other_user}","gists_url":"https://api.github.com/users/druid-io/gists{/gist_id}","starred_url":"https://api.github.com/users/druid-io/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/druid-io/subscriptions","organizations_url":"https://api.github.com/users/druid-io/orgs","repos_url":"https://api.github.com/users/druid-io/repos","events_url":"https://api.github.com/users/druid-io/events{/privacy}","received_events_url":"https://api.github.com/users/druid-io/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/druid-io/druid","description":"Column oriented distributed data store ideal for powering interactive applications","fork":false,"url":"https://api.github.com/repos/druid-io/druid","forks_url":"https://api.github.com/repos/druid-io/druid/forks","keys_url":"https://api.github.com/repos/druid-io/druid/keys{/key_id}","collaborators_url":"https://api.github.com/repos/druid-io/druid/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/druid-io/druid/teams","hooks_url":"https://api.github.com/repos/druid-io/druid/hooks","issue_events_url":"https://api.github.com/repos/druid-io/druid/issues/events{/number}","events_url":"https://api.github.com/repos/druid-io/druid/events","assignees_url":"https://api.github.com/repos/druid-io/druid/assignees{/user}","branches_url":"https://api.github.com/repos/druid-io/druid/branches{/branch}","tags_url":"https://api.github.com/repos/druid-io/druid/tags","blobs_url":"https://api.github.com/repos/druid-io/druid/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/druid-io/druid/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/druid-io/druid/git/refs{/sha}","trees_url":"https://api.github.com/repos/druid-io/druid/git/trees{/sha}","statuses_url":"https://api.github.com/repos/druid-io/druid/statuses/{sha}","languages_url":"https://api.github.com/repos/druid-io/druid/languages","stargazers_url":"https://api.github.com/repos/druid-io/druid/stargazers","contributors_url":"https://api.github.com/repos/druid-io/druid/contributors","subscribers_url":"https://api.github.com/repos/druid-io/druid/subscribers","subscription_url":"https://api.github.com/repos/druid-io/druid/subscription","commits_url":"https://api.github.com/repos/druid-io/druid/commits{/sha}","git_commits_url":"https://api.github.com/repos/druid-io/druid/git/commits{/sha}","comments_url":"https://api.github.com/repos/druid-io/druid/comments{/number}","issue_comment_url":"https://api.github.com/repos/druid-io/druid/issues/comments{/number}","contents_url":"https://api.github.com/repos/druid-io/druid/contents/{+path}","compare_url":"https://api.github.com/repos/druid-io/druid/compare/{base}...{head}","merges_url":"https://api.github.com/repos/druid-io/druid/merges","archive_url":"https://api.github.com/repos/druid-io/druid/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/druid-io/druid/downloads","issues_url":"https://api.github.com/repos/druid-io/druid/issues{/number}","pulls_url":"https://api.github.com/repos/druid-io/druid/pulls{/number}","milestones_url":"https://api.github.com/repos/druid-io/druid/milestones{/number}","notifications_url":"https://api.github.com/repos/druid-io/druid/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/druid-io/druid/labels{/name}","releases_url":"https://api.github.com/repos/druid-io/druid/releases{/id}","deployments_url":"https://api.github.com/repos/druid-io/druid/deployments","created_at":"2012-10-23T19:08:07Z","updated_at":"2016-03-22T02:17:37Z","pushed_at":"2016-03-22T00:12:48Z","git_url":"git://github.com/druid-io/druid.git","ssh_url":"git@github.com:druid-io/druid.git","clone_url":"https://github.com/druid-io/druid.git","svn_url":"https://github.com/druid-io/druid","homepage":"http://druid.io","size":127917,"stargazers_count":2567,"watchers_count":2567,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":617,"mirror_url":null,"open_issues_count":378,"forks":617,"open_issues":378,"watchers":2567,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/druid-io/druid/pulls/2602"},"html":{"href":"https://github.com/druid-io/druid/pull/2602"},"issue":{"href":"https://api.github.com/repos/druid-io/druid/issues/2602"},"comments":{"href":"https://api.github.com/repos/druid-io/druid/issues/2602/comments"},"review_comments":{"href":"https://api.github.com/repos/druid-io/druid/pulls/2602/comments"},"review_comment":{"href":"https://api.github.com/repos/druid-io/druid/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/druid-io/druid/pulls/2602/commits"},"statuses":{"href":"https://api.github.com/repos/druid-io/druid/statuses/688f6f128d9b5845bd9d67ba59bd59e1e003b1b0"}}}}