{"action":"created","comment":{"url":"https://api.github.com/repos/pingcap/tidb/pulls/comments/41704016","id":41704016,"diff_hunk":"@@ -0,0 +1,164 @@\n+// Copyright 2015 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 expression\n+\n+import (\n+\t\"fmt\"\n+\t\"strings\"\n+\n+\t\"github.com/juju/errors\"\n+\t\"github.com/pingcap/tidb/context\"\n+\t\"github.com/pingcap/tidb/util/types\"\n+)\n+\n+var (\n+\t_ Expression = (*FunctionTrim)(nil)\n+)\n+\n+const (\n+\t// TrimBothDefault trims from both direction by default.\n+\tTrimBothDefault = iota\n+\t// TrimBoth trims from both direction with explicit notation.\n+\tTrimBoth\n+\t// TrimLeading trims from left.\n+\tTrimLeading\n+\t// TrimTrailing trims from right.\n+\tTrimTrailing\n+)\n+\n+// FunctionTrim remove leading/trailing/both remstr.\n+// See: https://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_trim\n+type FunctionTrim struct {\n+\tStr       Expression\n+\tRemStr    Expression\n+\tDirection int\n+}\n+\n+// Clone implements the Expression Clone interface.\n+func (f *FunctionTrim) Clone() Expression {\n+\tnf := &FunctionTrim{\n+\t\tStr:       f.Str.Clone(),\n+\t\tDirection: f.Direction,\n+\t}\n+\tif f.RemStr != nil {\n+\t\tnf.RemStr = f.RemStr.Clone()\n+\t}\n+\treturn nf\n+}\n+\n+// IsStatic implements the Expression IsStatic interface.\n+func (f *FunctionTrim) IsStatic() bool {\n+\treturn f.Str.IsStatic() && (f.RemStr == nil || f.RemStr.IsStatic())\n+}\n+\n+func (f *FunctionTrim) getDirectionStr() string {\n+\tswitch f.Direction {\n+\tcase TrimBoth:\n+\t\treturn \"BOTH\"\n+\tcase TrimLeading:\n+\t\treturn \"LEADING\"\n+\tcase TrimTrailing:\n+\t\treturn \"TRAILING\"\n+\tdefault:\n+\t\treturn \"\"\n+\t}\n+}\n+\n+// String implements the Expression String interface.\n+func (f *FunctionTrim) String() string {\n+\tif f.Direction == TrimBothDefault {\n+\t\tif f.RemStr == nil {\n+\t\t\treturn fmt.Sprintf(\"TRIM(%s)\", f.Str)\n+\t\t}\n+\t\treturn fmt.Sprintf(\"TRIM(%s FROM %s)\", f.RemStr, f.Str)\n+\t}\n+\tif f.RemStr == nil {\n+\t\treturn fmt.Sprintf(\"TRIM(%s FROM %s)\", f.getDirectionStr(), f.Str)\n+\t}\n+\treturn fmt.Sprintf(\"TRIM(%s %s FROM %s)\", f.getDirectionStr(), f.RemStr, f.Str)\n+}\n+\n+// Eval implements the Expression Eval interface.\n+func (f *FunctionTrim) Eval(ctx context.Context, args map[interface{}]interface{}) (interface{}, error) {\n+\t// eval str\n+\tfs, err := f.Str.Eval(ctx, args)\n+\tif err != nil {\n+\t\treturn nil, errors.Trace(err)\n+\t}\n+\tif fs == nil {\n+\t\treturn nil, nil\n+\t}\n+\tstr, err := types.ToString(fs)\n+\tif err != nil {\n+\t\treturn nil, errors.Trace(err)\n+\t}\n+\tremstr := \"\"\n+\t// eval remstr\n+\tif f.RemStr != nil {\n+\t\tfs, err = f.RemStr.Eval(ctx, args)\n+\t\tif err != nil {\n+\t\t\treturn nil, errors.Trace(err)\n+\t\t}\n+\t\tif fs == nil {\n+\t\t\treturn nil, nil\n+\t\t}\n+\t\tremstr, err = types.ToString(fs)\n+\t\tif err != nil {\n+\t\t\treturn nil, errors.Trace(err)\n+\t\t}\n+\t}\n+\t// Do trim\n+\tif f.Direction == TrimLeading {\n+\t\tif len(remstr) > 0 {\n+\t\t\treturn trimLeft(str, remstr), nil\n+\t\t}\n+\t\treturn strings.TrimLeft(str, \"\\n\\t \"), nil\n+\t} else if f.Direction == TrimTrailing {\n+\t\tif len(remstr) > 0 {\n+\t\t\treturn trimRight(str, remstr), nil\n+\t\t}\n+\t\treturn strings.TrimRight(str, \"\\n\\t \"), nil\n+\t}\n+\tif len(remstr) > 0 {\n+\t\tx := trimLeft(str, remstr)\n+\t\tx = trimRight(x, remstr)\n+\t\treturn x, nil\n+\t}\n+\treturn strings.Trim(str, \"\\n\\t \"), nil\n+}\n+\n+func trimLeft(str, remstr string) string {","path":"expression/trim.go","position":141,"original_position":141,"commit_id":"38abb3576931a342736bc4d6393284042fe00bc1","original_commit_id":"38abb3576931a342736bc4d6393284042fe00bc1","user":{"login":"qiuyesuifeng","id":1953644,"avatar_url":"https://avatars.githubusercontent.com/u/1953644?v=3","gravatar_id":"","url":"https://api.github.com/users/qiuyesuifeng","html_url":"https://github.com/qiuyesuifeng","followers_url":"https://api.github.com/users/qiuyesuifeng/followers","following_url":"https://api.github.com/users/qiuyesuifeng/following{/other_user}","gists_url":"https://api.github.com/users/qiuyesuifeng/gists{/gist_id}","starred_url":"https://api.github.com/users/qiuyesuifeng/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/qiuyesuifeng/subscriptions","organizations_url":"https://api.github.com/users/qiuyesuifeng/orgs","repos_url":"https://api.github.com/users/qiuyesuifeng/repos","events_url":"https://api.github.com/users/qiuyesuifeng/events{/privacy}","received_events_url":"https://api.github.com/users/qiuyesuifeng/received_events","type":"User","site_admin":false},"body":"Can use TrimLeft instead?\r\nLike `strings.TrimLeft(\"abcabcxxxabc\", \"abc\")`, the result is `xxxabc`, is it what u want?","created_at":"2015-10-11T01:50:01Z","updated_at":"2015-10-11T01:50:01Z","html_url":"https://github.com/pingcap/tidb/pull/346#discussion_r41704016","pull_request_url":"https://api.github.com/repos/pingcap/tidb/pulls/346","_links":{"self":{"href":"https://api.github.com/repos/pingcap/tidb/pulls/comments/41704016"},"html":{"href":"https://github.com/pingcap/tidb/pull/346#discussion_r41704016"},"pull_request":{"href":"https://api.github.com/repos/pingcap/tidb/pulls/346"}}},"pull_request":{"url":"https://api.github.com/repos/pingcap/tidb/pulls/346","id":47339400,"html_url":"https://github.com/pingcap/tidb/pull/346","diff_url":"https://github.com/pingcap/tidb/pull/346.diff","patch_url":"https://github.com/pingcap/tidb/pull/346.patch","issue_url":"https://api.github.com/repos/pingcap/tidb/issues/346","number":346,"state":"open","locked":false,"title":"*: Support trim function","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":"See: https://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_trim","created_at":"2015-10-10T06:58:42Z","updated_at":"2015-10-11T01:50:01Z","closed_at":null,"merged_at":null,"merge_commit_sha":"0ed22ad1fe4e1a620359b9e278ce98f12be8f6c4","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/pingcap/tidb/pulls/346/commits","review_comments_url":"https://api.github.com/repos/pingcap/tidb/pulls/346/comments","review_comment_url":"https://api.github.com/repos/pingcap/tidb/pulls/comments{/number}","comments_url":"https://api.github.com/repos/pingcap/tidb/issues/346/comments","statuses_url":"https://api.github.com/repos/pingcap/tidb/statuses/38abb3576931a342736bc4d6393284042fe00bc1","head":{"label":"pingcap:shenli/trim","ref":"shenli/trim","sha":"38abb3576931a342736bc4d6393284042fe00bc1","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 SQL 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}","created_at":"2015-09-06T04:01:52Z","updated_at":"2015-10-11T01:15:37Z","pushed_at":"2015-10-11T01:21:09Z","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":9798,"stargazers_count":2560,"watchers_count":2560,"language":"Go","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":241,"mirror_url":null,"open_issues_count":55,"forks":241,"open_issues":55,"watchers":2560,"default_branch":"master"}},"base":{"label":"pingcap:master","ref":"master","sha":"c3d5566b0581e76a8507545c0cf46997986cca32","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 SQL 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}","created_at":"2015-09-06T04:01:52Z","updated_at":"2015-10-11T01:15:37Z","pushed_at":"2015-10-11T01:21:09Z","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":9798,"stargazers_count":2560,"watchers_count":2560,"language":"Go","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":241,"mirror_url":null,"open_issues_count":55,"forks":241,"open_issues":55,"watchers":2560,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/pingcap/tidb/pulls/346"},"html":{"href":"https://github.com/pingcap/tidb/pull/346"},"issue":{"href":"https://api.github.com/repos/pingcap/tidb/issues/346"},"comments":{"href":"https://api.github.com/repos/pingcap/tidb/issues/346/comments"},"review_comments":{"href":"https://api.github.com/repos/pingcap/tidb/pulls/346/comments"},"review_comment":{"href":"https://api.github.com/repos/pingcap/tidb/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/pingcap/tidb/pulls/346/commits"},"statuses":{"href":"https://api.github.com/repos/pingcap/tidb/statuses/38abb3576931a342736bc4d6393284042fe00bc1"}}}}