You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@doris.apache.org by GitBox <gi...@apache.org> on 2020/06/17 13:34:56 UTC

[GitHub] [incubator-doris] morningman commented on a change in pull request #3320: (#3319) support java version hyperloglog

morningman commented on a change in pull request #3320:
URL: https://github.com/apache/incubator-doris/pull/3320#discussion_r441548250



##########
File path: fe/src/test/java/org/apache/doris/load/loadv2/HllTest.java
##########
@@ -0,0 +1,268 @@
+// 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.doris.load.loadv2;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.DataInputStream;
+import java.io.DataOutput;
+import java.io.DataOutputStream;
+import java.io.IOException;
+
+import static org.apache.doris.load.loadv2.Hll.*;
+
+public class HllTest {
+
+    @Test
+    public void testFindFirstNonZeroBitPosition() {
+        Assert.assertTrue(getLongTailZeroNum(0) == 0);
+        Assert.assertTrue(getLongTailZeroNum(1) == 0);
+        Assert.assertTrue(getLongTailZeroNum(1l << 30) == 30);
+        Assert.assertTrue(getLongTailZeroNum(1l << 62) == 62);
+    }
+
+    @Test
+    public void HllBasicTest() throws IOException {
+        // test empty
+        Hll emptyHll = new Hll();
+
+        Assert.assertTrue(emptyHll.getType() == HLL_DATA_EMPTY);
+        Assert.assertTrue(emptyHll.estimateCardinality() == 0);
+
+        ByteArrayOutputStream emptyOutputStream = new ByteArrayOutputStream();
+        DataOutput output = new DataOutputStream(emptyOutputStream);
+        emptyHll.serialize(output);
+        DataInputStream emptyInputStream = new DataInputStream(new ByteArrayInputStream(emptyOutputStream.toByteArray()));
+        Hll deserializedEmptyHll = new Hll();
+        deserializedEmptyHll.deserialize(emptyInputStream);
+        Assert.assertTrue(deserializedEmptyHll.getType() == HLL_DATA_EMPTY);
+
+        // test explicit
+        Hll explicitHll = new Hll();
+        for (int i = 0; i < HLL_EXPLICLIT_INT64_NUM; i++) {
+            explicitHll.updateWithHash(i);
+        }
+        Assert.assertTrue(explicitHll.getType() == HLL_DATA_EXPLICIT);
+        Assert.assertTrue(explicitHll.estimateCardinality() == HLL_EXPLICLIT_INT64_NUM);
+
+        ByteArrayOutputStream explicitOutputStream = new ByteArrayOutputStream();
+        DataOutput explicitOutput = new DataOutputStream(explicitOutputStream);
+        explicitHll.serialize(explicitOutput);
+        DataInputStream explicitInputStream = new DataInputStream(new ByteArrayInputStream(explicitOutputStream.toByteArray()));
+        Hll deserializedExplicitHll = new Hll();
+        deserializedExplicitHll.deserialize(explicitInputStream);
+        Assert.assertTrue(deserializedExplicitHll.getType() == HLL_DATA_EXPLICIT);
+
+        // test sparse
+        Hll sparseHll = new Hll();
+        for (int i = 0; i < HLL_SPARSE_THRESHOLD; i++) {
+            sparseHll.updateWithHash(i);
+        }
+        Assert.assertTrue(sparseHll.getType() == HLL_DATA_FULL);
+        // 2% error rate
+        Assert.assertTrue(sparseHll.estimateCardinality() > HLL_SPARSE_THRESHOLD * (1 - 0.02) &&
+                sparseHll.estimateCardinality() < HLL_SPARSE_THRESHOLD * (1 + 0.02));
+
+        ByteArrayOutputStream sparseOutputStream = new ByteArrayOutputStream();
+        DataOutput sparseOutput = new DataOutputStream(sparseOutputStream);
+        sparseHll.serialize(sparseOutput);
+        DataInputStream sparseInputStream = new DataInputStream(new ByteArrayInputStream(sparseOutputStream.toByteArray()));
+        Hll deserializedSparseHll = new Hll();
+        deserializedSparseHll.deserialize(sparseInputStream);
+        Assert.assertTrue(deserializedSparseHll.getType() == HLL_DATA_SPARSE);
+        Assert.assertTrue(sparseHll.estimateCardinality() == deserializedSparseHll.estimateCardinality());
+
+
+        // test full
+        Hll fullHll = new Hll();
+        for (int i = 1; i <= Short.MAX_VALUE; i++) {
+            fullHll.updateWithHash(i);
+        }
+        Assert.assertTrue(fullHll.getType() == HLL_DATA_FULL);
+        // the result 32748 is consistent with C++ 's implementation
+        Assert.assertTrue(fullHll.estimateCardinality() == 32748);
+        Assert.assertTrue(fullHll.estimateCardinality() > Short.MAX_VALUE * (1 - 0.02) &&
+                fullHll.estimateCardinality() < Short.MAX_VALUE * (1 + 0.02));
+
+        ByteArrayOutputStream fullHllOutputStream = new ByteArrayOutputStream();
+        DataOutput fullHllOutput = new DataOutputStream(fullHllOutputStream);
+        fullHll.serialize(fullHllOutput);
+        DataInputStream fullHllInputStream = new DataInputStream(new ByteArrayInputStream(fullHllOutputStream.toByteArray()));
+        Hll deserializedFullHll = new Hll();
+        deserializedFullHll.deserialize(fullHllInputStream);
+        Assert.assertTrue(deserializedFullHll.getType() == HLL_DATA_FULL);
+        Assert.assertTrue(deserializedFullHll.estimateCardinality() == fullHll.estimateCardinality());
+
+    }
+
+    // keep logic same with C++ version
+    // add additional compare logic with C++ version's estimateValue

Review comment:
       better to add comment in `be/test/olap/hll_test.cpp` to notice that the following test case should be consistent with java version in `fe/src/test/java/org/apache/doris/load/loadv2/HllTest.java`




----------------------------------------------------------------
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



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org