{"action":"created","comment":{"url":"https://api.github.com/repos/apache/spark/pulls/comments/33419103","id":33419103,"diff_hunk":"@@ -70,67 +92,38 @@\n   private final boolean enablePerfMetrics;\n \n   /**\n-   * @return true if UnsafeFixedWidthAggregationMap supports grouping keys with the given schema,\n-   *         false otherwise.\n-   */\n-  public static boolean supportsGroupKeySchema(StructType schema) {\n-    for (StructField field: schema.fields()) {\n-      if (!UnsafeRow.readableFieldTypes.contains(field.dataType())) {\n-        return false;\n-      }\n-    }\n-    return true;\n-  }\n-\n-  /**\n-   * @return true if UnsafeFixedWidthAggregationMap supports aggregation buffers with the given\n-   *         schema, false otherwise.\n-   */\n-  public static boolean supportsAggregationBufferSchema(StructType schema) {\n-    for (StructField field: schema.fields()) {\n-      if (!UnsafeRow.settableFieldTypes.contains(field.dataType())) {\n-        return false;\n-      }\n-    }\n-    return true;\n-  }\n-\n-  /**\n    * Create a new UnsafeFixedWidthAggregationMap.\n    *\n-   * @param emptyAggregationBuffer the default value for new keys (a \"zero\" of the agg. function)\n-   * @param aggregationBufferSchema the schema of the aggregation buffer, used for row conversion.\n-   * @param groupingKeySchema the schema of the grouping key, used for row conversion.\n+   * @param initProjection the default value for new keys (a \"zero\" of the agg. function)\n+   * @param keyConverter the converter of the grouping key, used for row conversion.\n+   * @param bufferConverter the converter of the aggregation buffer, used for row conversion.\n    * @param memoryManager the memory manager used to allocate our Unsafe memory structures.\n    * @param initialCapacity the initial capacity of the map (a sizing hint to avoid re-hashing).\n    * @param enablePerfMetrics if true, performance metrics will be recorded (has minor perf impact)\n    */\n   public UnsafeFixedWidthAggregationMap(\n-      InternalRow emptyAggregationBuffer,\n-      StructType aggregationBufferSchema,\n-      StructType groupingKeySchema,\n+      Function1<InternalRow, InternalRow> initProjection,\n+      UnsafeRowConverter keyConverter,\n+      UnsafeRowConverter bufferConverter,\n       TaskMemoryManager memoryManager,\n       int initialCapacity,\n       boolean enablePerfMetrics) {\n-    this.emptyAggregationBuffer =\n-      convertToUnsafeRow(emptyAggregationBuffer, aggregationBufferSchema);\n-    this.aggregationBufferSchema = aggregationBufferSchema;\n-    this.groupingKeyToUnsafeRowConverter = new UnsafeRowConverter(groupingKeySchema);\n-    this.groupingKeySchema = groupingKeySchema;\n-    this.map = new BytesToBytesMap(memoryManager, initialCapacity, enablePerfMetrics);\n+    this.initProjection = initProjection;\n+    this.keyConverter = keyConverter;\n+    this.bufferConverter = bufferConverter;\n     this.enablePerfMetrics = enablePerfMetrics;\n-  }\n \n-  /**\n-   * Convert a Java object row into an UnsafeRow, allocating it into a new byte array.\n-   */\n-  private static byte[] convertToUnsafeRow(InternalRow javaRow, StructType schema) {\n-    final UnsafeRowConverter converter = new UnsafeRowConverter(schema);\n-    final byte[] unsafeRow = new byte[converter.getSizeRequirement(javaRow)];\n-    final int writtenLength =\n-      converter.writeRow(javaRow, unsafeRow, PlatformDependent.BYTE_ARRAY_OFFSET);\n-    assert (writtenLength == unsafeRow.length): \"Size requirement calculation was wrong!\";\n-    return unsafeRow;\n+    this.map = new BytesToBytesMap(memoryManager, initialCapacity, enablePerfMetrics);\n+    this.keyPool = new UniqueObjectPool(100);\n+    this.bufferPool = new ObjectPool(initialCapacity);\n+\n+    InternalRow initRow = initProjection.apply(emptyRow);\n+    this.emptyBuffer = new byte[bufferConverter.getSizeRequirement(initRow)];\n+    int writtenLength = bufferConverter.writeRow(\n+      initRow, emptyBuffer, PlatformDependent.BYTE_ARRAY_OFFSET, bufferPool);\n+    assert (writtenLength == emptyBuffer.length): \"Size requirement calculation was wrong!\";\n+    // re-use the empty buffer only when there is no object saved in pool.\n+    reuseEmptyBuffer = bufferPool.size() == 0;","path":"sql/catalyst/src/main/java/org/apache/spark/sql/catalyst/expressions/UnsafeFixedWidthAggregationMap.java","position":151,"original_position":151,"commit_id":"4859b80efa5f88c184b5d987a4c8bc3e9ac8a7b8","original_commit_id":"4859b80efa5f88c184b5d987a4c8bc3e9ac8a7b8","user":{"login":"davies","id":40902,"avatar_url":"https://avatars.githubusercontent.com/u/40902?v=3","gravatar_id":"","url":"https://api.github.com/users/davies","html_url":"https://github.com/davies","followers_url":"https://api.github.com/users/davies/followers","following_url":"https://api.github.com/users/davies/following{/other_user}","gists_url":"https://api.github.com/users/davies/gists{/gist_id}","starred_url":"https://api.github.com/users/davies/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/davies/subscriptions","organizations_url":"https://api.github.com/users/davies/orgs","repos_url":"https://api.github.com/users/davies/repos","events_url":"https://api.github.com/users/davies/events{/privacy}","received_events_url":"https://api.github.com/users/davies/received_events","type":"User","site_admin":false},"body":"change it to final","created_at":"2015-06-28T05:45:06Z","updated_at":"2015-06-28T05:45:06Z","html_url":"https://github.com/apache/spark/pull/6959#discussion_r33419103","pull_request_url":"https://api.github.com/repos/apache/spark/pulls/6959","_links":{"self":{"href":"https://api.github.com/repos/apache/spark/pulls/comments/33419103"},"html":{"href":"https://github.com/apache/spark/pull/6959#discussion_r33419103"},"pull_request":{"href":"https://api.github.com/repos/apache/spark/pulls/6959"}}},"pull_request":{"url":"https://api.github.com/repos/apache/spark/pulls/6959","id":38372738,"html_url":"https://github.com/apache/spark/pull/6959","diff_url":"https://github.com/apache/spark/pull/6959.diff","patch_url":"https://github.com/apache/spark/pull/6959.patch","issue_url":"https://api.github.com/repos/apache/spark/issues/6959","number":6959,"state":"open","locked":false,"title":"[SPARK-8579] [SQL] support arbitrary object in UnsafeRow","user":{"login":"davies","id":40902,"avatar_url":"https://avatars.githubusercontent.com/u/40902?v=3","gravatar_id":"","url":"https://api.github.com/users/davies","html_url":"https://github.com/davies","followers_url":"https://api.github.com/users/davies/followers","following_url":"https://api.github.com/users/davies/following{/other_user}","gists_url":"https://api.github.com/users/davies/gists{/gist_id}","starred_url":"https://api.github.com/users/davies/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/davies/subscriptions","organizations_url":"https://api.github.com/users/davies/orgs","repos_url":"https://api.github.com/users/davies/repos","events_url":"https://api.github.com/users/davies/events{/privacy}","received_events_url":"https://api.github.com/users/davies/received_events","type":"User","site_admin":false},"body":"This PR brings arbitrary object support in UnsafeRow (both in grouping key and aggregation buffer).\r\n\r\nTwo object pools will be created to hold those non-primitive objects, and put the index of them into UnsafeRow. In order to compare the grouping key as bytes, the objects in key will be stored in a unique object pool, to make sure same objects will have same index (used as hashCode).\r\n\r\nFor StringType and BinaryType, we still put them as var-length in UnsafeRow when initializing for better performance. But for update, they will be an object inside object pools (there will be some garbages left in the buffer).\r\n\r\nBTW: Will create a JIRA once issue.apache.org is available.\r\n\r\ncc @JoshRosen @rxin ","created_at":"2015-06-23T19:19:40Z","updated_at":"2015-06-28T05:45:06Z","closed_at":null,"merged_at":null,"merge_commit_sha":"3ccaeffcd43513e27d57b9b9344c1a72921c48ea","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/apache/spark/pulls/6959/commits","review_comments_url":"https://api.github.com/repos/apache/spark/pulls/6959/comments","review_comment_url":"https://api.github.com/repos/apache/spark/pulls/comments{/number}","comments_url":"https://api.github.com/repos/apache/spark/issues/6959/comments","statuses_url":"https://api.github.com/repos/apache/spark/statuses/4859b80efa5f88c184b5d987a4c8bc3e9ac8a7b8","head":{"label":"davies:unsafe_obj","ref":"unsafe_obj","sha":"4859b80efa5f88c184b5d987a4c8bc3e9ac8a7b8","user":{"login":"davies","id":40902,"avatar_url":"https://avatars.githubusercontent.com/u/40902?v=3","gravatar_id":"","url":"https://api.github.com/users/davies","html_url":"https://github.com/davies","followers_url":"https://api.github.com/users/davies/followers","following_url":"https://api.github.com/users/davies/following{/other_user}","gists_url":"https://api.github.com/users/davies/gists{/gist_id}","starred_url":"https://api.github.com/users/davies/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/davies/subscriptions","organizations_url":"https://api.github.com/users/davies/orgs","repos_url":"https://api.github.com/users/davies/repos","events_url":"https://api.github.com/users/davies/events{/privacy}","received_events_url":"https://api.github.com/users/davies/received_events","type":"User","site_admin":false},"repo":{"id":21724875,"name":"spark","full_name":"davies/spark","owner":{"login":"davies","id":40902,"avatar_url":"https://avatars.githubusercontent.com/u/40902?v=3","gravatar_id":"","url":"https://api.github.com/users/davies","html_url":"https://github.com/davies","followers_url":"https://api.github.com/users/davies/followers","following_url":"https://api.github.com/users/davies/following{/other_user}","gists_url":"https://api.github.com/users/davies/gists{/gist_id}","starred_url":"https://api.github.com/users/davies/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/davies/subscriptions","organizations_url":"https://api.github.com/users/davies/orgs","repos_url":"https://api.github.com/users/davies/repos","events_url":"https://api.github.com/users/davies/events{/privacy}","received_events_url":"https://api.github.com/users/davies/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/davies/spark","description":"Mirror of Apache Spark","fork":true,"url":"https://api.github.com/repos/davies/spark","forks_url":"https://api.github.com/repos/davies/spark/forks","keys_url":"https://api.github.com/repos/davies/spark/keys{/key_id}","collaborators_url":"https://api.github.com/repos/davies/spark/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/davies/spark/teams","hooks_url":"https://api.github.com/repos/davies/spark/hooks","issue_events_url":"https://api.github.com/repos/davies/spark/issues/events{/number}","events_url":"https://api.github.com/repos/davies/spark/events","assignees_url":"https://api.github.com/repos/davies/spark/assignees{/user}","branches_url":"https://api.github.com/repos/davies/spark/branches{/branch}","tags_url":"https://api.github.com/repos/davies/spark/tags","blobs_url":"https://api.github.com/repos/davies/spark/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/davies/spark/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/davies/spark/git/refs{/sha}","trees_url":"https://api.github.com/repos/davies/spark/git/trees{/sha}","statuses_url":"https://api.github.com/repos/davies/spark/statuses/{sha}","languages_url":"https://api.github.com/repos/davies/spark/languages","stargazers_url":"https://api.github.com/repos/davies/spark/stargazers","contributors_url":"https://api.github.com/repos/davies/spark/contributors","subscribers_url":"https://api.github.com/repos/davies/spark/subscribers","subscription_url":"https://api.github.com/repos/davies/spark/subscription","commits_url":"https://api.github.com/repos/davies/spark/commits{/sha}","git_commits_url":"https://api.github.com/repos/davies/spark/git/commits{/sha}","comments_url":"https://api.github.com/repos/davies/spark/comments{/number}","issue_comment_url":"https://api.github.com/repos/davies/spark/issues/comments{/number}","contents_url":"https://api.github.com/repos/davies/spark/contents/{+path}","compare_url":"https://api.github.com/repos/davies/spark/compare/{base}...{head}","merges_url":"https://api.github.com/repos/davies/spark/merges","archive_url":"https://api.github.com/repos/davies/spark/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/davies/spark/downloads","issues_url":"https://api.github.com/repos/davies/spark/issues{/number}","pulls_url":"https://api.github.com/repos/davies/spark/pulls{/number}","milestones_url":"https://api.github.com/repos/davies/spark/milestones{/number}","notifications_url":"https://api.github.com/repos/davies/spark/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/davies/spark/labels{/name}","releases_url":"https://api.github.com/repos/davies/spark/releases{/id}","created_at":"2014-07-11T06:39:41Z","updated_at":"2015-04-08T19:45:01Z","pushed_at":"2015-06-28T05:06:52Z","git_url":"git://github.com/davies/spark.git","ssh_url":"git@github.com:davies/spark.git","clone_url":"https://github.com/davies/spark.git","svn_url":"https://github.com/davies/spark","homepage":null,"size":116248,"stargazers_count":1,"watchers_count":1,"language":"Scala","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":1,"default_branch":"master"}},"base":{"label":"apache:master","ref":"master","sha":"a8031183aff2e23de9204ddfc7e7f5edbf052a7e","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":17165658,"name":"spark","full_name":"apache/spark","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/spark","description":"Mirror of Apache Spark","fork":false,"url":"https://api.github.com/repos/apache/spark","forks_url":"https://api.github.com/repos/apache/spark/forks","keys_url":"https://api.github.com/repos/apache/spark/keys{/key_id}","collaborators_url":"https://api.github.com/repos/apache/spark/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/apache/spark/teams","hooks_url":"https://api.github.com/repos/apache/spark/hooks","issue_events_url":"https://api.github.com/repos/apache/spark/issues/events{/number}","events_url":"https://api.github.com/repos/apache/spark/events","assignees_url":"https://api.github.com/repos/apache/spark/assignees{/user}","branches_url":"https://api.github.com/repos/apache/spark/branches{/branch}","tags_url":"https://api.github.com/repos/apache/spark/tags","blobs_url":"https://api.github.com/repos/apache/spark/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/apache/spark/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/apache/spark/git/refs{/sha}","trees_url":"https://api.github.com/repos/apache/spark/git/trees{/sha}","statuses_url":"https://api.github.com/repos/apache/spark/statuses/{sha}","languages_url":"https://api.github.com/repos/apache/spark/languages","stargazers_url":"https://api.github.com/repos/apache/spark/stargazers","contributors_url":"https://api.github.com/repos/apache/spark/contributors","subscribers_url":"https://api.github.com/repos/apache/spark/subscribers","subscription_url":"https://api.github.com/repos/apache/spark/subscription","commits_url":"https://api.github.com/repos/apache/spark/commits{/sha}","git_commits_url":"https://api.github.com/repos/apache/spark/git/commits{/sha}","comments_url":"https://api.github.com/repos/apache/spark/comments{/number}","issue_comment_url":"https://api.github.com/repos/apache/spark/issues/comments{/number}","contents_url":"https://api.github.com/repos/apache/spark/contents/{+path}","compare_url":"https://api.github.com/repos/apache/spark/compare/{base}...{head}","merges_url":"https://api.github.com/repos/apache/spark/merges","archive_url":"https://api.github.com/repos/apache/spark/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/apache/spark/downloads","issues_url":"https://api.github.com/repos/apache/spark/issues{/number}","pulls_url":"https://api.github.com/repos/apache/spark/pulls{/number}","milestones_url":"https://api.github.com/repos/apache/spark/milestones{/number}","notifications_url":"https://api.github.com/repos/apache/spark/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/apache/spark/labels{/name}","releases_url":"https://api.github.com/repos/apache/spark/releases{/id}","created_at":"2014-02-25T08:00:08Z","updated_at":"2015-06-28T04:50:41Z","pushed_at":"2015-06-28T05:06:53Z","git_url":"git://github.com/apache/spark.git","ssh_url":"git@github.com:apache/spark.git","clone_url":"https://github.com/apache/spark.git","svn_url":"https://github.com/apache/spark","homepage":null,"size":1526359,"stargazers_count":4560,"watchers_count":4560,"language":"Scala","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":4019,"mirror_url":"git://git.apache.org/spark.git","open_issues_count":375,"forks":4019,"open_issues":375,"watchers":4560,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/apache/spark/pulls/6959"},"html":{"href":"https://github.com/apache/spark/pull/6959"},"issue":{"href":"https://api.github.com/repos/apache/spark/issues/6959"},"comments":{"href":"https://api.github.com/repos/apache/spark/issues/6959/comments"},"review_comments":{"href":"https://api.github.com/repos/apache/spark/pulls/6959/comments"},"review_comment":{"href":"https://api.github.com/repos/apache/spark/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/apache/spark/pulls/6959/commits"},"statuses":{"href":"https://api.github.com/repos/apache/spark/statuses/4859b80efa5f88c184b5d987a4c8bc3e9ac8a7b8"}}}}