{"action":"created","comment":{"url":"https://api.github.com/repos/apache/spark/pulls/comments/42581800","id":42581800,"diff_hunk":"@@ -270,6 +273,146 @@ class Analyzer(\n   }\n \n   /**\n+   * Rewrite the [[Exists]] [[In]] with left semi join or anti join.\n+   */\n+  object RewriteFilterSubQuery extends Rule[LogicalPlan] with PredicateHelper {\n+    def unapply(condition: Expression): Option[(Expression, Seq[Expression])] = {\n+      if (condition.resolved == false) {\n+        return None\n+      }\n+\n+      val conjuctions = splitConjunctivePredicates(condition).map(_ transformDown {\n+          // Remove the Cast expression for SubQueryExpression.\n+          case Cast(f: SubQueryExpression, BooleanType) => f\n+        }\n+      )\n+\n+      val (subqueries, others) = conjuctions.partition(c => c.isInstanceOf[SubQueryExpression])\n+      if (subqueries.isEmpty) {\n+        None\n+      } else if (subqueries.length > 1) {\n+        throw new AnalysisException(\n+          s\"Only 1 SubQuery expression is supported, but we got $subqueries\")\n+      } else {\n+        val subQueryExpr = subqueries(0).asInstanceOf[SubQueryExpression]\n+        // try to resolve the subquery\n+\n+        val subquery = Analyzer.this.execute(subQueryExpr.subquery) match {\n+          case Distinct(child) => child // Distinct is useless for semi join, ignore it.\n+          case other => other\n+        }\n+        Some((subQueryExpr.withNewSubQuery(subquery), others))\n+      }\n+    }\n+\n+    def apply(plan: LogicalPlan): LogicalPlan = plan transformUp {\n+      case f if f.childrenResolved == false => f\n+\n+      case f @ Filter(RewriteFilterSubQuery(subquery, others), left) =>\n+        subquery match {\n+          case Exists(Project(_, Filter(condition, right)), positive) =>\n+            checkAnalysis(right)\n+            if (condition.resolved) {\n+              // Apparently, it should be not resolved here, since EXIST should be correlated.\n+              throw new AnalysisException(\n+                s\"Exist clause should be correlated, but we got $condition\")\n+            }\n+            Join(others.reduceOption(And).map(Filter(_, left)).getOrElse(left), right,\n+              if (positive) LeftSemi else LeftAnti,\n+              Some(ResolveReferences.tryResolveAttributes(condition, right)))\n+\n+          case Exists(right, positive) =>\n+            throw new AnalysisException(s\"Exist clause should be correlated, but we got $right\")\n+\n+          case InSubquery(key, Project(projectList, Filter(condition, right)), positive) =>\n+            checkAnalysis(right)\n+            if (projectList.length != 1) {\n+              throw new AnalysisException(\n+                s\"Expect only 1 projection in In Subquery Expression, but we got $projectList\")\n+            } else {\n+              val rightKey = ResolveReferences.tryResolveAttributes(projectList(0), right)\n+\n+              if (!rightKey.resolved) {\n+                throw new AnalysisException(\n+                  s\"Outer query expression should be only presented at the filter clause, \" +\n+                    s\"but we got $rightKey\")\n+              }\n+              Join(others.reduceOption(And).map(Filter(_, left)).getOrElse(left), right,\n+                if (positive) LeftSemi else LeftAnti,\n+                Some(\n+                  And(\n+                    ResolveReferences.tryResolveAttributes(condition, right),\n+                    EqualTo(rightKey, key))))","path":"sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/Analyzer.scala","position":95,"original_position":95,"commit_id":"7511f47089ed58f913a81df2113cbe300903be63","original_commit_id":"7511f47089ed58f913a81df2113cbe300903be63","user":{"login":"yhuai","id":2072857,"avatar_url":"https://avatars.githubusercontent.com/u/2072857?v=3","gravatar_id":"","url":"https://api.github.com/users/yhuai","html_url":"https://github.com/yhuai","followers_url":"https://api.github.com/users/yhuai/followers","following_url":"https://api.github.com/users/yhuai/following{/other_user}","gists_url":"https://api.github.com/users/yhuai/gists{/gist_id}","starred_url":"https://api.github.com/users/yhuai/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/yhuai/subscriptions","organizations_url":"https://api.github.com/users/yhuai/orgs","repos_url":"https://api.github.com/users/yhuai/repos","events_url":"https://api.github.com/users/yhuai/events{/privacy}","received_events_url":"https://api.github.com/users/yhuai/received_events","type":"User","site_admin":false},"body":"It will be better to split it to multiple lines.","created_at":"2015-10-21T04:12:12Z","updated_at":"2015-10-21T04:12:12Z","html_url":"https://github.com/apache/spark/pull/9055#discussion_r42581800","pull_request_url":"https://api.github.com/repos/apache/spark/pulls/9055","_links":{"self":{"href":"https://api.github.com/repos/apache/spark/pulls/comments/42581800"},"html":{"href":"https://github.com/apache/spark/pull/9055#discussion_r42581800"},"pull_request":{"href":"https://api.github.com/repos/apache/spark/pulls/9055"}}},"pull_request":{"url":"https://api.github.com/repos/apache/spark/pulls/9055","id":47333914,"html_url":"https://github.com/apache/spark/pull/9055","diff_url":"https://github.com/apache/spark/pull/9055.diff","patch_url":"https://github.com/apache/spark/pull/9055.patch","issue_url":"https://api.github.com/repos/apache/spark/issues/9055","number":9055,"state":"open","locked":false,"title":"[SPARK-4226][SQL]Add subquery (not) in/exists support","user":{"login":"chenghao-intel","id":2367031,"avatar_url":"https://avatars.githubusercontent.com/u/2367031?v=3","gravatar_id":"","url":"https://api.github.com/users/chenghao-intel","html_url":"https://github.com/chenghao-intel","followers_url":"https://api.github.com/users/chenghao-intel/followers","following_url":"https://api.github.com/users/chenghao-intel/following{/other_user}","gists_url":"https://api.github.com/users/chenghao-intel/gists{/gist_id}","starred_url":"https://api.github.com/users/chenghao-intel/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/chenghao-intel/subscriptions","organizations_url":"https://api.github.com/users/chenghao-intel/orgs","repos_url":"https://api.github.com/users/chenghao-intel/repos","events_url":"https://api.github.com/users/chenghao-intel/events{/privacy}","received_events_url":"https://api.github.com/users/chenghao-intel/received_events","type":"User","site_admin":false},"body":"Some known feature that we don't support right now, but will add it later.\r\n\r\nWe don't support the outer UDAF function used in the correlated query, combined with\r\nouter having clause, which requires the implicit projection change for the outer query.\r\n```sql\r\nselect b.key, min(b.value)\r\nfrom src b\r\ngroup by b.key\r\nhaving exists (\r\n    select a.key\r\n    from src a\r\n     where a.value > 'val_9' and a.value = min(b.value) -- min(b.value) implicits requires the outer query to add more field in the projection.\r\n)\r\n```\r\n\r\nWe don't support the multiple references for the outer query in both the subquery in both projection and filter clause.\r\n```sql\r\nselect key, value\r\nfrom src b\r\nwhere value in\r\n    (select s1.key+ b.key\r\n     from src s1 \r\n     where s1.key > '9'  and s1.value = b.value) -- both b.value and b.key present in the subquery, but in projection and filter clause respectively.\r\n```","created_at":"2015-10-10T01:11:31Z","updated_at":"2015-10-21T04:12:12Z","closed_at":null,"merged_at":null,"merge_commit_sha":"e16bd5a4bed5876c3dbaba3d4cab1f7369e57b85","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/apache/spark/pulls/9055/commits","review_comments_url":"https://api.github.com/repos/apache/spark/pulls/9055/comments","review_comment_url":"https://api.github.com/repos/apache/spark/pulls/comments{/number}","comments_url":"https://api.github.com/repos/apache/spark/issues/9055/comments","statuses_url":"https://api.github.com/repos/apache/spark/statuses/7511f47089ed58f913a81df2113cbe300903be63","head":{"label":"chenghao-intel:anti_join","ref":"anti_join","sha":"7511f47089ed58f913a81df2113cbe300903be63","user":{"login":"chenghao-intel","id":2367031,"avatar_url":"https://avatars.githubusercontent.com/u/2367031?v=3","gravatar_id":"","url":"https://api.github.com/users/chenghao-intel","html_url":"https://github.com/chenghao-intel","followers_url":"https://api.github.com/users/chenghao-intel/followers","following_url":"https://api.github.com/users/chenghao-intel/following{/other_user}","gists_url":"https://api.github.com/users/chenghao-intel/gists{/gist_id}","starred_url":"https://api.github.com/users/chenghao-intel/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/chenghao-intel/subscriptions","organizations_url":"https://api.github.com/users/chenghao-intel/orgs","repos_url":"https://api.github.com/users/chenghao-intel/repos","events_url":"https://api.github.com/users/chenghao-intel/events{/privacy}","received_events_url":"https://api.github.com/users/chenghao-intel/received_events","type":"User","site_admin":false},"repo":{"id":18098468,"name":"spark","full_name":"chenghao-intel/spark","owner":{"login":"chenghao-intel","id":2367031,"avatar_url":"https://avatars.githubusercontent.com/u/2367031?v=3","gravatar_id":"","url":"https://api.github.com/users/chenghao-intel","html_url":"https://github.com/chenghao-intel","followers_url":"https://api.github.com/users/chenghao-intel/followers","following_url":"https://api.github.com/users/chenghao-intel/following{/other_user}","gists_url":"https://api.github.com/users/chenghao-intel/gists{/gist_id}","starred_url":"https://api.github.com/users/chenghao-intel/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/chenghao-intel/subscriptions","organizations_url":"https://api.github.com/users/chenghao-intel/orgs","repos_url":"https://api.github.com/users/chenghao-intel/repos","events_url":"https://api.github.com/users/chenghao-intel/events{/privacy}","received_events_url":"https://api.github.com/users/chenghao-intel/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/chenghao-intel/spark","description":"Mirror of Apache Spark","fork":true,"url":"https://api.github.com/repos/chenghao-intel/spark","forks_url":"https://api.github.com/repos/chenghao-intel/spark/forks","keys_url":"https://api.github.com/repos/chenghao-intel/spark/keys{/key_id}","collaborators_url":"https://api.github.com/repos/chenghao-intel/spark/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/chenghao-intel/spark/teams","hooks_url":"https://api.github.com/repos/chenghao-intel/spark/hooks","issue_events_url":"https://api.github.com/repos/chenghao-intel/spark/issues/events{/number}","events_url":"https://api.github.com/repos/chenghao-intel/spark/events","assignees_url":"https://api.github.com/repos/chenghao-intel/spark/assignees{/user}","branches_url":"https://api.github.com/repos/chenghao-intel/spark/branches{/branch}","tags_url":"https://api.github.com/repos/chenghao-intel/spark/tags","blobs_url":"https://api.github.com/repos/chenghao-intel/spark/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/chenghao-intel/spark/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/chenghao-intel/spark/git/refs{/sha}","trees_url":"https://api.github.com/repos/chenghao-intel/spark/git/trees{/sha}","statuses_url":"https://api.github.com/repos/chenghao-intel/spark/statuses/{sha}","languages_url":"https://api.github.com/repos/chenghao-intel/spark/languages","stargazers_url":"https://api.github.com/repos/chenghao-intel/spark/stargazers","contributors_url":"https://api.github.com/repos/chenghao-intel/spark/contributors","subscribers_url":"https://api.github.com/repos/chenghao-intel/spark/subscribers","subscription_url":"https://api.github.com/repos/chenghao-intel/spark/subscription","commits_url":"https://api.github.com/repos/chenghao-intel/spark/commits{/sha}","git_commits_url":"https://api.github.com/repos/chenghao-intel/spark/git/commits{/sha}","comments_url":"https://api.github.com/repos/chenghao-intel/spark/comments{/number}","issue_comment_url":"https://api.github.com/repos/chenghao-intel/spark/issues/comments{/number}","contents_url":"https://api.github.com/repos/chenghao-intel/spark/contents/{+path}","compare_url":"https://api.github.com/repos/chenghao-intel/spark/compare/{base}...{head}","merges_url":"https://api.github.com/repos/chenghao-intel/spark/merges","archive_url":"https://api.github.com/repos/chenghao-intel/spark/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/chenghao-intel/spark/downloads","issues_url":"https://api.github.com/repos/chenghao-intel/spark/issues{/number}","pulls_url":"https://api.github.com/repos/chenghao-intel/spark/pulls{/number}","milestones_url":"https://api.github.com/repos/chenghao-intel/spark/milestones{/number}","notifications_url":"https://api.github.com/repos/chenghao-intel/spark/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/chenghao-intel/spark/labels{/name}","releases_url":"https://api.github.com/repos/chenghao-intel/spark/releases{/id}","created_at":"2014-03-25T11:52:04Z","updated_at":"2015-03-05T08:13:37Z","pushed_at":"2015-10-21T04:11:34Z","git_url":"git://github.com/chenghao-intel/spark.git","ssh_url":"git@github.com:chenghao-intel/spark.git","clone_url":"https://github.com/chenghao-intel/spark.git","svn_url":"https://github.com/chenghao-intel/spark","homepage":null,"size":133650,"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":"9808052b5adfed7dafd6c1b3971b998e45b2799a","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-10-21T04:07:29Z","pushed_at":"2015-10-21T04:11:36Z","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":1638064,"stargazers_count":5821,"watchers_count":5821,"language":"Scala","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":5426,"mirror_url":"git://git.apache.org/spark.git","open_issues_count":318,"forks":5426,"open_issues":318,"watchers":5821,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/apache/spark/pulls/9055"},"html":{"href":"https://github.com/apache/spark/pull/9055"},"issue":{"href":"https://api.github.com/repos/apache/spark/issues/9055"},"comments":{"href":"https://api.github.com/repos/apache/spark/issues/9055/comments"},"review_comments":{"href":"https://api.github.com/repos/apache/spark/pulls/9055/comments"},"review_comment":{"href":"https://api.github.com/repos/apache/spark/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/apache/spark/pulls/9055/commits"},"statuses":{"href":"https://api.github.com/repos/apache/spark/statuses/7511f47089ed58f913a81df2113cbe300903be63"}}}}