{"action":"created","comment":{"url":"https://api.github.com/repos/apache/spark/pulls/comments/214832531","pull_request_review_id":151965409,"id":214832531,"node_id":"MDI0OlB1bGxSZXF1ZXN0UmV2aWV3Q29tbWVudDIxNDgzMjUzMQ==","diff_hunk":"@@ -54,27 +55,27 @@ import org.apache.spark.sql.types._\n  * builder methods mentioned above can only be found in test code, where all tested filters are\n  * known to be convertible.\n  */\n-private[orc] object OrcFilters {\n+private[sql] object OrcFilters {\n+  private[sql] def buildTree(filters: Seq[Filter]): Option[Filter] = {\n+    filters match {\n+      case Seq() => None\n+      case Seq(filter) => Some(filter)\n+      case Seq(filter1, filter2) => Some(And(filter1, filter2))\n+      case _ => // length > 2\n+        val (left, right) = filters.splitAt(filters.length / 2)\n+        Some(And(buildTree(left).get, buildTree(right).get))\n+    }\n+  }\n \n   /**\n    * Create ORC filter as a SearchArgument instance.\n    */\n   def createFilter(schema: StructType, filters: Seq[Filter]): Option[SearchArgument] = {\n     val dataTypeMap = schema.map(f => f.name -> f.dataType).toMap\n \n-    // First, tries to convert each filter individually to see whether it's convertible, and then\n-    // collect all convertible ones to build the final `SearchArgument`.\n-    val convertibleFilters = for {\n-      filter <- filters\n-      _ <- buildSearchArgument(dataTypeMap, filter, SearchArgumentFactory.newBuilder())\n-    } yield filter\n-\n-    for {\n-      // Combines all convertible filters using `And` to produce a single conjunction\n-      conjunction <- convertibleFilters.reduceOption(org.apache.spark.sql.sources.And)\n-      // Then tries to build a single ORC `SearchArgument` for the conjunction predicate\n-      builder <- buildSearchArgument(dataTypeMap, conjunction, SearchArgumentFactory.newBuilder())\n-    } yield builder.build()\n+    buildTree(filters.filter(buildSearchArgument(dataTypeMap, _, newBuilder).isDefined))\n+      .flatMap(buildSearchArgument(dataTypeMap, _, newBuilder))\n+      .map(_.build)","path":"sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/orc/OrcFilters.scala","position":53,"original_position":53,"commit_id":"4a372a328b33961a16ae6ad69bb58ba0720e9b63","original_commit_id":"4a372a328b33961a16ae6ad69bb58ba0720e9b63","user":{"login":"dongjoon-hyun","id":9700541,"node_id":"MDQ6VXNlcjk3MDA1NDE=","avatar_url":"https://avatars2.githubusercontent.com/u/9700541?v=4","gravatar_id":"","url":"https://api.github.com/users/dongjoon-hyun","html_url":"https://github.com/dongjoon-hyun","followers_url":"https://api.github.com/users/dongjoon-hyun/followers","following_url":"https://api.github.com/users/dongjoon-hyun/following{/other_user}","gists_url":"https://api.github.com/users/dongjoon-hyun/gists{/gist_id}","starred_url":"https://api.github.com/users/dongjoon-hyun/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dongjoon-hyun/subscriptions","organizations_url":"https://api.github.com/users/dongjoon-hyun/orgs","repos_url":"https://api.github.com/users/dongjoon-hyun/repos","events_url":"https://api.github.com/users/dongjoon-hyun/events{/privacy}","received_events_url":"https://api.github.com/users/dongjoon-hyun/received_events","type":"User","site_admin":false},"body":"Sure. No problem, @cloud-fan . :)","created_at":"2018-09-04T08:44:09Z","updated_at":"2018-09-04T08:44:09Z","html_url":"https://github.com/apache/spark/pull/22313#discussion_r214832531","pull_request_url":"https://api.github.com/repos/apache/spark/pulls/22313","author_association":"MEMBER","_links":{"self":{"href":"https://api.github.com/repos/apache/spark/pulls/comments/214832531"},"html":{"href":"https://github.com/apache/spark/pull/22313#discussion_r214832531"},"pull_request":{"href":"https://api.github.com/repos/apache/spark/pulls/22313"}},"in_reply_to_id":214810021},"pull_request":{"url":"https://api.github.com/repos/apache/spark/pulls/22313","id":212567265,"node_id":"MDExOlB1bGxSZXF1ZXN0MjEyNTY3MjY1","html_url":"https://github.com/apache/spark/pull/22313","diff_url":"https://github.com/apache/spark/pull/22313.diff","patch_url":"https://github.com/apache/spark/pull/22313.patch","issue_url":"https://api.github.com/repos/apache/spark/issues/22313","number":22313,"state":"open","locked":false,"title":"[SPARK-25306][SQL] Avoid skewed filter trees to speed up `createFilter` in ORC","user":{"login":"dongjoon-hyun","id":9700541,"node_id":"MDQ6VXNlcjk3MDA1NDE=","avatar_url":"https://avatars2.githubusercontent.com/u/9700541?v=4","gravatar_id":"","url":"https://api.github.com/users/dongjoon-hyun","html_url":"https://github.com/dongjoon-hyun","followers_url":"https://api.github.com/users/dongjoon-hyun/followers","following_url":"https://api.github.com/users/dongjoon-hyun/following{/other_user}","gists_url":"https://api.github.com/users/dongjoon-hyun/gists{/gist_id}","starred_url":"https://api.github.com/users/dongjoon-hyun/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dongjoon-hyun/subscriptions","organizations_url":"https://api.github.com/users/dongjoon-hyun/orgs","repos_url":"https://api.github.com/users/dongjoon-hyun/repos","events_url":"https://api.github.com/users/dongjoon-hyun/events{/privacy}","received_events_url":"https://api.github.com/users/dongjoon-hyun/received_events","type":"User","site_admin":false},"body":"## What changes were proposed in this pull request?\r\n\r\nIn both ORC data sources, `createFilter` function has exponential time complexity due to its skewed filter tree generation. This PR aims to improve it by using new `buildTree` function.\r\n\r\n**REPRODUCE**\r\n```scala\r\n// Create and read 1 row table with 1000 columns\r\nsql(\"set spark.sql.orc.filterPushdown=true\")\r\nval selectExpr = (1 to 1000).map(i => s\"id c$i\")\r\nspark.range(1).selectExpr(selectExpr: _*).write.mode(\"overwrite\").orc(\"/tmp/orc\")\r\nprint(s\"With 0 filters, \")\r\nspark.time(spark.read.orc(\"/tmp/orc\").count)\r\n\r\n// Increase the number of filters\r\n(20 to 30).foreach { width =>\r\n  val whereExpr = (1 to width).map(i => s\"c$i is not null\").mkString(\" and \")\r\n  print(s\"With $width filters, \")\r\n  spark.time(spark.read.orc(\"/tmp/orc\").where(whereExpr).count)\r\n}\r\n```\r\n\r\n**RESULT**\r\n```scala\r\nWith 0 filters, Time taken: 653 ms                                              \r\nWith 20 filters, Time taken: 962 ms\r\nWith 21 filters, Time taken: 1282 ms\r\nWith 22 filters, Time taken: 1982 ms\r\nWith 23 filters, Time taken: 3855 ms\r\nWith 24 filters, Time taken: 6719 ms\r\nWith 25 filters, Time taken: 12669 ms\r\nWith 26 filters, Time taken: 25032 ms\r\nWith 27 filters, Time taken: 49585 ms\r\nWith 28 filters, Time taken: 98980 ms    // over 1 min 38 seconds\r\nWith 29 filters, Time taken: 198368 ms   // over 3 mins\r\nWith 30 filters, Time taken: 393744 ms   // over 6 mins\r\n```\r\n\r\n**AFTER THIS PR**\r\n```scala\r\nWith 0 filters, Time taken: 774 ms\r\nWith 20 filters, Time taken: 601 ms\r\nWith 21 filters, Time taken: 399 ms\r\nWith 22 filters, Time taken: 679 ms\r\nWith 23 filters, Time taken: 363 ms\r\nWith 24 filters, Time taken: 342 ms\r\nWith 25 filters, Time taken: 336 ms\r\nWith 26 filters, Time taken: 352 ms\r\nWith 27 filters, Time taken: 322 ms\r\nWith 28 filters, Time taken: 302 ms\r\nWith 29 filters, Time taken: 307 ms\r\nWith 30 filters, Time taken: 301 ms\r\n```\r\n\r\n## How was this patch tested?\r\n\r\nPass the Jenkins with newly added test cases.","created_at":"2018-09-01T22:35:12Z","updated_at":"2018-09-04T08:44:09Z","closed_at":null,"merged_at":null,"merge_commit_sha":"c62c85c384419751944e0cefb0ac0c39e81cb9a2","assignee":null,"assignees":[],"requested_reviewers":[],"requested_teams":[],"labels":[],"milestone":null,"commits_url":"https://api.github.com/repos/apache/spark/pulls/22313/commits","review_comments_url":"https://api.github.com/repos/apache/spark/pulls/22313/comments","review_comment_url":"https://api.github.com/repos/apache/spark/pulls/comments{/number}","comments_url":"https://api.github.com/repos/apache/spark/issues/22313/comments","statuses_url":"https://api.github.com/repos/apache/spark/statuses/4a372a328b33961a16ae6ad69bb58ba0720e9b63","head":{"label":"dongjoon-hyun:SPARK-25306","ref":"SPARK-25306","sha":"4a372a328b33961a16ae6ad69bb58ba0720e9b63","user":{"login":"dongjoon-hyun","id":9700541,"node_id":"MDQ6VXNlcjk3MDA1NDE=","avatar_url":"https://avatars2.githubusercontent.com/u/9700541?v=4","gravatar_id":"","url":"https://api.github.com/users/dongjoon-hyun","html_url":"https://github.com/dongjoon-hyun","followers_url":"https://api.github.com/users/dongjoon-hyun/followers","following_url":"https://api.github.com/users/dongjoon-hyun/following{/other_user}","gists_url":"https://api.github.com/users/dongjoon-hyun/gists{/gist_id}","starred_url":"https://api.github.com/users/dongjoon-hyun/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dongjoon-hyun/subscriptions","organizations_url":"https://api.github.com/users/dongjoon-hyun/orgs","repos_url":"https://api.github.com/users/dongjoon-hyun/repos","events_url":"https://api.github.com/users/dongjoon-hyun/events{/privacy}","received_events_url":"https://api.github.com/users/dongjoon-hyun/received_events","type":"User","site_admin":false},"repo":{"id":28900400,"node_id":"MDEwOlJlcG9zaXRvcnkyODkwMDQwMA==","name":"spark","full_name":"dongjoon-hyun/spark","owner":{"login":"dongjoon-hyun","id":9700541,"node_id":"MDQ6VXNlcjk3MDA1NDE=","avatar_url":"https://avatars2.githubusercontent.com/u/9700541?v=4","gravatar_id":"","url":"https://api.github.com/users/dongjoon-hyun","html_url":"https://github.com/dongjoon-hyun","followers_url":"https://api.github.com/users/dongjoon-hyun/followers","following_url":"https://api.github.com/users/dongjoon-hyun/following{/other_user}","gists_url":"https://api.github.com/users/dongjoon-hyun/gists{/gist_id}","starred_url":"https://api.github.com/users/dongjoon-hyun/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dongjoon-hyun/subscriptions","organizations_url":"https://api.github.com/users/dongjoon-hyun/orgs","repos_url":"https://api.github.com/users/dongjoon-hyun/repos","events_url":"https://api.github.com/users/dongjoon-hyun/events{/privacy}","received_events_url":"https://api.github.com/users/dongjoon-hyun/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/dongjoon-hyun/spark","description":"Mirror of Apache Spark","fork":true,"url":"https://api.github.com/repos/dongjoon-hyun/spark","forks_url":"https://api.github.com/repos/dongjoon-hyun/spark/forks","keys_url":"https://api.github.com/repos/dongjoon-hyun/spark/keys{/key_id}","collaborators_url":"https://api.github.com/repos/dongjoon-hyun/spark/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/dongjoon-hyun/spark/teams","hooks_url":"https://api.github.com/repos/dongjoon-hyun/spark/hooks","issue_events_url":"https://api.github.com/repos/dongjoon-hyun/spark/issues/events{/number}","events_url":"https://api.github.com/repos/dongjoon-hyun/spark/events","assignees_url":"https://api.github.com/repos/dongjoon-hyun/spark/assignees{/user}","branches_url":"https://api.github.com/repos/dongjoon-hyun/spark/branches{/branch}","tags_url":"https://api.github.com/repos/dongjoon-hyun/spark/tags","blobs_url":"https://api.github.com/repos/dongjoon-hyun/spark/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/dongjoon-hyun/spark/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/dongjoon-hyun/spark/git/refs{/sha}","trees_url":"https://api.github.com/repos/dongjoon-hyun/spark/git/trees{/sha}","statuses_url":"https://api.github.com/repos/dongjoon-hyun/spark/statuses/{sha}","languages_url":"https://api.github.com/repos/dongjoon-hyun/spark/languages","stargazers_url":"https://api.github.com/repos/dongjoon-hyun/spark/stargazers","contributors_url":"https://api.github.com/repos/dongjoon-hyun/spark/contributors","subscribers_url":"https://api.github.com/repos/dongjoon-hyun/spark/subscribers","subscription_url":"https://api.github.com/repos/dongjoon-hyun/spark/subscription","commits_url":"https://api.github.com/repos/dongjoon-hyun/spark/commits{/sha}","git_commits_url":"https://api.github.com/repos/dongjoon-hyun/spark/git/commits{/sha}","comments_url":"https://api.github.com/repos/dongjoon-hyun/spark/comments{/number}","issue_comment_url":"https://api.github.com/repos/dongjoon-hyun/spark/issues/comments{/number}","contents_url":"https://api.github.com/repos/dongjoon-hyun/spark/contents/{+path}","compare_url":"https://api.github.com/repos/dongjoon-hyun/spark/compare/{base}...{head}","merges_url":"https://api.github.com/repos/dongjoon-hyun/spark/merges","archive_url":"https://api.github.com/repos/dongjoon-hyun/spark/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/dongjoon-hyun/spark/downloads","issues_url":"https://api.github.com/repos/dongjoon-hyun/spark/issues{/number}","pulls_url":"https://api.github.com/repos/dongjoon-hyun/spark/pulls{/number}","milestones_url":"https://api.github.com/repos/dongjoon-hyun/spark/milestones{/number}","notifications_url":"https://api.github.com/repos/dongjoon-hyun/spark/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/dongjoon-hyun/spark/labels{/name}","releases_url":"https://api.github.com/repos/dongjoon-hyun/spark/releases{/id}","deployments_url":"https://api.github.com/repos/dongjoon-hyun/spark/deployments","created_at":"2015-01-07T05:46:34Z","updated_at":"2018-09-04T07:30:12Z","pushed_at":"2018-09-04T07:30:09Z","git_url":"git://github.com/dongjoon-hyun/spark.git","ssh_url":"git@github.com:dongjoon-hyun/spark.git","clone_url":"https://github.com/dongjoon-hyun/spark.git","svn_url":"https://github.com/dongjoon-hyun/spark","homepage":null,"size":274355,"stargazers_count":1,"watchers_count":1,"language":"Scala","has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":1,"mirror_url":null,"archived":false,"open_issues_count":0,"license":{"key":"apache-2.0","name":"Apache License 2.0","spdx_id":"Apache-2.0","url":"https://api.github.com/licenses/apache-2.0","node_id":"MDc6TGljZW5zZTI="},"forks":1,"open_issues":0,"watchers":1,"default_branch":"master"}},"base":{"label":"apache:master","ref":"master","sha":"6ad8d4c375772c0c907c25837de762b5b9266a8e","user":{"login":"apache","id":47359,"node_id":"MDEyOk9yZ2FuaXphdGlvbjQ3MzU5","avatar_url":"https://avatars0.githubusercontent.com/u/47359?v=4","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,"node_id":"MDEwOlJlcG9zaXRvcnkxNzE2NTY1OA==","name":"spark","full_name":"apache/spark","owner":{"login":"apache","id":47359,"node_id":"MDEyOk9yZ2FuaXphdGlvbjQ3MzU5","avatar_url":"https://avatars0.githubusercontent.com/u/47359?v=4","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}","deployments_url":"https://api.github.com/repos/apache/spark/deployments","created_at":"2014-02-25T08:00:08Z","updated_at":"2018-09-04T07:30:54Z","pushed_at":"2018-09-04T08:42: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":280679,"stargazers_count":18634,"watchers_count":18634,"language":"Scala","has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":16815,"mirror_url":null,"archived":false,"open_issues_count":508,"license":{"key":"apache-2.0","name":"Apache License 2.0","spdx_id":"Apache-2.0","url":"https://api.github.com/licenses/apache-2.0","node_id":"MDc6TGljZW5zZTI="},"forks":16815,"open_issues":508,"watchers":18634,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/apache/spark/pulls/22313"},"html":{"href":"https://github.com/apache/spark/pull/22313"},"issue":{"href":"https://api.github.com/repos/apache/spark/issues/22313"},"comments":{"href":"https://api.github.com/repos/apache/spark/issues/22313/comments"},"review_comments":{"href":"https://api.github.com/repos/apache/spark/pulls/22313/comments"},"review_comment":{"href":"https://api.github.com/repos/apache/spark/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/apache/spark/pulls/22313/commits"},"statuses":{"href":"https://api.github.com/repos/apache/spark/statuses/4a372a328b33961a16ae6ad69bb58ba0720e9b63"}},"author_association":"MEMBER"}}
{"actor":{"display_login":"dongjoon-hyun"}}