You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by GitBox <gi...@apache.org> on 2022/07/17 07:32:47 UTC

[GitHub] [flink] swuferhong commented on a diff in pull request #20243: [FLINK-28491][table-planner] Introduce APPROX_COUNT_DISTINCT aggregate function for batch sql

swuferhong commented on code in PR #20243:
URL: https://github.com/apache/flink/pull/20243#discussion_r922784619


##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/aggregate/BatchApproxCountDistinctAggFunctions.java:
##########
@@ -0,0 +1,333 @@
+/*
+ * 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.flink.table.runtime.functions.aggregate;
+
+import org.apache.flink.core.memory.MemorySegment;
+import org.apache.flink.core.memory.MemorySegmentFactory;
+import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.table.data.DecimalData;
+import org.apache.flink.table.data.TimestampData;
+import org.apache.flink.table.data.binary.BinaryStringData;
+import org.apache.flink.table.runtime.functions.aggregate.hyperloglog.HllBuffer;
+import org.apache.flink.table.runtime.functions.aggregate.hyperloglog.HyperLogLogPlusPlus;
+import org.apache.flink.table.types.DataType;
+import org.apache.flink.table.types.logical.BigIntType;
+import org.apache.flink.table.types.logical.DateType;
+import org.apache.flink.table.types.logical.DecimalType;
+import org.apache.flink.table.types.logical.DoubleType;
+import org.apache.flink.table.types.logical.FloatType;
+import org.apache.flink.table.types.logical.IntType;
+import org.apache.flink.table.types.logical.LocalZonedTimestampType;
+import org.apache.flink.table.types.logical.LogicalType;
+import org.apache.flink.table.types.logical.SmallIntType;
+import org.apache.flink.table.types.logical.TimeType;
+import org.apache.flink.table.types.logical.TimestampType;
+import org.apache.flink.table.types.logical.TinyIntType;
+import org.apache.flink.table.types.logical.VarCharType;
+
+import java.util.Collections;
+import java.util.List;
+
+import static org.apache.flink.table.runtime.functions.aggregate.hyperloglog.XXH64.DEFAULT_SEED;
+import static org.apache.flink.table.runtime.functions.aggregate.hyperloglog.XXH64.hashInt;
+import static org.apache.flink.table.runtime.functions.aggregate.hyperloglog.XXH64.hashLong;
+import static org.apache.flink.table.runtime.functions.aggregate.hyperloglog.XXH64.hashUnsafeBytes;
+import static org.apache.flink.table.types.utils.DataTypeUtils.toInternalDataType;
+
+/** Built-in APPROX_COUNT_DISTINCT aggregate function for Batch sql. */
+public class BatchApproxCountDistinctAggFunctions {
+
+    /** Base function for APPROX_COUNT_DISTINCT aggregate. */
+    public abstract static class ApproxCountDistinctAggFunction<T>
+            extends BuiltInAggregateFunction<Long, HllBuffer> {
+
+        private static final Double RELATIVE_SD = 0.01;

Review Comment:
   I think here need to add some comments to explain why choose 0.01 ?



##########
flink-table/flink-table-planner/src/test/scala/org/apache/flink/table/planner/runtime/batch/sql/agg/SortAggITCase.scala:
##########
@@ -310,6 +312,57 @@ class SortAggITCase extends AggregateITCaseBase("SortAggregate") {
       Seq(row("null"))
     )
   }
+
+  @Test
+  def testApproximateCountDistinct(): Unit = {
+    val dataId = TestValuesTableFactory.registerData(TestData.fullDataTypesData)
+    tEnv.executeSql(
+      s"""
+         |CREATE TABLE MyTable (
+         |  `boolean` BOOLEAN,
+         |  `byte` TINYINT,
+         |  `short` SMALLINT,
+         |  `int` INT,
+         |  `long` BIGINT,
+         |  `float` FLOAT,
+         |  `double` DOUBLE,
+         |  `decimal52` DECIMAL(5, 2),

Review Comment:
   Adding more decimal types, like decimal (14, 2) and decimal (38, 2).



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

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org