{"action":"created","comment":{"url":"https://api.github.com/repos/apache/spark/pulls/comments/46506278","id":46506278,"diff_hunk":"@@ -712,6 +711,54 @@ object PushPredicateThroughAggregate extends Rule[LogicalPlan] with PredicateHel\n }\n \n /**\n+  * Reorder the joins and push all the conditions into join, so that the bottom ones have at least\n+  * one condition.\n+  *\n+  * The order of joins will not be changed if all of them already have at least one condition.\n+  */\n+object ReorderJoin extends Rule[LogicalPlan] with PredicateHelper {\n+\n+  /**\n+    * Join a list of plans together and push down the conditions into them.\n+    *\n+    * The joined plan are picked from left to right, prefer those has at least one join condition.\n+    *\n+    * @param input a list of LogicalPlans to join.\n+    * @param conditions a list of condition for join.\n+    */\n+  def createOrderedJoin(input: Seq[LogicalPlan], conditions: Seq[Expression]): LogicalPlan = {\n+    assert(input.size >= 2)\n+    if (input.size == 2) {\n+      Join(input(0), input(1), Inner, conditions.reduceLeftOption(And))\n+    } else {\n+      val left :: rest = input.toList\n+      // find out the first join that have at least one join condition\n+      val conditionalJoin = rest.find { plan =>\n+        val refs = left.outputSet ++ plan.outputSet\n+        conditions.filterNot(canEvaluate(_, left)).filterNot(canEvaluate(_, plan))","path":"sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala","position":54,"original_position":54,"commit_id":"eb31c37ef3411a73899be7005b6115ebfa8c3571","original_commit_id":"eb31c37ef3411a73899be7005b6115ebfa8c3571","user":{"login":"watermen","id":1400819,"avatar_url":"https://avatars.githubusercontent.com/u/1400819?v=3","gravatar_id":"","url":"https://api.github.com/users/watermen","html_url":"https://github.com/watermen","followers_url":"https://api.github.com/users/watermen/followers","following_url":"https://api.github.com/users/watermen/following{/other_user}","gists_url":"https://api.github.com/users/watermen/gists{/gist_id}","starred_url":"https://api.github.com/users/watermen/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/watermen/subscriptions","organizations_url":"https://api.github.com/users/watermen/orgs","repos_url":"https://api.github.com/users/watermen/repos","events_url":"https://api.github.com/users/watermen/events{/privacy}","received_events_url":"https://api.github.com/users/watermen/received_events","type":"User","site_admin":false},"body":"I see, thanks!","created_at":"2015-12-03T02:29:19Z","updated_at":"2015-12-03T02:29:19Z","html_url":"https://github.com/apache/spark/pull/10073#discussion_r46506278","pull_request_url":"https://api.github.com/repos/apache/spark/pulls/10073","_links":{"self":{"href":"https://api.github.com/repos/apache/spark/pulls/comments/46506278"},"html":{"href":"https://github.com/apache/spark/pull/10073#discussion_r46506278"},"pull_request":{"href":"https://api.github.com/repos/apache/spark/pulls/10073"}}},"pull_request":{"url":"https://api.github.com/repos/apache/spark/pulls/10073","id":52313600,"html_url":"https://github.com/apache/spark/pull/10073","diff_url":"https://github.com/apache/spark/pull/10073.diff","patch_url":"https://github.com/apache/spark/pull/10073.patch","issue_url":"https://api.github.com/repos/apache/spark/issues/10073","number":10073,"state":"open","locked":false,"title":"[SPARK-12032] [SQL] Re-order inner joins to do join with conditions first","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":"Currently, the order of joins is exactly the same as SQL query, some conditions may not pushed down to the correct join, then those join will become cross product and is extremely slow.\r\n\r\nThis patch try to re-order the inner joins (which are common in SQL query), pick the joins that have self-contain conditions first, delay those that does not have conditions.\r\n\r\nAfter this patch, the TPCDS query Q64/65 can run hundreds times faster.\r\n\r\ncc @marmbrus @nongli ","created_at":"2015-12-01T20:49:31Z","updated_at":"2015-12-03T02:29:20Z","closed_at":null,"merged_at":null,"merge_commit_sha":"4a7f350eea6038b6820031b635d5bc06f56a5229","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/apache/spark/pulls/10073/commits","review_comments_url":"https://api.github.com/repos/apache/spark/pulls/10073/comments","review_comment_url":"https://api.github.com/repos/apache/spark/pulls/comments{/number}","comments_url":"https://api.github.com/repos/apache/spark/issues/10073/comments","statuses_url":"https://api.github.com/repos/apache/spark/statuses/eb31c37ef3411a73899be7005b6115ebfa8c3571","head":{"label":"davies:reorder_joins","ref":"reorder_joins","sha":"eb31c37ef3411a73899be7005b6115ebfa8c3571","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-12-03T00:59:02Z","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":142349,"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":"34e7093c1131162b3aa05b65a19a633a0b5b633e","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-12-03T02:19:35Z","pushed_at":"2015-12-03T01:35:39Z","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":389044,"stargazers_count":6432,"watchers_count":6432,"language":"Scala","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":5941,"mirror_url":"git://git.apache.org/spark.git","open_issues_count":394,"forks":5941,"open_issues":394,"watchers":6432,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/apache/spark/pulls/10073"},"html":{"href":"https://github.com/apache/spark/pull/10073"},"issue":{"href":"https://api.github.com/repos/apache/spark/issues/10073"},"comments":{"href":"https://api.github.com/repos/apache/spark/issues/10073/comments"},"review_comments":{"href":"https://api.github.com/repos/apache/spark/pulls/10073/comments"},"review_comment":{"href":"https://api.github.com/repos/apache/spark/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/apache/spark/pulls/10073/commits"},"statuses":{"href":"https://api.github.com/repos/apache/spark/statuses/eb31c37ef3411a73899be7005b6115ebfa8c3571"}}}}