You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by GitBox <gi...@apache.org> on 2020/09/30 07:38:15 UTC
[GitHub] [beam] rezarokni opened a new pull request #12973: [BEAM-10234] Create ApproximateDistinct using HLL Impl
rezarokni opened a new pull request #12973:
URL: https://github.com/apache/beam/pull/12973
[BEAM-10234] Create ApproximateDistinct using HLL Impl
Create ApproximateDistinct which uses HLL Impl
Marked ApproximateUnique as deprecated in preparation for removal in favour of ApproximateDistinct.
------------------------
Thank you for your contribution! Follow this checklist to help us incorporate your contribution quickly and easily:
- [ ] [**Choose reviewer(s)**](https://beam.apache.org/contribute/#make-your-change) and mention them in a comment (`R: @username`).
- [ ] Format the pull request title like `[BEAM-XXX] Fixes bug in ApproximateQuantiles`, where you replace `BEAM-XXX` with the appropriate JIRA issue, if applicable. This will automatically link the pull request to the issue.
- [ ] Update `CHANGES.md` with noteworthy changes.
- [ ] If this contribution is large, please file an Apache [Individual Contributor License Agreement](https://www.apache.org/licenses/icla.pdf).
See the [Contributor Guide](https://beam.apache.org/contribute) for more tips on [how to make review process smoother](https://beam.apache.org/contribute/#make-reviewers-job-easier).
Post-Commit Tests Status (on master branch)
------------------------------------------------------------------------------------------------
Lang | SDK | Dataflow | Flink | Samza | Spark | Twister2
--- | --- | --- | --- | --- | --- | ---
Go | [](https://ci-beam.apache.org/job/beam_PostCommit_Go/lastCompletedBuild/) | --- | [](https://ci-beam.apache.org/job/beam_PostCommit_Go_VR_Flink/lastCompletedBuild/) | --- | [](https://ci-beam.apache.org/job/beam_PostCommit_Go_VR_Spark/lastCompletedBuild/) | ---
Java | [](https://ci-beam.apache.org/job/beam_PostCommit_Java/lastCompletedBuild/) | [](https://ci-beam.apache.org/job/beam_PostCommit_Java_ValidatesRunner_Dataflow/lastCompletedBuild/)<br>[](https://ci-beam.apache.org/job/beam_PostCommit_Java_ValidatesRunner_Dataflow_Java11/lastCompletedBuild/) | [](https://ci-beam.apache.org/job/beam_PostCommit_Java_ValidatesRunner_Flink/lastCompletedBuild/)<br>[](https://ci-beam.apache.org/job/beam_PostCommit_Java_ValidatesRunner_Flink_Java11/lastCompletedBuild/)<br>[](https://ci-beam.apache.org/job/beam_PostCommit_Java_PVR_Flink_Batch/lastCompletedBuild/)<br>[](https://ci-beam.apache.org/job/beam_PostCommit_Java_PVR_Flink_Streaming/lastCompletedBuild/) | [](https://ci-beam.apache.org/job/beam_PostCommit_Java_ValidatesRunner_Samza/lastCompletedBuild/) | [](https://ci-beam.apache.org/job/beam_PostCommit_Java_ValidatesRunner_Spark/lastCompletedBuild/)<br>[](https://ci-beam.apache.org/job/beam_PostCommit_Java_PVR_Spark_Batch/lastCompletedBuild/)<br>[](https://ci-beam.apache.org/job/beam_PostCommit_Java_ValidatesRunner_SparkStructuredStreaming/lastCompletedBuild/) | [](https://ci-beam.apache.org/job/beam_PostCommit_Java_ValidatesRunner_Twister2/lastCompletedBuild/)
Python | [](https://ci-beam.apache.org/job/beam_PostCommit_Python36/lastCompletedBuild/)<br>[](https://ci-beam.apache.org/job/beam_PostCommit_Python37/lastCompletedBuild/)<br>[](https://ci-beam.apache.org/job/beam_PostCommit_Python38/lastCompletedBuild/) | [](https://ci-beam.apache.org/job/beam_PostCommit_Py_VR_Dataflow/lastCompletedBuild/)<br>[](https://ci-beam.apache.org/job/beam_PostCommit_Py_VR_Dataflow_V2/lastCompletedBuild/)<br>[](https://ci-beam.apache.org/job/beam_PostCommit_Py_ValCont/lastCompletedBuild/) | [](https://ci-beam.apache.org/job/beam_PreCommit_Python_PVR_Flink_Cron/lastCompletedBuild/)<br>[](https://ci-beam.apache.org/job/beam_PostCommit_Python_VR_Flink/lastCompletedBuild/) | --- | [](https://ci-beam.apache.org/job/beam_PostCommit_Python_VR_Spark/lastCompletedBuild/) | ---
XLang | [](https://ci-beam.apache.org/job/beam_PostCommit_XVR_Direct/lastCompletedBuild/) | --- | [](https://ci-beam.apache.org/job/beam_PostCommit_XVR_Flink/lastCompletedBuild/) | --- | [](https://ci-beam.apache.org/job/beam_PostCommit_XVR_Spark/lastCompletedBuild/) | ---
Pre-Commit Tests Status (on master branch)
------------------------------------------------------------------------------------------------
--- |Java | Python | Go | Website | Whitespace | Typescript
--- | --- | --- | --- | --- | --- | ---
Non-portable | [](https://ci-beam.apache.org/job/beam_PreCommit_Java_Cron/lastCompletedBuild/) | [](https://ci-beam.apache.org/job/beam_PreCommit_Python_Cron/lastCompletedBuild/)<br>[](https://ci-beam.apache.org/job/beam_PreCommit_PythonLint_Cron/lastCompletedBuild/)<br>[](https://ci-beam.apache.org/job/beam_PreCommit_PythonDocker_Cron/lastCompletedBuild/) <br>[](https://ci-beam.apache.org/job/beam_PreCommit_PythonDocs_Cron/lastCompletedBuild/) | [](https://ci-beam.apache.org/job/beam_PreCommit_Go_Cron/lastCompletedBuild/) | [](https://ci-beam.apache.org/job/beam_PreCommit_Website_Cron/lastCompletedBuild/) | [](https://ci-beam.apache.org/job/beam_PreCommit_Whitespace_Cron/lastCompletedBuild/) | [](https://ci-beam.apache.org/job/beam_PreCommit_Typescript_Cron/lastCompletedBuild/)
Portable | --- | [](https://ci-beam.apache.org/job/beam_PreCommit_Portable_Python_Cron/lastCompletedBuild/) | --- | --- | --- | ---
See [.test-infra/jenkins/README](https://github.com/apache/beam/blob/master/.test-infra/jenkins/README.md) for trigger phrase, status and link of all Jenkins jobs.
GitHub Actions Tests Status (on master branch)
------------------------------------------------------------------------------------------------
[](https://github.com/apache/beam/actions?query=workflow%3A%22Build+python+source+distribution+and+wheels%22+branch%3Amaster+event%3Aschedule)
[](https://github.com/apache/beam/actions?query=workflow%3A%22Python+Tests%22+branch%3Amaster+event%3Aschedule)
[](https://github.com/apache/beam/actions?query=workflow%3A%22Java+Tests%22+branch%3Amaster+event%3Aschedule)
See [CI.md](https://github.com/apache/beam/blob/master/CI.md) for more information about GitHub Actions CI.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] rezarokni commented on a change in pull request #12973: [BEAM-10234] Create ApproximateDistinct using HLL Impl
Posted by GitBox <gi...@apache.org>.
rezarokni commented on a change in pull request #12973:
URL: https://github.com/apache/beam/pull/12973#discussion_r534094224
##########
File path: sdks/java/extensions/zetasketch/src/test/java/org/apache/beam/sdk/extensions/zetasketch/ApproximateCountDistinctTest.java
##########
@@ -0,0 +1,342 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.beam.sdk.extensions.zetasketch;
+
+import com.google.zetasketch.HyperLogLogPlusPlus;
+import java.nio.ByteBuffer;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.Collectors;
+import org.apache.beam.sdk.testing.NeedsRunner;
+import org.apache.beam.sdk.testing.PAssert;
+import org.apache.beam.sdk.testing.TestPipeline;
+import org.apache.beam.sdk.transforms.Create;
+import org.apache.beam.sdk.values.KV;
+import org.apache.beam.sdk.values.PCollection;
+import org.apache.beam.sdk.values.TypeDescriptor;
+import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableList;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+
+/** Tests for {@link ApproximateCountDistinct}. */
+public class ApproximateCountDistinctTest {
+
+ @Rule public final transient TestPipeline p = TestPipeline.create();
+
+ // Integer
+ private static final List<Integer> INTS1 = Arrays.asList(1, 2, 3, 3, 1, 4);
+ private static final Long INTS1_ESTIMATE;
+
+ private static final int TEST_PRECISION = 20;
+
+ static {
+ HyperLogLogPlusPlus<Integer> hll = new HyperLogLogPlusPlus.Builder().buildForIntegers();
+ INTS1.forEach(hll::add);
+ INTS1_ESTIMATE = hll.longResult();
+ }
+
+ /** Test correct Builder is returned from Generic type. * */
+ @Test
+ public void testIntegerBuilder() {
+
+ PCollection<Integer> ints = p.apply(Create.of(1));
+ HllCount.Init.Builder<Integer> builder =
+ ApproximateCountDistinct.<Integer>builderForType(
+ ints.getCoder().getEncodedTypeDescriptor());
+ PCollection<Long> result = ints.apply(builder.globally()).apply(HllCount.Extract.globally());
+ PAssert.that(result).containsInAnyOrder(1L);
+ p.run();
+ }
+ /** Test correct Builder is returned from Generic type. * */
+ @Test
+ public void testStringBuilder() {
+
+ PCollection<String> strings = p.apply(Create.<String>of("43"));
+ HllCount.Init.Builder<String> builder =
+ ApproximateCountDistinct.<String>builderForType(
+ strings.getCoder().getEncodedTypeDescriptor());
+ PCollection<Long> result = strings.apply(builder.globally()).apply(HllCount.Extract.globally());
+ PAssert.that(result).containsInAnyOrder(1L);
+ p.run();
+ }
+ /** Test correct Builder is returned from Generic type. * */
+ @Test
+ public void testLongBuilder() {
+
+ PCollection<Long> longs = p.apply(Create.<Long>of(1L));
+ HllCount.Init.Builder<Long> builder =
+ ApproximateCountDistinct.<Long>builderForType(longs.getCoder().getEncodedTypeDescriptor());
+ PCollection<Long> result = longs.apply(builder.globally()).apply(HllCount.Extract.globally());
+ PAssert.that(result).containsInAnyOrder(1L);
+ p.run();
+ }
+ /** Test correct Builder is returned from Generic type. * */
+ @Test
+ public void testBytesBuilder() {
+
+ byte[] byteArray = new byte[] {'A'};
+ PCollection<byte[]> bytes = p.apply(Create.of(byteArray));
+ TypeDescriptor a = bytes.getCoder().getEncodedTypeDescriptor();
+ HllCount.Init.Builder<byte[]> builder =
+ ApproximateCountDistinct.<byte[]>builderForType(
+ bytes.getCoder().getEncodedTypeDescriptor());
+ PCollection<Long> result = bytes.apply(builder.globally()).apply(HllCount.Extract.globally());
+ PAssert.that(result).containsInAnyOrder(1L);
+ p.run();
+ }
+
+ /** Test Integer Globally. */
+ @Test
+ @Category(NeedsRunner.class)
+ public void testStandardTypesGlobalForInteger() {
+ PCollection<Long> approxResultInteger =
+ p.apply("Int", Create.of(INTS1)).apply("IntHLL", ApproximateCountDistinct.globally());
+ PAssert.thatSingleton(approxResultInteger).isEqualTo(INTS1_ESTIMATE);
+ p.run();
+ }
+
+ /** Test Long Globally. */
+ @Test
+ @Category(NeedsRunner.class)
+ public void testStandardTypesGlobalForLong() {
+
+ PCollection<Long> approxResultLong =
+ p.apply("Long", Create.of(INTS1.stream().map(Long::valueOf).collect(Collectors.toList())))
+ .apply("LongHLL", ApproximateCountDistinct.globally());
+
+ PAssert.thatSingleton(approxResultLong).isEqualTo(INTS1_ESTIMATE);
+
+ p.run();
+ }
+
+ /** Test String Globally. */
+ @Test
+ @Category(NeedsRunner.class)
+ public void testStandardTypesGlobalForStrings() {
+ PCollection<Long> approxResultString =
+ p.apply("Str", Create.of(INTS1.stream().map(String::valueOf).collect(Collectors.toList())))
+ .apply("StrHLL", ApproximateCountDistinct.globally());
+
+ PAssert.thatSingleton(approxResultString).isEqualTo(INTS1_ESTIMATE);
+
+ p.run();
+ }
+
+ /** Test Byte Globally. */
+ @Test
+ @Category(NeedsRunner.class)
+ public void testStandardTypesGlobalForBytes() {
+ PCollection<Long> approxResultByte =
+ p.apply(
+ "BytesHLL",
+ Create.of(
+ INTS1.stream()
+ .map(x -> ByteBuffer.allocate(4).putInt(x).array())
+ .collect(Collectors.toList())))
+ .apply(ApproximateCountDistinct.globally());
+
+ PAssert.thatSingleton(approxResultByte).isEqualTo(INTS1_ESTIMATE);
+
+ p.run();
+ }
+
+ /** Test Integer Globally. */
+ @Test
+ @Category(NeedsRunner.class)
+ public void testStandardTypesPerKeyForInteger() {
+
+ List<KV<Integer, Integer>> ints = new ArrayList<>();
+
+ for (int i = 0; i < 3; i++) {
+ for (int k : INTS1) {
+ ints.add(KV.of(i, k));
+ }
+ }
+
+ PCollection<KV<Integer, Long>> result =
+ p.apply("Int", Create.of(ints)).apply("IntHLL", ApproximateCountDistinct.perKey());
+
+ PAssert.that(result)
+ .containsInAnyOrder(
+ ImmutableList.of(
+ KV.of(0, INTS1_ESTIMATE), KV.of(1, INTS1_ESTIMATE), KV.of(2, INTS1_ESTIMATE)));
+
+ p.run();
+ }
+
+ /** Test Long Globally. */
+ @Test
+ @Category(NeedsRunner.class)
+ public void testStandardTypesPerKeyForLong() {
+
+ List<KV<Integer, Long>> longs = new ArrayList<>();
+
+ for (int i = 0; i < 3; i++) {
+ for (int k : INTS1) {
+ longs.add(KV.of(i, (long) k));
+ }
+ }
+
+ PCollection<KV<Integer, Long>> result =
+ p.apply("Long", Create.of(longs)).apply("LongHLL", ApproximateCountDistinct.perKey());
+
+ PAssert.that(result)
+ .containsInAnyOrder(
+ ImmutableList.of(
+ KV.of(0, INTS1_ESTIMATE), KV.of(1, INTS1_ESTIMATE), KV.of(2, INTS1_ESTIMATE)));
+
+ p.run();
+ }
+
+ /** Test String Globally. */
+ @Test
+ @Category(NeedsRunner.class)
+ public void testStandardTypesPerKeyForStrings() {
+ List<KV<Integer, String>> strings = new ArrayList<>();
+
+ for (int i = 0; i < 3; i++) {
+ for (int k : INTS1) {
+ strings.add(KV.of(i, String.valueOf(k)));
+ }
+ }
+
+ PCollection<KV<Integer, Long>> result =
+ p.apply("Str", Create.of(strings)).apply("StrHLL", ApproximateCountDistinct.perKey());
+
+ PAssert.that(result)
+ .containsInAnyOrder(
+ ImmutableList.of(
+ KV.of(0, INTS1_ESTIMATE), KV.of(1, INTS1_ESTIMATE), KV.of(2, INTS1_ESTIMATE)));
+
+ p.run();
+ }
+
+ /** Test Byte Globally. */
+ @Test
+ @Category(NeedsRunner.class)
+ public void testStandardTypesPerKeyForBytes() {
+
+ List<KV<Integer, byte[]>> bytes = new ArrayList<>();
+
+ for (int i = 0; i < 3; i++) {
+ for (int k : INTS1) {
+ bytes.add(KV.of(i, ByteBuffer.allocate(4).putInt(k).array()));
+ }
+ }
+
+ PCollection<KV<Integer, Long>> result =
+ p.apply("BytesHLL", Create.of(bytes)).apply(ApproximateCountDistinct.perKey());
+
+ PAssert.that(result)
+ .containsInAnyOrder(
+ ImmutableList.of(
+ KV.of(0, INTS1_ESTIMATE), KV.of(1, INTS1_ESTIMATE), KV.of(2, INTS1_ESTIMATE)));
+
+ p.run();
+ }
+
+ /** Test a general object, we will make use of a KV as the object as it already has a coder. */
+ @Test
+ @Category(NeedsRunner.class)
+ public void testObjectTypesGlobal() {
+
+ PCollection<Long> approxResultInteger =
+ p.apply(
+ "Int",
+ Create.of(
+ INTS1.stream().map(x -> KV.of(x, KV.of(x, x))).collect(Collectors.toList())))
+ .apply(
+ "IntHLL",
+ ApproximateCountDistinct.<KV<Integer, KV<Integer, Integer>>>globally()
+ .via((KV<Integer, KV<Integer, Integer>> x) -> (long) x.getValue().hashCode()));
Review comment:
The value in this case is the KV<int,int> we are using a KV here as a convenient object , so we need the unique from the value which comes from KV<int,KV<int,int>>
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] robinyqiu commented on a change in pull request #12973: [BEAM-10234] Create ApproximateDistinct using HLL Impl
Posted by GitBox <gi...@apache.org>.
robinyqiu commented on a change in pull request #12973:
URL: https://github.com/apache/beam/pull/12973#discussion_r549458806
##########
File path: sdks/java/extensions/zetasketch/src/test/java/org/apache/beam/sdk/extensions/zetasketch/ApproximateCountDistinctTest.java
##########
@@ -0,0 +1,342 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.beam.sdk.extensions.zetasketch;
+
+import com.google.zetasketch.HyperLogLogPlusPlus;
+import java.nio.ByteBuffer;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.Collectors;
+import org.apache.beam.sdk.testing.NeedsRunner;
+import org.apache.beam.sdk.testing.PAssert;
+import org.apache.beam.sdk.testing.TestPipeline;
+import org.apache.beam.sdk.transforms.Create;
+import org.apache.beam.sdk.values.KV;
+import org.apache.beam.sdk.values.PCollection;
+import org.apache.beam.sdk.values.TypeDescriptor;
+import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableList;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+
+/** Tests for {@link ApproximateCountDistinct}. */
+public class ApproximateCountDistinctTest {
+
+ @Rule public final transient TestPipeline p = TestPipeline.create();
+
+ // Integer
+ private static final List<Integer> INTS1 = Arrays.asList(1, 2, 3, 3, 1, 4);
+ private static final Long INTS1_ESTIMATE;
+
+ private static final int TEST_PRECISION = 20;
+
+ static {
+ HyperLogLogPlusPlus<Integer> hll = new HyperLogLogPlusPlus.Builder().buildForIntegers();
+ INTS1.forEach(hll::add);
+ INTS1_ESTIMATE = hll.longResult();
+ }
+
+ /** Test correct Builder is returned from Generic type. * */
+ @Test
+ public void testIntegerBuilder() {
+
+ PCollection<Integer> ints = p.apply(Create.of(1));
+ HllCount.Init.Builder<Integer> builder =
+ ApproximateCountDistinct.<Integer>builderForType(
+ ints.getCoder().getEncodedTypeDescriptor());
+ PCollection<Long> result = ints.apply(builder.globally()).apply(HllCount.Extract.globally());
+ PAssert.that(result).containsInAnyOrder(1L);
+ p.run();
+ }
+ /** Test correct Builder is returned from Generic type. * */
+ @Test
+ public void testStringBuilder() {
+
+ PCollection<String> strings = p.apply(Create.<String>of("43"));
+ HllCount.Init.Builder<String> builder =
+ ApproximateCountDistinct.<String>builderForType(
+ strings.getCoder().getEncodedTypeDescriptor());
+ PCollection<Long> result = strings.apply(builder.globally()).apply(HllCount.Extract.globally());
+ PAssert.that(result).containsInAnyOrder(1L);
+ p.run();
+ }
+ /** Test correct Builder is returned from Generic type. * */
+ @Test
+ public void testLongBuilder() {
+
+ PCollection<Long> longs = p.apply(Create.<Long>of(1L));
+ HllCount.Init.Builder<Long> builder =
+ ApproximateCountDistinct.<Long>builderForType(longs.getCoder().getEncodedTypeDescriptor());
+ PCollection<Long> result = longs.apply(builder.globally()).apply(HllCount.Extract.globally());
+ PAssert.that(result).containsInAnyOrder(1L);
+ p.run();
+ }
+ /** Test correct Builder is returned from Generic type. * */
+ @Test
+ public void testBytesBuilder() {
+
+ byte[] byteArray = new byte[] {'A'};
+ PCollection<byte[]> bytes = p.apply(Create.of(byteArray));
+ TypeDescriptor a = bytes.getCoder().getEncodedTypeDescriptor();
+ HllCount.Init.Builder<byte[]> builder =
+ ApproximateCountDistinct.<byte[]>builderForType(
+ bytes.getCoder().getEncodedTypeDescriptor());
+ PCollection<Long> result = bytes.apply(builder.globally()).apply(HllCount.Extract.globally());
+ PAssert.that(result).containsInAnyOrder(1L);
+ p.run();
+ }
+
+ /** Test Integer Globally. */
+ @Test
+ @Category(NeedsRunner.class)
+ public void testStandardTypesGlobalForInteger() {
+ PCollection<Long> approxResultInteger =
+ p.apply("Int", Create.of(INTS1)).apply("IntHLL", ApproximateCountDistinct.globally());
+ PAssert.thatSingleton(approxResultInteger).isEqualTo(INTS1_ESTIMATE);
+ p.run();
+ }
+
+ /** Test Long Globally. */
+ @Test
+ @Category(NeedsRunner.class)
+ public void testStandardTypesGlobalForLong() {
+
+ PCollection<Long> approxResultLong =
+ p.apply("Long", Create.of(INTS1.stream().map(Long::valueOf).collect(Collectors.toList())))
+ .apply("LongHLL", ApproximateCountDistinct.globally());
+
+ PAssert.thatSingleton(approxResultLong).isEqualTo(INTS1_ESTIMATE);
+
+ p.run();
+ }
+
+ /** Test String Globally. */
+ @Test
+ @Category(NeedsRunner.class)
+ public void testStandardTypesGlobalForStrings() {
+ PCollection<Long> approxResultString =
+ p.apply("Str", Create.of(INTS1.stream().map(String::valueOf).collect(Collectors.toList())))
+ .apply("StrHLL", ApproximateCountDistinct.globally());
+
+ PAssert.thatSingleton(approxResultString).isEqualTo(INTS1_ESTIMATE);
+
+ p.run();
+ }
+
+ /** Test Byte Globally. */
+ @Test
+ @Category(NeedsRunner.class)
+ public void testStandardTypesGlobalForBytes() {
+ PCollection<Long> approxResultByte =
+ p.apply(
+ "BytesHLL",
+ Create.of(
+ INTS1.stream()
+ .map(x -> ByteBuffer.allocate(4).putInt(x).array())
+ .collect(Collectors.toList())))
+ .apply(ApproximateCountDistinct.globally());
+
+ PAssert.thatSingleton(approxResultByte).isEqualTo(INTS1_ESTIMATE);
+
+ p.run();
+ }
+
+ /** Test Integer Globally. */
+ @Test
+ @Category(NeedsRunner.class)
+ public void testStandardTypesPerKeyForInteger() {
+
+ List<KV<Integer, Integer>> ints = new ArrayList<>();
+
+ for (int i = 0; i < 3; i++) {
+ for (int k : INTS1) {
+ ints.add(KV.of(i, k));
+ }
+ }
+
+ PCollection<KV<Integer, Long>> result =
+ p.apply("Int", Create.of(ints)).apply("IntHLL", ApproximateCountDistinct.perKey());
+
+ PAssert.that(result)
+ .containsInAnyOrder(
+ ImmutableList.of(
+ KV.of(0, INTS1_ESTIMATE), KV.of(1, INTS1_ESTIMATE), KV.of(2, INTS1_ESTIMATE)));
+
+ p.run();
+ }
+
+ /** Test Long Globally. */
+ @Test
+ @Category(NeedsRunner.class)
+ public void testStandardTypesPerKeyForLong() {
+
+ List<KV<Integer, Long>> longs = new ArrayList<>();
+
+ for (int i = 0; i < 3; i++) {
+ for (int k : INTS1) {
+ longs.add(KV.of(i, (long) k));
+ }
+ }
+
+ PCollection<KV<Integer, Long>> result =
+ p.apply("Long", Create.of(longs)).apply("LongHLL", ApproximateCountDistinct.perKey());
+
+ PAssert.that(result)
+ .containsInAnyOrder(
+ ImmutableList.of(
+ KV.of(0, INTS1_ESTIMATE), KV.of(1, INTS1_ESTIMATE), KV.of(2, INTS1_ESTIMATE)));
+
+ p.run();
+ }
+
+ /** Test String Globally. */
+ @Test
+ @Category(NeedsRunner.class)
+ public void testStandardTypesPerKeyForStrings() {
+ List<KV<Integer, String>> strings = new ArrayList<>();
+
+ for (int i = 0; i < 3; i++) {
+ for (int k : INTS1) {
+ strings.add(KV.of(i, String.valueOf(k)));
+ }
+ }
+
+ PCollection<KV<Integer, Long>> result =
+ p.apply("Str", Create.of(strings)).apply("StrHLL", ApproximateCountDistinct.perKey());
+
+ PAssert.that(result)
+ .containsInAnyOrder(
+ ImmutableList.of(
+ KV.of(0, INTS1_ESTIMATE), KV.of(1, INTS1_ESTIMATE), KV.of(2, INTS1_ESTIMATE)));
+
+ p.run();
+ }
+
+ /** Test Byte Globally. */
+ @Test
+ @Category(NeedsRunner.class)
+ public void testStandardTypesPerKeyForBytes() {
+
+ List<KV<Integer, byte[]>> bytes = new ArrayList<>();
+
+ for (int i = 0; i < 3; i++) {
+ for (int k : INTS1) {
+ bytes.add(KV.of(i, ByteBuffer.allocate(4).putInt(k).array()));
+ }
+ }
+
+ PCollection<KV<Integer, Long>> result =
+ p.apply("BytesHLL", Create.of(bytes)).apply(ApproximateCountDistinct.perKey());
+
+ PAssert.that(result)
+ .containsInAnyOrder(
+ ImmutableList.of(
+ KV.of(0, INTS1_ESTIMATE), KV.of(1, INTS1_ESTIMATE), KV.of(2, INTS1_ESTIMATE)));
+
+ p.run();
+ }
+
+ /** Test a general object, we will make use of a KV as the object as it already has a coder. */
+ @Test
+ @Category(NeedsRunner.class)
+ public void testObjectTypesGlobal() {
+
+ PCollection<Long> approxResultInteger =
+ p.apply(
+ "Int",
+ Create.of(
+ INTS1.stream().map(x -> KV.of(x, KV.of(x, x))).collect(Collectors.toList())))
+ .apply(
+ "IntHLL",
+ ApproximateCountDistinct.<KV<Integer, KV<Integer, Integer>>>globally()
+ .via((KV<Integer, KV<Integer, Integer>> x) -> (long) x.getValue().hashCode()));
Review comment:
I am ok with merging it as it is.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] rezarokni commented on a change in pull request #12973: [BEAM-10234] Create ApproximateDistinct using HLL Impl
Posted by GitBox <gi...@apache.org>.
rezarokni commented on a change in pull request #12973:
URL: https://github.com/apache/beam/pull/12973#discussion_r547263060
##########
File path: sdks/java/extensions/zetasketch/src/test/java/org/apache/beam/sdk/extensions/zetasketch/ApproximateCountDistinctTest.java
##########
@@ -0,0 +1,342 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.beam.sdk.extensions.zetasketch;
+
+import com.google.zetasketch.HyperLogLogPlusPlus;
+import java.nio.ByteBuffer;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.Collectors;
+import org.apache.beam.sdk.testing.NeedsRunner;
+import org.apache.beam.sdk.testing.PAssert;
+import org.apache.beam.sdk.testing.TestPipeline;
+import org.apache.beam.sdk.transforms.Create;
+import org.apache.beam.sdk.values.KV;
+import org.apache.beam.sdk.values.PCollection;
+import org.apache.beam.sdk.values.TypeDescriptor;
+import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableList;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+
+/** Tests for {@link ApproximateCountDistinct}. */
+public class ApproximateCountDistinctTest {
+
+ @Rule public final transient TestPipeline p = TestPipeline.create();
+
+ // Integer
+ private static final List<Integer> INTS1 = Arrays.asList(1, 2, 3, 3, 1, 4);
+ private static final Long INTS1_ESTIMATE;
+
+ private static final int TEST_PRECISION = 20;
+
+ static {
+ HyperLogLogPlusPlus<Integer> hll = new HyperLogLogPlusPlus.Builder().buildForIntegers();
+ INTS1.forEach(hll::add);
+ INTS1_ESTIMATE = hll.longResult();
+ }
+
+ /** Test correct Builder is returned from Generic type. * */
+ @Test
+ public void testIntegerBuilder() {
+
+ PCollection<Integer> ints = p.apply(Create.of(1));
+ HllCount.Init.Builder<Integer> builder =
+ ApproximateCountDistinct.<Integer>builderForType(
+ ints.getCoder().getEncodedTypeDescriptor());
+ PCollection<Long> result = ints.apply(builder.globally()).apply(HllCount.Extract.globally());
+ PAssert.that(result).containsInAnyOrder(1L);
+ p.run();
+ }
+ /** Test correct Builder is returned from Generic type. * */
+ @Test
+ public void testStringBuilder() {
+
+ PCollection<String> strings = p.apply(Create.<String>of("43"));
+ HllCount.Init.Builder<String> builder =
+ ApproximateCountDistinct.<String>builderForType(
+ strings.getCoder().getEncodedTypeDescriptor());
+ PCollection<Long> result = strings.apply(builder.globally()).apply(HllCount.Extract.globally());
+ PAssert.that(result).containsInAnyOrder(1L);
+ p.run();
+ }
+ /** Test correct Builder is returned from Generic type. * */
+ @Test
+ public void testLongBuilder() {
+
+ PCollection<Long> longs = p.apply(Create.<Long>of(1L));
+ HllCount.Init.Builder<Long> builder =
+ ApproximateCountDistinct.<Long>builderForType(longs.getCoder().getEncodedTypeDescriptor());
+ PCollection<Long> result = longs.apply(builder.globally()).apply(HllCount.Extract.globally());
+ PAssert.that(result).containsInAnyOrder(1L);
+ p.run();
+ }
+ /** Test correct Builder is returned from Generic type. * */
+ @Test
+ public void testBytesBuilder() {
+
+ byte[] byteArray = new byte[] {'A'};
+ PCollection<byte[]> bytes = p.apply(Create.of(byteArray));
+ TypeDescriptor a = bytes.getCoder().getEncodedTypeDescriptor();
+ HllCount.Init.Builder<byte[]> builder =
+ ApproximateCountDistinct.<byte[]>builderForType(
+ bytes.getCoder().getEncodedTypeDescriptor());
+ PCollection<Long> result = bytes.apply(builder.globally()).apply(HllCount.Extract.globally());
+ PAssert.that(result).containsInAnyOrder(1L);
+ p.run();
+ }
+
+ /** Test Integer Globally. */
+ @Test
+ @Category(NeedsRunner.class)
+ public void testStandardTypesGlobalForInteger() {
+ PCollection<Long> approxResultInteger =
+ p.apply("Int", Create.of(INTS1)).apply("IntHLL", ApproximateCountDistinct.globally());
+ PAssert.thatSingleton(approxResultInteger).isEqualTo(INTS1_ESTIMATE);
+ p.run();
+ }
+
+ /** Test Long Globally. */
+ @Test
+ @Category(NeedsRunner.class)
+ public void testStandardTypesGlobalForLong() {
+
+ PCollection<Long> approxResultLong =
+ p.apply("Long", Create.of(INTS1.stream().map(Long::valueOf).collect(Collectors.toList())))
+ .apply("LongHLL", ApproximateCountDistinct.globally());
+
+ PAssert.thatSingleton(approxResultLong).isEqualTo(INTS1_ESTIMATE);
+
+ p.run();
+ }
+
+ /** Test String Globally. */
+ @Test
+ @Category(NeedsRunner.class)
+ public void testStandardTypesGlobalForStrings() {
+ PCollection<Long> approxResultString =
+ p.apply("Str", Create.of(INTS1.stream().map(String::valueOf).collect(Collectors.toList())))
+ .apply("StrHLL", ApproximateCountDistinct.globally());
+
+ PAssert.thatSingleton(approxResultString).isEqualTo(INTS1_ESTIMATE);
+
+ p.run();
+ }
+
+ /** Test Byte Globally. */
+ @Test
+ @Category(NeedsRunner.class)
+ public void testStandardTypesGlobalForBytes() {
+ PCollection<Long> approxResultByte =
+ p.apply(
+ "BytesHLL",
+ Create.of(
+ INTS1.stream()
+ .map(x -> ByteBuffer.allocate(4).putInt(x).array())
+ .collect(Collectors.toList())))
+ .apply(ApproximateCountDistinct.globally());
+
+ PAssert.thatSingleton(approxResultByte).isEqualTo(INTS1_ESTIMATE);
+
+ p.run();
+ }
+
+ /** Test Integer Globally. */
+ @Test
+ @Category(NeedsRunner.class)
+ public void testStandardTypesPerKeyForInteger() {
+
+ List<KV<Integer, Integer>> ints = new ArrayList<>();
+
+ for (int i = 0; i < 3; i++) {
+ for (int k : INTS1) {
+ ints.add(KV.of(i, k));
+ }
+ }
+
+ PCollection<KV<Integer, Long>> result =
+ p.apply("Int", Create.of(ints)).apply("IntHLL", ApproximateCountDistinct.perKey());
+
+ PAssert.that(result)
+ .containsInAnyOrder(
+ ImmutableList.of(
+ KV.of(0, INTS1_ESTIMATE), KV.of(1, INTS1_ESTIMATE), KV.of(2, INTS1_ESTIMATE)));
+
+ p.run();
+ }
+
+ /** Test Long Globally. */
+ @Test
+ @Category(NeedsRunner.class)
+ public void testStandardTypesPerKeyForLong() {
+
+ List<KV<Integer, Long>> longs = new ArrayList<>();
+
+ for (int i = 0; i < 3; i++) {
+ for (int k : INTS1) {
+ longs.add(KV.of(i, (long) k));
+ }
+ }
+
+ PCollection<KV<Integer, Long>> result =
+ p.apply("Long", Create.of(longs)).apply("LongHLL", ApproximateCountDistinct.perKey());
+
+ PAssert.that(result)
+ .containsInAnyOrder(
+ ImmutableList.of(
+ KV.of(0, INTS1_ESTIMATE), KV.of(1, INTS1_ESTIMATE), KV.of(2, INTS1_ESTIMATE)));
+
+ p.run();
+ }
+
+ /** Test String Globally. */
+ @Test
+ @Category(NeedsRunner.class)
+ public void testStandardTypesPerKeyForStrings() {
+ List<KV<Integer, String>> strings = new ArrayList<>();
+
+ for (int i = 0; i < 3; i++) {
+ for (int k : INTS1) {
+ strings.add(KV.of(i, String.valueOf(k)));
+ }
+ }
+
+ PCollection<KV<Integer, Long>> result =
+ p.apply("Str", Create.of(strings)).apply("StrHLL", ApproximateCountDistinct.perKey());
+
+ PAssert.that(result)
+ .containsInAnyOrder(
+ ImmutableList.of(
+ KV.of(0, INTS1_ESTIMATE), KV.of(1, INTS1_ESTIMATE), KV.of(2, INTS1_ESTIMATE)));
+
+ p.run();
+ }
+
+ /** Test Byte Globally. */
+ @Test
+ @Category(NeedsRunner.class)
+ public void testStandardTypesPerKeyForBytes() {
+
+ List<KV<Integer, byte[]>> bytes = new ArrayList<>();
+
+ for (int i = 0; i < 3; i++) {
+ for (int k : INTS1) {
+ bytes.add(KV.of(i, ByteBuffer.allocate(4).putInt(k).array()));
+ }
+ }
+
+ PCollection<KV<Integer, Long>> result =
+ p.apply("BytesHLL", Create.of(bytes)).apply(ApproximateCountDistinct.perKey());
+
+ PAssert.that(result)
+ .containsInAnyOrder(
+ ImmutableList.of(
+ KV.of(0, INTS1_ESTIMATE), KV.of(1, INTS1_ESTIMATE), KV.of(2, INTS1_ESTIMATE)));
+
+ p.run();
+ }
+
+ /** Test a general object, we will make use of a KV as the object as it already has a coder. */
+ @Test
+ @Category(NeedsRunner.class)
+ public void testObjectTypesGlobal() {
+
+ PCollection<Long> approxResultInteger =
+ p.apply(
+ "Int",
+ Create.of(
+ INTS1.stream().map(x -> KV.of(x, KV.of(x, x))).collect(Collectors.toList())))
+ .apply(
+ "IntHLL",
+ ApproximateCountDistinct.<KV<Integer, KV<Integer, Integer>>>globally()
+ .via((KV<Integer, KV<Integer, Integer>> x) -> (long) x.getValue().hashCode()));
Review comment:
If we getValue().getValue() then we will get the value of the 2nd KV which is not going to be unique as KV<1,KV<2,3>> is not == to KV<1,KV<3,3>> . If KV is an issue I can create a new POJO just for this test. Using a KV within a KV is just an easy convenience.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] rezarokni commented on a change in pull request #12973: [BEAM-10234] Create ApproximateDistinct using HLL Impl
Posted by GitBox <gi...@apache.org>.
rezarokni commented on a change in pull request #12973:
URL: https://github.com/apache/beam/pull/12973#discussion_r549053206
##########
File path: sdks/java/extensions/zetasketch/src/test/java/org/apache/beam/sdk/extensions/zetasketch/ApproximateCountDistinctTest.java
##########
@@ -0,0 +1,342 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.beam.sdk.extensions.zetasketch;
+
+import com.google.zetasketch.HyperLogLogPlusPlus;
+import java.nio.ByteBuffer;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.Collectors;
+import org.apache.beam.sdk.testing.NeedsRunner;
+import org.apache.beam.sdk.testing.PAssert;
+import org.apache.beam.sdk.testing.TestPipeline;
+import org.apache.beam.sdk.transforms.Create;
+import org.apache.beam.sdk.values.KV;
+import org.apache.beam.sdk.values.PCollection;
+import org.apache.beam.sdk.values.TypeDescriptor;
+import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableList;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+
+/** Tests for {@link ApproximateCountDistinct}. */
+public class ApproximateCountDistinctTest {
+
+ @Rule public final transient TestPipeline p = TestPipeline.create();
+
+ // Integer
+ private static final List<Integer> INTS1 = Arrays.asList(1, 2, 3, 3, 1, 4);
+ private static final Long INTS1_ESTIMATE;
+
+ private static final int TEST_PRECISION = 20;
+
+ static {
+ HyperLogLogPlusPlus<Integer> hll = new HyperLogLogPlusPlus.Builder().buildForIntegers();
+ INTS1.forEach(hll::add);
+ INTS1_ESTIMATE = hll.longResult();
+ }
+
+ /** Test correct Builder is returned from Generic type. * */
+ @Test
+ public void testIntegerBuilder() {
+
+ PCollection<Integer> ints = p.apply(Create.of(1));
+ HllCount.Init.Builder<Integer> builder =
+ ApproximateCountDistinct.<Integer>builderForType(
+ ints.getCoder().getEncodedTypeDescriptor());
+ PCollection<Long> result = ints.apply(builder.globally()).apply(HllCount.Extract.globally());
+ PAssert.that(result).containsInAnyOrder(1L);
+ p.run();
+ }
+ /** Test correct Builder is returned from Generic type. * */
+ @Test
+ public void testStringBuilder() {
+
+ PCollection<String> strings = p.apply(Create.<String>of("43"));
+ HllCount.Init.Builder<String> builder =
+ ApproximateCountDistinct.<String>builderForType(
+ strings.getCoder().getEncodedTypeDescriptor());
+ PCollection<Long> result = strings.apply(builder.globally()).apply(HllCount.Extract.globally());
+ PAssert.that(result).containsInAnyOrder(1L);
+ p.run();
+ }
+ /** Test correct Builder is returned from Generic type. * */
+ @Test
+ public void testLongBuilder() {
+
+ PCollection<Long> longs = p.apply(Create.<Long>of(1L));
+ HllCount.Init.Builder<Long> builder =
+ ApproximateCountDistinct.<Long>builderForType(longs.getCoder().getEncodedTypeDescriptor());
+ PCollection<Long> result = longs.apply(builder.globally()).apply(HllCount.Extract.globally());
+ PAssert.that(result).containsInAnyOrder(1L);
+ p.run();
+ }
+ /** Test correct Builder is returned from Generic type. * */
+ @Test
+ public void testBytesBuilder() {
+
+ byte[] byteArray = new byte[] {'A'};
+ PCollection<byte[]> bytes = p.apply(Create.of(byteArray));
+ TypeDescriptor a = bytes.getCoder().getEncodedTypeDescriptor();
+ HllCount.Init.Builder<byte[]> builder =
+ ApproximateCountDistinct.<byte[]>builderForType(
+ bytes.getCoder().getEncodedTypeDescriptor());
+ PCollection<Long> result = bytes.apply(builder.globally()).apply(HllCount.Extract.globally());
+ PAssert.that(result).containsInAnyOrder(1L);
+ p.run();
+ }
+
+ /** Test Integer Globally. */
+ @Test
+ @Category(NeedsRunner.class)
+ public void testStandardTypesGlobalForInteger() {
+ PCollection<Long> approxResultInteger =
+ p.apply("Int", Create.of(INTS1)).apply("IntHLL", ApproximateCountDistinct.globally());
+ PAssert.thatSingleton(approxResultInteger).isEqualTo(INTS1_ESTIMATE);
+ p.run();
+ }
+
+ /** Test Long Globally. */
+ @Test
+ @Category(NeedsRunner.class)
+ public void testStandardTypesGlobalForLong() {
+
+ PCollection<Long> approxResultLong =
+ p.apply("Long", Create.of(INTS1.stream().map(Long::valueOf).collect(Collectors.toList())))
+ .apply("LongHLL", ApproximateCountDistinct.globally());
+
+ PAssert.thatSingleton(approxResultLong).isEqualTo(INTS1_ESTIMATE);
+
+ p.run();
+ }
+
+ /** Test String Globally. */
+ @Test
+ @Category(NeedsRunner.class)
+ public void testStandardTypesGlobalForStrings() {
+ PCollection<Long> approxResultString =
+ p.apply("Str", Create.of(INTS1.stream().map(String::valueOf).collect(Collectors.toList())))
+ .apply("StrHLL", ApproximateCountDistinct.globally());
+
+ PAssert.thatSingleton(approxResultString).isEqualTo(INTS1_ESTIMATE);
+
+ p.run();
+ }
+
+ /** Test Byte Globally. */
+ @Test
+ @Category(NeedsRunner.class)
+ public void testStandardTypesGlobalForBytes() {
+ PCollection<Long> approxResultByte =
+ p.apply(
+ "BytesHLL",
+ Create.of(
+ INTS1.stream()
+ .map(x -> ByteBuffer.allocate(4).putInt(x).array())
+ .collect(Collectors.toList())))
+ .apply(ApproximateCountDistinct.globally());
+
+ PAssert.thatSingleton(approxResultByte).isEqualTo(INTS1_ESTIMATE);
+
+ p.run();
+ }
+
+ /** Test Integer Globally. */
+ @Test
+ @Category(NeedsRunner.class)
+ public void testStandardTypesPerKeyForInteger() {
+
+ List<KV<Integer, Integer>> ints = new ArrayList<>();
+
+ for (int i = 0; i < 3; i++) {
+ for (int k : INTS1) {
+ ints.add(KV.of(i, k));
+ }
+ }
+
+ PCollection<KV<Integer, Long>> result =
+ p.apply("Int", Create.of(ints)).apply("IntHLL", ApproximateCountDistinct.perKey());
+
+ PAssert.that(result)
+ .containsInAnyOrder(
+ ImmutableList.of(
+ KV.of(0, INTS1_ESTIMATE), KV.of(1, INTS1_ESTIMATE), KV.of(2, INTS1_ESTIMATE)));
+
+ p.run();
+ }
+
+ /** Test Long Globally. */
+ @Test
+ @Category(NeedsRunner.class)
+ public void testStandardTypesPerKeyForLong() {
+
+ List<KV<Integer, Long>> longs = new ArrayList<>();
+
+ for (int i = 0; i < 3; i++) {
+ for (int k : INTS1) {
+ longs.add(KV.of(i, (long) k));
+ }
+ }
+
+ PCollection<KV<Integer, Long>> result =
+ p.apply("Long", Create.of(longs)).apply("LongHLL", ApproximateCountDistinct.perKey());
+
+ PAssert.that(result)
+ .containsInAnyOrder(
+ ImmutableList.of(
+ KV.of(0, INTS1_ESTIMATE), KV.of(1, INTS1_ESTIMATE), KV.of(2, INTS1_ESTIMATE)));
+
+ p.run();
+ }
+
+ /** Test String Globally. */
+ @Test
+ @Category(NeedsRunner.class)
+ public void testStandardTypesPerKeyForStrings() {
+ List<KV<Integer, String>> strings = new ArrayList<>();
+
+ for (int i = 0; i < 3; i++) {
+ for (int k : INTS1) {
+ strings.add(KV.of(i, String.valueOf(k)));
+ }
+ }
+
+ PCollection<KV<Integer, Long>> result =
+ p.apply("Str", Create.of(strings)).apply("StrHLL", ApproximateCountDistinct.perKey());
+
+ PAssert.that(result)
+ .containsInAnyOrder(
+ ImmutableList.of(
+ KV.of(0, INTS1_ESTIMATE), KV.of(1, INTS1_ESTIMATE), KV.of(2, INTS1_ESTIMATE)));
+
+ p.run();
+ }
+
+ /** Test Byte Globally. */
+ @Test
+ @Category(NeedsRunner.class)
+ public void testStandardTypesPerKeyForBytes() {
+
+ List<KV<Integer, byte[]>> bytes = new ArrayList<>();
+
+ for (int i = 0; i < 3; i++) {
+ for (int k : INTS1) {
+ bytes.add(KV.of(i, ByteBuffer.allocate(4).putInt(k).array()));
+ }
+ }
+
+ PCollection<KV<Integer, Long>> result =
+ p.apply("BytesHLL", Create.of(bytes)).apply(ApproximateCountDistinct.perKey());
+
+ PAssert.that(result)
+ .containsInAnyOrder(
+ ImmutableList.of(
+ KV.of(0, INTS1_ESTIMATE), KV.of(1, INTS1_ESTIMATE), KV.of(2, INTS1_ESTIMATE)));
+
+ p.run();
+ }
+
+ /** Test a general object, we will make use of a KV as the object as it already has a coder. */
+ @Test
+ @Category(NeedsRunner.class)
+ public void testObjectTypesGlobal() {
+
+ PCollection<Long> approxResultInteger =
+ p.apply(
+ "Int",
+ Create.of(
+ INTS1.stream().map(x -> KV.of(x, KV.of(x, x))).collect(Collectors.toList())))
+ .apply(
+ "IntHLL",
+ ApproximateCountDistinct.<KV<Integer, KV<Integer, Integer>>>globally()
+ .via((KV<Integer, KV<Integer, Integer>> x) -> (long) x.getValue().hashCode()));
Review comment:
@robinyqiu are you ok with this being merged without the change to the Test ? Thanx.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] rezarokni commented on a change in pull request #12973: [BEAM-10234] Create ApproximateDistinct using HLL Impl
Posted by GitBox <gi...@apache.org>.
rezarokni commented on a change in pull request #12973:
URL: https://github.com/apache/beam/pull/12973#discussion_r547337917
##########
File path: sdks/java/extensions/zetasketch/src/test/java/org/apache/beam/sdk/extensions/zetasketch/ApproximateCountDistinctTest.java
##########
@@ -0,0 +1,342 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.beam.sdk.extensions.zetasketch;
+
+import com.google.zetasketch.HyperLogLogPlusPlus;
+import java.nio.ByteBuffer;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.Collectors;
+import org.apache.beam.sdk.testing.NeedsRunner;
+import org.apache.beam.sdk.testing.PAssert;
+import org.apache.beam.sdk.testing.TestPipeline;
+import org.apache.beam.sdk.transforms.Create;
+import org.apache.beam.sdk.values.KV;
+import org.apache.beam.sdk.values.PCollection;
+import org.apache.beam.sdk.values.TypeDescriptor;
+import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableList;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+
+/** Tests for {@link ApproximateCountDistinct}. */
+public class ApproximateCountDistinctTest {
+
+ @Rule public final transient TestPipeline p = TestPipeline.create();
+
+ // Integer
+ private static final List<Integer> INTS1 = Arrays.asList(1, 2, 3, 3, 1, 4);
+ private static final Long INTS1_ESTIMATE;
+
+ private static final int TEST_PRECISION = 20;
+
+ static {
+ HyperLogLogPlusPlus<Integer> hll = new HyperLogLogPlusPlus.Builder().buildForIntegers();
+ INTS1.forEach(hll::add);
+ INTS1_ESTIMATE = hll.longResult();
+ }
+
+ /** Test correct Builder is returned from Generic type. * */
+ @Test
+ public void testIntegerBuilder() {
+
+ PCollection<Integer> ints = p.apply(Create.of(1));
+ HllCount.Init.Builder<Integer> builder =
+ ApproximateCountDistinct.<Integer>builderForType(
+ ints.getCoder().getEncodedTypeDescriptor());
+ PCollection<Long> result = ints.apply(builder.globally()).apply(HllCount.Extract.globally());
+ PAssert.that(result).containsInAnyOrder(1L);
+ p.run();
+ }
+ /** Test correct Builder is returned from Generic type. * */
+ @Test
+ public void testStringBuilder() {
+
+ PCollection<String> strings = p.apply(Create.<String>of("43"));
+ HllCount.Init.Builder<String> builder =
+ ApproximateCountDistinct.<String>builderForType(
+ strings.getCoder().getEncodedTypeDescriptor());
+ PCollection<Long> result = strings.apply(builder.globally()).apply(HllCount.Extract.globally());
+ PAssert.that(result).containsInAnyOrder(1L);
+ p.run();
+ }
+ /** Test correct Builder is returned from Generic type. * */
+ @Test
+ public void testLongBuilder() {
+
+ PCollection<Long> longs = p.apply(Create.<Long>of(1L));
+ HllCount.Init.Builder<Long> builder =
+ ApproximateCountDistinct.<Long>builderForType(longs.getCoder().getEncodedTypeDescriptor());
+ PCollection<Long> result = longs.apply(builder.globally()).apply(HllCount.Extract.globally());
+ PAssert.that(result).containsInAnyOrder(1L);
+ p.run();
+ }
+ /** Test correct Builder is returned from Generic type. * */
+ @Test
+ public void testBytesBuilder() {
+
+ byte[] byteArray = new byte[] {'A'};
+ PCollection<byte[]> bytes = p.apply(Create.of(byteArray));
+ TypeDescriptor a = bytes.getCoder().getEncodedTypeDescriptor();
+ HllCount.Init.Builder<byte[]> builder =
+ ApproximateCountDistinct.<byte[]>builderForType(
+ bytes.getCoder().getEncodedTypeDescriptor());
+ PCollection<Long> result = bytes.apply(builder.globally()).apply(HllCount.Extract.globally());
+ PAssert.that(result).containsInAnyOrder(1L);
+ p.run();
+ }
+
+ /** Test Integer Globally. */
+ @Test
+ @Category(NeedsRunner.class)
+ public void testStandardTypesGlobalForInteger() {
+ PCollection<Long> approxResultInteger =
+ p.apply("Int", Create.of(INTS1)).apply("IntHLL", ApproximateCountDistinct.globally());
+ PAssert.thatSingleton(approxResultInteger).isEqualTo(INTS1_ESTIMATE);
+ p.run();
+ }
+
+ /** Test Long Globally. */
+ @Test
+ @Category(NeedsRunner.class)
+ public void testStandardTypesGlobalForLong() {
+
+ PCollection<Long> approxResultLong =
+ p.apply("Long", Create.of(INTS1.stream().map(Long::valueOf).collect(Collectors.toList())))
+ .apply("LongHLL", ApproximateCountDistinct.globally());
+
+ PAssert.thatSingleton(approxResultLong).isEqualTo(INTS1_ESTIMATE);
+
+ p.run();
+ }
+
+ /** Test String Globally. */
+ @Test
+ @Category(NeedsRunner.class)
+ public void testStandardTypesGlobalForStrings() {
+ PCollection<Long> approxResultString =
+ p.apply("Str", Create.of(INTS1.stream().map(String::valueOf).collect(Collectors.toList())))
+ .apply("StrHLL", ApproximateCountDistinct.globally());
+
+ PAssert.thatSingleton(approxResultString).isEqualTo(INTS1_ESTIMATE);
+
+ p.run();
+ }
+
+ /** Test Byte Globally. */
+ @Test
+ @Category(NeedsRunner.class)
+ public void testStandardTypesGlobalForBytes() {
+ PCollection<Long> approxResultByte =
+ p.apply(
+ "BytesHLL",
+ Create.of(
+ INTS1.stream()
+ .map(x -> ByteBuffer.allocate(4).putInt(x).array())
+ .collect(Collectors.toList())))
+ .apply(ApproximateCountDistinct.globally());
+
+ PAssert.thatSingleton(approxResultByte).isEqualTo(INTS1_ESTIMATE);
+
+ p.run();
+ }
+
+ /** Test Integer Globally. */
+ @Test
+ @Category(NeedsRunner.class)
+ public void testStandardTypesPerKeyForInteger() {
+
+ List<KV<Integer, Integer>> ints = new ArrayList<>();
+
+ for (int i = 0; i < 3; i++) {
+ for (int k : INTS1) {
+ ints.add(KV.of(i, k));
+ }
+ }
+
+ PCollection<KV<Integer, Long>> result =
+ p.apply("Int", Create.of(ints)).apply("IntHLL", ApproximateCountDistinct.perKey());
+
+ PAssert.that(result)
+ .containsInAnyOrder(
+ ImmutableList.of(
+ KV.of(0, INTS1_ESTIMATE), KV.of(1, INTS1_ESTIMATE), KV.of(2, INTS1_ESTIMATE)));
+
+ p.run();
+ }
+
+ /** Test Long Globally. */
+ @Test
+ @Category(NeedsRunner.class)
+ public void testStandardTypesPerKeyForLong() {
+
+ List<KV<Integer, Long>> longs = new ArrayList<>();
+
+ for (int i = 0; i < 3; i++) {
+ for (int k : INTS1) {
+ longs.add(KV.of(i, (long) k));
+ }
+ }
+
+ PCollection<KV<Integer, Long>> result =
+ p.apply("Long", Create.of(longs)).apply("LongHLL", ApproximateCountDistinct.perKey());
+
+ PAssert.that(result)
+ .containsInAnyOrder(
+ ImmutableList.of(
+ KV.of(0, INTS1_ESTIMATE), KV.of(1, INTS1_ESTIMATE), KV.of(2, INTS1_ESTIMATE)));
+
+ p.run();
+ }
+
+ /** Test String Globally. */
+ @Test
+ @Category(NeedsRunner.class)
+ public void testStandardTypesPerKeyForStrings() {
+ List<KV<Integer, String>> strings = new ArrayList<>();
+
+ for (int i = 0; i < 3; i++) {
+ for (int k : INTS1) {
+ strings.add(KV.of(i, String.valueOf(k)));
+ }
+ }
+
+ PCollection<KV<Integer, Long>> result =
+ p.apply("Str", Create.of(strings)).apply("StrHLL", ApproximateCountDistinct.perKey());
+
+ PAssert.that(result)
+ .containsInAnyOrder(
+ ImmutableList.of(
+ KV.of(0, INTS1_ESTIMATE), KV.of(1, INTS1_ESTIMATE), KV.of(2, INTS1_ESTIMATE)));
+
+ p.run();
+ }
+
+ /** Test Byte Globally. */
+ @Test
+ @Category(NeedsRunner.class)
+ public void testStandardTypesPerKeyForBytes() {
+
+ List<KV<Integer, byte[]>> bytes = new ArrayList<>();
+
+ for (int i = 0; i < 3; i++) {
+ for (int k : INTS1) {
+ bytes.add(KV.of(i, ByteBuffer.allocate(4).putInt(k).array()));
+ }
+ }
+
+ PCollection<KV<Integer, Long>> result =
+ p.apply("BytesHLL", Create.of(bytes)).apply(ApproximateCountDistinct.perKey());
+
+ PAssert.that(result)
+ .containsInAnyOrder(
+ ImmutableList.of(
+ KV.of(0, INTS1_ESTIMATE), KV.of(1, INTS1_ESTIMATE), KV.of(2, INTS1_ESTIMATE)));
+
+ p.run();
+ }
+
+ /** Test a general object, we will make use of a KV as the object as it already has a coder. */
+ @Test
+ @Category(NeedsRunner.class)
+ public void testObjectTypesGlobal() {
+
+ PCollection<Long> approxResultInteger =
+ p.apply(
+ "Int",
+ Create.of(
+ INTS1.stream().map(x -> KV.of(x, KV.of(x, x))).collect(Collectors.toList())))
+ .apply(
+ "IntHLL",
+ ApproximateCountDistinct.<KV<Integer, KV<Integer, Integer>>>globally()
+ .via((KV<Integer, KV<Integer, Integer>> x) -> (long) x.getValue().hashCode()));
Review comment:
PS as you have LGTM is it ok to merge as is , or do you need the test changed before ?
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] rezarokni commented on pull request #12973: [BEAM-10234] Create ApproximateDistinct using HLL Impl
Posted by GitBox <gi...@apache.org>.
rezarokni commented on pull request #12973:
URL: https://github.com/apache/beam/pull/12973#issuecomment-701219619
@robinyqiu
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] rezarokni merged pull request #12973: [BEAM-10234] Create ApproximateDistinct using HLL Impl
Posted by GitBox <gi...@apache.org>.
rezarokni merged pull request #12973:
URL: https://github.com/apache/beam/pull/12973
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] robinyqiu commented on a change in pull request #12973: [BEAM-10234] Create ApproximateDistinct using HLL Impl
Posted by GitBox <gi...@apache.org>.
robinyqiu commented on a change in pull request #12973:
URL: https://github.com/apache/beam/pull/12973#discussion_r538015847
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/ApproximateUnique.java
##########
@@ -56,7 +56,10 @@
*
* For more details about using {@code HllCount} and the {@code zetasketch} extension module, see
* https://s.apache.org/hll-in-beam#bookmark=id.v6chsij1ixo7.
+ *
+ * @deprecated use {@code ApproximateCountDistinct} which has moved to HLL implementation.
Review comment:
I am ok with that.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] rezarokni commented on pull request #12973: [BEAM-10234] Create ApproximateDistinct using HLL Impl
Posted by GitBox <gi...@apache.org>.
rezarokni commented on pull request #12973:
URL: https://github.com/apache/beam/pull/12973#issuecomment-701316654
@zfraa
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] robinyqiu commented on a change in pull request #12973: [BEAM-10234] Create ApproximateDistinct using HLL Impl
Posted by GitBox <gi...@apache.org>.
robinyqiu commented on a change in pull request #12973:
URL: https://github.com/apache/beam/pull/12973#discussion_r516972827
##########
File path: sdks/java/extensions/zetasketch/src/main/java/org/apache/beam/sdk/extensions/zetasketch/ApproximateCountDistinct.java
##########
@@ -0,0 +1,287 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.beam.sdk.extensions.zetasketch;
+
+import com.google.auto.value.AutoValue;
+import java.util.List;
+import javax.annotation.Nullable;
+import org.apache.beam.sdk.annotations.Experimental;
+import org.apache.beam.sdk.coders.Coder;
+import org.apache.beam.sdk.coders.KvCoder;
+import org.apache.beam.sdk.extensions.zetasketch.HllCount.Init.Builder;
+import org.apache.beam.sdk.transforms.Contextful;
+import org.apache.beam.sdk.transforms.Contextful.Fn;
+import org.apache.beam.sdk.transforms.MapElements;
+import org.apache.beam.sdk.transforms.PTransform;
+import org.apache.beam.sdk.transforms.ProcessFunction;
+import org.apache.beam.sdk.transforms.display.DisplayData;
+import org.apache.beam.sdk.values.KV;
+import org.apache.beam.sdk.values.PCollection;
+import org.apache.beam.sdk.values.TypeDescriptor;
+import org.apache.beam.sdk.values.TypeDescriptors;
+import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableList;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * {@code PTransform}s for estimating the number of distinct elements in a {@code PCollection}, or
+ * the number of distinct values associated with each key in a {@code PCollection} of {@code KV}s.
+ *
+ * <p>We make use of the {@link HllCount} implementation for this transform. Please use {@link
+ * HllCount} directly if you need access to the sketches.
+ *
+ * <p>If the object is not one of {@link Byte[]} {@link Integer} {@link Double} {@link String} make
+ * use of {@link Globally#via} or {@link PerKey#via}
+ *
+ * <h3>Examples</h3>
+ *
+ * <h4>Example 1: Approximate Count of Longs {@code PCollection<Long>} and specify precision</h4>
+ *
+ * <pre>{@Code
+ * p.apply("Int", Create.of(ints)).apply("IntHLL", ApproximateCountDistinct.globally()
Review comment:
You mean longs?
##########
File path: sdks/java/extensions/zetasketch/src/main/java/org/apache/beam/sdk/extensions/zetasketch/ApproximateCountDistinct.java
##########
@@ -0,0 +1,287 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.beam.sdk.extensions.zetasketch;
+
+import com.google.auto.value.AutoValue;
+import java.util.List;
+import javax.annotation.Nullable;
+import org.apache.beam.sdk.annotations.Experimental;
+import org.apache.beam.sdk.coders.Coder;
+import org.apache.beam.sdk.coders.KvCoder;
+import org.apache.beam.sdk.extensions.zetasketch.HllCount.Init.Builder;
+import org.apache.beam.sdk.transforms.Contextful;
+import org.apache.beam.sdk.transforms.Contextful.Fn;
+import org.apache.beam.sdk.transforms.MapElements;
+import org.apache.beam.sdk.transforms.PTransform;
+import org.apache.beam.sdk.transforms.ProcessFunction;
+import org.apache.beam.sdk.transforms.display.DisplayData;
+import org.apache.beam.sdk.values.KV;
+import org.apache.beam.sdk.values.PCollection;
+import org.apache.beam.sdk.values.TypeDescriptor;
+import org.apache.beam.sdk.values.TypeDescriptors;
+import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableList;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * {@code PTransform}s for estimating the number of distinct elements in a {@code PCollection}, or
+ * the number of distinct values associated with each key in a {@code PCollection} of {@code KV}s.
+ *
+ * <p>We make use of the {@link HllCount} implementation for this transform. Please use {@link
+ * HllCount} directly if you need access to the sketches.
+ *
+ * <p>If the object is not one of {@link Byte[]} {@link Integer} {@link Double} {@link String} make
+ * use of {@link Globally#via} or {@link PerKey#via}
+ *
+ * <h3>Examples</h3>
+ *
+ * <h4>Example 1: Approximate Count of Longs {@code PCollection<Long>} and specify precision</h4>
Review comment:
Nit: the indentation of the code snippets below are off, making it a bit hard to read.
##########
File path: sdks/java/extensions/zetasketch/src/main/java/org/apache/beam/sdk/extensions/zetasketch/ApproximateCountDistinct.java
##########
@@ -0,0 +1,287 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.beam.sdk.extensions.zetasketch;
+
+import com.google.auto.value.AutoValue;
+import java.util.List;
+import javax.annotation.Nullable;
+import org.apache.beam.sdk.annotations.Experimental;
+import org.apache.beam.sdk.coders.Coder;
+import org.apache.beam.sdk.coders.KvCoder;
+import org.apache.beam.sdk.extensions.zetasketch.HllCount.Init.Builder;
+import org.apache.beam.sdk.transforms.Contextful;
+import org.apache.beam.sdk.transforms.Contextful.Fn;
+import org.apache.beam.sdk.transforms.MapElements;
+import org.apache.beam.sdk.transforms.PTransform;
+import org.apache.beam.sdk.transforms.ProcessFunction;
+import org.apache.beam.sdk.transforms.display.DisplayData;
+import org.apache.beam.sdk.values.KV;
+import org.apache.beam.sdk.values.PCollection;
+import org.apache.beam.sdk.values.TypeDescriptor;
+import org.apache.beam.sdk.values.TypeDescriptors;
+import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableList;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * {@code PTransform}s for estimating the number of distinct elements in a {@code PCollection}, or
+ * the number of distinct values associated with each key in a {@code PCollection} of {@code KV}s.
+ *
+ * <p>We make use of the {@link HllCount} implementation for this transform. Please use {@link
+ * HllCount} directly if you need access to the sketches.
+ *
+ * <p>If the object is not one of {@link Byte[]} {@link Integer} {@link Double} {@link String} make
+ * use of {@link Globally#via} or {@link PerKey#via}
+ *
+ * <h3>Examples</h3>
+ *
+ * <h4>Example 1: Approximate Count of Longs {@code PCollection<Long>} and specify precision</h4>
+ *
+ * <pre>{@Code
+ * p.apply("Int", Create.of(ints)).apply("IntHLL", ApproximateCountDistinct.globally()
+ * .withPercision(PRECISION));
+ * }</pre>
+ *
+ * <h4>Example 2: Approximate Count of Key Value {@code PCollection<KV<Integer,Foo>>}</h4>
+ *
+ * <pre>{@code
+ * PCollection<KV<Integer, Long>> result =
+ * p.apply("Int", Create.of(ints)).apply("LongHLL", ApproximateCountDistinct.perKey());
+ * }</pre>
+ *
+ * <h4>Example 3: Approximate Count of Key Value {@code PCollection<KV<Integer,Foo>>}</h4>
+ *
+ * <pre>{@code
+ * PCollection<KV<Integer, Foo>> approxResultInteger =
+ * p.apply("Int", Create.of(Foo))
+ * .apply("IntHLL", ApproximateCountDistinct.<Integer, KV<Integer, Integer>>perKey()
+ * .via(x -> KV.of(x.getKey(), (long) x.hashCode())));
+ * }</pre>
+ */
+@Experimental
+public class ApproximateCountDistinct {
+
+ private static final Logger LOG = LoggerFactory.getLogger(ApproximateCountDistinct.class);
+
+ private static final List<TypeDescriptor<?>> HLL_IMPLEMENTED_TYPES =
+ ImmutableList.of(
+ TypeDescriptors.strings(),
+ TypeDescriptors.longs(),
+ TypeDescriptors.integers(),
+ new TypeDescriptor<byte[]>() {});
+
+ public static <T> Globally<T> globally() {
+ return new AutoValue_ApproximateCountDistinct_Globally.Builder<T>()
+ .setPrecision(HllCount.DEFAULT_PRECISION)
+ .build();
+ }
+
+ public static <K, V> PerKey<K, V> perKey() {
+ return new AutoValue_ApproximateCountDistinct_PerKey.Builder<K, V>()
+ .setPrecision(HllCount.DEFAULT_PRECISION)
+ .build();
+ }
+
+ /////////////////////////////////////////////////////////////////////////////
+
+ /**
+ * {@code PTransform} for estimating the number of distinct elements in a {@code PCollection}.
+ *
+ * @param <T> the type of the elements in the input {@code PCollection}
+ */
+ @AutoValue
+ public abstract static class Globally<T> extends PTransform<PCollection<T>, PCollection<Long>> {
+
+ public abstract int getPrecision();
+
+ public abstract Builder<T> toBuilder();
+
+ @Nullable
+ public abstract Contextful<Fn<T, Long>> getMapping();
+
+ @AutoValue.Builder
+ public abstract static class Builder<T> {
+
+ public abstract Builder<T> setPrecision(int precision);
+
+ public abstract Builder<T> setMapping(Contextful<Fn<T, Long>> value);
+
+ public abstract Globally<T> build();
+ }
+
+ public Globally<T> via(ProcessFunction<T, Long> fn) {
+
+ return toBuilder().setMapping(Contextful.<T, Long>fn(fn)).build();
+ }
+
+ public <V> Globally<V> withPercision(Integer withPercision) {
+ this.toBuilder().setPrecision(withPercision).build();
Review comment:
Duplicate line of code?
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/ApproximateUnique.java
##########
@@ -56,7 +56,10 @@
*
* For more details about using {@code HllCount} and the {@code zetasketch} extension module, see
* https://s.apache.org/hll-in-beam#bookmark=id.v6chsij1ixo7.
+ *
+ * @deprecated use {@code ApproximateCountDistinct} which has moved to HLL implementation.
Review comment:
Could you please update the comments above as well? It talks about HLL, maybe we should change the code snippet to `ApproximateCountDistinct`
##########
File path: sdks/java/extensions/zetasketch/src/main/java/org/apache/beam/sdk/extensions/zetasketch/ApproximateCountDistinct.java
##########
@@ -0,0 +1,287 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.beam.sdk.extensions.zetasketch;
+
+import com.google.auto.value.AutoValue;
+import java.util.List;
+import javax.annotation.Nullable;
+import org.apache.beam.sdk.annotations.Experimental;
+import org.apache.beam.sdk.coders.Coder;
+import org.apache.beam.sdk.coders.KvCoder;
+import org.apache.beam.sdk.extensions.zetasketch.HllCount.Init.Builder;
+import org.apache.beam.sdk.transforms.Contextful;
+import org.apache.beam.sdk.transforms.Contextful.Fn;
+import org.apache.beam.sdk.transforms.MapElements;
+import org.apache.beam.sdk.transforms.PTransform;
+import org.apache.beam.sdk.transforms.ProcessFunction;
+import org.apache.beam.sdk.transforms.display.DisplayData;
+import org.apache.beam.sdk.values.KV;
+import org.apache.beam.sdk.values.PCollection;
+import org.apache.beam.sdk.values.TypeDescriptor;
+import org.apache.beam.sdk.values.TypeDescriptors;
+import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableList;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * {@code PTransform}s for estimating the number of distinct elements in a {@code PCollection}, or
+ * the number of distinct values associated with each key in a {@code PCollection} of {@code KV}s.
+ *
+ * <p>We make use of the {@link HllCount} implementation for this transform. Please use {@link
+ * HllCount} directly if you need access to the sketches.
+ *
+ * <p>If the object is not one of {@link Byte[]} {@link Integer} {@link Double} {@link String} make
+ * use of {@link Globally#via} or {@link PerKey#via}
+ *
+ * <h3>Examples</h3>
+ *
+ * <h4>Example 1: Approximate Count of Longs {@code PCollection<Long>} and specify precision</h4>
+ *
+ * <pre>{@Code
+ * p.apply("Int", Create.of(ints)).apply("IntHLL", ApproximateCountDistinct.globally()
+ * .withPercision(PRECISION));
+ * }</pre>
+ *
+ * <h4>Example 2: Approximate Count of Key Value {@code PCollection<KV<Integer,Foo>>}</h4>
+ *
+ * <pre>{@code
+ * PCollection<KV<Integer, Long>> result =
+ * p.apply("Int", Create.of(ints)).apply("LongHLL", ApproximateCountDistinct.perKey());
Review comment:
This loc does not match description above
##########
File path: sdks/java/extensions/zetasketch/src/main/java/org/apache/beam/sdk/extensions/zetasketch/ApproximateCountDistinct.java
##########
@@ -0,0 +1,287 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.beam.sdk.extensions.zetasketch;
+
+import com.google.auto.value.AutoValue;
+import java.util.List;
+import javax.annotation.Nullable;
+import org.apache.beam.sdk.annotations.Experimental;
+import org.apache.beam.sdk.coders.Coder;
+import org.apache.beam.sdk.coders.KvCoder;
+import org.apache.beam.sdk.extensions.zetasketch.HllCount.Init.Builder;
+import org.apache.beam.sdk.transforms.Contextful;
+import org.apache.beam.sdk.transforms.Contextful.Fn;
+import org.apache.beam.sdk.transforms.MapElements;
+import org.apache.beam.sdk.transforms.PTransform;
+import org.apache.beam.sdk.transforms.ProcessFunction;
+import org.apache.beam.sdk.transforms.display.DisplayData;
+import org.apache.beam.sdk.values.KV;
+import org.apache.beam.sdk.values.PCollection;
+import org.apache.beam.sdk.values.TypeDescriptor;
+import org.apache.beam.sdk.values.TypeDescriptors;
+import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableList;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * {@code PTransform}s for estimating the number of distinct elements in a {@code PCollection}, or
+ * the number of distinct values associated with each key in a {@code PCollection} of {@code KV}s.
+ *
+ * <p>We make use of the {@link HllCount} implementation for this transform. Please use {@link
+ * HllCount} directly if you need access to the sketches.
+ *
+ * <p>If the object is not one of {@link Byte[]} {@link Integer} {@link Double} {@link String} make
+ * use of {@link Globally#via} or {@link PerKey#via}
+ *
+ * <h3>Examples</h3>
+ *
+ * <h4>Example 1: Approximate Count of Longs {@code PCollection<Long>} and specify precision</h4>
+ *
+ * <pre>{@Code
+ * p.apply("Int", Create.of(ints)).apply("IntHLL", ApproximateCountDistinct.globally()
+ * .withPercision(PRECISION));
+ * }</pre>
+ *
+ * <h4>Example 2: Approximate Count of Key Value {@code PCollection<KV<Integer,Foo>>}</h4>
+ *
+ * <pre>{@code
+ * PCollection<KV<Integer, Long>> result =
+ * p.apply("Int", Create.of(ints)).apply("LongHLL", ApproximateCountDistinct.perKey());
+ * }</pre>
+ *
+ * <h4>Example 3: Approximate Count of Key Value {@code PCollection<KV<Integer,Foo>>}</h4>
+ *
+ * <pre>{@code
+ * PCollection<KV<Integer, Foo>> approxResultInteger =
+ * p.apply("Int", Create.of(Foo))
+ * .apply("IntHLL", ApproximateCountDistinct.<Integer, KV<Integer, Integer>>perKey()
+ * .via(x -> KV.of(x.getKey(), (long) x.hashCode())));
Review comment:
I think the function should be something like `kv -> KV.of(kv.getKey(), kv.getValue().hashcode())`?
##########
File path: sdks/java/extensions/zetasketch/src/test/java/org/apache/beam/sdk/extensions/zetasketch/ApproximateCountDistinctTest.java
##########
@@ -0,0 +1,342 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.beam.sdk.extensions.zetasketch;
+
+import com.google.zetasketch.HyperLogLogPlusPlus;
+import java.nio.ByteBuffer;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.Collectors;
+import org.apache.beam.sdk.testing.NeedsRunner;
+import org.apache.beam.sdk.testing.PAssert;
+import org.apache.beam.sdk.testing.TestPipeline;
+import org.apache.beam.sdk.transforms.Create;
+import org.apache.beam.sdk.values.KV;
+import org.apache.beam.sdk.values.PCollection;
+import org.apache.beam.sdk.values.TypeDescriptor;
+import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableList;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+
+/** Tests for {@link ApproximateCountDistinct}. */
+public class ApproximateCountDistinctTest {
+
+ @Rule public final transient TestPipeline p = TestPipeline.create();
+
+ // Integer
+ private static final List<Integer> INTS1 = Arrays.asList(1, 2, 3, 3, 1, 4);
+ private static final Long INTS1_ESTIMATE;
+
+ private static final int TEST_PRECISION = 20;
+
+ static {
+ HyperLogLogPlusPlus<Integer> hll = new HyperLogLogPlusPlus.Builder().buildForIntegers();
+ INTS1.forEach(hll::add);
+ INTS1_ESTIMATE = hll.longResult();
+ }
+
+ /** Test correct Builder is returned from Generic type. * */
+ @Test
+ public void testIntegerBuilder() {
+
+ PCollection<Integer> ints = p.apply(Create.of(1));
+ HllCount.Init.Builder<Integer> builder =
+ ApproximateCountDistinct.<Integer>builderForType(
+ ints.getCoder().getEncodedTypeDescriptor());
+ PCollection<Long> result = ints.apply(builder.globally()).apply(HllCount.Extract.globally());
+ PAssert.that(result).containsInAnyOrder(1L);
+ p.run();
+ }
+ /** Test correct Builder is returned from Generic type. * */
+ @Test
+ public void testStringBuilder() {
+
+ PCollection<String> strings = p.apply(Create.<String>of("43"));
+ HllCount.Init.Builder<String> builder =
+ ApproximateCountDistinct.<String>builderForType(
+ strings.getCoder().getEncodedTypeDescriptor());
+ PCollection<Long> result = strings.apply(builder.globally()).apply(HllCount.Extract.globally());
+ PAssert.that(result).containsInAnyOrder(1L);
+ p.run();
+ }
+ /** Test correct Builder is returned from Generic type. * */
+ @Test
+ public void testLongBuilder() {
+
+ PCollection<Long> longs = p.apply(Create.<Long>of(1L));
+ HllCount.Init.Builder<Long> builder =
+ ApproximateCountDistinct.<Long>builderForType(longs.getCoder().getEncodedTypeDescriptor());
+ PCollection<Long> result = longs.apply(builder.globally()).apply(HllCount.Extract.globally());
+ PAssert.that(result).containsInAnyOrder(1L);
+ p.run();
+ }
+ /** Test correct Builder is returned from Generic type. * */
+ @Test
+ public void testBytesBuilder() {
+
+ byte[] byteArray = new byte[] {'A'};
+ PCollection<byte[]> bytes = p.apply(Create.of(byteArray));
+ TypeDescriptor a = bytes.getCoder().getEncodedTypeDescriptor();
+ HllCount.Init.Builder<byte[]> builder =
+ ApproximateCountDistinct.<byte[]>builderForType(
+ bytes.getCoder().getEncodedTypeDescriptor());
+ PCollection<Long> result = bytes.apply(builder.globally()).apply(HllCount.Extract.globally());
+ PAssert.that(result).containsInAnyOrder(1L);
+ p.run();
+ }
+
+ /** Test Integer Globally. */
+ @Test
+ @Category(NeedsRunner.class)
+ public void testStandardTypesGlobalForInteger() {
+ PCollection<Long> approxResultInteger =
+ p.apply("Int", Create.of(INTS1)).apply("IntHLL", ApproximateCountDistinct.globally());
+ PAssert.thatSingleton(approxResultInteger).isEqualTo(INTS1_ESTIMATE);
+ p.run();
+ }
+
+ /** Test Long Globally. */
+ @Test
+ @Category(NeedsRunner.class)
+ public void testStandardTypesGlobalForLong() {
+
+ PCollection<Long> approxResultLong =
+ p.apply("Long", Create.of(INTS1.stream().map(Long::valueOf).collect(Collectors.toList())))
+ .apply("LongHLL", ApproximateCountDistinct.globally());
+
+ PAssert.thatSingleton(approxResultLong).isEqualTo(INTS1_ESTIMATE);
+
+ p.run();
+ }
+
+ /** Test String Globally. */
+ @Test
+ @Category(NeedsRunner.class)
+ public void testStandardTypesGlobalForStrings() {
+ PCollection<Long> approxResultString =
+ p.apply("Str", Create.of(INTS1.stream().map(String::valueOf).collect(Collectors.toList())))
+ .apply("StrHLL", ApproximateCountDistinct.globally());
+
+ PAssert.thatSingleton(approxResultString).isEqualTo(INTS1_ESTIMATE);
+
+ p.run();
+ }
+
+ /** Test Byte Globally. */
+ @Test
+ @Category(NeedsRunner.class)
+ public void testStandardTypesGlobalForBytes() {
+ PCollection<Long> approxResultByte =
+ p.apply(
+ "BytesHLL",
+ Create.of(
+ INTS1.stream()
+ .map(x -> ByteBuffer.allocate(4).putInt(x).array())
+ .collect(Collectors.toList())))
+ .apply(ApproximateCountDistinct.globally());
+
+ PAssert.thatSingleton(approxResultByte).isEqualTo(INTS1_ESTIMATE);
+
+ p.run();
+ }
+
+ /** Test Integer Globally. */
+ @Test
+ @Category(NeedsRunner.class)
+ public void testStandardTypesPerKeyForInteger() {
+
+ List<KV<Integer, Integer>> ints = new ArrayList<>();
+
+ for (int i = 0; i < 3; i++) {
+ for (int k : INTS1) {
+ ints.add(KV.of(i, k));
+ }
+ }
+
+ PCollection<KV<Integer, Long>> result =
+ p.apply("Int", Create.of(ints)).apply("IntHLL", ApproximateCountDistinct.perKey());
+
+ PAssert.that(result)
+ .containsInAnyOrder(
+ ImmutableList.of(
+ KV.of(0, INTS1_ESTIMATE), KV.of(1, INTS1_ESTIMATE), KV.of(2, INTS1_ESTIMATE)));
+
+ p.run();
+ }
+
+ /** Test Long Globally. */
+ @Test
+ @Category(NeedsRunner.class)
+ public void testStandardTypesPerKeyForLong() {
+
+ List<KV<Integer, Long>> longs = new ArrayList<>();
+
+ for (int i = 0; i < 3; i++) {
+ for (int k : INTS1) {
+ longs.add(KV.of(i, (long) k));
+ }
+ }
+
+ PCollection<KV<Integer, Long>> result =
+ p.apply("Long", Create.of(longs)).apply("LongHLL", ApproximateCountDistinct.perKey());
+
+ PAssert.that(result)
+ .containsInAnyOrder(
+ ImmutableList.of(
+ KV.of(0, INTS1_ESTIMATE), KV.of(1, INTS1_ESTIMATE), KV.of(2, INTS1_ESTIMATE)));
+
+ p.run();
+ }
+
+ /** Test String Globally. */
+ @Test
+ @Category(NeedsRunner.class)
+ public void testStandardTypesPerKeyForStrings() {
+ List<KV<Integer, String>> strings = new ArrayList<>();
+
+ for (int i = 0; i < 3; i++) {
+ for (int k : INTS1) {
+ strings.add(KV.of(i, String.valueOf(k)));
+ }
+ }
+
+ PCollection<KV<Integer, Long>> result =
+ p.apply("Str", Create.of(strings)).apply("StrHLL", ApproximateCountDistinct.perKey());
+
+ PAssert.that(result)
+ .containsInAnyOrder(
+ ImmutableList.of(
+ KV.of(0, INTS1_ESTIMATE), KV.of(1, INTS1_ESTIMATE), KV.of(2, INTS1_ESTIMATE)));
+
+ p.run();
+ }
+
+ /** Test Byte Globally. */
+ @Test
+ @Category(NeedsRunner.class)
+ public void testStandardTypesPerKeyForBytes() {
+
+ List<KV<Integer, byte[]>> bytes = new ArrayList<>();
+
+ for (int i = 0; i < 3; i++) {
+ for (int k : INTS1) {
+ bytes.add(KV.of(i, ByteBuffer.allocate(4).putInt(k).array()));
+ }
+ }
+
+ PCollection<KV<Integer, Long>> result =
+ p.apply("BytesHLL", Create.of(bytes)).apply(ApproximateCountDistinct.perKey());
+
+ PAssert.that(result)
+ .containsInAnyOrder(
+ ImmutableList.of(
+ KV.of(0, INTS1_ESTIMATE), KV.of(1, INTS1_ESTIMATE), KV.of(2, INTS1_ESTIMATE)));
+
+ p.run();
+ }
+
+ /** Test a general object, we will make use of a KV as the object as it already has a coder. */
+ @Test
+ @Category(NeedsRunner.class)
+ public void testObjectTypesGlobal() {
+
+ PCollection<Long> approxResultInteger =
+ p.apply(
+ "Int",
+ Create.of(
+ INTS1.stream().map(x -> KV.of(x, KV.of(x, x))).collect(Collectors.toList())))
+ .apply(
+ "IntHLL",
+ ApproximateCountDistinct.<KV<Integer, KV<Integer, Integer>>>globally()
+ .via((KV<Integer, KV<Integer, Integer>> x) -> (long) x.getValue().hashCode()));
Review comment:
I think the function here should be 'x -> x.hashCode()' without 'getValue'?
##########
File path: sdks/java/extensions/zetasketch/src/test/java/org/apache/beam/sdk/extensions/zetasketch/ApproximateCountDistinctTest.java
##########
@@ -0,0 +1,342 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.beam.sdk.extensions.zetasketch;
+
+import com.google.zetasketch.HyperLogLogPlusPlus;
+import java.nio.ByteBuffer;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.Collectors;
+import org.apache.beam.sdk.testing.NeedsRunner;
+import org.apache.beam.sdk.testing.PAssert;
+import org.apache.beam.sdk.testing.TestPipeline;
+import org.apache.beam.sdk.transforms.Create;
+import org.apache.beam.sdk.values.KV;
+import org.apache.beam.sdk.values.PCollection;
+import org.apache.beam.sdk.values.TypeDescriptor;
+import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableList;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+
+/** Tests for {@link ApproximateCountDistinct}. */
+public class ApproximateCountDistinctTest {
+
+ @Rule public final transient TestPipeline p = TestPipeline.create();
+
+ // Integer
+ private static final List<Integer> INTS1 = Arrays.asList(1, 2, 3, 3, 1, 4);
+ private static final Long INTS1_ESTIMATE;
+
+ private static final int TEST_PRECISION = 20;
+
+ static {
+ HyperLogLogPlusPlus<Integer> hll = new HyperLogLogPlusPlus.Builder().buildForIntegers();
+ INTS1.forEach(hll::add);
+ INTS1_ESTIMATE = hll.longResult();
+ }
+
+ /** Test correct Builder is returned from Generic type. * */
+ @Test
+ public void testIntegerBuilder() {
+
+ PCollection<Integer> ints = p.apply(Create.of(1));
+ HllCount.Init.Builder<Integer> builder =
+ ApproximateCountDistinct.<Integer>builderForType(
+ ints.getCoder().getEncodedTypeDescriptor());
+ PCollection<Long> result = ints.apply(builder.globally()).apply(HllCount.Extract.globally());
+ PAssert.that(result).containsInAnyOrder(1L);
+ p.run();
+ }
+ /** Test correct Builder is returned from Generic type. * */
+ @Test
+ public void testStringBuilder() {
+
+ PCollection<String> strings = p.apply(Create.<String>of("43"));
+ HllCount.Init.Builder<String> builder =
+ ApproximateCountDistinct.<String>builderForType(
+ strings.getCoder().getEncodedTypeDescriptor());
+ PCollection<Long> result = strings.apply(builder.globally()).apply(HllCount.Extract.globally());
+ PAssert.that(result).containsInAnyOrder(1L);
+ p.run();
+ }
+ /** Test correct Builder is returned from Generic type. * */
+ @Test
+ public void testLongBuilder() {
+
+ PCollection<Long> longs = p.apply(Create.<Long>of(1L));
+ HllCount.Init.Builder<Long> builder =
+ ApproximateCountDistinct.<Long>builderForType(longs.getCoder().getEncodedTypeDescriptor());
+ PCollection<Long> result = longs.apply(builder.globally()).apply(HllCount.Extract.globally());
+ PAssert.that(result).containsInAnyOrder(1L);
+ p.run();
+ }
+ /** Test correct Builder is returned from Generic type. * */
+ @Test
+ public void testBytesBuilder() {
+
+ byte[] byteArray = new byte[] {'A'};
+ PCollection<byte[]> bytes = p.apply(Create.of(byteArray));
+ TypeDescriptor a = bytes.getCoder().getEncodedTypeDescriptor();
+ HllCount.Init.Builder<byte[]> builder =
+ ApproximateCountDistinct.<byte[]>builderForType(
+ bytes.getCoder().getEncodedTypeDescriptor());
+ PCollection<Long> result = bytes.apply(builder.globally()).apply(HllCount.Extract.globally());
+ PAssert.that(result).containsInAnyOrder(1L);
+ p.run();
+ }
+
+ /** Test Integer Globally. */
+ @Test
+ @Category(NeedsRunner.class)
+ public void testStandardTypesGlobalForInteger() {
+ PCollection<Long> approxResultInteger =
+ p.apply("Int", Create.of(INTS1)).apply("IntHLL", ApproximateCountDistinct.globally());
+ PAssert.thatSingleton(approxResultInteger).isEqualTo(INTS1_ESTIMATE);
+ p.run();
+ }
+
+ /** Test Long Globally. */
+ @Test
+ @Category(NeedsRunner.class)
+ public void testStandardTypesGlobalForLong() {
+
+ PCollection<Long> approxResultLong =
+ p.apply("Long", Create.of(INTS1.stream().map(Long::valueOf).collect(Collectors.toList())))
+ .apply("LongHLL", ApproximateCountDistinct.globally());
+
+ PAssert.thatSingleton(approxResultLong).isEqualTo(INTS1_ESTIMATE);
+
+ p.run();
+ }
+
+ /** Test String Globally. */
+ @Test
+ @Category(NeedsRunner.class)
+ public void testStandardTypesGlobalForStrings() {
+ PCollection<Long> approxResultString =
+ p.apply("Str", Create.of(INTS1.stream().map(String::valueOf).collect(Collectors.toList())))
+ .apply("StrHLL", ApproximateCountDistinct.globally());
+
+ PAssert.thatSingleton(approxResultString).isEqualTo(INTS1_ESTIMATE);
+
+ p.run();
+ }
+
+ /** Test Byte Globally. */
+ @Test
+ @Category(NeedsRunner.class)
+ public void testStandardTypesGlobalForBytes() {
+ PCollection<Long> approxResultByte =
+ p.apply(
+ "BytesHLL",
+ Create.of(
+ INTS1.stream()
+ .map(x -> ByteBuffer.allocate(4).putInt(x).array())
+ .collect(Collectors.toList())))
+ .apply(ApproximateCountDistinct.globally());
+
+ PAssert.thatSingleton(approxResultByte).isEqualTo(INTS1_ESTIMATE);
+
+ p.run();
+ }
+
+ /** Test Integer Globally. */
+ @Test
+ @Category(NeedsRunner.class)
+ public void testStandardTypesPerKeyForInteger() {
+
+ List<KV<Integer, Integer>> ints = new ArrayList<>();
+
+ for (int i = 0; i < 3; i++) {
+ for (int k : INTS1) {
+ ints.add(KV.of(i, k));
+ }
+ }
+
+ PCollection<KV<Integer, Long>> result =
+ p.apply("Int", Create.of(ints)).apply("IntHLL", ApproximateCountDistinct.perKey());
+
+ PAssert.that(result)
+ .containsInAnyOrder(
+ ImmutableList.of(
+ KV.of(0, INTS1_ESTIMATE), KV.of(1, INTS1_ESTIMATE), KV.of(2, INTS1_ESTIMATE)));
+
+ p.run();
+ }
+
+ /** Test Long Globally. */
+ @Test
+ @Category(NeedsRunner.class)
+ public void testStandardTypesPerKeyForLong() {
+
+ List<KV<Integer, Long>> longs = new ArrayList<>();
+
+ for (int i = 0; i < 3; i++) {
+ for (int k : INTS1) {
+ longs.add(KV.of(i, (long) k));
+ }
+ }
+
+ PCollection<KV<Integer, Long>> result =
+ p.apply("Long", Create.of(longs)).apply("LongHLL", ApproximateCountDistinct.perKey());
+
+ PAssert.that(result)
+ .containsInAnyOrder(
+ ImmutableList.of(
+ KV.of(0, INTS1_ESTIMATE), KV.of(1, INTS1_ESTIMATE), KV.of(2, INTS1_ESTIMATE)));
+
+ p.run();
+ }
+
+ /** Test String Globally. */
+ @Test
+ @Category(NeedsRunner.class)
+ public void testStandardTypesPerKeyForStrings() {
+ List<KV<Integer, String>> strings = new ArrayList<>();
+
+ for (int i = 0; i < 3; i++) {
+ for (int k : INTS1) {
+ strings.add(KV.of(i, String.valueOf(k)));
+ }
+ }
+
+ PCollection<KV<Integer, Long>> result =
+ p.apply("Str", Create.of(strings)).apply("StrHLL", ApproximateCountDistinct.perKey());
+
+ PAssert.that(result)
+ .containsInAnyOrder(
+ ImmutableList.of(
+ KV.of(0, INTS1_ESTIMATE), KV.of(1, INTS1_ESTIMATE), KV.of(2, INTS1_ESTIMATE)));
+
+ p.run();
+ }
+
+ /** Test Byte Globally. */
+ @Test
+ @Category(NeedsRunner.class)
+ public void testStandardTypesPerKeyForBytes() {
+
+ List<KV<Integer, byte[]>> bytes = new ArrayList<>();
+
+ for (int i = 0; i < 3; i++) {
+ for (int k : INTS1) {
+ bytes.add(KV.of(i, ByteBuffer.allocate(4).putInt(k).array()));
+ }
+ }
+
+ PCollection<KV<Integer, Long>> result =
+ p.apply("BytesHLL", Create.of(bytes)).apply(ApproximateCountDistinct.perKey());
+
+ PAssert.that(result)
+ .containsInAnyOrder(
+ ImmutableList.of(
+ KV.of(0, INTS1_ESTIMATE), KV.of(1, INTS1_ESTIMATE), KV.of(2, INTS1_ESTIMATE)));
+
+ p.run();
+ }
+
+ /** Test a general object, we will make use of a KV as the object as it already has a coder. */
+ @Test
+ @Category(NeedsRunner.class)
+ public void testObjectTypesGlobal() {
+
+ PCollection<Long> approxResultInteger =
+ p.apply(
+ "Int",
+ Create.of(
+ INTS1.stream().map(x -> KV.of(x, KV.of(x, x))).collect(Collectors.toList())))
+ .apply(
+ "IntHLL",
+ ApproximateCountDistinct.<KV<Integer, KV<Integer, Integer>>>globally()
+ .via((KV<Integer, KV<Integer, Integer>> x) -> (long) x.getValue().hashCode()));
+
+ PAssert.thatSingleton(approxResultInteger).isEqualTo(INTS1_ESTIMATE);
+
+ p.run();
+ }
+
+ /** Test a general object, we will make use of a KV as the object as it already has a coder. */
+ @Test
+ @Category(NeedsRunner.class)
+ public void testObjectTypesPerKey() {
+
+ List<KV<Integer, KV<Integer, Integer>>> ints = new ArrayList<>();
+
+ for (int i = 0; i < 3; i++) {
+ for (int k : INTS1) {
+ ints.add(KV.of(i, KV.of(i, k)));
+ }
+ }
+
+ PCollection<KV<Integer, Long>> approxResultInteger =
+ p.apply("Int", Create.of(ints))
+ .apply(
+ "IntHLL",
+ ApproximateCountDistinct.<Integer, KV<Integer, Integer>>perKey()
+ .via(x -> KV.of(x.getKey(), (long) x.hashCode()))
Review comment:
Same here. Shouldn't we use `x -> KV.of(x.getKey(), x.getValue().hashCode())` here?
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] rezarokni commented on a change in pull request #12973: [BEAM-10234] Create ApproximateDistinct using HLL Impl
Posted by GitBox <gi...@apache.org>.
rezarokni commented on a change in pull request #12973:
URL: https://github.com/apache/beam/pull/12973#discussion_r534093432
##########
File path: sdks/java/extensions/zetasketch/src/main/java/org/apache/beam/sdk/extensions/zetasketch/ApproximateCountDistinct.java
##########
@@ -0,0 +1,287 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.beam.sdk.extensions.zetasketch;
+
+import com.google.auto.value.AutoValue;
+import java.util.List;
+import javax.annotation.Nullable;
+import org.apache.beam.sdk.annotations.Experimental;
+import org.apache.beam.sdk.coders.Coder;
+import org.apache.beam.sdk.coders.KvCoder;
+import org.apache.beam.sdk.extensions.zetasketch.HllCount.Init.Builder;
+import org.apache.beam.sdk.transforms.Contextful;
+import org.apache.beam.sdk.transforms.Contextful.Fn;
+import org.apache.beam.sdk.transforms.MapElements;
+import org.apache.beam.sdk.transforms.PTransform;
+import org.apache.beam.sdk.transforms.ProcessFunction;
+import org.apache.beam.sdk.transforms.display.DisplayData;
+import org.apache.beam.sdk.values.KV;
+import org.apache.beam.sdk.values.PCollection;
+import org.apache.beam.sdk.values.TypeDescriptor;
+import org.apache.beam.sdk.values.TypeDescriptors;
+import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableList;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * {@code PTransform}s for estimating the number of distinct elements in a {@code PCollection}, or
+ * the number of distinct values associated with each key in a {@code PCollection} of {@code KV}s.
+ *
+ * <p>We make use of the {@link HllCount} implementation for this transform. Please use {@link
+ * HllCount} directly if you need access to the sketches.
+ *
+ * <p>If the object is not one of {@link Byte[]} {@link Integer} {@link Double} {@link String} make
+ * use of {@link Globally#via} or {@link PerKey#via}
+ *
+ * <h3>Examples</h3>
+ *
+ * <h4>Example 1: Approximate Count of Longs {@code PCollection<Long>} and specify precision</h4>
+ *
+ * <pre>{@Code
+ * p.apply("Int", Create.of(ints)).apply("IntHLL", ApproximateCountDistinct.globally()
+ * .withPercision(PRECISION));
+ * }</pre>
+ *
+ * <h4>Example 2: Approximate Count of Key Value {@code PCollection<KV<Integer,Foo>>}</h4>
+ *
+ * <pre>{@code
+ * PCollection<KV<Integer, Long>> result =
+ * p.apply("Int", Create.of(ints)).apply("LongHLL", ApproximateCountDistinct.perKey());
+ * }</pre>
+ *
+ * <h4>Example 3: Approximate Count of Key Value {@code PCollection<KV<Integer,Foo>>}</h4>
+ *
+ * <pre>{@code
+ * PCollection<KV<Integer, Foo>> approxResultInteger =
+ * p.apply("Int", Create.of(Foo))
+ * .apply("IntHLL", ApproximateCountDistinct.<Integer, KV<Integer, Integer>>perKey()
+ * .via(x -> KV.of(x.getKey(), (long) x.hashCode())));
+ * }</pre>
+ */
+@Experimental
+public class ApproximateCountDistinct {
+
+ private static final Logger LOG = LoggerFactory.getLogger(ApproximateCountDistinct.class);
+
+ private static final List<TypeDescriptor<?>> HLL_IMPLEMENTED_TYPES =
+ ImmutableList.of(
+ TypeDescriptors.strings(),
+ TypeDescriptors.longs(),
+ TypeDescriptors.integers(),
+ new TypeDescriptor<byte[]>() {});
+
+ public static <T> Globally<T> globally() {
+ return new AutoValue_ApproximateCountDistinct_Globally.Builder<T>()
+ .setPrecision(HllCount.DEFAULT_PRECISION)
+ .build();
+ }
+
+ public static <K, V> PerKey<K, V> perKey() {
+ return new AutoValue_ApproximateCountDistinct_PerKey.Builder<K, V>()
+ .setPrecision(HllCount.DEFAULT_PRECISION)
+ .build();
+ }
+
+ /////////////////////////////////////////////////////////////////////////////
+
+ /**
+ * {@code PTransform} for estimating the number of distinct elements in a {@code PCollection}.
+ *
+ * @param <T> the type of the elements in the input {@code PCollection}
+ */
+ @AutoValue
+ public abstract static class Globally<T> extends PTransform<PCollection<T>, PCollection<Long>> {
+
+ public abstract int getPrecision();
+
+ public abstract Builder<T> toBuilder();
+
+ @Nullable
+ public abstract Contextful<Fn<T, Long>> getMapping();
+
+ @AutoValue.Builder
+ public abstract static class Builder<T> {
+
+ public abstract Builder<T> setPrecision(int precision);
+
+ public abstract Builder<T> setMapping(Contextful<Fn<T, Long>> value);
+
+ public abstract Globally<T> build();
+ }
+
+ public Globally<T> via(ProcessFunction<T, Long> fn) {
+
+ return toBuilder().setMapping(Contextful.<T, Long>fn(fn)).build();
+ }
+
+ public <V> Globally<V> withPercision(Integer withPercision) {
+ this.toBuilder().setPrecision(withPercision).build();
Review comment:
Thanx.
##########
File path: sdks/java/extensions/zetasketch/src/main/java/org/apache/beam/sdk/extensions/zetasketch/ApproximateCountDistinct.java
##########
@@ -0,0 +1,287 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.beam.sdk.extensions.zetasketch;
+
+import com.google.auto.value.AutoValue;
+import java.util.List;
+import javax.annotation.Nullable;
+import org.apache.beam.sdk.annotations.Experimental;
+import org.apache.beam.sdk.coders.Coder;
+import org.apache.beam.sdk.coders.KvCoder;
+import org.apache.beam.sdk.extensions.zetasketch.HllCount.Init.Builder;
+import org.apache.beam.sdk.transforms.Contextful;
+import org.apache.beam.sdk.transforms.Contextful.Fn;
+import org.apache.beam.sdk.transforms.MapElements;
+import org.apache.beam.sdk.transforms.PTransform;
+import org.apache.beam.sdk.transforms.ProcessFunction;
+import org.apache.beam.sdk.transforms.display.DisplayData;
+import org.apache.beam.sdk.values.KV;
+import org.apache.beam.sdk.values.PCollection;
+import org.apache.beam.sdk.values.TypeDescriptor;
+import org.apache.beam.sdk.values.TypeDescriptors;
+import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableList;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * {@code PTransform}s for estimating the number of distinct elements in a {@code PCollection}, or
+ * the number of distinct values associated with each key in a {@code PCollection} of {@code KV}s.
+ *
+ * <p>We make use of the {@link HllCount} implementation for this transform. Please use {@link
+ * HllCount} directly if you need access to the sketches.
+ *
+ * <p>If the object is not one of {@link Byte[]} {@link Integer} {@link Double} {@link String} make
+ * use of {@link Globally#via} or {@link PerKey#via}
+ *
+ * <h3>Examples</h3>
+ *
+ * <h4>Example 1: Approximate Count of Longs {@code PCollection<Long>} and specify precision</h4>
+ *
+ * <pre>{@Code
+ * p.apply("Int", Create.of(ints)).apply("IntHLL", ApproximateCountDistinct.globally()
+ * .withPercision(PRECISION));
+ * }</pre>
+ *
+ * <h4>Example 2: Approximate Count of Key Value {@code PCollection<KV<Integer,Foo>>}</h4>
+ *
+ * <pre>{@code
+ * PCollection<KV<Integer, Long>> result =
+ * p.apply("Int", Create.of(ints)).apply("LongHLL", ApproximateCountDistinct.perKey());
Review comment:
Done.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] rezarokni commented on pull request #12973: [BEAM-10234] Create ApproximateDistinct using HLL Impl
Posted by GitBox <gi...@apache.org>.
rezarokni commented on pull request #12973:
URL: https://github.com/apache/beam/pull/12973#issuecomment-749503889
Sorry @robinyqiu I accidentally clicked on re-review and cant see a way to undo the click!
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] rezarokni commented on a change in pull request #12973: [BEAM-10234] Create ApproximateDistinct using HLL Impl
Posted by GitBox <gi...@apache.org>.
rezarokni commented on a change in pull request #12973:
URL: https://github.com/apache/beam/pull/12973#discussion_r534093226
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/ApproximateUnique.java
##########
@@ -56,7 +56,10 @@
*
* For more details about using {@code HllCount} and the {@code zetasketch} extension module, see
* https://s.apache.org/hll-in-beam#bookmark=id.v6chsij1ixo7.
+ *
+ * @deprecated use {@code ApproximateCountDistinct} which has moved to HLL implementation.
Review comment:
Changed the comments. But not added snippets yet, would it be ok to just assume they will look at the new location?
##########
File path: sdks/java/extensions/zetasketch/src/main/java/org/apache/beam/sdk/extensions/zetasketch/ApproximateCountDistinct.java
##########
@@ -0,0 +1,287 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.beam.sdk.extensions.zetasketch;
+
+import com.google.auto.value.AutoValue;
+import java.util.List;
+import javax.annotation.Nullable;
+import org.apache.beam.sdk.annotations.Experimental;
+import org.apache.beam.sdk.coders.Coder;
+import org.apache.beam.sdk.coders.KvCoder;
+import org.apache.beam.sdk.extensions.zetasketch.HllCount.Init.Builder;
+import org.apache.beam.sdk.transforms.Contextful;
+import org.apache.beam.sdk.transforms.Contextful.Fn;
+import org.apache.beam.sdk.transforms.MapElements;
+import org.apache.beam.sdk.transforms.PTransform;
+import org.apache.beam.sdk.transforms.ProcessFunction;
+import org.apache.beam.sdk.transforms.display.DisplayData;
+import org.apache.beam.sdk.values.KV;
+import org.apache.beam.sdk.values.PCollection;
+import org.apache.beam.sdk.values.TypeDescriptor;
+import org.apache.beam.sdk.values.TypeDescriptors;
+import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableList;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * {@code PTransform}s for estimating the number of distinct elements in a {@code PCollection}, or
+ * the number of distinct values associated with each key in a {@code PCollection} of {@code KV}s.
+ *
+ * <p>We make use of the {@link HllCount} implementation for this transform. Please use {@link
+ * HllCount} directly if you need access to the sketches.
+ *
+ * <p>If the object is not one of {@link Byte[]} {@link Integer} {@link Double} {@link String} make
+ * use of {@link Globally#via} or {@link PerKey#via}
+ *
+ * <h3>Examples</h3>
+ *
+ * <h4>Example 1: Approximate Count of Longs {@code PCollection<Long>} and specify precision</h4>
Review comment:
Done.
##########
File path: sdks/java/extensions/zetasketch/src/main/java/org/apache/beam/sdk/extensions/zetasketch/ApproximateCountDistinct.java
##########
@@ -0,0 +1,287 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.beam.sdk.extensions.zetasketch;
+
+import com.google.auto.value.AutoValue;
+import java.util.List;
+import javax.annotation.Nullable;
+import org.apache.beam.sdk.annotations.Experimental;
+import org.apache.beam.sdk.coders.Coder;
+import org.apache.beam.sdk.coders.KvCoder;
+import org.apache.beam.sdk.extensions.zetasketch.HllCount.Init.Builder;
+import org.apache.beam.sdk.transforms.Contextful;
+import org.apache.beam.sdk.transforms.Contextful.Fn;
+import org.apache.beam.sdk.transforms.MapElements;
+import org.apache.beam.sdk.transforms.PTransform;
+import org.apache.beam.sdk.transforms.ProcessFunction;
+import org.apache.beam.sdk.transforms.display.DisplayData;
+import org.apache.beam.sdk.values.KV;
+import org.apache.beam.sdk.values.PCollection;
+import org.apache.beam.sdk.values.TypeDescriptor;
+import org.apache.beam.sdk.values.TypeDescriptors;
+import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableList;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * {@code PTransform}s for estimating the number of distinct elements in a {@code PCollection}, or
+ * the number of distinct values associated with each key in a {@code PCollection} of {@code KV}s.
+ *
+ * <p>We make use of the {@link HllCount} implementation for this transform. Please use {@link
+ * HllCount} directly if you need access to the sketches.
+ *
+ * <p>If the object is not one of {@link Byte[]} {@link Integer} {@link Double} {@link String} make
+ * use of {@link Globally#via} or {@link PerKey#via}
+ *
+ * <h3>Examples</h3>
+ *
+ * <h4>Example 1: Approximate Count of Longs {@code PCollection<Long>} and specify precision</h4>
+ *
+ * <pre>{@Code
+ * p.apply("Int", Create.of(ints)).apply("IntHLL", ApproximateCountDistinct.globally()
Review comment:
Yes, corrected.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] rezarokni commented on pull request #12973: [BEAM-10234] Create ApproximateDistinct using HLL Impl
Posted by GitBox <gi...@apache.org>.
rezarokni commented on pull request #12973:
URL: https://github.com/apache/beam/pull/12973#issuecomment-754353610
Run Java PreCommit
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] robinyqiu commented on a change in pull request #12973: [BEAM-10234] Create ApproximateDistinct using HLL Impl
Posted by GitBox <gi...@apache.org>.
robinyqiu commented on a change in pull request #12973:
URL: https://github.com/apache/beam/pull/12973#discussion_r538018325
##########
File path: sdks/java/extensions/zetasketch/src/test/java/org/apache/beam/sdk/extensions/zetasketch/ApproximateCountDistinctTest.java
##########
@@ -0,0 +1,342 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.beam.sdk.extensions.zetasketch;
+
+import com.google.zetasketch.HyperLogLogPlusPlus;
+import java.nio.ByteBuffer;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.Collectors;
+import org.apache.beam.sdk.testing.NeedsRunner;
+import org.apache.beam.sdk.testing.PAssert;
+import org.apache.beam.sdk.testing.TestPipeline;
+import org.apache.beam.sdk.transforms.Create;
+import org.apache.beam.sdk.values.KV;
+import org.apache.beam.sdk.values.PCollection;
+import org.apache.beam.sdk.values.TypeDescriptor;
+import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableList;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+
+/** Tests for {@link ApproximateCountDistinct}. */
+public class ApproximateCountDistinctTest {
+
+ @Rule public final transient TestPipeline p = TestPipeline.create();
+
+ // Integer
+ private static final List<Integer> INTS1 = Arrays.asList(1, 2, 3, 3, 1, 4);
+ private static final Long INTS1_ESTIMATE;
+
+ private static final int TEST_PRECISION = 20;
+
+ static {
+ HyperLogLogPlusPlus<Integer> hll = new HyperLogLogPlusPlus.Builder().buildForIntegers();
+ INTS1.forEach(hll::add);
+ INTS1_ESTIMATE = hll.longResult();
+ }
+
+ /** Test correct Builder is returned from Generic type. * */
+ @Test
+ public void testIntegerBuilder() {
+
+ PCollection<Integer> ints = p.apply(Create.of(1));
+ HllCount.Init.Builder<Integer> builder =
+ ApproximateCountDistinct.<Integer>builderForType(
+ ints.getCoder().getEncodedTypeDescriptor());
+ PCollection<Long> result = ints.apply(builder.globally()).apply(HllCount.Extract.globally());
+ PAssert.that(result).containsInAnyOrder(1L);
+ p.run();
+ }
+ /** Test correct Builder is returned from Generic type. * */
+ @Test
+ public void testStringBuilder() {
+
+ PCollection<String> strings = p.apply(Create.<String>of("43"));
+ HllCount.Init.Builder<String> builder =
+ ApproximateCountDistinct.<String>builderForType(
+ strings.getCoder().getEncodedTypeDescriptor());
+ PCollection<Long> result = strings.apply(builder.globally()).apply(HllCount.Extract.globally());
+ PAssert.that(result).containsInAnyOrder(1L);
+ p.run();
+ }
+ /** Test correct Builder is returned from Generic type. * */
+ @Test
+ public void testLongBuilder() {
+
+ PCollection<Long> longs = p.apply(Create.<Long>of(1L));
+ HllCount.Init.Builder<Long> builder =
+ ApproximateCountDistinct.<Long>builderForType(longs.getCoder().getEncodedTypeDescriptor());
+ PCollection<Long> result = longs.apply(builder.globally()).apply(HllCount.Extract.globally());
+ PAssert.that(result).containsInAnyOrder(1L);
+ p.run();
+ }
+ /** Test correct Builder is returned from Generic type. * */
+ @Test
+ public void testBytesBuilder() {
+
+ byte[] byteArray = new byte[] {'A'};
+ PCollection<byte[]> bytes = p.apply(Create.of(byteArray));
+ TypeDescriptor a = bytes.getCoder().getEncodedTypeDescriptor();
+ HllCount.Init.Builder<byte[]> builder =
+ ApproximateCountDistinct.<byte[]>builderForType(
+ bytes.getCoder().getEncodedTypeDescriptor());
+ PCollection<Long> result = bytes.apply(builder.globally()).apply(HllCount.Extract.globally());
+ PAssert.that(result).containsInAnyOrder(1L);
+ p.run();
+ }
+
+ /** Test Integer Globally. */
+ @Test
+ @Category(NeedsRunner.class)
+ public void testStandardTypesGlobalForInteger() {
+ PCollection<Long> approxResultInteger =
+ p.apply("Int", Create.of(INTS1)).apply("IntHLL", ApproximateCountDistinct.globally());
+ PAssert.thatSingleton(approxResultInteger).isEqualTo(INTS1_ESTIMATE);
+ p.run();
+ }
+
+ /** Test Long Globally. */
+ @Test
+ @Category(NeedsRunner.class)
+ public void testStandardTypesGlobalForLong() {
+
+ PCollection<Long> approxResultLong =
+ p.apply("Long", Create.of(INTS1.stream().map(Long::valueOf).collect(Collectors.toList())))
+ .apply("LongHLL", ApproximateCountDistinct.globally());
+
+ PAssert.thatSingleton(approxResultLong).isEqualTo(INTS1_ESTIMATE);
+
+ p.run();
+ }
+
+ /** Test String Globally. */
+ @Test
+ @Category(NeedsRunner.class)
+ public void testStandardTypesGlobalForStrings() {
+ PCollection<Long> approxResultString =
+ p.apply("Str", Create.of(INTS1.stream().map(String::valueOf).collect(Collectors.toList())))
+ .apply("StrHLL", ApproximateCountDistinct.globally());
+
+ PAssert.thatSingleton(approxResultString).isEqualTo(INTS1_ESTIMATE);
+
+ p.run();
+ }
+
+ /** Test Byte Globally. */
+ @Test
+ @Category(NeedsRunner.class)
+ public void testStandardTypesGlobalForBytes() {
+ PCollection<Long> approxResultByte =
+ p.apply(
+ "BytesHLL",
+ Create.of(
+ INTS1.stream()
+ .map(x -> ByteBuffer.allocate(4).putInt(x).array())
+ .collect(Collectors.toList())))
+ .apply(ApproximateCountDistinct.globally());
+
+ PAssert.thatSingleton(approxResultByte).isEqualTo(INTS1_ESTIMATE);
+
+ p.run();
+ }
+
+ /** Test Integer Globally. */
+ @Test
+ @Category(NeedsRunner.class)
+ public void testStandardTypesPerKeyForInteger() {
+
+ List<KV<Integer, Integer>> ints = new ArrayList<>();
+
+ for (int i = 0; i < 3; i++) {
+ for (int k : INTS1) {
+ ints.add(KV.of(i, k));
+ }
+ }
+
+ PCollection<KV<Integer, Long>> result =
+ p.apply("Int", Create.of(ints)).apply("IntHLL", ApproximateCountDistinct.perKey());
+
+ PAssert.that(result)
+ .containsInAnyOrder(
+ ImmutableList.of(
+ KV.of(0, INTS1_ESTIMATE), KV.of(1, INTS1_ESTIMATE), KV.of(2, INTS1_ESTIMATE)));
+
+ p.run();
+ }
+
+ /** Test Long Globally. */
+ @Test
+ @Category(NeedsRunner.class)
+ public void testStandardTypesPerKeyForLong() {
+
+ List<KV<Integer, Long>> longs = new ArrayList<>();
+
+ for (int i = 0; i < 3; i++) {
+ for (int k : INTS1) {
+ longs.add(KV.of(i, (long) k));
+ }
+ }
+
+ PCollection<KV<Integer, Long>> result =
+ p.apply("Long", Create.of(longs)).apply("LongHLL", ApproximateCountDistinct.perKey());
+
+ PAssert.that(result)
+ .containsInAnyOrder(
+ ImmutableList.of(
+ KV.of(0, INTS1_ESTIMATE), KV.of(1, INTS1_ESTIMATE), KV.of(2, INTS1_ESTIMATE)));
+
+ p.run();
+ }
+
+ /** Test String Globally. */
+ @Test
+ @Category(NeedsRunner.class)
+ public void testStandardTypesPerKeyForStrings() {
+ List<KV<Integer, String>> strings = new ArrayList<>();
+
+ for (int i = 0; i < 3; i++) {
+ for (int k : INTS1) {
+ strings.add(KV.of(i, String.valueOf(k)));
+ }
+ }
+
+ PCollection<KV<Integer, Long>> result =
+ p.apply("Str", Create.of(strings)).apply("StrHLL", ApproximateCountDistinct.perKey());
+
+ PAssert.that(result)
+ .containsInAnyOrder(
+ ImmutableList.of(
+ KV.of(0, INTS1_ESTIMATE), KV.of(1, INTS1_ESTIMATE), KV.of(2, INTS1_ESTIMATE)));
+
+ p.run();
+ }
+
+ /** Test Byte Globally. */
+ @Test
+ @Category(NeedsRunner.class)
+ public void testStandardTypesPerKeyForBytes() {
+
+ List<KV<Integer, byte[]>> bytes = new ArrayList<>();
+
+ for (int i = 0; i < 3; i++) {
+ for (int k : INTS1) {
+ bytes.add(KV.of(i, ByteBuffer.allocate(4).putInt(k).array()));
+ }
+ }
+
+ PCollection<KV<Integer, Long>> result =
+ p.apply("BytesHLL", Create.of(bytes)).apply(ApproximateCountDistinct.perKey());
+
+ PAssert.that(result)
+ .containsInAnyOrder(
+ ImmutableList.of(
+ KV.of(0, INTS1_ESTIMATE), KV.of(1, INTS1_ESTIMATE), KV.of(2, INTS1_ESTIMATE)));
+
+ p.run();
+ }
+
+ /** Test a general object, we will make use of a KV as the object as it already has a coder. */
+ @Test
+ @Category(NeedsRunner.class)
+ public void testObjectTypesGlobal() {
+
+ PCollection<Long> approxResultInteger =
+ p.apply(
+ "Int",
+ Create.of(
+ INTS1.stream().map(x -> KV.of(x, KV.of(x, x))).collect(Collectors.toList())))
+ .apply(
+ "IntHLL",
+ ApproximateCountDistinct.<KV<Integer, KV<Integer, Integer>>>globally()
+ .via((KV<Integer, KV<Integer, Integer>> x) -> (long) x.getValue().hashCode()));
Review comment:
I see. This tests will pass, but I find it a bit hard to understand. How about we use `x -> x.getValue().getValue()`? I find that simpler to understand. (e.g. KV<x, KV<x, x>> will be identified by x)
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] rezarokni commented on pull request #12973: [BEAM-10234] Create ApproximateDistinct using HLL Impl
Posted by GitBox <gi...@apache.org>.
rezarokni commented on pull request #12973:
URL: https://github.com/apache/beam/pull/12973#issuecomment-749578394
Run Java PreCommit
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org