KurtYoung commented on a change in pull request #8110: [FLINK-12098] 
[table-planner-blink] Add support for generating optimized logical plan for 
simple group aggregate on stream
URL: https://github.com/apache/flink/pull/8110#discussion_r272872792
 
 

 ##########
 File path: 
flink-table/flink-table-planner-blink/src/main/java/org/apache/flink/table/functions/aggfunctions/MaxWithRetractAggFunction.java
 ##########
 @@ -0,0 +1,482 @@
+/*
+ * 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.functions.aggfunctions;
+
+import org.apache.flink.api.common.typeinfo.BasicTypeInfo;
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.api.common.typeinfo.Types;
+import org.apache.flink.table.api.dataview.MapView;
+import org.apache.flink.table.dataformat.Decimal;
+import org.apache.flink.table.functions.AggregateFunction;
+import org.apache.flink.table.typeutils.DecimalTypeInfo;
+
+import java.sql.Date;
+import java.sql.Time;
+import java.sql.Timestamp;
+import java.util.Comparator;
+import java.util.Iterator;
+import java.util.Map;
+
+/**
+ * built-in Max with retraction aggregate function.
+ */
+public abstract class MaxWithRetractAggFunction<T>
+               extends AggregateFunction<T, 
MaxWithRetractAggFunction.MaxWithRetractAccumulator<T>> {
+
+       /** The initial accumulator for Max with retraction aggregate function. 
*/
+       public static class MaxWithRetractAccumulator<T> {
+               public T max;
+               public Long distinctCount;
+               public MapView<T, Long> map;
+       }
+
+       @Override
+       public MaxWithRetractAccumulator<T> createAccumulator() {
+               MaxWithRetractAccumulator<T> acc = new 
MaxWithRetractAccumulator<>();
+               acc.max = getInitValue(); // max
+               acc.distinctCount = 0L;
+               // store the count for each value
+               acc.map = new MapView<>(getValueTypeInfo(), 
BasicTypeInfo.LONG_TYPE_INFO);
+               return acc;
+       }
+
+       public void accumulate(MaxWithRetractAccumulator<T> acc, Object value) 
throws Exception {
+               if (value != null) {
+                       T v = (T) value;
+
+                       if (acc.distinctCount == 0L || 
getComparator().compare(acc.max, v) < 0) {
+                               acc.max = v;
+                       }
+
+                       Long count = acc.map.get(v);
+                       if (count == null) {
+                               acc.map.put(v, 1L);
+                               acc.distinctCount += 1;
+                       } else {
+                               count += 1L;
+                               acc.map.put(v, count);
+                       }
+               }
+       }
+
+       public void retract(MaxWithRetractAccumulator<T> acc, Object value) 
throws Exception {
+               if (value != null) {
+                       T v = (T) value;
+
+                       Long count = acc.map.get(v);
+                       if (count == null || count == 1L) {
+                               //remove the key v from the map if the number 
of appearance of the value v is 0
+                               if (count != null) {
+                                       acc.map.remove(v);
+                               }
+                               //if the total count is 0, we could just simply 
set the f0(max) to the initial value
+                               acc.distinctCount -= 1L;
+                               if (acc.distinctCount == 0L) {
+                                       acc.max = getInitValue();
+                                       return;
+                               }
+                               //if v is the current max value, we have to 
iterate the map to find the 2nd biggest
+                               // value to replace v as the max value
+                               if (v == acc.max) {
+                                       Iterator<T> iterator = 
acc.map.keys().iterator();
+                                       boolean hasMax = false;
+                                       Comparator<T> comparator = 
getComparator();
+                                       while (iterator.hasNext()) {
+                                               T key = iterator.next();
+                                               if (!hasMax || 
comparator.compare(acc.max, key) < 0) {
+                                                       acc.max = key;
+                                                       hasMax = true;
+                                               }
+                                       }
+                                       if (!hasMax) {
 
 Review comment:
   Why would this happen?

----------------------------------------------------------------
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:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to