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/15 06:45:27 UTC

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

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


##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/aggregate/hyperloglog/XxHash64Function.java:
##########
@@ -0,0 +1,100 @@
+/*
+ * 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.hyperloglog;
+
+import org.apache.flink.core.memory.MemorySegment;
+import org.apache.flink.core.memory.MemorySegmentFactory;
+import org.apache.flink.table.api.TableException;
+import org.apache.flink.table.data.DecimalData;
+import org.apache.flink.table.data.binary.BinaryStringData;
+
+import java.math.BigDecimal;
+import java.sql.Date;
+import java.sql.Time;
+import java.sql.Timestamp;
+
+/** Utility class to get hash code based on {@link XXH64}. */
+public class XxHash64Function {
+
+    public static final long DEFAULT_SEED = 42L;
+
+    public static long hashInt(int i, long seed) {
+        return XXH64.hashInt(i, seed);
+    }
+
+    public static long hashLong(long l, long seed) {
+        return XXH64.hashLong(l, seed);
+    }
+
+    public static long hashUnsafeBytes(MemorySegment base, int offset, int length, long seed) {
+        return XXH64.hashUnsafeBytes(base, offset, length, seed);
+    }
+
+    /**
+     * Computes hash of a given `value`. The caller needs to check the validity of input `value`.
+     */
+    public static long hash(Object value, long seed) {
+        if (value == null) {
+            return seed;
+        } else if (value instanceof Boolean) {
+            boolean b = (boolean) value;
+            return hashInt(b ? 1 : 0, seed);
+        } else if (value instanceof Byte) {
+            return hashInt((Byte) value, seed);
+        } else if (value instanceof Short) {
+            return hashInt((Short) value, seed);
+        } else if (value instanceof Integer) {
+            return hashInt((Integer) value, seed);
+        } else if (value instanceof Long) {
+            return hashLong((Long) value, seed);
+        } else if (value instanceof Float) {
+            return hashInt(Float.floatToIntBits((Float) value), seed);
+        } else if (value instanceof Double) {
+            return hashLong(Double.doubleToLongBits((Double) value), seed);
+        } else if (value instanceof Date) {
+            return hashLong(((Date) value).getTime(), seed);
+        } else if (value instanceof Time) {
+            return hashLong(((Time) value).getTime(), seed);
+        } else if (value instanceof Timestamp) {
+            return hashLong(((Timestamp) value).getTime(), seed);
+        } else if (value instanceof BigDecimal) {

Review Comment:
   I'm not understand why here ignore `DecimalData` branch?



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