JingsongLi commented on a change in pull request #9322: [FLINK-13471][table] 
Add FlatAggregate support to stream Table API(blink planner)
URL: https://github.com/apache/flink/pull/9322#discussion_r310102908
 
 

 ##########
 File path: 
flink-table/flink-table-runtime-blink/src/main/java/org/apache/flink/table/runtime/operators/aggregate/GroupTableAggFunction.java
 ##########
 @@ -0,0 +1,172 @@
+/*
+ * 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.operators.aggregate;
+
+import org.apache.flink.api.common.state.ValueState;
+import org.apache.flink.api.common.state.ValueStateDescriptor;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.table.dataformat.BaseRow;
+import org.apache.flink.table.runtime.dataview.PerKeyStateDataViewStore;
+import 
org.apache.flink.table.runtime.functions.KeyedProcessFunctionWithCleanupState;
+import 
org.apache.flink.table.runtime.generated.GeneratedTableAggsHandleFunction;
+import org.apache.flink.table.runtime.generated.TableAggsHandleFunction;
+import org.apache.flink.table.runtime.typeutils.BaseRowTypeInfo;
+import org.apache.flink.table.types.logical.LogicalType;
+import org.apache.flink.util.Collector;
+
+import static 
org.apache.flink.table.dataformat.util.BaseRowUtil.isAccumulateMsg;
+
+/**
+ * Aggregate Function used for the groupby (without window) table aggregate.
+ */
+public class GroupTableAggFunction extends 
KeyedProcessFunctionWithCleanupState<BaseRow, BaseRow, BaseRow> {
+
+       private static final long serialVersionUID = -4767158666069797704L;
+
+       /**
+        * The code generated function used to handle table aggregates.
+        */
+       private final GeneratedTableAggsHandleFunction genAggsHandler;
+
+       /**
+        * The accumulator types.
+        */
+       private final LogicalType[] accTypes;
+
+       /**
+        * Used to count the number of added and retracted input records.
+        */
+       private final RecordCounter recordCounter;
+
+       /**
+        * Whether this operator will generate retraction.
+        */
+       private final boolean generateRetraction;
+
+       // function used to handle all table aggregates
+       private transient TableAggsHandleFunction function = null;
+
+       // stores the accumulators
+       private transient ValueState<BaseRow> accState = null;
+
+       /**
+        * Creates a {@link GroupTableAggFunction}.
+        *
+        * @param minRetentionTime minimal state idle retention time.
+        * @param maxRetentionTime maximal state idle retention time.
+        * @param genAggsHandler The code generated function used to handle 
table aggregates.
+        * @param accTypes The accumulator types.
+        * @param indexOfCountStar The index of COUNT(*) in the aggregates.
+        *                          -1 when the input doesn't contain COUNT(*), 
i.e. doesn't contain retraction messages.
+        *                          We make sure there is a COUNT(*) if input 
stream contains retraction.
+        * @param generateRetraction Whether this operator will generate 
retraction.
+        */
+       public GroupTableAggFunction(
+                       long minRetentionTime,
+                       long maxRetentionTime,
+                       GeneratedTableAggsHandleFunction genAggsHandler,
+                       LogicalType[] accTypes,
+                       int indexOfCountStar,
+                       boolean generateRetraction) {
+               super(minRetentionTime, maxRetentionTime);
+               this.genAggsHandler = genAggsHandler;
+               this.accTypes = accTypes;
+               this.recordCounter = RecordCounter.of(indexOfCountStar);
+               this.generateRetraction = generateRetraction;
+       }
+
+       @Override
+       public void open(Configuration parameters) throws Exception {
+               super.open(parameters);
+               // instantiate function
+               function = 
genAggsHandler.newInstance(getRuntimeContext().getUserCodeClassLoader());
+               function.open(new 
PerKeyStateDataViewStore(getRuntimeContext()));
+
+               BaseRowTypeInfo accTypeInfo = new BaseRowTypeInfo(accTypes);
+               ValueStateDescriptor<BaseRow> accDesc = new 
ValueStateDescriptor<>("accState", accTypeInfo);
+               accState = getRuntimeContext().getState(accDesc);
+
+               initCleanupTimeState("GroupTableAggregateCleanupTime");
+
 
 Review comment:
   remove empty line?

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


With regards,
Apache Git Services

Reply via email to