{"action":"created","comment":{"url":"https://api.github.com/repos/pingcap/tidb/pulls/comments/53913296","id":53913296,"diff_hunk":"@@ -0,0 +1,216 @@\n+// Copyright 2016 PingCAP, Inc.\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+// See the License for the specific language governing permissions and\n+// limitations under the License.\n+\n+package executor\n+\n+import (\n+\t\"strconv\"\n+\t\"strings\"\n+\n+\t\"github.com/pingcap/tidb/ast\"\n+\t\"github.com/pingcap/tidb/optimizer/plan\"\n+\t\"github.com/pingcap/tidb/parser/opcode\"\n+)\n+\n+type explainEntry struct {\n+\tID           int64\n+\tselectType   string\n+\ttable        string\n+\tjoinType     string\n+\tpossibleKeys string\n+\tkey          string\n+\tkeyLen       string\n+\tref          string\n+\trows         int64\n+\textra        []string\n+}\n+\n+func (e *explainEntry) setJoinTypeForTableScan(p *plan.TableScan) {\n+\tif len(p.AccessConditions) == 0 {\n+\t\te.joinType = \"ALL\"\n+\t\treturn\n+\t}\n+\tif p.RefAccess {\n+\t\te.joinType = \"eq_ref\"\n+\t\treturn\n+\t}\n+\tfor _, con := range p.AccessConditions {\n+\t\tif x, ok := con.(*ast.BinaryOperationExpr); ok {\n+\t\t\tif x.Op == opcode.EQ {\n+\t\t\t\te.joinType = \"const\"\n+\t\t\t\treturn\n+\t\t\t}\n+\t\t}\n+\t}\n+\te.joinType = \"range\"\n+}\n+\n+func (e *explainEntry) setJoinTypeForIndexScan(p *plan.IndexScan) {\n+\tif len(p.AccessConditions) == 0 {\n+\t\te.joinType = \"index\"\n+\t\treturn\n+\t}\n+\tif len(p.AccessConditions) == p.AccessEqualCount {\n+\t\tif p.RefAccess {\n+\t\t\tif p.Index.Unique {\n+\t\t\t\te.joinType = \"eq_ref\"\n+\t\t\t} else {\n+\t\t\t\te.joinType = \"ref\"\n+\t\t\t}\n+\t\t} else {\n+\t\t\tif p.Index.Unique {\n+\t\t\t\te.joinType = \"const\"\n+\t\t\t} else {\n+\t\t\t\te.joinType = \"range\"\n+\t\t\t}\n+\t\t}\n+\t\treturn\n+\t}\n+\te.joinType = \"range\"\n+}\n+\n+// ExplainExec represents an explain executor.\n+// See: https://dev.mysql.com/doc/refman/5.7/en/explain-output.html\n+type ExplainExec struct {\n+\tStmtPlan plan.Plan\n+\tfields   []*ast.ResultField\n+\trows     []*Row\n+\tcursor   int\n+}\n+\n+// Fields implements Executor Fields interface.\n+func (e *ExplainExec) Fields() []*ast.ResultField {\n+\treturn e.fields\n+}\n+\n+// Next implements Execution Next interface.\n+func (e *ExplainExec) Next() (*Row, error) {\n+\tif e.rows == nil {\n+\t\te.fetchRows()\n+\t}\n+\tif e.cursor >= len(e.rows) {\n+\t\treturn nil, nil\n+\t}\n+\trow := e.rows[e.cursor]\n+\te.cursor++\n+\treturn row, nil\n+}\n+\n+func (e *ExplainExec) fetchRows() {\n+\tvisitor := &explainVisitor{id: 1}\n+\te.StmtPlan.Accept(visitor)\n+\tfor _, entry := range visitor.entries {\n+\t\trow := &Row{}\n+\t\trow.Data = []interface{}{\n+\t\t\tentry.ID,\n+\t\t\tentry.selectType,\n+\t\t\tentry.table,\n+\t\t\tentry.joinType,\n+\t\t\tentry.key,\n+\t\t\tentry.key,\n+\t\t\tentry.keyLen,\n+\t\t\tentry.ref,\n+\t\t\tentry.rows,\n+\t\t\tstrings.Join(entry.extra, \"; \"),\n+\t\t}\n+\t\tfor i := range row.Data {\n+\t\t\tif row.Data[i] == \"\" {\n+\t\t\t\trow.Data[i] = nil\n+\t\t\t}\n+\t\t}\n+\t\te.rows = append(e.rows, row)\n+\t}\n+}\n+\n+// Close implements Executor Close interface.\n+func (e *ExplainExec) Close() error {\n+\treturn nil\n+}\n+\n+type explainVisitor struct {\n+\tid int64\n+\n+\t// Sort extra should be appended in the first table in a join.\n+\tsort    bool\n+\tentries []*explainEntry\n+}\n+\n+func (v *explainVisitor) Enter(p plan.Plan) (plan.Plan, bool) {\n+\tswitch x := p.(type) {\n+\tcase *plan.TableScan:\n+\t\tv.entries = append(v.entries, v.newEntryForTableScan(x))\n+\tcase *plan.IndexScan:\n+\t\tv.entries = append(v.entries, v.newEntryForIndexScan(x))\n+\tcase *plan.Sort:\n+\t\tv.sort = true\n+\t}\n+\treturn p, false\n+}\n+\n+func (v *explainVisitor) Leave(p plan.Plan) (plan.Plan, bool) {\n+\treturn p, true\n+}\n+\n+func (v *explainVisitor) newEntryForTableScan(p *plan.TableScan) *explainEntry {\n+\tentry := &explainEntry{\n+\t\tID:         v.id,\n+\t\tselectType: \"SIMPLE\",\n+\t\ttable:      p.Table.Name.O,\n+\t}\n+\tentry.setJoinTypeForTableScan(p)\n+\tif entry.joinType != \"ALL\" {\n+\t\tentry.key = \"PRIMARY\"","path":"executor/explain.go","position":172,"original_position":172,"commit_id":"2a639f4e600af5accf9bcfa67e90171487e74e92","original_commit_id":"2a639f4e600af5accf9bcfa67e90171487e74e92","user":{"login":"shenli","id":1192573,"avatar_url":"https://avatars.githubusercontent.com/u/1192573?v=3","gravatar_id":"","url":"https://api.github.com/users/shenli","html_url":"https://github.com/shenli","followers_url":"https://api.github.com/users/shenli/followers","following_url":"https://api.github.com/users/shenli/following{/other_user}","gists_url":"https://api.github.com/users/shenli/gists{/gist_id}","starred_url":"https://api.github.com/users/shenli/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/shenli/subscriptions","organizations_url":"https://api.github.com/users/shenli/orgs","repos_url":"https://api.github.com/users/shenli/repos","events_url":"https://api.github.com/users/shenli/events{/privacy}","received_events_url":"https://api.github.com/users/shenli/received_events","type":"User","site_admin":false},"body":"why?","created_at":"2016-02-24T09:34:36Z","updated_at":"2016-02-24T09:34:36Z","html_url":"https://github.com/pingcap/tidb/pull/918#discussion_r53913296","pull_request_url":"https://api.github.com/repos/pingcap/tidb/pulls/918","_links":{"self":{"href":"https://api.github.com/repos/pingcap/tidb/pulls/comments/53913296"},"html":{"href":"https://github.com/pingcap/tidb/pull/918#discussion_r53913296"},"pull_request":{"href":"https://api.github.com/repos/pingcap/tidb/pulls/918"}}},"pull_request":{"url":"https://api.github.com/repos/pingcap/tidb/pulls/918","id":60326570,"html_url":"https://github.com/pingcap/tidb/pull/918","diff_url":"https://github.com/pingcap/tidb/pull/918.diff","patch_url":"https://github.com/pingcap/tidb/pull/918.patch","issue_url":"https://api.github.com/repos/pingcap/tidb/issues/918","number":918,"state":"open","locked":false,"title":"executor: support Explain","user":{"login":"coocood","id":891222,"avatar_url":"https://avatars.githubusercontent.com/u/891222?v=3","gravatar_id":"","url":"https://api.github.com/users/coocood","html_url":"https://github.com/coocood","followers_url":"https://api.github.com/users/coocood/followers","following_url":"https://api.github.com/users/coocood/following{/other_user}","gists_url":"https://api.github.com/users/coocood/gists{/gist_id}","starred_url":"https://api.github.com/users/coocood/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/coocood/subscriptions","organizations_url":"https://api.github.com/users/coocood/orgs","repos_url":"https://api.github.com/users/coocood/repos","events_url":"https://api.github.com/users/coocood/events{/privacy}","received_events_url":"https://api.github.com/users/coocood/received_events","type":"User","site_admin":false},"body":"","created_at":"2016-02-23T11:39:37Z","updated_at":"2016-02-24T09:34:36Z","closed_at":null,"merged_at":null,"merge_commit_sha":"3283228b33f004f7895cab9b6f74e2ac3833f0ce","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/pingcap/tidb/pulls/918/commits","review_comments_url":"https://api.github.com/repos/pingcap/tidb/pulls/918/comments","review_comment_url":"https://api.github.com/repos/pingcap/tidb/pulls/comments{/number}","comments_url":"https://api.github.com/repos/pingcap/tidb/issues/918/comments","statuses_url":"https://api.github.com/repos/pingcap/tidb/statuses/2a639f4e600af5accf9bcfa67e90171487e74e92","head":{"label":"pingcap:coocood/explain","ref":"coocood/explain","sha":"2a639f4e600af5accf9bcfa67e90171487e74e92","user":{"login":"pingcap","id":11855343,"avatar_url":"https://avatars.githubusercontent.com/u/11855343?v=3","gravatar_id":"","url":"https://api.github.com/users/pingcap","html_url":"https://github.com/pingcap","followers_url":"https://api.github.com/users/pingcap/followers","following_url":"https://api.github.com/users/pingcap/following{/other_user}","gists_url":"https://api.github.com/users/pingcap/gists{/gist_id}","starred_url":"https://api.github.com/users/pingcap/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pingcap/subscriptions","organizations_url":"https://api.github.com/users/pingcap/orgs","repos_url":"https://api.github.com/users/pingcap/repos","events_url":"https://api.github.com/users/pingcap/events{/privacy}","received_events_url":"https://api.github.com/users/pingcap/received_events","type":"Organization","site_admin":false},"repo":{"id":41986369,"name":"tidb","full_name":"pingcap/tidb","owner":{"login":"pingcap","id":11855343,"avatar_url":"https://avatars.githubusercontent.com/u/11855343?v=3","gravatar_id":"","url":"https://api.github.com/users/pingcap","html_url":"https://github.com/pingcap","followers_url":"https://api.github.com/users/pingcap/followers","following_url":"https://api.github.com/users/pingcap/following{/other_user}","gists_url":"https://api.github.com/users/pingcap/gists{/gist_id}","starred_url":"https://api.github.com/users/pingcap/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pingcap/subscriptions","organizations_url":"https://api.github.com/users/pingcap/orgs","repos_url":"https://api.github.com/users/pingcap/repos","events_url":"https://api.github.com/users/pingcap/events{/privacy}","received_events_url":"https://api.github.com/users/pingcap/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/pingcap/tidb","description":"TiDB is a distributed NewSQL database compatible with MySQL protocol ","fork":false,"url":"https://api.github.com/repos/pingcap/tidb","forks_url":"https://api.github.com/repos/pingcap/tidb/forks","keys_url":"https://api.github.com/repos/pingcap/tidb/keys{/key_id}","collaborators_url":"https://api.github.com/repos/pingcap/tidb/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/pingcap/tidb/teams","hooks_url":"https://api.github.com/repos/pingcap/tidb/hooks","issue_events_url":"https://api.github.com/repos/pingcap/tidb/issues/events{/number}","events_url":"https://api.github.com/repos/pingcap/tidb/events","assignees_url":"https://api.github.com/repos/pingcap/tidb/assignees{/user}","branches_url":"https://api.github.com/repos/pingcap/tidb/branches{/branch}","tags_url":"https://api.github.com/repos/pingcap/tidb/tags","blobs_url":"https://api.github.com/repos/pingcap/tidb/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/pingcap/tidb/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/pingcap/tidb/git/refs{/sha}","trees_url":"https://api.github.com/repos/pingcap/tidb/git/trees{/sha}","statuses_url":"https://api.github.com/repos/pingcap/tidb/statuses/{sha}","languages_url":"https://api.github.com/repos/pingcap/tidb/languages","stargazers_url":"https://api.github.com/repos/pingcap/tidb/stargazers","contributors_url":"https://api.github.com/repos/pingcap/tidb/contributors","subscribers_url":"https://api.github.com/repos/pingcap/tidb/subscribers","subscription_url":"https://api.github.com/repos/pingcap/tidb/subscription","commits_url":"https://api.github.com/repos/pingcap/tidb/commits{/sha}","git_commits_url":"https://api.github.com/repos/pingcap/tidb/git/commits{/sha}","comments_url":"https://api.github.com/repos/pingcap/tidb/comments{/number}","issue_comment_url":"https://api.github.com/repos/pingcap/tidb/issues/comments{/number}","contents_url":"https://api.github.com/repos/pingcap/tidb/contents/{+path}","compare_url":"https://api.github.com/repos/pingcap/tidb/compare/{base}...{head}","merges_url":"https://api.github.com/repos/pingcap/tidb/merges","archive_url":"https://api.github.com/repos/pingcap/tidb/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/pingcap/tidb/downloads","issues_url":"https://api.github.com/repos/pingcap/tidb/issues{/number}","pulls_url":"https://api.github.com/repos/pingcap/tidb/pulls{/number}","milestones_url":"https://api.github.com/repos/pingcap/tidb/milestones{/number}","notifications_url":"https://api.github.com/repos/pingcap/tidb/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/pingcap/tidb/labels{/name}","releases_url":"https://api.github.com/repos/pingcap/tidb/releases{/id}","deployments_url":"https://api.github.com/repos/pingcap/tidb/deployments","created_at":"2015-09-06T04:01:52Z","updated_at":"2016-02-24T09:07:32Z","pushed_at":"2016-02-24T08:21:23Z","git_url":"git://github.com/pingcap/tidb.git","ssh_url":"git@github.com:pingcap/tidb.git","clone_url":"https://github.com/pingcap/tidb.git","svn_url":"https://github.com/pingcap/tidb","homepage":"","size":10951,"stargazers_count":3451,"watchers_count":3451,"language":"Go","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":394,"mirror_url":null,"open_issues_count":60,"forks":394,"open_issues":60,"watchers":3451,"default_branch":"master"}},"base":{"label":"pingcap:master","ref":"master","sha":"77bedd0794223a4e06de7e2855004747d5167ab9","user":{"login":"pingcap","id":11855343,"avatar_url":"https://avatars.githubusercontent.com/u/11855343?v=3","gravatar_id":"","url":"https://api.github.com/users/pingcap","html_url":"https://github.com/pingcap","followers_url":"https://api.github.com/users/pingcap/followers","following_url":"https://api.github.com/users/pingcap/following{/other_user}","gists_url":"https://api.github.com/users/pingcap/gists{/gist_id}","starred_url":"https://api.github.com/users/pingcap/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pingcap/subscriptions","organizations_url":"https://api.github.com/users/pingcap/orgs","repos_url":"https://api.github.com/users/pingcap/repos","events_url":"https://api.github.com/users/pingcap/events{/privacy}","received_events_url":"https://api.github.com/users/pingcap/received_events","type":"Organization","site_admin":false},"repo":{"id":41986369,"name":"tidb","full_name":"pingcap/tidb","owner":{"login":"pingcap","id":11855343,"avatar_url":"https://avatars.githubusercontent.com/u/11855343?v=3","gravatar_id":"","url":"https://api.github.com/users/pingcap","html_url":"https://github.com/pingcap","followers_url":"https://api.github.com/users/pingcap/followers","following_url":"https://api.github.com/users/pingcap/following{/other_user}","gists_url":"https://api.github.com/users/pingcap/gists{/gist_id}","starred_url":"https://api.github.com/users/pingcap/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pingcap/subscriptions","organizations_url":"https://api.github.com/users/pingcap/orgs","repos_url":"https://api.github.com/users/pingcap/repos","events_url":"https://api.github.com/users/pingcap/events{/privacy}","received_events_url":"https://api.github.com/users/pingcap/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/pingcap/tidb","description":"TiDB is a distributed NewSQL database compatible with MySQL protocol ","fork":false,"url":"https://api.github.com/repos/pingcap/tidb","forks_url":"https://api.github.com/repos/pingcap/tidb/forks","keys_url":"https://api.github.com/repos/pingcap/tidb/keys{/key_id}","collaborators_url":"https://api.github.com/repos/pingcap/tidb/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/pingcap/tidb/teams","hooks_url":"https://api.github.com/repos/pingcap/tidb/hooks","issue_events_url":"https://api.github.com/repos/pingcap/tidb/issues/events{/number}","events_url":"https://api.github.com/repos/pingcap/tidb/events","assignees_url":"https://api.github.com/repos/pingcap/tidb/assignees{/user}","branches_url":"https://api.github.com/repos/pingcap/tidb/branches{/branch}","tags_url":"https://api.github.com/repos/pingcap/tidb/tags","blobs_url":"https://api.github.com/repos/pingcap/tidb/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/pingcap/tidb/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/pingcap/tidb/git/refs{/sha}","trees_url":"https://api.github.com/repos/pingcap/tidb/git/trees{/sha}","statuses_url":"https://api.github.com/repos/pingcap/tidb/statuses/{sha}","languages_url":"https://api.github.com/repos/pingcap/tidb/languages","stargazers_url":"https://api.github.com/repos/pingcap/tidb/stargazers","contributors_url":"https://api.github.com/repos/pingcap/tidb/contributors","subscribers_url":"https://api.github.com/repos/pingcap/tidb/subscribers","subscription_url":"https://api.github.com/repos/pingcap/tidb/subscription","commits_url":"https://api.github.com/repos/pingcap/tidb/commits{/sha}","git_commits_url":"https://api.github.com/repos/pingcap/tidb/git/commits{/sha}","comments_url":"https://api.github.com/repos/pingcap/tidb/comments{/number}","issue_comment_url":"https://api.github.com/repos/pingcap/tidb/issues/comments{/number}","contents_url":"https://api.github.com/repos/pingcap/tidb/contents/{+path}","compare_url":"https://api.github.com/repos/pingcap/tidb/compare/{base}...{head}","merges_url":"https://api.github.com/repos/pingcap/tidb/merges","archive_url":"https://api.github.com/repos/pingcap/tidb/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/pingcap/tidb/downloads","issues_url":"https://api.github.com/repos/pingcap/tidb/issues{/number}","pulls_url":"https://api.github.com/repos/pingcap/tidb/pulls{/number}","milestones_url":"https://api.github.com/repos/pingcap/tidb/milestones{/number}","notifications_url":"https://api.github.com/repos/pingcap/tidb/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/pingcap/tidb/labels{/name}","releases_url":"https://api.github.com/repos/pingcap/tidb/releases{/id}","deployments_url":"https://api.github.com/repos/pingcap/tidb/deployments","created_at":"2015-09-06T04:01:52Z","updated_at":"2016-02-24T09:07:32Z","pushed_at":"2016-02-24T08:21:23Z","git_url":"git://github.com/pingcap/tidb.git","ssh_url":"git@github.com:pingcap/tidb.git","clone_url":"https://github.com/pingcap/tidb.git","svn_url":"https://github.com/pingcap/tidb","homepage":"","size":10951,"stargazers_count":3451,"watchers_count":3451,"language":"Go","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":394,"mirror_url":null,"open_issues_count":60,"forks":394,"open_issues":60,"watchers":3451,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/pingcap/tidb/pulls/918"},"html":{"href":"https://github.com/pingcap/tidb/pull/918"},"issue":{"href":"https://api.github.com/repos/pingcap/tidb/issues/918"},"comments":{"href":"https://api.github.com/repos/pingcap/tidb/issues/918/comments"},"review_comments":{"href":"https://api.github.com/repos/pingcap/tidb/pulls/918/comments"},"review_comment":{"href":"https://api.github.com/repos/pingcap/tidb/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/pingcap/tidb/pulls/918/commits"},"statuses":{"href":"https://api.github.com/repos/pingcap/tidb/statuses/2a639f4e600af5accf9bcfa67e90171487e74e92"}}}}