{"action":"created","issue":{"url":"https://api.github.com/repos/apache/spark/issues/21381","repository_url":"https://api.github.com/repos/apache/spark","labels_url":"https://api.github.com/repos/apache/spark/issues/21381/labels{/name}","comments_url":"https://api.github.com/repos/apache/spark/issues/21381/comments","events_url":"https://api.github.com/repos/apache/spark/issues/21381/events","html_url":"https://github.com/apache/spark/pull/21381","id":324908866,"node_id":"MDExOlB1bGxSZXF1ZXN0MTg5MzczNTcy","number":21381,"title":"[SPARK-24330][SQL]Refactor ExecuteWriteTask and Use `while` in writing files","user":{"login":"gengliangwang","id":1097932,"node_id":"MDQ6VXNlcjEwOTc5MzI=","avatar_url":"https://avatars0.githubusercontent.com/u/1097932?v=4","gravatar_id":"","url":"https://api.github.com/users/gengliangwang","html_url":"https://github.com/gengliangwang","followers_url":"https://api.github.com/users/gengliangwang/followers","following_url":"https://api.github.com/users/gengliangwang/following{/other_user}","gists_url":"https://api.github.com/users/gengliangwang/gists{/gist_id}","starred_url":"https://api.github.com/users/gengliangwang/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gengliangwang/subscriptions","organizations_url":"https://api.github.com/users/gengliangwang/orgs","repos_url":"https://api.github.com/users/gengliangwang/repos","events_url":"https://api.github.com/users/gengliangwang/events{/privacy}","received_events_url":"https://api.github.com/users/gengliangwang/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":18,"created_at":"2018-05-21T13:05:02Z","updated_at":"2018-05-31T20:15:35Z","closed_at":null,"author_association":"CONTRIBUTOR","pull_request":{"url":"https://api.github.com/repos/apache/spark/pulls/21381","html_url":"https://github.com/apache/spark/pull/21381","diff_url":"https://github.com/apache/spark/pull/21381.diff","patch_url":"https://github.com/apache/spark/pull/21381.patch"},"body":"## What changes were proposed in this pull request?\r\n1. Refactor ExecuteWriteTask in FileFormatWriter to reduce common logic and improve readability.\r\nAfter the change, callers only need to call `commit()` or `abort` at the end of task.\r\nAlso there is less code in `SingleDirectoryWriteTask` and `DynamicPartitionWriteTask`.\r\nDefinitions of related classes are moved to a new file, and `ExecuteWriteTask` is renamed to `FileFormatDataWriter`.\r\n\r\n2. As per code style guide: https://github.com/databricks/scala-style-guide#traversal-and-zipwithindex , we avoid using `for` for looping in [FileFormatWriter](https://github.com/apache/spark/pull/21381/files#diff-3b69eb0963b68c65cfe8075f8a42e850L536) , or `foreach` in [WriteToDataSourceV2Exec](https://github.com/apache/spark/pull/21381/files#diff-6fbe10db766049a395bae2e785e9d56eL119).\r\nIn such critical code path, using `while` is good for performance.\r\n\r\n## How was this patch tested?\r\nExisting unit test.\r\nI tried the microbenchmark in https://github.com/apache/spark/pull/21409\r\n\r\n| Workload | Before changes(Best/Avg Time(ms)) | After changes(Best/Avg Time(ms)) |\r\n| --- | --- | -- |\r\n|Output Single Int Column|    2018 / 2043   |    2096 / 2236 |\r\n|Output Single Double Column| 1978 / 2043 | 2013 / 2018 |\r\n|Output Int and String Column| 6332 / 6706 | 6162 / 6298 |\r\n|Output Partitions| 4458 / 5094 |  3792 / 4008  |\r\n|Output Buckets|           5695 / 6102 |    5120 / 5154 |\r\n\r\nAlso a microbenchmark on my laptop for general comparison among while/foreach/for :\r\n```\r\nclass Writer {\r\n  var sum = 0L\r\n  def write(l: Long): Unit = sum += l\r\n}\r\n\r\ndef testWhile(iterator: Iterator[Long]): Long = {\r\n  val w = new Writer\r\n  while (iterator.hasNext) {\r\n    w.write(iterator.next())\r\n  }\r\n  w.sum\r\n}\r\n\r\ndef testForeach(iterator: Iterator[Long]): Long = {\r\n  val w = new Writer\r\n  iterator.foreach(w.write)\r\n  w.sum\r\n}\r\n\r\ndef testFor(iterator: Iterator[Long]): Long = {\r\n  val w = new Writer\r\n  for (x <- iterator) {\r\n    w.write(x)\r\n  }\r\n  w.sum\r\n}\r\n\r\nval data = 0L to 100000000L\r\nval start = System.nanoTime\r\n(0 to 10).foreach(_ => testWhile(data.iterator))\r\nprintln(\"benchmark while: \" + (System.nanoTime - start)/1000000)\r\n\r\nval start2 = System.nanoTime\r\n(0 to 10).foreach(_ => testForeach(data.iterator))\r\nprintln(\"benchmark foreach: \" + (System.nanoTime - start2)/1000000)\r\n\r\nval start3 = System.nanoTime\r\n(0 to 10).foreach(_ => testForeach(data.iterator))\r\nprintln(\"benchmark for: \" + (System.nanoTime - start3)/1000000)\r\n```\r\nBenchmark result:\r\n`while`: 15401 ms\r\n`foreach`: 43034 ms\r\n`for`: 41279 ms\r\n"},"comment":{"url":"https://api.github.com/repos/apache/spark/issues/comments/393665501","html_url":"https://github.com/apache/spark/pull/21381#issuecomment-393665501","issue_url":"https://api.github.com/repos/apache/spark/issues/21381","id":393665501,"node_id":"MDEyOklzc3VlQ29tbWVudDM5MzY2NTUwMQ==","user":{"login":"AmplabJenkins","id":4031291,"node_id":"MDQ6VXNlcjQwMzEyOTE=","avatar_url":"https://avatars3.githubusercontent.com/u/4031291?v=4","gravatar_id":"","url":"https://api.github.com/users/AmplabJenkins","html_url":"https://github.com/AmplabJenkins","followers_url":"https://api.github.com/users/AmplabJenkins/followers","following_url":"https://api.github.com/users/AmplabJenkins/following{/other_user}","gists_url":"https://api.github.com/users/AmplabJenkins/gists{/gist_id}","starred_url":"https://api.github.com/users/AmplabJenkins/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/AmplabJenkins/subscriptions","organizations_url":"https://api.github.com/users/AmplabJenkins/orgs","repos_url":"https://api.github.com/users/AmplabJenkins/repos","events_url":"https://api.github.com/users/AmplabJenkins/events{/privacy}","received_events_url":"https://api.github.com/users/AmplabJenkins/received_events","type":"User","site_admin":false},"created_at":"2018-05-31T20:15:35Z","updated_at":"2018-05-31T20:15:35Z","author_association":"NONE","body":"Merged build finished. Test PASSed."}}
{"actor":{"display_login":"AmplabJenkins"}}