{"action":"created","comment":{"url":"https://api.github.com/repos/apache/spark/pulls/comments/276574876","pull_request_review_id":228168361,"id":276574876,"node_id":"MDI0OlB1bGxSZXF1ZXN0UmV2aWV3Q29tbWVudDI3NjU3NDg3Ng==","diff_hunk":"@@ -551,3 +552,30 @@ object RewriteCorrelatedScalarSubquery extends Rule[LogicalPlan] {\n       }\n   }\n }\n+\n+/**\n+ * This rule rewrites uncorrelated PredicateSubquery expressions such as Exists.\n+ * The uncorrelated Exists can be evaluated using a subplan instead of a semi-join.\n+ * Also, we can use `limit 1` and `select 1` after the subquery to reduce the result set.\n+ * Example:\n+ * exists(select b from t where t.a = 2) => exists(select 1 from t where t.a = 2 limit 1)\n+ */\n+object RewriteUncorrelatedSubquery extends Rule[LogicalPlan] {\n+  /**\n+   * Wrap the subquery with `limit 1` and `project 1`.\n+   */\n+  def buildSubquery(sub: LogicalPlan): LogicalPlan = {\n+    Project(Seq(Alias(Literal.create(1, IntegerType), \"1\")()),\n+      Limit(Literal.create(1, IntegerType), sub))\n+  }\n+\n+  override def apply(plan: LogicalPlan): LogicalPlan = plan transform {\n+    case Filter(condition, child) =>\n+      val newCondition = condition transform {\n+        case Exists(sub, children, _) if children.isEmpty =>","path":"sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/subquery.scala","position":34,"original_position":34,"commit_id":"19578f7fe909ee9cabb523ba0d7cc9ebfd02305b","original_commit_id":"19578f7fe909ee9cabb523ba0d7cc9ebfd02305b","user":{"login":"viirya","id":68855,"node_id":"MDQ6VXNlcjY4ODU1","avatar_url":"https://avatars1.githubusercontent.com/u/68855?v=4","gravatar_id":"","url":"https://api.github.com/users/viirya","html_url":"https://github.com/viirya","followers_url":"https://api.github.com/users/viirya/followers","following_url":"https://api.github.com/users/viirya/following{/other_user}","gists_url":"https://api.github.com/users/viirya/gists{/gist_id}","starred_url":"https://api.github.com/users/viirya/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/viirya/subscriptions","organizations_url":"https://api.github.com/users/viirya/orgs","repos_url":"https://api.github.com/users/viirya/repos","events_url":"https://api.github.com/users/viirya/events{/privacy}","received_events_url":"https://api.github.com/users/viirya/received_events","type":"User","site_admin":false},"body":"Only deal with `Exists`? Will it process `InSubquery`?","created_at":"2019-04-18T08:57:25Z","updated_at":"2019-04-18T08:58:33Z","html_url":"https://github.com/apache/spark/pull/24344#discussion_r276574876","pull_request_url":"https://api.github.com/repos/apache/spark/pulls/24344","author_association":"CONTRIBUTOR","_links":{"self":{"href":"https://api.github.com/repos/apache/spark/pulls/comments/276574876"},"html":{"href":"https://github.com/apache/spark/pull/24344#discussion_r276574876"},"pull_request":{"href":"https://api.github.com/repos/apache/spark/pulls/24344"}}},"pull_request":{"url":"https://api.github.com/repos/apache/spark/pulls/24344","id":269511007,"node_id":"MDExOlB1bGxSZXF1ZXN0MjY5NTExMDA3","html_url":"https://github.com/apache/spark/pull/24344","diff_url":"https://github.com/apache/spark/pull/24344.diff","patch_url":"https://github.com/apache/spark/pull/24344.patch","issue_url":"https://api.github.com/repos/apache/spark/issues/24344","number":24344,"state":"open","locked":false,"title":"[SPARK-27440][SQL] Optimize uncorrelated predicate subquery","user":{"login":"francis0407","id":25431723,"node_id":"MDQ6VXNlcjI1NDMxNzIz","avatar_url":"https://avatars1.githubusercontent.com/u/25431723?v=4","gravatar_id":"","url":"https://api.github.com/users/francis0407","html_url":"https://github.com/francis0407","followers_url":"https://api.github.com/users/francis0407/followers","following_url":"https://api.github.com/users/francis0407/following{/other_user}","gists_url":"https://api.github.com/users/francis0407/gists{/gist_id}","starred_url":"https://api.github.com/users/francis0407/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/francis0407/subscriptions","organizations_url":"https://api.github.com/users/francis0407/orgs","repos_url":"https://api.github.com/users/francis0407/repos","events_url":"https://api.github.com/users/francis0407/events{/privacy}","received_events_url":"https://api.github.com/users/francis0407/received_events","type":"User","site_admin":false},"body":"## What changes were proposed in this pull request?\r\n\r\nThis PR is trying to optimize uncorrelated predicate subqueries(InSubquery, Exists).\r\nCurrently, we rewrite all the predicate subqueries(`InSubquery`, `Exists`) as semi-join/anti-join. But uncorrelated predicate subquery can be evaluated using a subplan instead of a join. We can firstly  optimize the subquery and then compute it using a subquery physical plan.\r\n\r\nThis PR adds a new Optimize rule: RewriteUncorrelatedSubquery.\r\nThis rule rewrites uncorrelated PredicateSubquery expressions such as Exists(it can also be used for ANY/SOME/ALL). We can use `limit 1` and `select 1` after the Exists subquery to reduce the result set. \r\n\r\nAlso, this PR adds physical plans of 'InSubquery' and 'Exists'.\r\n\r\n## How was this patch tested?\r\n\r\nut\r\n","created_at":"2019-04-11T10:22:22Z","updated_at":"2019-04-18T08:58:33Z","closed_at":null,"merged_at":null,"merge_commit_sha":"3e970b22a996bcab30bec2b46291e9b47b4e1688","assignee":null,"assignees":[],"requested_reviewers":[],"requested_teams":[],"labels":[],"milestone":null,"commits_url":"https://api.github.com/repos/apache/spark/pulls/24344/commits","review_comments_url":"https://api.github.com/repos/apache/spark/pulls/24344/comments","review_comment_url":"https://api.github.com/repos/apache/spark/pulls/comments{/number}","comments_url":"https://api.github.com/repos/apache/spark/issues/24344/comments","statuses_url":"https://api.github.com/repos/apache/spark/statuses/19578f7fe909ee9cabb523ba0d7cc9ebfd02305b","head":{"label":"francis0407:SPARK-27440","ref":"SPARK-27440","sha":"19578f7fe909ee9cabb523ba0d7cc9ebfd02305b","user":{"login":"francis0407","id":25431723,"node_id":"MDQ6VXNlcjI1NDMxNzIz","avatar_url":"https://avatars1.githubusercontent.com/u/25431723?v=4","gravatar_id":"","url":"https://api.github.com/users/francis0407","html_url":"https://github.com/francis0407","followers_url":"https://api.github.com/users/francis0407/followers","following_url":"https://api.github.com/users/francis0407/following{/other_user}","gists_url":"https://api.github.com/users/francis0407/gists{/gist_id}","starred_url":"https://api.github.com/users/francis0407/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/francis0407/subscriptions","organizations_url":"https://api.github.com/users/francis0407/orgs","repos_url":"https://api.github.com/users/francis0407/repos","events_url":"https://api.github.com/users/francis0407/events{/privacy}","received_events_url":"https://api.github.com/users/francis0407/received_events","type":"User","site_admin":false},"repo":{"id":169244879,"node_id":"MDEwOlJlcG9zaXRvcnkxNjkyNDQ4Nzk=","name":"spark","full_name":"francis0407/spark","private":false,"owner":{"login":"francis0407","id":25431723,"node_id":"MDQ6VXNlcjI1NDMxNzIz","avatar_url":"https://avatars1.githubusercontent.com/u/25431723?v=4","gravatar_id":"","url":"https://api.github.com/users/francis0407","html_url":"https://github.com/francis0407","followers_url":"https://api.github.com/users/francis0407/followers","following_url":"https://api.github.com/users/francis0407/following{/other_user}","gists_url":"https://api.github.com/users/francis0407/gists{/gist_id}","starred_url":"https://api.github.com/users/francis0407/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/francis0407/subscriptions","organizations_url":"https://api.github.com/users/francis0407/orgs","repos_url":"https://api.github.com/users/francis0407/repos","events_url":"https://api.github.com/users/francis0407/events{/privacy}","received_events_url":"https://api.github.com/users/francis0407/received_events","type":"User","site_admin":false},"html_url":"https://github.com/francis0407/spark","description":"Apache Spark","fork":true,"url":"https://api.github.com/repos/francis0407/spark","forks_url":"https://api.github.com/repos/francis0407/spark/forks","keys_url":"https://api.github.com/repos/francis0407/spark/keys{/key_id}","collaborators_url":"https://api.github.com/repos/francis0407/spark/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/francis0407/spark/teams","hooks_url":"https://api.github.com/repos/francis0407/spark/hooks","issue_events_url":"https://api.github.com/repos/francis0407/spark/issues/events{/number}","events_url":"https://api.github.com/repos/francis0407/spark/events","assignees_url":"https://api.github.com/repos/francis0407/spark/assignees{/user}","branches_url":"https://api.github.com/repos/francis0407/spark/branches{/branch}","tags_url":"https://api.github.com/repos/francis0407/spark/tags","blobs_url":"https://api.github.com/repos/francis0407/spark/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/francis0407/spark/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/francis0407/spark/git/refs{/sha}","trees_url":"https://api.github.com/repos/francis0407/spark/git/trees{/sha}","statuses_url":"https://api.github.com/repos/francis0407/spark/statuses/{sha}","languages_url":"https://api.github.com/repos/francis0407/spark/languages","stargazers_url":"https://api.github.com/repos/francis0407/spark/stargazers","contributors_url":"https://api.github.com/repos/francis0407/spark/contributors","subscribers_url":"https://api.github.com/repos/francis0407/spark/subscribers","subscription_url":"https://api.github.com/repos/francis0407/spark/subscription","commits_url":"https://api.github.com/repos/francis0407/spark/commits{/sha}","git_commits_url":"https://api.github.com/repos/francis0407/spark/git/commits{/sha}","comments_url":"https://api.github.com/repos/francis0407/spark/comments{/number}","issue_comment_url":"https://api.github.com/repos/francis0407/spark/issues/comments{/number}","contents_url":"https://api.github.com/repos/francis0407/spark/contents/{+path}","compare_url":"https://api.github.com/repos/francis0407/spark/compare/{base}...{head}","merges_url":"https://api.github.com/repos/francis0407/spark/merges","archive_url":"https://api.github.com/repos/francis0407/spark/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/francis0407/spark/downloads","issues_url":"https://api.github.com/repos/francis0407/spark/issues{/number}","pulls_url":"https://api.github.com/repos/francis0407/spark/pulls{/number}","milestones_url":"https://api.github.com/repos/francis0407/spark/milestones{/number}","notifications_url":"https://api.github.com/repos/francis0407/spark/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/francis0407/spark/labels{/name}","releases_url":"https://api.github.com/repos/francis0407/spark/releases{/id}","deployments_url":"https://api.github.com/repos/francis0407/spark/deployments","created_at":"2019-02-05T13:23:20Z","updated_at":"2019-04-08T06:39:02Z","pushed_at":"2019-04-18T06:55:26Z","git_url":"git://github.com/francis0407/spark.git","ssh_url":"git@github.com:francis0407/spark.git","clone_url":"https://github.com/francis0407/spark.git","svn_url":"https://github.com/francis0407/spark","homepage":"","size":295951,"stargazers_count":0,"watchers_count":0,"language":"Scala","has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"archived":false,"disabled":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":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"apache:master","ref":"master","sha":"074533334d01afdd7862a1ac6c5a7a672bcce3f8","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","private":false,"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},"html_url":"https://github.com/apache/spark","description":"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":"2019-04-18T08:30:40Z","pushed_at":"2019-04-18T08:23:23Z","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":"","size":296629,"stargazers_count":21425,"watchers_count":21425,"language":"Scala","has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":18621,"mirror_url":null,"archived":false,"disabled":false,"open_issues_count":443,"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":18621,"open_issues":443,"watchers":21425,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/apache/spark/pulls/24344"},"html":{"href":"https://github.com/apache/spark/pull/24344"},"issue":{"href":"https://api.github.com/repos/apache/spark/issues/24344"},"comments":{"href":"https://api.github.com/repos/apache/spark/issues/24344/comments"},"review_comments":{"href":"https://api.github.com/repos/apache/spark/pulls/24344/comments"},"review_comment":{"href":"https://api.github.com/repos/apache/spark/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/apache/spark/pulls/24344/commits"},"statuses":{"href":"https://api.github.com/repos/apache/spark/statuses/19578f7fe909ee9cabb523ba0d7cc9ebfd02305b"}},"author_association":"CONTRIBUTOR"}}
{"actor":{"display_login":"viirya"}}