{"action":"created","comment":{"url":"https://api.github.com/repos/apache/flink/pulls/comments/55013024","id":55013024,"diff_hunk":"@@ -0,0 +1,329 @@\n+/*\n+ * Licensed to the Apache Software Foundation (ASF) under one\n+ * or more contributor license agreements.  See the NOTICE file\n+ * distributed with this work for additional information\n+ * regarding copyright ownership.  The ASF licenses this file\n+ * to you under the Apache License, Version 2.0 (the\n+ * \"License\"); you may not use this file except in compliance\n+ * with the License.  You may obtain a copy of the License at\n+ *\n+ *     http://www.apache.org/licenses/LICENSE-2.0\n+ *\n+ * Unless required by applicable law or agreed to in writing, software\n+ * distributed under the License is distributed on an \"AS IS\" BASIS,\n+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n+ * See the License for the specific language governing permissions and\n+ * limitations under the License.\n+ */\n+package org.apache.flink.api.table.runtime.aggregate\n+\n+import java.util\n+\n+import org.apache.calcite.rel.`type`._\n+import org.apache.calcite.rel.core.AggregateCall\n+import org.apache.calcite.sql.SqlAggFunction\n+import org.apache.calcite.sql.`type`.SqlTypeName._\n+import org.apache.calcite.sql.`type`.{SqlTypeFactoryImpl, SqlTypeName}\n+import org.apache.calcite.sql.fun._\n+import org.apache.flink.api.common.functions.{GroupReduceFunction, MapFunction}\n+import org.apache.flink.api.common.typeinfo.TypeInformation\n+import org.apache.flink.api.table.plan.PlanGenException\n+import org.apache.flink.api.table.typeinfo.RowTypeInfo\n+import org.apache.flink.api.table.{Row, TableConfig}\n+\n+import scala.collection.JavaConversions._\n+import scala.collection.mutable.ArrayBuffer\n+\n+object AggregateUtil {\n+\n+  type CalcitePair[T, R] = org.apache.calcite.util.Pair[T, R]\n+  type JavaList[T] = java.util.List[T]\n+\n+  /**\n+   * Create Flink operator functions for aggregates. It includes 2 implementations of Flink \n+   * operator functions:\n+   * [[org.apache.flink.api.common.functions.MapFunction]] and \n+   * [[org.apache.flink.api.common.functions.GroupReduceFunction]](if it's partial aggregate,\n+   * should also implement [[org.apache.flink.api.common.functions.CombineFunction]] as well). \n+   * The output of [[org.apache.flink.api.common.functions.MapFunction]] contains the \n+   * intermediate aggregate values of all aggregate function, it's stored in Row by the following\n+   * format:\n+   *\n+   * {{{\n+   *                   avg(x) aggOffsetInRow = 2          count(z) aggOffsetInRow = 5\n+   *                             |                          |\n+   *                             v                          v\n+   *        +---------+---------+--------+--------+--------+--------+\n+   *        |groupKey1|groupKey2|  sum1  | count1 |  sum2  | count2 |\n+   *        +---------+---------+--------+--------+--------+--------+\n+   *                                              ^\n+   *                                              |\n+   *                               sum(y) aggOffsetInRow = 4\n+   * }}}\n+   *\n+   */\n+  def createOperatorFunctionsForAggregates(namedAggregates: Seq[CalcitePair[AggregateCall, String]],\n+      inputType: RelDataType, outputType: RelDataType,\n+      groupings: Array[Int]): AggregateResult = {\n+\n+    val aggregateFunctionsAndFieldIndexes =\n+      transformToAggregateFunctions(namedAggregates.map(_.getKey), inputType, groupings.length)\n+    // store the aggregate fields of each aggregate function, by the same order of aggregates.\n+    val aggFieldIndexes = aggregateFunctionsAndFieldIndexes._1\n+    val aggregates = aggregateFunctionsAndFieldIndexes._2\n+\n+    val mapFunction = (\n+        config: TableConfig,\n+        inputType: TypeInformation[Any],\n+        returnType: TypeInformation[Any]) => {\n+\n+      val aggregateMapFunction = new AggregateMapFunction[Row, Row](\n+        aggregates, aggFieldIndexes, groupings, returnType.asInstanceOf[RowTypeInfo])\n+\n+      aggregateMapFunction.asInstanceOf[MapFunction[Any, Any]]\n+    }\n+\n+    val bufferDataType: RelRecordType =\n+      createAggregateBufferDataType(groupings, aggregates, inputType)\n+\n+    // the mapping relation between field index of intermediate aggregate Row and output Row.\n+    val groupingOffsetMapping = getGroupKeysMapping(inputType, outputType, groupings)\n+\n+    // the mapping relation between aggregate function index in list and its corresponding\n+    // field index in output Row.\n+    val aggOffsetMapping = getAggregateMapping(namedAggregates, outputType)\n+\n+    if (groupingOffsetMapping.length != groupings.length ||\n+        aggOffsetMapping.length != namedAggregates.length) {\n+      throw new PlanGenException(\"Could not find output field in input data type \" +\n+          \"or aggregate functions.\")\n+    }\n+\n+    val allPartialAggregate = aggregates.map(_.supportPartial).reduce(_ && _)\n+\n+    val intermediateRowArity = groupings.length + aggregates.map(_.intermediateDataType.length).sum\n+\n+    val reduceGroupFunction =\n+      if (allPartialAggregate) {\n+        (config: TableConfig, inputType: TypeInformation[Row], returnType: TypeInformation[Row]) =>\n+          new AggregateReduceCombineFunction(aggregates, groupingOffsetMapping,\n+            aggOffsetMapping, intermediateRowArity)\n+      } else {\n+        (config: TableConfig, inputType: TypeInformation[Row], returnType: TypeInformation[Row]) =>\n+          new AggregateReduceGroupFunction(aggregates, groupingOffsetMapping,\n+            aggOffsetMapping, intermediateRowArity)\n+      }\n+\n+    new AggregateResult(mapFunction, reduceGroupFunction, bufferDataType)\n+  }\n+\n+  private def transformToAggregateFunctions(\n+      aggregateCalls: Seq[AggregateCall],\n+      inputType: RelDataType,\n+      groupKeysCount: Int): (Array[Int], Array[Aggregate[_ <: Any]]) = {\n+\n+    // store the aggregate fields of each aggregate function, by the same order of aggregates.\n+    val aggFieldIndexes = new Array[Int](aggregateCalls.size)\n+    val aggregates = new Array[Aggregate[_ <: Any]](aggregateCalls.size)\n+\n+    // set the start offset of aggregate buffer value to group keys' length, \n+    // as all the group keys would be moved to the start fields of intermediate\n+    // aggregate data.\n+    var aggOffset = groupKeysCount\n+\n+    // create aggregate function instances by function type and aggregate field data type.\n+    aggregateCalls.zipWithIndex.foreach { case (aggregateCall, index) =>\n+      val argList: util.List[Integer] = aggregateCall.getArgList\n+      if (argList.isEmpty) {\n+        if (aggregateCall.getAggregation.isInstanceOf[SqlCountAggFunction]) {\n+          aggFieldIndexes(index) = 0\n+        } else {\n+          throw new PlanGenException(\"Aggregate fields should not be empty.\")\n+        }\n+      } else {\n+        if (argList.size() > 1) {\n+          throw new PlanGenException(\"Currently, do not support aggregate on multi fields.\")\n+        }\n+        aggFieldIndexes(index) = argList.get(0)\n+      }\n+      val sqlTypeName = inputType.getFieldList.get(aggFieldIndexes(index)).getType.getSqlTypeName\n+      aggregateCall.getAggregation match {\n+        case _: SqlSumAggFunction | _: SqlSumEmptyIsZeroAggFunction => {\n+          sqlTypeName match {","path":"flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/runtime/aggregate/AggregateUtil.scala","position":152,"original_position":152,"commit_id":"4ac49d746bfd45ee4ee529e228d5ba8ec2beb834","original_commit_id":"4ac49d746bfd45ee4ee529e228d5ba8ec2beb834","user":{"login":"fhueske","id":2388347,"avatar_url":"https://avatars.githubusercontent.com/u/2388347?v=3","gravatar_id":"","url":"https://api.github.com/users/fhueske","html_url":"https://github.com/fhueske","followers_url":"https://api.github.com/users/fhueske/followers","following_url":"https://api.github.com/users/fhueske/following{/other_user}","gists_url":"https://api.github.com/users/fhueske/gists{/gist_id}","starred_url":"https://api.github.com/users/fhueske/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/fhueske/subscriptions","organizations_url":"https://api.github.com/users/fhueske/orgs","repos_url":"https://api.github.com/users/fhueske/repos","events_url":"https://api.github.com/users/fhueske/events{/privacy}","received_events_url":"https://api.github.com/users/fhueske/received_events","type":"User","site_admin":false},"body":"we can make the code more concise like this:\r\n```\r\naggregates(index) = sqlTypeName match {\r\n  case TINYINT => new ByteSumAggregate\r\n  ...\r\n}\r\n```","created_at":"2016-03-04T10:19:41Z","updated_at":"2016-03-04T10:19:41Z","html_url":"https://github.com/apache/flink/pull/1746#discussion_r55013024","pull_request_url":"https://api.github.com/repos/apache/flink/pulls/1746","_links":{"self":{"href":"https://api.github.com/repos/apache/flink/pulls/comments/55013024"},"html":{"href":"https://github.com/apache/flink/pull/1746#discussion_r55013024"},"pull_request":{"href":"https://api.github.com/repos/apache/flink/pulls/1746"}}},"pull_request":{"url":"https://api.github.com/repos/apache/flink/pulls/1746","id":61194368,"html_url":"https://github.com/apache/flink/pull/1746","diff_url":"https://github.com/apache/flink/pull/1746.diff","patch_url":"https://github.com/apache/flink/pull/1746.patch","issue_url":"https://api.github.com/repos/apache/flink/issues/1746","number":1746,"state":"open","locked":false,"title":"[FLINK-3474] support partial aggregate","user":{"login":"ChengXiangLi","id":4425616,"avatar_url":"https://avatars.githubusercontent.com/u/4425616?v=3","gravatar_id":"","url":"https://api.github.com/users/ChengXiangLi","html_url":"https://github.com/ChengXiangLi","followers_url":"https://api.github.com/users/ChengXiangLi/followers","following_url":"https://api.github.com/users/ChengXiangLi/following{/other_user}","gists_url":"https://api.github.com/users/ChengXiangLi/gists{/gist_id}","starred_url":"https://api.github.com/users/ChengXiangLi/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ChengXiangLi/subscriptions","organizations_url":"https://api.github.com/users/ChengXiangLi/orgs","repos_url":"https://api.github.com/users/ChengXiangLi/repos","events_url":"https://api.github.com/users/ChengXiangLi/events{/privacy}","received_events_url":"https://api.github.com/users/ChengXiangLi/received_events","type":"User","site_admin":false},"body":"This PR includes:\r\n* New aggregate interface which support partial aggregate.\r\n* Simple aggregate function implementation, such as SUM/AVG/COUNT/MIN/MAX.\r\n* DataSetAggregateRule which translate logical calcite aggregate node to Flink operator functions.","created_at":"2016-03-01T05:18:34Z","updated_at":"2016-03-04T10:19:41Z","closed_at":null,"merged_at":null,"merge_commit_sha":"30042992114df897f3cc1ef60b47942becde01cc","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/apache/flink/pulls/1746/commits","review_comments_url":"https://api.github.com/repos/apache/flink/pulls/1746/comments","review_comment_url":"https://api.github.com/repos/apache/flink/pulls/comments{/number}","comments_url":"https://api.github.com/repos/apache/flink/issues/1746/comments","statuses_url":"https://api.github.com/repos/apache/flink/statuses/4ac49d746bfd45ee4ee529e228d5ba8ec2beb834","head":{"label":"ChengXiangLi:partialAggregate","ref":"partialAggregate","sha":"4ac49d746bfd45ee4ee529e228d5ba8ec2beb834","user":{"login":"ChengXiangLi","id":4425616,"avatar_url":"https://avatars.githubusercontent.com/u/4425616?v=3","gravatar_id":"","url":"https://api.github.com/users/ChengXiangLi","html_url":"https://github.com/ChengXiangLi","followers_url":"https://api.github.com/users/ChengXiangLi/followers","following_url":"https://api.github.com/users/ChengXiangLi/following{/other_user}","gists_url":"https://api.github.com/users/ChengXiangLi/gists{/gist_id}","starred_url":"https://api.github.com/users/ChengXiangLi/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ChengXiangLi/subscriptions","organizations_url":"https://api.github.com/users/ChengXiangLi/orgs","repos_url":"https://api.github.com/users/ChengXiangLi/repos","events_url":"https://api.github.com/users/ChengXiangLi/events{/privacy}","received_events_url":"https://api.github.com/users/ChengXiangLi/received_events","type":"User","site_admin":false},"repo":{"id":34307818,"name":"flink","full_name":"ChengXiangLi/flink","owner":{"login":"ChengXiangLi","id":4425616,"avatar_url":"https://avatars.githubusercontent.com/u/4425616?v=3","gravatar_id":"","url":"https://api.github.com/users/ChengXiangLi","html_url":"https://github.com/ChengXiangLi","followers_url":"https://api.github.com/users/ChengXiangLi/followers","following_url":"https://api.github.com/users/ChengXiangLi/following{/other_user}","gists_url":"https://api.github.com/users/ChengXiangLi/gists{/gist_id}","starred_url":"https://api.github.com/users/ChengXiangLi/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ChengXiangLi/subscriptions","organizations_url":"https://api.github.com/users/ChengXiangLi/orgs","repos_url":"https://api.github.com/users/ChengXiangLi/repos","events_url":"https://api.github.com/users/ChengXiangLi/events{/privacy}","received_events_url":"https://api.github.com/users/ChengXiangLi/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/ChengXiangLi/flink","description":"Mirror of Apache Flink","fork":true,"url":"https://api.github.com/repos/ChengXiangLi/flink","forks_url":"https://api.github.com/repos/ChengXiangLi/flink/forks","keys_url":"https://api.github.com/repos/ChengXiangLi/flink/keys{/key_id}","collaborators_url":"https://api.github.com/repos/ChengXiangLi/flink/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/ChengXiangLi/flink/teams","hooks_url":"https://api.github.com/repos/ChengXiangLi/flink/hooks","issue_events_url":"https://api.github.com/repos/ChengXiangLi/flink/issues/events{/number}","events_url":"https://api.github.com/repos/ChengXiangLi/flink/events","assignees_url":"https://api.github.com/repos/ChengXiangLi/flink/assignees{/user}","branches_url":"https://api.github.com/repos/ChengXiangLi/flink/branches{/branch}","tags_url":"https://api.github.com/repos/ChengXiangLi/flink/tags","blobs_url":"https://api.github.com/repos/ChengXiangLi/flink/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/ChengXiangLi/flink/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/ChengXiangLi/flink/git/refs{/sha}","trees_url":"https://api.github.com/repos/ChengXiangLi/flink/git/trees{/sha}","statuses_url":"https://api.github.com/repos/ChengXiangLi/flink/statuses/{sha}","languages_url":"https://api.github.com/repos/ChengXiangLi/flink/languages","stargazers_url":"https://api.github.com/repos/ChengXiangLi/flink/stargazers","contributors_url":"https://api.github.com/repos/ChengXiangLi/flink/contributors","subscribers_url":"https://api.github.com/repos/ChengXiangLi/flink/subscribers","subscription_url":"https://api.github.com/repos/ChengXiangLi/flink/subscription","commits_url":"https://api.github.com/repos/ChengXiangLi/flink/commits{/sha}","git_commits_url":"https://api.github.com/repos/ChengXiangLi/flink/git/commits{/sha}","comments_url":"https://api.github.com/repos/ChengXiangLi/flink/comments{/number}","issue_comment_url":"https://api.github.com/repos/ChengXiangLi/flink/issues/comments{/number}","contents_url":"https://api.github.com/repos/ChengXiangLi/flink/contents/{+path}","compare_url":"https://api.github.com/repos/ChengXiangLi/flink/compare/{base}...{head}","merges_url":"https://api.github.com/repos/ChengXiangLi/flink/merges","archive_url":"https://api.github.com/repos/ChengXiangLi/flink/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/ChengXiangLi/flink/downloads","issues_url":"https://api.github.com/repos/ChengXiangLi/flink/issues{/number}","pulls_url":"https://api.github.com/repos/ChengXiangLi/flink/pulls{/number}","milestones_url":"https://api.github.com/repos/ChengXiangLi/flink/milestones{/number}","notifications_url":"https://api.github.com/repos/ChengXiangLi/flink/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/ChengXiangLi/flink/labels{/name}","releases_url":"https://api.github.com/repos/ChengXiangLi/flink/releases{/id}","deployments_url":"https://api.github.com/repos/ChengXiangLi/flink/deployments","created_at":"2015-04-21T06:06:37Z","updated_at":"2016-01-20T09:32:32Z","pushed_at":"2016-03-03T10:33:02Z","git_url":"git://github.com/ChengXiangLi/flink.git","ssh_url":"git@github.com:ChengXiangLi/flink.git","clone_url":"https://github.com/ChengXiangLi/flink.git","svn_url":"https://github.com/ChengXiangLi/flink","homepage":null,"size":81591,"stargazers_count":0,"watchers_count":0,"language":"Java","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"apache:tableOnCalcite","ref":"tableOnCalcite","sha":"d940c36639f31fa36b062a4683300af3b11a2ccb","user":{"login":"apache","id":47359,"avatar_url":"https://avatars.githubusercontent.com/u/47359?v=3","gravatar_id":"","url":"https://api.github.com/users/apache","html_url":"https://github.com/apache","followers_url":"https://api.github.com/users/apache/followers","following_url":"https://api.github.com/users/apache/following{/other_user}","gists_url":"https://api.github.com/users/apache/gists{/gist_id}","starred_url":"https://api.github.com/users/apache/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/apache/subscriptions","organizations_url":"https://api.github.com/users/apache/orgs","repos_url":"https://api.github.com/users/apache/repos","events_url":"https://api.github.com/users/apache/events{/privacy}","received_events_url":"https://api.github.com/users/apache/received_events","type":"Organization","site_admin":false},"repo":{"id":20587599,"name":"flink","full_name":"apache/flink","owner":{"login":"apache","id":47359,"avatar_url":"https://avatars.githubusercontent.com/u/47359?v=3","gravatar_id":"","url":"https://api.github.com/users/apache","html_url":"https://github.com/apache","followers_url":"https://api.github.com/users/apache/followers","following_url":"https://api.github.com/users/apache/following{/other_user}","gists_url":"https://api.github.com/users/apache/gists{/gist_id}","starred_url":"https://api.github.com/users/apache/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/apache/subscriptions","organizations_url":"https://api.github.com/users/apache/orgs","repos_url":"https://api.github.com/users/apache/repos","events_url":"https://api.github.com/users/apache/events{/privacy}","received_events_url":"https://api.github.com/users/apache/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/apache/flink","description":"Mirror of Apache Flink","fork":false,"url":"https://api.github.com/repos/apache/flink","forks_url":"https://api.github.com/repos/apache/flink/forks","keys_url":"https://api.github.com/repos/apache/flink/keys{/key_id}","collaborators_url":"https://api.github.com/repos/apache/flink/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/apache/flink/teams","hooks_url":"https://api.github.com/repos/apache/flink/hooks","issue_events_url":"https://api.github.com/repos/apache/flink/issues/events{/number}","events_url":"https://api.github.com/repos/apache/flink/events","assignees_url":"https://api.github.com/repos/apache/flink/assignees{/user}","branches_url":"https://api.github.com/repos/apache/flink/branches{/branch}","tags_url":"https://api.github.com/repos/apache/flink/tags","blobs_url":"https://api.github.com/repos/apache/flink/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/apache/flink/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/apache/flink/git/refs{/sha}","trees_url":"https://api.github.com/repos/apache/flink/git/trees{/sha}","statuses_url":"https://api.github.com/repos/apache/flink/statuses/{sha}","languages_url":"https://api.github.com/repos/apache/flink/languages","stargazers_url":"https://api.github.com/repos/apache/flink/stargazers","contributors_url":"https://api.github.com/repos/apache/flink/contributors","subscribers_url":"https://api.github.com/repos/apache/flink/subscribers","subscription_url":"https://api.github.com/repos/apache/flink/subscription","commits_url":"https://api.github.com/repos/apache/flink/commits{/sha}","git_commits_url":"https://api.github.com/repos/apache/flink/git/commits{/sha}","comments_url":"https://api.github.com/repos/apache/flink/comments{/number}","issue_comment_url":"https://api.github.com/repos/apache/flink/issues/comments{/number}","contents_url":"https://api.github.com/repos/apache/flink/contents/{+path}","compare_url":"https://api.github.com/repos/apache/flink/compare/{base}...{head}","merges_url":"https://api.github.com/repos/apache/flink/merges","archive_url":"https://api.github.com/repos/apache/flink/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/apache/flink/downloads","issues_url":"https://api.github.com/repos/apache/flink/issues{/number}","pulls_url":"https://api.github.com/repos/apache/flink/pulls{/number}","milestones_url":"https://api.github.com/repos/apache/flink/milestones{/number}","notifications_url":"https://api.github.com/repos/apache/flink/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/apache/flink/labels{/name}","releases_url":"https://api.github.com/repos/apache/flink/releases{/id}","deployments_url":"https://api.github.com/repos/apache/flink/deployments","created_at":"2014-06-07T07:00:10Z","updated_at":"2016-03-03T16:33:22Z","pushed_at":"2016-03-04T09:36:27Z","git_url":"git://github.com/apache/flink.git","ssh_url":"git@github.com:apache/flink.git","clone_url":"https://github.com/apache/flink.git","svn_url":"https://github.com/apache/flink","homepage":null,"size":107108,"stargazers_count":1035,"watchers_count":1035,"language":"Java","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":663,"mirror_url":"git://git.apache.org/flink.git","open_issues_count":90,"forks":663,"open_issues":90,"watchers":1035,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/apache/flink/pulls/1746"},"html":{"href":"https://github.com/apache/flink/pull/1746"},"issue":{"href":"https://api.github.com/repos/apache/flink/issues/1746"},"comments":{"href":"https://api.github.com/repos/apache/flink/issues/1746/comments"},"review_comments":{"href":"https://api.github.com/repos/apache/flink/pulls/1746/comments"},"review_comment":{"href":"https://api.github.com/repos/apache/flink/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/apache/flink/pulls/1746/commits"},"statuses":{"href":"https://api.github.com/repos/apache/flink/statuses/4ac49d746bfd45ee4ee529e228d5ba8ec2beb834"}}}}