{"action":"created","issue":{"url":"https://api.github.com/repos/pingcap/tidb/issues/277","labels_url":"https://api.github.com/repos/pingcap/tidb/issues/277/labels{/name}","comments_url":"https://api.github.com/repos/pingcap/tidb/issues/277/comments","events_url":"https://api.github.com/repos/pingcap/tidb/issues/277/events","html_url":"https://github.com/pingcap/tidb/issues/277","id":108313380,"number":277,"title":"Refactor for optimizer","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},"labels":[{"url":"https://api.github.com/repos/pingcap/tidb/labels/proposal","name":"proposal","color":"eb6420"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-09-25T12:13:48Z","updated_at":"2015-09-25T12:43:17Z","closed_at":null,"body":"Optimizer need to know everything to do the job, like transform the statement/expression, choose best index to use. But currently the only optimization work is done inside plan.Fillter method, which is not capable for the job because a plan knows nothing more than itself.\r\n\r\nThe execution flow can be seperated in three stages.\r\n##### prepare\r\nValidate the statement is legal, make sure table and identifier reference does exists, etc.\r\n##### optimize\r\nTransform the statement into optimized and normalized form and choose index to use.\r\n##### execute\r\nDo execution.\r\n\r\nCurrently the execution is performed directly in `Statment.Exec` method, as a result, the `stmts` package imports a lot of dependencies, makes it hard to be imported by other packages.\r\n\r\nWe have difficulty in doing optimisation right now because of cyclic import restriction.\r\n\r\nplans and rsets can not import stmts.\r\n\r\nSo the proposal is to move the execution functionality out of `stmt/stmts` package, add a new `exec` package and do the optimisation and execution work there. So `stmt/stmts` only keeps basic functionality and can be imported by plans.\r\n\r\nThe result dependency relation is going to be:\r\n\r\nexpression -> stmts -> plans -> rsets -> exec\r\n\r\nPlan interface and Statement interface and their implmentations are going to be in the same package, this also makes it easier to apply visitor pattern.\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/pingcap/tidb/issues/comments/143209039","html_url":"https://github.com/pingcap/tidb/issues/277#issuecomment-143209039","issue_url":"https://api.github.com/repos/pingcap/tidb/issues/277","id":143209039,"user":{"login":"siddontang","id":1080370,"avatar_url":"https://avatars.githubusercontent.com/u/1080370?v=3","gravatar_id":"","url":"https://api.github.com/users/siddontang","html_url":"https://github.com/siddontang","followers_url":"https://api.github.com/users/siddontang/followers","following_url":"https://api.github.com/users/siddontang/following{/other_user}","gists_url":"https://api.github.com/users/siddontang/gists{/gist_id}","starred_url":"https://api.github.com/users/siddontang/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/siddontang/subscriptions","organizations_url":"https://api.github.com/users/siddontang/orgs","repos_url":"https://api.github.com/users/siddontang/repos","events_url":"https://api.github.com/users/siddontang/events{/privacy}","received_events_url":"https://api.github.com/users/siddontang/received_events","type":"User","site_admin":false},"created_at":"2015-09-25T12:43:17Z","updated_at":"2015-09-25T12:43:17Z","body":"what's the structure for PostgreSQL?\r\nMaybe we can refer it. "}}