hequn8128 commented on a change in pull request #8230: [FLINK-10977][table] Add 
streaming non-window FlatAggregate to Table API
URL: https://github.com/apache/flink/pull/8230#discussion_r277634729
 
 

 ##########
 File path: 
flink-table/flink-table-planner/src/main/scala/org/apache/flink/table/functions/utils/TableAggSqlFunction.scala
 ##########
 @@ -0,0 +1,114 @@
+/*
+ * 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.utils
+
+import java.util
+
+import org.apache.calcite.rel.`type`.RelDataType
+import org.apache.calcite.sql._
+import org.apache.calcite.sql.parser.SqlParserPos
+import org.apache.calcite.sql.validate.SqlUserDefinedAggFunction
+import org.apache.calcite.util.Optionality
+import org.apache.flink.api.common.typeinfo._
+import org.apache.flink.table.calcite.FlinkTypeFactory
+import org.apache.flink.table.functions.TableAggregateFunction
+import 
org.apache.flink.table.functions.utils.AggSqlFunction.{createOperandTypeChecker,
 createOperandTypeInference, createReturnTypeInference}
+
+import scala.collection.JavaConversions._
+
+/**
+  * Calcite wrapper for user-defined table aggregate functions.
+  *
+  * @param name function name (used by SQL parser)
+  * @param displayName name to be displayed in operator name
+  * @param aggregateFunction aggregate function to be called
+  * @param returnType the type information of returned value
+  * @param accType the type information of the accumulator
+  * @param typeFactory type factory for converting Flink's between Calcite's 
types
+  */
+class TableAggSqlFunction(
+    name: String,
+    displayName: String,
+    aggregateFunction: TableAggregateFunction[_, _],
+    alias: Seq[String],
+    val returnType: TypeInformation[_],
+    val accType: TypeInformation[_],
+    typeFactory: FlinkTypeFactory)
+  extends SqlUserDefinedAggFunction(
+    new SqlIdentifier(name, SqlParserPos.ZERO),
+    createReturnTypeInference(returnType, typeFactory),
+    createOperandTypeInference(aggregateFunction, typeFactory),
+    createOperandTypeChecker(aggregateFunction),
+    // Do not need to provide a calcite aggregateFunction here. Flink 
aggregation function
+    // will be generated when translating the calcite relnode to flink runtime 
execution plan
+    null,
+    false,
+    false,
+    Optionality.FORBIDDEN,
+    typeFactory
+  ) {
+
+  def getRelTypeAfterAlias(flinkTypeFactory: FlinkTypeFactory): RelDataType = {
+    val builder = flinkTypeFactory.builder
+    val relDataType = flinkTypeFactory.createTypeFromTypeInfo(returnType, 
isNullable = true)
+    val fieldNames: Seq[String] = if (alias.nonEmpty) {
+      alias
+    } else {
+      relDataType.getFieldNames
+    }
+
+    fieldNames
+      .zip(relDataType.getFieldList)
+      .foreach { f =>
+        builder.add(f._1, f._2.getType)
+      }
+    builder.build()
+  }
+
+  def getFunction: TableAggregateFunction[_, _] = aggregateFunction
+
+  override def isDeterministic: Boolean = aggregateFunction.isDeterministic
+
+  override def toString: String = displayName
+
+  override def getParamTypes: util.List[RelDataType] = null
+}
+
+
+object TableAggSqlFunction {
 
 Review comment:
   Yes, it's good to share logic with `AggSqlFunction`. The problem here is 
`TableAggSqlFunction` contains an alias property. However, I can put alias in 
`AggSqlFunction` and unify them if you think it makes sense. 

----------------------------------------------------------------
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