ajian2002 commented on code in PR #121:
URL: https://github.com/apache/flink-table-store/pull/121#discussion_r885750661


##########
flink-table-store-core/src/main/java/org/apache/flink/table/store/file/mergetree/compact/AggregateMergeFunction.java:
##########
@@ -0,0 +1,165 @@
+/*
+ * 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.store.file.mergetree.compact;
+
+import org.apache.flink.table.data.GenericRowData;
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.table.store.file.FileStoreOptions;
+import org.apache.flink.table.types.logical.LogicalType;
+import org.apache.flink.table.types.logical.RowType;
+
+import javax.annotation.Nullable;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+/**
+ * A {@link MergeFunction} where key is primary key (unique) and value is the 
partial record,
+ * aggregate specifies field on merge.
+ */
+@SuppressWarnings("checkstyle:RegexpSingleline")
+public class AggregateMergeFunction implements MergeFunction {
+
+    private static final long serialVersionUID = 1L;
+
+    private final RowData.FieldGetter[] getters;
+
+    private final RowType rowType;
+    private final ArrayList<ColumnAggregateFunction<?>> aggregateFunctions;
+    private final boolean[] isPrimaryKey;
+    private final RowType primaryKeyType;
+    private transient GenericRowData row;
+    private final Map<String, AggregationKind> aggregationKindMap;
+
+    public AggregateMergeFunction(
+            RowType primaryKeyType,
+            RowType rowType,
+            Map<String, AggregationKind> aggregationKindMap) {
+        this.primaryKeyType = primaryKeyType;
+        this.rowType = rowType;
+        this.aggregationKindMap = aggregationKindMap;
+
+        List<LogicalType> fieldTypes = rowType.getChildren();
+        this.getters = new RowData.FieldGetter[fieldTypes.size()];
+        for (int i = 0; i < fieldTypes.size(); i++) {
+            getters[i] = RowData.createFieldGetter(fieldTypes.get(i), i);
+        }
+
+        this.isPrimaryKey = new boolean[this.getters.length];
+        List<String> rowNames = rowType.getFieldNames();
+        for (String primaryKeyName : primaryKeyType.getFieldNames()) {
+            isPrimaryKey[rowNames.indexOf(primaryKeyName)] = true;
+        }
+
+        this.aggregateFunctions = new ArrayList<>(rowType.getFieldCount());
+        for (int i = 0; i < rowType.getFieldCount(); i++) {
+            ColumnAggregateFunction<?> f = null;
+            if (aggregationKindMap.containsKey(rowNames.get(i))) {
+                f =
+                        
ColumnAggregateFunctionFactory.getColumnAggregateFunction(
+                                aggregationKindMap.get(rowNames.get(i)), 
rowType.getTypeAt(i));
+            } else {
+                if (!isPrimaryKey[i]) {
+                    throw new IllegalArgumentException(
+                            "should  set aggregate function for every column 
not part of primary key");
+                }
+            }
+            aggregateFunctions.add(f);
+        }
+    }
+
+    @Override
+    public void reset() {
+        this.row = new GenericRowData(getters.length);
+    }
+
+    @Override
+    public void add(RowData value) {
+        for (int i = 0; i < getters.length; i++) {
+            Object currentField = getters[i].getFieldOrNull(value);
+            ColumnAggregateFunction<?> f = aggregateFunctions.get(i);
+            if (isPrimaryKey[i]) {
+                // primary key
+                if (currentField != null) {
+                    row.setField(i, currentField);
+                }
+            } else {
+                if (f != null) {
+                    f.reset();
+                    Object oldValue = row.getField(i);
+                    if (oldValue != null) {
+                        f.aggregate(oldValue);
+                    }
+                    switch (value.getRowKind()) {
+                        case INSERT:
+                            f.aggregate(currentField);
+                            break;
+                        case DELETE:
+                        case UPDATE_AFTER:
+                        case UPDATE_BEFORE:
+                        default:
+                            throw new UnsupportedOperationException(
+                                    "Unsupported row kind: " + 
row.getRowKind());
+                    }
+                    Object result = f.getResult();
+                    if (result != null) {
+                        row.setField(i, result);

Review Comment:
   > `AggregateMergeFunction#reset` will be called before adding the first row 
of a new primary key, and `AggregateMergeFunction#getValue` will be called 
after the last row of that primary key. See `SortMergeReader` if you're 
intersted.
   > 
   > > I don't know when AggregateMergeFunction#reset and 
AggregateMergeFunction#getValue are called, and the breakpoint is not called 
when passing the test.
   > 
   > Are you sure about this? My breakpoint at `AggregateMergeFunction#reset` 
and `AggregateMergeFunction#getValue` can be triggered when running 
`AggregationITCase`.
   
   I'm sorry , i got it wrong



-- 
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-unsubscr...@flink.apache.org

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

Reply via email to