[ 
https://issues.apache.org/jira/browse/FLINK-5906?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15994842#comment-15994842
 ] 

ASF GitHub Bot commented on FLINK-5906:
---------------------------------------

Github user fhueske commented on a diff in the pull request:

    https://github.com/apache/flink/pull/3809#discussion_r114542402
  
    --- Diff: 
flink-libraries/flink-table/src/main/scala/org/apache/flink/table/codegen/CodeGenerator.scala
 ---
    @@ -274,27 +278,74 @@ class CodeGenerator(
           constantFlags: Option[Array[(Int, Boolean)]],
           outputArity: Int,
           needRetract: Boolean,
    -      needMerge: Boolean)
    +      needMerge: Boolean,
    +      needReset: Boolean)
       : GeneratedAggregationsFunction = {
     
         // get unique function name
         val funcName = newName(name)
         // register UDAGGs
         val aggs = aggregates.map(a => generator.addReusableFunction(a))
         // get java types of accumulators
    -    val accTypes = aggregates.map { a =>
    -      
a.getClass.getMethod("createAccumulator").getReturnType.getCanonicalName
    +    val accTypeClasses = aggregates.map { a =>
    +      a.getClass.getMethod("createAccumulator").getReturnType
         }
    +    val accTypes = accTypeClasses.map(_.getCanonicalName)
     
    -    // get java types of input fields
    -    val javaTypes = inputType.getFieldList
    +    // get java classes of input fields
    +    val javaClasses = inputType.getFieldList
           .map(f => FlinkTypeFactory.toTypeInfo(f.getType))
    -      .map(t => t.getTypeClass.getCanonicalName)
    +      .map(t => t.getTypeClass)
         // get parameter lists for aggregation functions
    -    val parameters = aggFields.map {inFields =>
    -      val fields = for (f <- inFields) yield s"(${javaTypes(f)}) 
input.getField($f)"
    +    val parameters = aggFields.map { inFields =>
    +      val fields = for (f <- inFields) yield
    +        s"(${javaClasses(f).getCanonicalName}) input.getField($f)"
           fields.mkString(", ")
         }
    +    val methodSignaturesList = aggFields.map {
    +      inFields => for (f <- inFields) yield javaClasses(f)
    +    }
    +
    +    // check and validate the needed methods
    +    aggregates.zipWithIndex.map {
    +      case (a, i) => {
    +        getUserDefinedMethod(a, "accumulate", Array(accTypeClasses(i)) ++ 
methodSignaturesList(i))
    +          .getOrElse(
    +            throw new CodeGenException(
    +              s"No matching accumulate method found for aggregate " +
    +                s"'${a.getClass.getCanonicalName}'" +
    +                s"with parameters 
'${signatureToString(methodSignaturesList(i))}'.")
    +          )
    +
    +        if (needRetract) {
    +          getUserDefinedMethod(a, "retract", Array(accTypeClasses(i)) ++ 
methodSignaturesList(i))
    +            .getOrElse(
    +              throw new CodeGenException(
    +                s"No matching retract method found for aggregate " +
    +                  s"'${a.getClass.getCanonicalName}'" +
    +                  s"with parameters 
'${signatureToString(methodSignaturesList(i))}'.")
    +            )
    +        }
    +
    +        if (needMerge) {
    +          val methods = checkAndExtractMethods(a, "merge")
    --- End diff --
    
    This check be done as:
    
    ```
    // get method
    val mergeMethod =
                getUserDefinedMethod(a, "merge", Array(accTypeClasses(i), 
classOf[JIterable[Any]]))
                .getOrElse(
                  throw new CodeGenException(
                    s"No matching merge method found for aggregate " +
                      s"${a.getClass.getCanonicalName}'.")
                )
              // get class of iterable type
              val iterableTypeClass = 
mergeMethod.getGenericParameterTypes.apply(1)
                .asInstanceOf[ParameterizedType].getActualTypeArguments.apply(0)
              if (iterableTypeClass != accTypeClasses(i)) {
                throw new CodeGenException(
                  s"No matching merge method found for aggregate " +
                    s"${a.getClass.getCanonicalName}'.")
              }
    ```


> Add support to register UDAGG in Table and SQL API
> --------------------------------------------------
>
>                 Key: FLINK-5906
>                 URL: https://issues.apache.org/jira/browse/FLINK-5906
>             Project: Flink
>          Issue Type: Sub-task
>          Components: Table API & SQL
>            Reporter: Fabian Hueske
>            Assignee: Shaoxuan Wang
>




--
This message was sent by Atlassian JIRA
(v6.3.15#6346)

Reply via email to