dylanhz commented on code in PR #25291:
URL: https://github.com/apache/flink/pull/25291#discussion_r1746857379


##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/aggregate/PercentileAggFunction.java:
##########
@@ -0,0 +1,382 @@
+/*
+ * 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.annotation.Internal;
+import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.table.api.dataview.MapView;
+import org.apache.flink.table.data.DecimalData;
+import org.apache.flink.table.data.DecimalDataUtils;
+import org.apache.flink.table.types.DataType;
+import org.apache.flink.table.types.logical.LogicalType;
+
+import org.apache.commons.math3.util.Pair;
+
+import javax.annotation.Nullable;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
+
+import static 
org.apache.flink.table.types.utils.DataTypeUtils.toInternalDataType;
+
+/** Built-in PERCENTILE aggregate function. */
+@Internal
+public abstract class PercentileAggFunction<T>
+        extends BuiltInAggregateFunction<T, 
PercentileAggFunction.PercentileAccumulator> {
+
+    protected final transient DataType valueType;
+    protected final transient DataType frequencyType;
+
+    public PercentileAggFunction(LogicalType inputType, LogicalType 
frequencyType) {
+        this.valueType = toInternalDataType(inputType);
+        this.frequencyType = frequencyType == null ? null : 
toInternalDataType(frequencyType);
+    }
+
+    // 
--------------------------------------------------------------------------------------------
+    // Accumulator
+    // 
--------------------------------------------------------------------------------------------
+
+    /** Accumulator for PERCENTILE. */
+    public static class PercentileAccumulator {
+
+        public double[] percentages;
+        public long totalCount;
+        public MapView<Double, Long> valueCount;
+
+        @Override
+        public boolean equals(Object o) {
+            if (this == o) {
+                return true;
+            }
+            if (o == null || getClass() != o.getClass()) {
+                return false;
+            }
+            PercentileAccumulator that = (PercentileAccumulator) o;
+            return Arrays.equals(percentages, that.percentages)
+                    && totalCount == that.totalCount
+                    && Objects.equals(valueCount, that.valueCount);
+        }
+
+        @Override
+        public int hashCode() {
+            return Objects.hash(Arrays.hashCode(percentages), totalCount, 
valueCount.hashCode());
+        }
+
+        public Double[] getValue() {
+            List<Pair<Double, Integer>> sortedPercentages =
+                    IntStream.range(0, percentages.length)
+                            .mapToObj(
+                                    index ->
+                                            new Pair<>(
+                                                    percentages[index] * 
(totalCount + 1), index))
+                            .sorted(Comparator.comparing(Pair::getKey))
+                            .collect(Collectors.toList());
+
+            List<Map.Entry<Double, Long>> sortedList = new ArrayList<>();
+            try {
+                for (Map.Entry<Double, Long> entry : valueCount.entries()) {
+                    sortedList.add(entry);
+                }
+            } catch (Throwable t) {
+                t.printStackTrace();
+            }
+
+            sortedList =
+                    sortedList.stream()
+                            .sorted(Map.Entry.comparingByKey())
+                            .collect(Collectors.toList());
+
+            Double[] percentiles = new Double[percentages.length];
+
+            long preCnt = sortedList.get(0).getValue();
+            for (int i = 0, j = 0; i < sortedPercentages.size(); i++) {
+                double position = sortedPercentages.get(i).getKey();
+                long lower = (long) Math.floor(position);
+                long higher = (long) Math.ceil(position);
+
+                while (preCnt < lower) {
+                    j++;

Review Comment:
   You are right, I forgot that `lower` can be `totalCount + 1` when 
`percentage = 1.0`.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to