{"action":"created","comment":{"url":"https://api.github.com/repos/cockroachdb/cockroach/pulls/comments/39780311","id":39780311,"diff_hunk":"@@ -0,0 +1,137 @@\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\"fmt\"\n+\t\"strings\"\n+\n+\t\"github.com/cockroachdb/cockroach/sql/parser\"\n+)\n+\n+// distinct constructs a distinctNode.\n+func (*planner) distinct(n *parser.Select) *distinctNode {\n+\tif n.Distinct {\n+\t\tdistinct := distinctNode{}\n+\t\tdistinct.suffixSeen = make(map[string]struct{})\n+\t\treturn &distinct\n+\t}\n+\treturn nil\n+}\n+\n+type distinctNode struct {\n+\tplanNode\n+\t// All the columns that are part of the Sort. Set to nil if no-sort, or\n+\t// sort used an expression that was not part of the requested column set.\n+\tcolumnsInOrder []bool\n+\t// encoding of the columnsInOrder columns for the previous row.\n+\tprefixSeen string\n+\t// encoding of the non-columnInOrder columns for rows sharing the same\n+\t// prefixSeen value.\n+\tsuffixSeen map[string]struct{}\n+}\n+\n+// wrap the supplied planNode with the distinctNode if distinct is required.\n+func (n *distinctNode) wrap(plan planNode) planNode {\n+\tif n == nil {\n+\t\treturn plan\n+\t}\n+\tn.planNode = plan\n+\tif len(plan.Ordering()) > 0 {\n+\t\tn.columnsInOrder = make([]bool, len(plan.Columns()))\n+\t}\n+\tfor _, p := range plan.Ordering() {\n+\t\tif p < 0 {\n+\t\t\tp = -p\n+\t\t}\n+\t\tif p <= len(n.columnsInOrder) {\n+\t\t\tn.columnsInOrder[p-1] = true\n+\t\t} else {\n+\t\t\t// Cannot use sort order for distinct.\n+\t\t\tn.columnsInOrder = nil\n+\t\t\tbreak\n+\t\t}\n+\t}\n+\treturn n\n+}\n+\n+func (n *distinctNode) Next() bool {\n+\tfor n.planNode.Next() {\n+\t\t// Detect duplicates\n+\t\tprefix, suffix := n.encodeValues(n.Values())\n+\t\tpKey := string(prefix)\n+\t\tsKey := string(suffix)\n+\t\tif pKey == n.prefixSeen {\n+\t\t\t// The prefix of the row is the same as the last row; check\n+\t\t\t// to see if the suffix which is not ordered has been seen.\n+\t\t\tif sKey == \"\" {\n+\t\t\t\t// duplicate\n+\t\t\t\tcontinue\n+\t\t\t}\n+\n+\t\t\tif _, ok := n.suffixSeen[sKey]; ok {\n+\t\t\t\t// duplicate\n+\t\t\t\tcontinue\n+\t\t\t}\n+\t\t} else {\n+\t\t\t// The prefix of the row which is ordered differs from the last row;\n+\t\t\t// reset our seen set.\n+\t\t\tif len(n.suffixSeen) > 0 {\n+\t\t\t\tn.suffixSeen = make(map[string]struct{})\n+\t\t\t}\n+\t\t\tn.prefixSeen = pKey\n+\t\t}\n+\t\tif sKey != \"\" {\n+\t\t\tn.suffixSeen[sKey] = struct{}{}\n+\t\t}\n+\t\treturn true\n+\t}\n+\treturn false\n+}\n+\n+func (n *distinctNode) encodeValues(values parser.DTuple) ([]byte, []byte) {\n+\tprefix := make([]byte, 0, 100)\n+\tsuffix := make([]byte, 0, 100)\n+\tfor i, val := range values {\n+\t\tvar err error\n+\t\tif n.columnsInOrder != nil && n.columnsInOrder[i] {\n+\t\t\tprefix, err = encodeTableKey(prefix, val)\n+\t\t} else {\n+\t\t\tsuffix, err = encodeTableKey(suffix, val)\n+\t\t}\n+\t\tif err != nil {\n+\t\t\tpanic(fmt.Sprintf(\"unable to encode value: %v\", val))","path":"sql/distinct.go","position":118,"original_position":118,"commit_id":"9c19e0c1a598a8cd2c8669bd1cac3c8f08ab5fa1","original_commit_id":"9c19e0c1a598a8cd2c8669bd1cac3c8f08ab5fa1","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":"Probably should return an error here. This is usually done by having a `distinctNode.err` field and implementing the `Err()` method. You'll also need to handle the case where `n.planNode.Next()` returns false because an error occurred. ","created_at":"2015-09-17T18:20:48Z","updated_at":"2015-09-17T18:20:48Z","html_url":"https://github.com/cockroachdb/cockroach/pull/2523#discussion_r39780311","pull_request_url":"https://api.github.com/repos/cockroachdb/cockroach/pulls/2523","_links":{"self":{"href":"https://api.github.com/repos/cockroachdb/cockroach/pulls/comments/39780311"},"html":{"href":"https://github.com/cockroachdb/cockroach/pull/2523#discussion_r39780311"},"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-17T18:20:48Z","closed_at":null,"merged_at":null,"merge_commit_sha":"42109638b195650d7f1b4e4c71720faf633c3343","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/9c19e0c1a598a8cd2c8669bd1cac3c8f08ab5fa1","head":{"label":"cockroachdb:vivek/set","ref":"vivek/set","sha":"9c19e0c1a598a8cd2c8669bd1cac3c8f08ab5fa1","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-17T14:25:42Z","pushed_at":"2015-09-17T18:15:40Z","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":55557,"stargazers_count":5273,"watchers_count":5273,"language":"Go","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":428,"mirror_url":null,"open_issues_count":179,"forks":428,"open_issues":179,"watchers":5273,"default_branch":"master"}},"base":{"label":"cockroachdb:master","ref":"master","sha":"a518d322badade7c50ebe430c432af6e3d1430fb","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-17T14:25:42Z","pushed_at":"2015-09-17T18:15:40Z","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":55557,"stargazers_count":5273,"watchers_count":5273,"language":"Go","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":428,"mirror_url":null,"open_issues_count":179,"forks":428,"open_issues":179,"watchers":5273,"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/9c19e0c1a598a8cd2c8669bd1cac3c8f08ab5fa1"}}}}