{"action":"created","comment":{"url":"https://api.github.com/repos/cockroachdb/cockroach/pulls/comments/39644813","id":39644813,"diff_hunk":"@@ -0,0 +1,181 @@\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: Tamir Duberstein (tamird@gmail.com)\n+\n+package sql\n+\n+import (\n+\t\"crypto/md5\"\n+\t\"fmt\"\n+\t\"math\"\n+\t\"strconv\"\n+\n+\t\"github.com/cockroachdb/cockroach/sql/parser\"\n+)\n+\n+// presentation constructs a presentationNode.\n+func (*planner) presentation(n *parser.Select, p planNode, s *scanNode) (planNode, error) {\n+\tif n.Limit == nil && n.Distinct == nil {\n+\t\treturn p, nil\n+\t}\n+\t// Mark sort node as not presenting the results.\n+\tsNode, ok := p.(*sortNode)\n+\tif ok {\n+\t\tsNode.dontPresent = true\n+\t}\n+\tvar count, offset int64\n+\tif n.Limit != nil {\n+\t\tdata := []struct {\n+\t\t\tname       string\n+\t\t\tsrc        parser.Expr\n+\t\t\tdst        *int64\n+\t\t\tdefaultVal int64\n+\t\t}{\n+\t\t\t{\"LIMIT\", n.Limit.Count, &count, math.MaxInt64},\n+\t\t\t{\"OFFSET\", n.Limit.Offset, &offset, 0},\n+\t\t}\n+\n+\t\tfor _, datum := range data {\n+\t\t\tif datum.src == nil {\n+\t\t\t\t*datum.dst = datum.defaultVal\n+\t\t\t} else {\n+\t\t\t\tif parser.ContainsVars(datum.src) {\n+\t\t\t\t\treturn nil, fmt.Errorf(\"argument of %s must not contain variables\", datum.name)\n+\t\t\t\t}\n+\n+\t\t\t\tnormalized, err := parser.NormalizeExpr(datum.src)\n+\t\t\t\tif err != nil {\n+\t\t\t\t\treturn nil, err\n+\t\t\t\t}\n+\t\t\t\tdstDatum, err := parser.EvalExpr(normalized)\n+\t\t\t\tif err != nil {\n+\t\t\t\t\treturn nil, err\n+\t\t\t\t}\n+\n+\t\t\t\tif dstDatum == parser.DNull {\n+\t\t\t\t\t*datum.dst = datum.defaultVal\n+\t\t\t\t\tcontinue\n+\t\t\t\t}\n+\n+\t\t\t\tif dstDInt, ok := dstDatum.(parser.DInt); ok {\n+\t\t\t\t\t*datum.dst = int64(dstDInt)\n+\t\t\t\t\tcontinue\n+\t\t\t\t}\n+\n+\t\t\t\treturn nil, fmt.Errorf(\"argument of %s must be type %s, not type %s\", datum.name, parser.DummyInt.Type(), dstDatum.Type())\n+\t\t\t}\n+\t\t}\n+\t}\n+\tpNode := &presentationNode{planNode: p, columns: p.Columns(), limit: (n.Limit != nil), count: count, offset: offset}\n+\n+\tif n.Distinct != nil {\n+\t\tpNode.checksums = make(map[[md5.Size]byte]struct{})\n+\t\tpNode.distinctIdx = make([]int, 0, len(n.Distinct))\n+\t\tfor _, expr := range n.Distinct {\n+\t\t\tindex, err := s.maybeAddRender(expr)\n+\t\t\tif err != nil {\n+\t\t\t\treturn nil, err\n+\t\t\t}\n+\t\t\tpNode.distinctIdx = append(pNode.distinctIdx, index)\n+\t\t}\n+\t}\n+\treturn pNode, nil\n+}\n+\n+type presentationNode struct {\n+\tplanNode\n+\t// This map is surely going to overflow memory.\n+\t// TODO(vivek): Return an error once map has grown above a configured threshold.","path":"sql/presentation.go","position":101,"original_position":101,"commit_id":"60a6d86fd2e0f5b18c6fadb5db2f0b6c5cc6011f","original_commit_id":"60a6d86fd2e0f5b18c6fadb5db2f0b6c5cc6011f","user":{"login":"vivekmenezes","id":11774212,"avatar_url":"https://avatars.githubusercontent.com/u/11774212?v=3","gravatar_id":"","url":"https://api.github.com/users/vivekmenezes","html_url":"https://github.com/vivekmenezes","followers_url":"https://api.github.com/users/vivekmenezes/followers","following_url":"https://api.github.com/users/vivekmenezes/following{/other_user}","gists_url":"https://api.github.com/users/vivekmenezes/gists{/gist_id}","starred_url":"https://api.github.com/users/vivekmenezes/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vivekmenezes/subscriptions","organizations_url":"https://api.github.com/users/vivekmenezes/orgs","repos_url":"https://api.github.com/users/vivekmenezes/repos","events_url":"https://api.github.com/users/vivekmenezes/events{/privacy}","received_events_url":"https://api.github.com/users/vivekmenezes/received_events","type":"User","site_admin":false},"body":"yeah we could do that but I think what might make sense is to actually always sort when the results are not in the right order. The problem is then limited to how do you scale sorting.","created_at":"2015-09-16T15:29:54Z","updated_at":"2015-09-16T15:29:54Z","html_url":"https://github.com/cockroachdb/cockroach/pull/2523#discussion_r39644813","pull_request_url":"https://api.github.com/repos/cockroachdb/cockroach/pulls/2523","_links":{"self":{"href":"https://api.github.com/repos/cockroachdb/cockroach/pulls/comments/39644813"},"html":{"href":"https://github.com/cockroachdb/cockroach/pull/2523#discussion_r39644813"},"pull_request":{"href":"https://api.github.com/repos/cockroachdb/cockroach/pulls/2523"}}},"pull_request":{"url":"https://api.github.com/repos/cockroachdb/cockroach/pulls/2523","id":45170169,"html_url":"https://github.com/cockroachdb/cockroach/pull/2523","diff_url":"https://github.com/cockroachdb/cockroach/pull/2523.diff","patch_url":"https://github.com/cockroachdb/cockroach/pull/2523.patch","issue_url":"https://api.github.com/repos/cockroachdb/cockroach/issues/2523","number":2523,"state":"open","locked":false,"title":"Add DISTINCT/DISTINCT ON","user":{"login":"vivekmenezes","id":11774212,"avatar_url":"https://avatars.githubusercontent.com/u/11774212?v=3","gravatar_id":"","url":"https://api.github.com/users/vivekmenezes","html_url":"https://github.com/vivekmenezes","followers_url":"https://api.github.com/users/vivekmenezes/followers","following_url":"https://api.github.com/users/vivekmenezes/following{/other_user}","gists_url":"https://api.github.com/users/vivekmenezes/gists{/gist_id}","starred_url":"https://api.github.com/users/vivekmenezes/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vivekmenezes/subscriptions","organizations_url":"https://api.github.com/users/vivekmenezes/orgs","repos_url":"https://api.github.com/users/vivekmenezes/repos","events_url":"https://api.github.com/users/vivekmenezes/events{/privacy}","received_events_url":"https://api.github.com/users/vivekmenezes/received_events","type":"User","site_admin":false},"body":"closes #2247","created_at":"2015-09-16T13:21:04Z","updated_at":"2015-09-16T15:29:54Z","closed_at":null,"merged_at":null,"merge_commit_sha":"6bab6cc5946d6ad9540ec389bf88c66ea1e0ca6b","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/cockroachdb/cockroach/pulls/2523/commits","review_comments_url":"https://api.github.com/repos/cockroachdb/cockroach/pulls/2523/comments","review_comment_url":"https://api.github.com/repos/cockroachdb/cockroach/pulls/comments{/number}","comments_url":"https://api.github.com/repos/cockroachdb/cockroach/issues/2523/comments","statuses_url":"https://api.github.com/repos/cockroachdb/cockroach/statuses/60a6d86fd2e0f5b18c6fadb5db2f0b6c5cc6011f","head":{"label":"cockroachdb:vivek/set","ref":"vivek/set","sha":"60a6d86fd2e0f5b18c6fadb5db2f0b6c5cc6011f","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-16T15:12:00Z","pushed_at":"2015-09-16T15:18:58Z","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":55036,"stargazers_count":5258,"watchers_count":5258,"language":"Go","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":426,"mirror_url":null,"open_issues_count":179,"forks":426,"open_issues":179,"watchers":5258,"default_branch":"master"}},"base":{"label":"cockroachdb:master","ref":"master","sha":"66314f7d4f82890630c733014706631293556481","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-16T15:12:00Z","pushed_at":"2015-09-16T15:18:58Z","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":55036,"stargazers_count":5258,"watchers_count":5258,"language":"Go","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":426,"mirror_url":null,"open_issues_count":179,"forks":426,"open_issues":179,"watchers":5258,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/cockroachdb/cockroach/pulls/2523"},"html":{"href":"https://github.com/cockroachdb/cockroach/pull/2523"},"issue":{"href":"https://api.github.com/repos/cockroachdb/cockroach/issues/2523"},"comments":{"href":"https://api.github.com/repos/cockroachdb/cockroach/issues/2523/comments"},"review_comments":{"href":"https://api.github.com/repos/cockroachdb/cockroach/pulls/2523/comments"},"review_comment":{"href":"https://api.github.com/repos/cockroachdb/cockroach/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/cockroachdb/cockroach/pulls/2523/commits"},"statuses":{"href":"https://api.github.com/repos/cockroachdb/cockroach/statuses/60a6d86fd2e0f5b18c6fadb5db2f0b6c5cc6011f"}}}}