{"action":"created","comment":{"url":"https://api.github.com/repos/cockroachdb/cockroach/pulls/comments/38998681","id":38998681,"diff_hunk":"@@ -0,0 +1,149 @@\n+// Copyright 2015 The Cockroach Authors.\n+//\n+// Licensed under the Apache License, Version 2.0 (the \"License\");\n+// you may not use this file except in compliance with the License.\n+// You may obtain a copy of the License at\n+//\n+//     http://www.apache.org/licenses/LICENSE-2.0\n+//\n+// Unless required by applicable law or agreed to in writing, software\n+// distributed under the License is distributed on an \"AS IS\" BASIS,\n+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or\n+// implied. See the License for the specific language governing\n+// permissions and limitations under the License. See the AUTHORS file\n+// for names of contributors.\n+//\n+// Author: Peter Mattis (peter@cockroachlabs.com)\n+\n+package sql\n+\n+import (\n+\t\"fmt\"\n+\n+\t\"github.com/cockroachdb/cockroach/sql/parser\"\n+)\n+\n+func (p *planner) expandSubqueries(expr parser.Expr) (parser.Expr, error) {\n+\tv := subqueryVisitor{planner: p}\n+\texpr = parser.WalkExpr(&v, expr)\n+\treturn expr, v.err\n+}\n+\n+type subqueryVisitor struct {\n+\t*planner\n+\tpath []parser.Expr // parent expressions\n+\terr  error\n+}\n+\n+var _ parser.Visitor = &subqueryVisitor{}\n+\n+func (v *subqueryVisitor) Visit(expr parser.Expr, pre bool) (parser.Visitor, parser.Expr) {\n+\tif v.err != nil {\n+\t\treturn nil, expr\n+\t}\n+\tif !pre {\n+\t\tv.path = v.path[:len(v.path)-1]\n+\t\treturn nil, expr\n+\t}\n+\tv.path = append(v.path, expr)\n+\n+\tsubquery, ok := expr.(*parser.Subquery)\n+\tif !ok {\n+\t\treturn v, expr\n+\t}\n+\n+\tvar plan planNode\n+\tif plan, v.err = v.makePlan(subquery.Select); v.err != nil {\n+\t\treturn nil, expr\n+\t}\n+\n+\tcolumns, multipleRows := v.getSubqueryContext()\n+\tif columns != len(plan.Columns()) {\n+\t\tswitch columns {\n+\t\tcase 1:\n+\t\t\tv.err = fmt.Errorf(\"subquery must return only one column\")\n+\t\tdefault:\n+\t\t\tv.err = fmt.Errorf(\"subquery must return %d columns\", columns)\n+\t\t}\n+\t\treturn nil, expr\n+\t}\n+\n+\tvar result parser.Expr\n+\tif multipleRows {\n+\t\tvar rows parser.DTuple\n+\t\tfor plan.Next() {\n+\t\t\tvalues := plan.Values()\n+\t\t\tswitch len(values) {\n+\t\t\tcase 1:\n+\t\t\t\t// This seems hokey, but if we don't do this then the subquery expands to\n+\t\t\t\t// a tuple of tuples instead of a tuple of values and an expression like\n+\t\t\t\t// \"k IN (SELECT foo FROM bar)\" will fail because we're comparing a\n+\t\t\t\t// single value against a tuple. Perhaps comparison of a single value\n+\t\t\t\t// against a tuple should succeed if the tuple is one element in length.\n+\t\t\t\trows = append(rows, values[0])\n+\t\t\tdefault:\n+\t\t\t\t// The result from plan.Values() is only valid until the next call to\n+\t\t\t\t// plan.Next(), so make a copy.\n+\t\t\t\tvaluesCopy := make(parser.DTuple, len(values))\n+\t\t\t\tcopy(valuesCopy, values)\n+\t\t\t\trows = append(rows, valuesCopy)\n+\t\t\t}\n+\t\t}\n+\t\tresult = rows\n+\t} else {\n+\t\tresult = parser.DNull\n+\t\tfor plan.Next() {\n+\t\t\tvalues := plan.Values()\n+\t\t\tswitch len(values) {\n+\t\t\tcase 1:\n+\t\t\t\tresult = values[0]\n+\t\t\tdefault:\n+\t\t\t\tvaluesCopy := make(parser.DTuple, len(values))\n+\t\t\t\tcopy(valuesCopy, values)\n+\t\t\t\tresult = valuesCopy\n+\t\t\t}\n+\t\t\tif plan.Next() {\n+\t\t\t\tv.err = fmt.Errorf(\"more than one row returned by a subquery used as an expression\")\n+\t\t\t\treturn nil, expr\n+\t\t\t}\n+\t\t}\n+\t}\n+\n+\tv.err = plan.Err()\n+\tif v.err != nil {\n+\t\treturn nil, expr\n+\t}\n+\treturn v, result\n+}\n+\n+// getSubqueryContext returns the number of columns and rows the subquery is\n+// allowed to have.\n+func (v *subqueryVisitor) getSubqueryContext() (columns int, multipleRows bool) {","path":"sql/subquery.go","position":121,"original_position":121,"commit_id":"0ef2176ff3b981cdb1bfb863ebd1deb62a68c552","original_commit_id":"0ef2176ff3b981cdb1bfb863ebd1deb62a68c552","user":{"login":"petermattis","id":6201335,"avatar_url":"https://avatars.githubusercontent.com/u/6201335?v=3","gravatar_id":"","url":"https://api.github.com/users/petermattis","html_url":"https://github.com/petermattis","followers_url":"https://api.github.com/users/petermattis/followers","following_url":"https://api.github.com/users/petermattis/following{/other_user}","gists_url":"https://api.github.com/users/petermattis/gists{/gist_id}","starred_url":"https://api.github.com/users/petermattis/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/petermattis/subscriptions","organizations_url":"https://api.github.com/users/petermattis/orgs","repos_url":"https://api.github.com/users/petermattis/repos","events_url":"https://api.github.com/users/petermattis/events{/privacy}","received_events_url":"https://api.github.com/users/petermattis/received_events","type":"User","site_admin":false},"body":"Yes, but they add a bit of documentation as to what return value is what. Do you really want them removed?","created_at":"2015-09-09T01:09:56Z","updated_at":"2015-09-09T01:09:56Z","html_url":"https://github.com/cockroachdb/cockroach/pull/2412#discussion_r38998681","pull_request_url":"https://api.github.com/repos/cockroachdb/cockroach/pulls/2412","_links":{"self":{"href":"https://api.github.com/repos/cockroachdb/cockroach/pulls/comments/38998681"},"html":{"href":"https://github.com/cockroachdb/cockroach/pull/2412#discussion_r38998681"},"pull_request":{"href":"https://api.github.com/repos/cockroachdb/cockroach/pulls/2412"}}},"pull_request":{"url":"https://api.github.com/repos/cockroachdb/cockroach/pulls/2412","id":44477121,"html_url":"https://github.com/cockroachdb/cockroach/pull/2412","diff_url":"https://github.com/cockroachdb/cockroach/pull/2412.diff","patch_url":"https://github.com/cockroachdb/cockroach/pull/2412.patch","issue_url":"https://api.github.com/repos/cockroachdb/cockroach/issues/2412","number":2412,"state":"open","locked":false,"title":"Fix subquery handling.","user":{"login":"petermattis","id":6201335,"avatar_url":"https://avatars.githubusercontent.com/u/6201335?v=3","gravatar_id":"","url":"https://api.github.com/users/petermattis","html_url":"https://github.com/petermattis","followers_url":"https://api.github.com/users/petermattis/followers","following_url":"https://api.github.com/users/petermattis/following{/other_user}","gists_url":"https://api.github.com/users/petermattis/gists{/gist_id}","starred_url":"https://api.github.com/users/petermattis/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/petermattis/subscriptions","organizations_url":"https://api.github.com/users/petermattis/orgs","repos_url":"https://api.github.com/users/petermattis/repos","events_url":"https://api.github.com/users/petermattis/events{/privacy}","received_events_url":"https://api.github.com/users/petermattis/received_events","type":"User","site_admin":false},"body":"When processing a subquery determine whether it is being used in a single\nor multi-value context and how many columns the subquery should contain.\n\nFixes #2402.","created_at":"2015-09-08T20:11:28Z","updated_at":"2015-09-09T01:09:56Z","closed_at":null,"merged_at":null,"merge_commit_sha":"47fc73bf5464afdd25e625a046efe1010bdce598","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/cockroachdb/cockroach/pulls/2412/commits","review_comments_url":"https://api.github.com/repos/cockroachdb/cockroach/pulls/2412/comments","review_comment_url":"https://api.github.com/repos/cockroachdb/cockroach/pulls/comments{/number}","comments_url":"https://api.github.com/repos/cockroachdb/cockroach/issues/2412/comments","statuses_url":"https://api.github.com/repos/cockroachdb/cockroach/statuses/0ef2176ff3b981cdb1bfb863ebd1deb62a68c552","head":{"label":"cockroachdb:pmattis/sql-subquery","ref":"pmattis/sql-subquery","sha":"0ef2176ff3b981cdb1bfb863ebd1deb62a68c552","user":{"login":"cockroachdb","id":6748139,"avatar_url":"https://avatars.githubusercontent.com/u/6748139?v=3","gravatar_id":"","url":"https://api.github.com/users/cockroachdb","html_url":"https://github.com/cockroachdb","followers_url":"https://api.github.com/users/cockroachdb/followers","following_url":"https://api.github.com/users/cockroachdb/following{/other_user}","gists_url":"https://api.github.com/users/cockroachdb/gists{/gist_id}","starred_url":"https://api.github.com/users/cockroachdb/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cockroachdb/subscriptions","organizations_url":"https://api.github.com/users/cockroachdb/orgs","repos_url":"https://api.github.com/users/cockroachdb/repos","events_url":"https://api.github.com/users/cockroachdb/events{/privacy}","received_events_url":"https://api.github.com/users/cockroachdb/received_events","type":"Organization","site_admin":false},"repo":{"id":16563587,"name":"cockroach","full_name":"cockroachdb/cockroach","owner":{"login":"cockroachdb","id":6748139,"avatar_url":"https://avatars.githubusercontent.com/u/6748139?v=3","gravatar_id":"","url":"https://api.github.com/users/cockroachdb","html_url":"https://github.com/cockroachdb","followers_url":"https://api.github.com/users/cockroachdb/followers","following_url":"https://api.github.com/users/cockroachdb/following{/other_user}","gists_url":"https://api.github.com/users/cockroachdb/gists{/gist_id}","starred_url":"https://api.github.com/users/cockroachdb/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cockroachdb/subscriptions","organizations_url":"https://api.github.com/users/cockroachdb/orgs","repos_url":"https://api.github.com/users/cockroachdb/repos","events_url":"https://api.github.com/users/cockroachdb/events{/privacy}","received_events_url":"https://api.github.com/users/cockroachdb/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/cockroachdb/cockroach","description":"A Scalable, Geo-Replicated, Transactional Datastore","fork":false,"url":"https://api.github.com/repos/cockroachdb/cockroach","forks_url":"https://api.github.com/repos/cockroachdb/cockroach/forks","keys_url":"https://api.github.com/repos/cockroachdb/cockroach/keys{/key_id}","collaborators_url":"https://api.github.com/repos/cockroachdb/cockroach/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/cockroachdb/cockroach/teams","hooks_url":"https://api.github.com/repos/cockroachdb/cockroach/hooks","issue_events_url":"https://api.github.com/repos/cockroachdb/cockroach/issues/events{/number}","events_url":"https://api.github.com/repos/cockroachdb/cockroach/events","assignees_url":"https://api.github.com/repos/cockroachdb/cockroach/assignees{/user}","branches_url":"https://api.github.com/repos/cockroachdb/cockroach/branches{/branch}","tags_url":"https://api.github.com/repos/cockroachdb/cockroach/tags","blobs_url":"https://api.github.com/repos/cockroachdb/cockroach/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/cockroachdb/cockroach/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/cockroachdb/cockroach/git/refs{/sha}","trees_url":"https://api.github.com/repos/cockroachdb/cockroach/git/trees{/sha}","statuses_url":"https://api.github.com/repos/cockroachdb/cockroach/statuses/{sha}","languages_url":"https://api.github.com/repos/cockroachdb/cockroach/languages","stargazers_url":"https://api.github.com/repos/cockroachdb/cockroach/stargazers","contributors_url":"https://api.github.com/repos/cockroachdb/cockroach/contributors","subscribers_url":"https://api.github.com/repos/cockroachdb/cockroach/subscribers","subscription_url":"https://api.github.com/repos/cockroachdb/cockroach/subscription","commits_url":"https://api.github.com/repos/cockroachdb/cockroach/commits{/sha}","git_commits_url":"https://api.github.com/repos/cockroachdb/cockroach/git/commits{/sha}","comments_url":"https://api.github.com/repos/cockroachdb/cockroach/comments{/number}","issue_comment_url":"https://api.github.com/repos/cockroachdb/cockroach/issues/comments{/number}","contents_url":"https://api.github.com/repos/cockroachdb/cockroach/contents/{+path}","compare_url":"https://api.github.com/repos/cockroachdb/cockroach/compare/{base}...{head}","merges_url":"https://api.github.com/repos/cockroachdb/cockroach/merges","archive_url":"https://api.github.com/repos/cockroachdb/cockroach/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/cockroachdb/cockroach/downloads","issues_url":"https://api.github.com/repos/cockroachdb/cockroach/issues{/number}","pulls_url":"https://api.github.com/repos/cockroachdb/cockroach/pulls{/number}","milestones_url":"https://api.github.com/repos/cockroachdb/cockroach/milestones{/number}","notifications_url":"https://api.github.com/repos/cockroachdb/cockroach/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/cockroachdb/cockroach/labels{/name}","releases_url":"https://api.github.com/repos/cockroachdb/cockroach/releases{/id}","created_at":"2014-02-06T00:18:47Z","updated_at":"2015-09-09T00:22:23Z","pushed_at":"2015-09-09T00:59:57Z","git_url":"git://github.com/cockroachdb/cockroach.git","ssh_url":"git@github.com:cockroachdb/cockroach.git","clone_url":"https://github.com/cockroachdb/cockroach.git","svn_url":"https://github.com/cockroachdb/cockroach","homepage":null,"size":52473,"stargazers_count":5189,"watchers_count":5189,"language":"Go","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":417,"mirror_url":null,"open_issues_count":161,"forks":417,"open_issues":161,"watchers":5189,"default_branch":"master"}},"base":{"label":"cockroachdb:master","ref":"master","sha":"36e28a268d0d2f711ecce3a85997c30ca0c98e98","user":{"login":"cockroachdb","id":6748139,"avatar_url":"https://avatars.githubusercontent.com/u/6748139?v=3","gravatar_id":"","url":"https://api.github.com/users/cockroachdb","html_url":"https://github.com/cockroachdb","followers_url":"https://api.github.com/users/cockroachdb/followers","following_url":"https://api.github.com/users/cockroachdb/following{/other_user}","gists_url":"https://api.github.com/users/cockroachdb/gists{/gist_id}","starred_url":"https://api.github.com/users/cockroachdb/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cockroachdb/subscriptions","organizations_url":"https://api.github.com/users/cockroachdb/orgs","repos_url":"https://api.github.com/users/cockroachdb/repos","events_url":"https://api.github.com/users/cockroachdb/events{/privacy}","received_events_url":"https://api.github.com/users/cockroachdb/received_events","type":"Organization","site_admin":false},"repo":{"id":16563587,"name":"cockroach","full_name":"cockroachdb/cockroach","owner":{"login":"cockroachdb","id":6748139,"avatar_url":"https://avatars.githubusercontent.com/u/6748139?v=3","gravatar_id":"","url":"https://api.github.com/users/cockroachdb","html_url":"https://github.com/cockroachdb","followers_url":"https://api.github.com/users/cockroachdb/followers","following_url":"https://api.github.com/users/cockroachdb/following{/other_user}","gists_url":"https://api.github.com/users/cockroachdb/gists{/gist_id}","starred_url":"https://api.github.com/users/cockroachdb/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cockroachdb/subscriptions","organizations_url":"https://api.github.com/users/cockroachdb/orgs","repos_url":"https://api.github.com/users/cockroachdb/repos","events_url":"https://api.github.com/users/cockroachdb/events{/privacy}","received_events_url":"https://api.github.com/users/cockroachdb/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/cockroachdb/cockroach","description":"A Scalable, Geo-Replicated, Transactional Datastore","fork":false,"url":"https://api.github.com/repos/cockroachdb/cockroach","forks_url":"https://api.github.com/repos/cockroachdb/cockroach/forks","keys_url":"https://api.github.com/repos/cockroachdb/cockroach/keys{/key_id}","collaborators_url":"https://api.github.com/repos/cockroachdb/cockroach/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/cockroachdb/cockroach/teams","hooks_url":"https://api.github.com/repos/cockroachdb/cockroach/hooks","issue_events_url":"https://api.github.com/repos/cockroachdb/cockroach/issues/events{/number}","events_url":"https://api.github.com/repos/cockroachdb/cockroach/events","assignees_url":"https://api.github.com/repos/cockroachdb/cockroach/assignees{/user}","branches_url":"https://api.github.com/repos/cockroachdb/cockroach/branches{/branch}","tags_url":"https://api.github.com/repos/cockroachdb/cockroach/tags","blobs_url":"https://api.github.com/repos/cockroachdb/cockroach/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/cockroachdb/cockroach/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/cockroachdb/cockroach/git/refs{/sha}","trees_url":"https://api.github.com/repos/cockroachdb/cockroach/git/trees{/sha}","statuses_url":"https://api.github.com/repos/cockroachdb/cockroach/statuses/{sha}","languages_url":"https://api.github.com/repos/cockroachdb/cockroach/languages","stargazers_url":"https://api.github.com/repos/cockroachdb/cockroach/stargazers","contributors_url":"https://api.github.com/repos/cockroachdb/cockroach/contributors","subscribers_url":"https://api.github.com/repos/cockroachdb/cockroach/subscribers","subscription_url":"https://api.github.com/repos/cockroachdb/cockroach/subscription","commits_url":"https://api.github.com/repos/cockroachdb/cockroach/commits{/sha}","git_commits_url":"https://api.github.com/repos/cockroachdb/cockroach/git/commits{/sha}","comments_url":"https://api.github.com/repos/cockroachdb/cockroach/comments{/number}","issue_comment_url":"https://api.github.com/repos/cockroachdb/cockroach/issues/comments{/number}","contents_url":"https://api.github.com/repos/cockroachdb/cockroach/contents/{+path}","compare_url":"https://api.github.com/repos/cockroachdb/cockroach/compare/{base}...{head}","merges_url":"https://api.github.com/repos/cockroachdb/cockroach/merges","archive_url":"https://api.github.com/repos/cockroachdb/cockroach/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/cockroachdb/cockroach/downloads","issues_url":"https://api.github.com/repos/cockroachdb/cockroach/issues{/number}","pulls_url":"https://api.github.com/repos/cockroachdb/cockroach/pulls{/number}","milestones_url":"https://api.github.com/repos/cockroachdb/cockroach/milestones{/number}","notifications_url":"https://api.github.com/repos/cockroachdb/cockroach/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/cockroachdb/cockroach/labels{/name}","releases_url":"https://api.github.com/repos/cockroachdb/cockroach/releases{/id}","created_at":"2014-02-06T00:18:47Z","updated_at":"2015-09-09T00:22:23Z","pushed_at":"2015-09-09T00:59:57Z","git_url":"git://github.com/cockroachdb/cockroach.git","ssh_url":"git@github.com:cockroachdb/cockroach.git","clone_url":"https://github.com/cockroachdb/cockroach.git","svn_url":"https://github.com/cockroachdb/cockroach","homepage":null,"size":52473,"stargazers_count":5189,"watchers_count":5189,"language":"Go","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":417,"mirror_url":null,"open_issues_count":161,"forks":417,"open_issues":161,"watchers":5189,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/cockroachdb/cockroach/pulls/2412"},"html":{"href":"https://github.com/cockroachdb/cockroach/pull/2412"},"issue":{"href":"https://api.github.com/repos/cockroachdb/cockroach/issues/2412"},"comments":{"href":"https://api.github.com/repos/cockroachdb/cockroach/issues/2412/comments"},"review_comments":{"href":"https://api.github.com/repos/cockroachdb/cockroach/pulls/2412/comments"},"review_comment":{"href":"https://api.github.com/repos/cockroachdb/cockroach/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/cockroachdb/cockroach/pulls/2412/commits"},"statuses":{"href":"https://api.github.com/repos/cockroachdb/cockroach/statuses/0ef2176ff3b981cdb1bfb863ebd1deb62a68c552"}}}}