JingsongLi commented on a change in pull request #8001: [FLINK-11949][table-planner-blink] Introduce DeclarativeAggregateFunction and AggsHandlerCodeGenerator to blink planner URL: https://github.com/apache/flink/pull/8001#discussion_r266863931
########## File path: flink-table/flink-table-planner-blink/src/main/java/org/apache/flink/table/functions/AvgAggFunction.java ########## @@ -0,0 +1,158 @@ +/* + * 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; + +import org.apache.flink.api.common.typeinfo.TypeInformation; +import org.apache.flink.api.common.typeinfo.Types; +import org.apache.flink.table.expressions.Expression; +import org.apache.flink.table.expressions.UnresolvedAggBufferReference; +import org.apache.flink.table.type.DecimalType; +import org.apache.flink.table.typeutils.DecimalTypeInfo; + +import java.math.BigDecimal; + +import static org.apache.flink.table.expressions.ExpressionBuilder.div; +import static org.apache.flink.table.expressions.ExpressionBuilder.equalTo; +import static org.apache.flink.table.expressions.ExpressionBuilder.ifThenElse; +import static org.apache.flink.table.expressions.ExpressionBuilder.isNull; +import static org.apache.flink.table.expressions.ExpressionBuilder.literal; +import static org.apache.flink.table.expressions.ExpressionBuilder.minus; +import static org.apache.flink.table.expressions.ExpressionBuilder.nullValue; +import static org.apache.flink.table.expressions.ExpressionBuilder.plus; + +/** + * built-in avg aggregate function. + */ +public abstract class AvgAggFunction extends DeclarativeAggregateFunction { + + private UnresolvedAggBufferReference sum = new UnresolvedAggBufferReference("sum", getSumType()); + private UnresolvedAggBufferReference count = new UnresolvedAggBufferReference("count", Types.LONG); + + public TypeInformation getSumType() { + return Types.LONG; + } + + @Override + public int inputCount() { + return 1; + } + + @Override + public UnresolvedAggBufferReference[] aggBufferAttributes() { + return new UnresolvedAggBufferReference[] {sum, count}; + } + + @Override + public Expression[] initialValuesExpressions() { + return new Expression[] {literal(0L, getSumType()), literal(0L)}; + } + + @Override + public Expression[] accumulateExpressions() { + return new Expression[] { + ifThenElse(isNull(operands()[0]), sum, plus(sum, operands()[0])), + ifThenElse(isNull(operands()[0]), count, plus(count, literal(1L))), + }; + } + + @Override + public Expression[] retractExpressions() { + return new Expression[] { + ifThenElse(isNull(operands()[0]), sum, minus(sum, operands()[0])), + ifThenElse(isNull(operands()[0]), count, minus(count, literal(1L))), + }; + } + + @Override + public Expression[] mergeExpressions() { + return new Expression[] { + plus(sum, mergeInput(sum)), Review comment: I prefer keep it in Java. ``` override def mergeExpressions: Seq[Expression] = Seq( /* sum = */ sum+ mergeOperand(sum), /* count = */ count+ mergeOperand(count) ) ``` mergeOperand corresponds to operand, which makes it easier to understand. ---------------------------------------------------------------- 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
