Github user shaoxuan-wang commented on a diff in the pull request:

    https://github.com/apache/flink/pull/3809#discussion_r114692346
  
    --- Diff: 
flink-libraries/flink-table/src/main/scala/org/apache/flink/table/functions/utils/AggSqlFunction.scala
 ---
    @@ -0,0 +1,177 @@
    +/*
    + * 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 org.apache.calcite.rel.`type`.RelDataType
    +import org.apache.calcite.sql._
    +import org.apache.calcite.sql.`type`._
    +import org.apache.calcite.sql.`type`.SqlOperandTypeChecker.Consistency
    +import org.apache.calcite.sql.parser.SqlParserPos
    +import org.apache.calcite.sql.validate.SqlUserDefinedAggFunction
    +import org.apache.flink.api.common.typeinfo._
    +import org.apache.flink.table.api.ValidationException
    +import org.apache.flink.table.calcite.FlinkTypeFactory
    +import org.apache.flink.table.functions.AggregateFunction
    +import 
org.apache.flink.table.functions.utils.AggSqlFunction.{createOperandTypeChecker,
 createOperandTypeInference, createReturnTypeInference}
    +import org.apache.flink.table.functions.utils.UserDefinedFunctionUtils._
    +
    +/**
    +  * Calcite wrapper for user-defined aggregate functions.
    +  *
    +  * @param name function name (used by SQL parser)
    +  * @param aggregateFunction aggregate function to be called
    +  * @param returnType the type information of returned value
    +  * @param typeFactory type factory for converting Flink's between 
Calcite's types
    +  */
    +class AggSqlFunction(
    +    name: String,
    +    aggregateFunction: AggregateFunction[_, _],
    +    returnType: 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 
aggregateion function
    +    // will be generated when translating the calcite relnode to flink 
runtime execution plan
    +    null
    +  ) {
    +
    +  def getFunction: AggregateFunction[_, _] = aggregateFunction
    +}
    +
    +object AggSqlFunction {
    +
    +  def apply(
    +      name: String,
    +      aggregateFunction: AggregateFunction[_, _],
    +      returnType: TypeInformation[_],
    +      typeFactory: FlinkTypeFactory): AggSqlFunction = {
    +
    +    new AggSqlFunction(name, aggregateFunction, returnType, typeFactory)
    +  }
    +
    +  private[flink] def createOperandTypeInference(
    +      aggregateFunction: AggregateFunction[_, _],
    +      typeFactory: FlinkTypeFactory)
    +  : SqlOperandTypeInference = {
    +    /**
    +      * Operand type inference based on [[AggregateFunction]] given 
information.
    +      */
    +    new SqlOperandTypeInference {
    +      override def inferOperandTypes(
    +          callBinding: SqlCallBinding,
    +          returnType: RelDataType,
    +          operandTypes: Array[RelDataType]): Unit = {
    +
    +        val operandTypeInfo = getOperandTypeInfo(callBinding)
    +
    +        val foundSignature = 
getAccumulateMethodSignature(aggregateFunction, operandTypeInfo)
    +          .getOrElse(throw new ValidationException(s"Operand types of 
could not be inferred."))
    +
    +        val inferredTypes = getParameterTypes(aggregateFunction, 
foundSignature.drop(1))
    +          .map(typeFactory.createTypeFromTypeInfo)
    +
    +        for (i <- operandTypes.indices) {
    +          if (i < inferredTypes.length - 1) {
    +            operandTypes(i) = inferredTypes(i)
    +          } else if (null != inferredTypes.last.getComponentType) {
    --- End diff --
    
    Yes, if the last parameter is a component, say Array[Int]. We want to get 
the type of Int, not the type of Array.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

Reply via email to