ajian2002 commented on code in PR #121: URL: https://github.com/apache/flink-table-store/pull/121#discussion_r885242104
########## 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: > 的值`row`仅在被调用时才被检查`AggregateMergeFunction#getValue`。您已将汇总结果存储在 中`f`,为什么每次都重置它并汇总两次? > > 我的建议: > > * 在`AggregateMergeFunction#reset`中,重置每个聚合函数。 > * 在`AggregateMergeFunction#add`中,将每个字段聚合到相应的函数中。当前结果现在存储在函数中。 > * 在`AggregateMergeFunction#getValue`中,将聚合结果从函数移到行中。 My aggregation function f belongs to each column. If the result is not stored in row and reset, the aggregation result of another primary key on the same column will be wrong the next time I add it. Can your method solve this problem? I don't know when AggregateMergeFunction#reset and AggregateMergeFunction#getValue are called, and the breakpoint is not called when passing the test. Let me know if I'm sure it's okay to do this and I'll start coding -- 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