{"action":"created","comment":{"url":"https://api.github.com/repos/apache/spark/pulls/comments/27836566","id":27836566,"diff_hunk":"@@ -94,11 +178,95 @@ trait ScalaReflection {\n     case (other, _) => other\n   }\n \n+  /**\n+   * Creates a converter function that will convert Catalyst types to Scala type.\n+   */\n+  private[sql] def createScalaConverter(dataType: DataType): Any => Any = dataType match {\n+    // Check UDT first since UDTs can override other types\n+    case udt: UserDefinedType[_] =>\n+      (item: Any) => udt.deserialize(item)\n+\n+    case arrayType: ArrayType =>\n+      val elementConverter = createScalaConverter(arrayType.elementType)\n+      (item: Any) => item match {\n+        case s: Seq[_] => s.map(elementConverter)\n+        case other => other\n+      }\n+\n+    case mapType: MapType =>\n+      val keyConverter = createScalaConverter(mapType.keyType)\n+      val valueConverter = createScalaConverter(mapType.valueType)\n+      (item: Any) =>\n+        item match {\n+          case m: Map[_, _] => m.map { case (k, v) =>\n+            keyConverter(k) -> valueConverter(v)\n+          }\n+          case other => other\n+        }\n+\n+    case s: StructType =>\n+      val converters = createConvertersForStruct(s)\n+      (item: Any) =>\n+        item match {\n+          case r: Row => convertRowToScalaWithConverters(r, s, converters)\n+          case other => other\n+        }\n+\n+    case _: DecimalType =>\n+      (item: Any) => item match {\n+        case d: Decimal => d.toJavaBigDecimal\n+        case other => other\n+      }\n+\n+    case DateType =>\n+      (item: Any) => item match {\n+        case i: Int => DateUtils.toJavaDate(i)\n+        case other => other\n+      }\n+\n+    case other =>\n+      (item: Any) => item\n+  }\n+\n   def convertRowToScala(r: Row, schema: StructType): Row = {\n-    // TODO: This is very slow!!!\n-    new GenericRowWithSchema(\n-      r.toSeq.zip(schema.fields.map(_.dataType))\n-        .map(r_dt => convertToScala(r_dt._1, r_dt._2)).toArray, schema)\n+    val ar = new Array[Any](r.size)\n+    var idx = 0\n+    while (idx < r.size) {\n+      ar(idx) = convertToScala(r(idx), schema.fields(idx).dataType)\n+      idx += 1\n+    }\n+    new GenericRowWithSchema(ar, schema)\n+  }\n+\n+  /**\n+   * Creates Catalyst->Scala converter functions for each field of the given StructType.\n+   */\n+  private[sql] def createConvertersForStruct(s: StructType): Array[Any => Any] = {\n+    val converters = new Array[Any => Any](s.length)\n+    val iter = s.fields.iterator\n+    var idx = 0\n+    while (iter.hasNext) {","path":"sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/ScalaReflection.scala","position":166,"original_position":166,"commit_id":"8cad6e28cbad1b42bc13225ed6f5fd7edb0601b5","original_commit_id":"8cad6e28cbad1b42bc13225ed6f5fd7edb0601b5","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":"nit: you could use `zipWIthIndex` to avoid the while loop.","created_at":"2015-04-06T22:23:45Z","updated_at":"2015-04-06T22:23:45Z","html_url":"https://github.com/apache/spark/pull/5279#discussion_r27836566","pull_request_url":"https://api.github.com/repos/apache/spark/pulls/5279","_links":{"self":{"href":"https://api.github.com/repos/apache/spark/pulls/comments/27836566"},"html":{"href":"https://github.com/apache/spark/pull/5279#discussion_r27836566"},"pull_request":{"href":"https://api.github.com/repos/apache/spark/pulls/5279"}}},"pull_request":{"url":"https://api.github.com/repos/apache/spark/pulls/5279","id":32287993,"html_url":"https://github.com/apache/spark/pull/5279","diff_url":"https://github.com/apache/spark/pull/5279.diff","patch_url":"https://github.com/apache/spark/pull/5279.patch","issue_url":"https://api.github.com/repos/apache/spark/issues/5279","number":5279,"state":"open","locked":false,"title":"[SPARK-6620] Speed up toDF() and rdd() functions by constructing converters in ScalaReflection","user":{"login":"vlyubin","id":3303322,"avatar_url":"https://avatars.githubusercontent.com/u/3303322?v=3","gravatar_id":"","url":"https://api.github.com/users/vlyubin","html_url":"https://github.com/vlyubin","followers_url":"https://api.github.com/users/vlyubin/followers","following_url":"https://api.github.com/users/vlyubin/following{/other_user}","gists_url":"https://api.github.com/users/vlyubin/gists{/gist_id}","starred_url":"https://api.github.com/users/vlyubin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vlyubin/subscriptions","organizations_url":"https://api.github.com/users/vlyubin/orgs","repos_url":"https://api.github.com/users/vlyubin/repos","events_url":"https://api.github.com/users/vlyubin/events{/privacy}","received_events_url":"https://api.github.com/users/vlyubin/received_events","type":"User","site_admin":false},"body":"cc @marmbrus","created_at":"2015-03-31T03:18:51Z","updated_at":"2015-04-06T22:23:45Z","closed_at":null,"merged_at":null,"merge_commit_sha":"51d27d0097ba704f188aae9b11f150718ecbb741","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/apache/spark/pulls/5279/commits","review_comments_url":"https://api.github.com/repos/apache/spark/pulls/5279/comments","review_comment_url":"https://api.github.com/repos/apache/spark/pulls/comments{/number}","comments_url":"https://api.github.com/repos/apache/spark/issues/5279/comments","statuses_url":"https://api.github.com/repos/apache/spark/statuses/8cad6e28cbad1b42bc13225ed6f5fd7edb0601b5","head":{"label":"vlyubin:speedup","ref":"speedup","sha":"8cad6e28cbad1b42bc13225ed6f5fd7edb0601b5","user":{"login":"vlyubin","id":3303322,"avatar_url":"https://avatars.githubusercontent.com/u/3303322?v=3","gravatar_id":"","url":"https://api.github.com/users/vlyubin","html_url":"https://github.com/vlyubin","followers_url":"https://api.github.com/users/vlyubin/followers","following_url":"https://api.github.com/users/vlyubin/following{/other_user}","gists_url":"https://api.github.com/users/vlyubin/gists{/gist_id}","starred_url":"https://api.github.com/users/vlyubin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vlyubin/subscriptions","organizations_url":"https://api.github.com/users/vlyubin/orgs","repos_url":"https://api.github.com/users/vlyubin/repos","events_url":"https://api.github.com/users/vlyubin/events{/privacy}","received_events_url":"https://api.github.com/users/vlyubin/received_events","type":"User","site_admin":false},"repo":{"id":31569399,"name":"spark","full_name":"vlyubin/spark","owner":{"login":"vlyubin","id":3303322,"avatar_url":"https://avatars.githubusercontent.com/u/3303322?v=3","gravatar_id":"","url":"https://api.github.com/users/vlyubin","html_url":"https://github.com/vlyubin","followers_url":"https://api.github.com/users/vlyubin/followers","following_url":"https://api.github.com/users/vlyubin/following{/other_user}","gists_url":"https://api.github.com/users/vlyubin/gists{/gist_id}","starred_url":"https://api.github.com/users/vlyubin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vlyubin/subscriptions","organizations_url":"https://api.github.com/users/vlyubin/orgs","repos_url":"https://api.github.com/users/vlyubin/repos","events_url":"https://api.github.com/users/vlyubin/events{/privacy}","received_events_url":"https://api.github.com/users/vlyubin/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/vlyubin/spark","description":"Mirror of Apache Spark","fork":true,"url":"https://api.github.com/repos/vlyubin/spark","forks_url":"https://api.github.com/repos/vlyubin/spark/forks","keys_url":"https://api.github.com/repos/vlyubin/spark/keys{/key_id}","collaborators_url":"https://api.github.com/repos/vlyubin/spark/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/vlyubin/spark/teams","hooks_url":"https://api.github.com/repos/vlyubin/spark/hooks","issue_events_url":"https://api.github.com/repos/vlyubin/spark/issues/events{/number}","events_url":"https://api.github.com/repos/vlyubin/spark/events","assignees_url":"https://api.github.com/repos/vlyubin/spark/assignees{/user}","branches_url":"https://api.github.com/repos/vlyubin/spark/branches{/branch}","tags_url":"https://api.github.com/repos/vlyubin/spark/tags","blobs_url":"https://api.github.com/repos/vlyubin/spark/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/vlyubin/spark/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/vlyubin/spark/git/refs{/sha}","trees_url":"https://api.github.com/repos/vlyubin/spark/git/trees{/sha}","statuses_url":"https://api.github.com/repos/vlyubin/spark/statuses/{sha}","languages_url":"https://api.github.com/repos/vlyubin/spark/languages","stargazers_url":"https://api.github.com/repos/vlyubin/spark/stargazers","contributors_url":"https://api.github.com/repos/vlyubin/spark/contributors","subscribers_url":"https://api.github.com/repos/vlyubin/spark/subscribers","subscription_url":"https://api.github.com/repos/vlyubin/spark/subscription","commits_url":"https://api.github.com/repos/vlyubin/spark/commits{/sha}","git_commits_url":"https://api.github.com/repos/vlyubin/spark/git/commits{/sha}","comments_url":"https://api.github.com/repos/vlyubin/spark/comments{/number}","issue_comment_url":"https://api.github.com/repos/vlyubin/spark/issues/comments{/number}","contents_url":"https://api.github.com/repos/vlyubin/spark/contents/{+path}","compare_url":"https://api.github.com/repos/vlyubin/spark/compare/{base}...{head}","merges_url":"https://api.github.com/repos/vlyubin/spark/merges","archive_url":"https://api.github.com/repos/vlyubin/spark/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/vlyubin/spark/downloads","issues_url":"https://api.github.com/repos/vlyubin/spark/issues{/number}","pulls_url":"https://api.github.com/repos/vlyubin/spark/pulls{/number}","milestones_url":"https://api.github.com/repos/vlyubin/spark/milestones{/number}","notifications_url":"https://api.github.com/repos/vlyubin/spark/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/vlyubin/spark/labels{/name}","releases_url":"https://api.github.com/repos/vlyubin/spark/releases{/id}","created_at":"2015-03-02T23:34:27Z","updated_at":"2015-03-26T19:38:32Z","pushed_at":"2015-04-06T19:43:58Z","git_url":"git://github.com/vlyubin/spark.git","ssh_url":"git@github.com:vlyubin/spark.git","clone_url":"https://github.com/vlyubin/spark.git","svn_url":"https://github.com/vlyubin/spark","homepage":null,"size":97456,"stargazers_count":0,"watchers_count":0,"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":0,"default_branch":"master"}},"base":{"label":"apache:master","ref":"master","sha":"9fe41252198df71f4629843d363db8c83f36440c","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-04-06T22:16:12Z","pushed_at":"2015-04-06T21:57:15Z","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":1455986,"stargazers_count":3512,"watchers_count":3512,"language":"Scala","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":3020,"mirror_url":"git://git.apache.org/spark.git","open_issues_count":311,"forks":3020,"open_issues":311,"watchers":3512,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/apache/spark/pulls/5279"},"html":{"href":"https://github.com/apache/spark/pull/5279"},"issue":{"href":"https://api.github.com/repos/apache/spark/issues/5279"},"comments":{"href":"https://api.github.com/repos/apache/spark/issues/5279/comments"},"review_comments":{"href":"https://api.github.com/repos/apache/spark/pulls/5279/comments"},"review_comment":{"href":"https://api.github.com/repos/apache/spark/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/apache/spark/pulls/5279/commits"},"statuses":{"href":"https://api.github.com/repos/apache/spark/statuses/8cad6e28cbad1b42bc13225ed6f5fd7edb0601b5"}}}}