{"action":"created","comment":{"url":"https://api.github.com/repos/tikv/tikv/pulls/comments/231085980","pull_request_review_id":171970782,"id":231085980,"node_id":"MDI0OlB1bGxSZXF1ZXN0UmV2aWV3Q29tbWVudDIzMTA4NTk4MA==","diff_hunk":"@@ -340,6 +340,125 @@ impl ScalarFunc {\n         };\n         Ok(Some(Cow::Owned(r.into_bytes())))\n     }\n+\n+    #[inline]\n+    pub fn substring_2_args<'a, 'b: 'a>(\n+        &'b self,\n+        ctx: &mut EvalContext,\n+        row: &'a [Datum],\n+    ) -> Result<Option<Cow<'a, [u8]>>> {\n+        let s = try_opt!(self.children[0].eval_string_and_decode(ctx, row));\n+        let pos = try_opt!(self.children[1].eval_int(ctx, row));\n+        if pos == 0 {\n+            return Ok(Some(Cow::Borrowed(b\"\")));\n+        }\n+\n+        let start = if mysql::has_unsigned_flag(self.children[1].get_tp().get_flag()) {\n+            find_start(pos as u64 as usize, s.char_indices())\n+        } else if pos > 0 {\n+            find_start(pos as usize, s.char_indices())\n+        } else {\n+            let pos = 0_i64\n+                .checked_sub(pos)\n+                .map(|i| i as usize)\n+                .unwrap_or_else(|| i64::max_value() as usize + 1);\n+            find_start(pos, s.char_indices().rev())\n+        };\n+\n+        if let Some(start) = start {\n+            Ok(Some(Cow::Owned(s[start..].as_bytes().to_vec())))\n+        } else {\n+            return Ok(Some(Cow::Borrowed(b\"\")));\n+        }\n+    }\n+\n+    #[inline]\n+    pub fn substring_3_args<'a, 'b: 'a>(\n+        &'b self,\n+        ctx: &mut EvalContext,\n+        row: &'a [Datum],\n+    ) -> Result<Option<Cow<'a, [u8]>>> {\n+        let s = try_opt!(self.children[0].eval_string_and_decode(ctx, row));\n+        let pos = try_opt!(self.children[1].eval_int(ctx, row));\n+        let len = try_opt!(self.children[2].eval_int(ctx, row));\n+        let mut positive_search = true;\n+\n+        let pos = if mysql::has_unsigned_flag(self.children[1].get_tp().get_flag()) {\n+            pos as u64 as usize\n+        } else if pos > 0 {\n+            pos as usize\n+        } else {\n+            positive_search = false;\n+            0_i64\n+                .checked_sub(pos)\n+                .map(|i| i as usize)\n+                .unwrap_or_else(|| i64::max_value() as usize + 1)\n+        };\n+\n+        let len = if mysql::has_unsigned_flag(self.children[2].get_tp().get_flag()) {\n+            len as u64 as usize\n+        } else if len > 0 {\n+            len as usize\n+        } else {\n+            0\n+        };\n+\n+        if pos == 0 || len == 0 {\n+            return Ok(Some(Cow::Borrowed(b\"\")));\n+        }\n+        let mut start = None;\n+        let mut end = s.len();\n+        let mut cnt = 1;\n+        if positive_search {\n+            for (i, _) in s.char_indices() {\n+                if cnt > len && (cnt - len) >= pos {\n+                    end = i;\n+                    break;\n+                }\n+                if cnt == pos {\n+                    start = Some(i);\n+                }\n+                cnt += 1;\n+            }\n+        } else {\n+            let mut positions = VecDeque::with_capacity(len);\n+            positions.push_back(end);\n+            for (i, _) in s.char_indices().rev() {","path":"src/coprocessor/dag/expr/builtin_string.rs","position":null,"original_position":87,"commit_id":"07653c6aa54080008dafb94a9e40d1d2a72ef1bf","original_commit_id":"edd8e6313eae9a224b896bbc1f480ca262131d83","user":{"login":"niedhui","id":66329,"node_id":"MDQ6VXNlcjY2MzI5","avatar_url":"https://avatars0.githubusercontent.com/u/66329?v=4","gravatar_id":"","url":"https://api.github.com/users/niedhui","html_url":"https://github.com/niedhui","followers_url":"https://api.github.com/users/niedhui/followers","following_url":"https://api.github.com/users/niedhui/following{/other_user}","gists_url":"https://api.github.com/users/niedhui/gists{/gist_id}","starred_url":"https://api.github.com/users/niedhui/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/niedhui/subscriptions","organizations_url":"https://api.github.com/users/niedhui/orgs","repos_url":"https://api.github.com/users/niedhui/repos","events_url":"https://api.github.com/users/niedhui/events{/privacy}","received_events_url":"https://api.github.com/users/niedhui/received_events","type":"User","site_admin":false},"body":"I updated the code using find here, but it looks a little weird, since it not only find the start or end position,  it also has side-effect  for other variables, what do you think?","created_at":"2018-11-06T11:21:49Z","updated_at":"2018-11-06T11:21:49Z","html_url":"https://github.com/tikv/tikv/pull/3742#discussion_r231085980","pull_request_url":"https://api.github.com/repos/tikv/tikv/pulls/3742","author_association":"CONTRIBUTOR","_links":{"self":{"href":"https://api.github.com/repos/tikv/tikv/pulls/comments/231085980"},"html":{"href":"https://github.com/tikv/tikv/pull/3742#discussion_r231085980"},"pull_request":{"href":"https://api.github.com/repos/tikv/tikv/pulls/3742"}},"in_reply_to_id":230946224},"pull_request":{"url":"https://api.github.com/repos/tikv/tikv/pulls/3742","id":228375339,"node_id":"MDExOlB1bGxSZXF1ZXN0MjI4Mzc1MzM5","html_url":"https://github.com/tikv/tikv/pull/3742","diff_url":"https://github.com/tikv/tikv/pull/3742.diff","patch_url":"https://github.com/tikv/tikv/pull/3742.patch","issue_url":"https://api.github.com/repos/tikv/tikv/issues/3742","number":3742,"state":"open","locked":false,"title":"implement Substring2Args & Substring3Args","user":{"login":"niedhui","id":66329,"node_id":"MDQ6VXNlcjY2MzI5","avatar_url":"https://avatars0.githubusercontent.com/u/66329?v=4","gravatar_id":"","url":"https://api.github.com/users/niedhui","html_url":"https://github.com/niedhui","followers_url":"https://api.github.com/users/niedhui/followers","following_url":"https://api.github.com/users/niedhui/following{/other_user}","gists_url":"https://api.github.com/users/niedhui/gists{/gist_id}","starred_url":"https://api.github.com/users/niedhui/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/niedhui/subscriptions","organizations_url":"https://api.github.com/users/niedhui/orgs","repos_url":"https://api.github.com/users/niedhui/repos","events_url":"https://api.github.com/users/niedhui/events{/privacy}","received_events_url":"https://api.github.com/users/niedhui/received_events","type":"User","site_admin":false},"body":"Signed-off-by: niedhui <niedhui@gmail.com>\r\n\r\n## What have you changed? (mandatory)\r\n\r\nimplment builtin string function: Substring2Args & Substring3Args\r\n\r\n## What are the type of the changes? (mandatory)\r\n\r\nImprovement \r\n\r\n## How has this PR been tested? (mandatory)\r\n\r\nunit test\r\n\r\n## Does this PR affect documentation (docs/docs-cn) update? (mandatory)\r\n\r\nNo\r\n\r\n## Does this PR affect tidb-ansible update? (mandatory)\r\n\r\nNo\r\n\r\n## Refer to a related PR or issue link (optional)\r\n\r\n#3275 \r\n\r\n","created_at":"2018-11-05T14:43:08Z","updated_at":"2018-11-06T11:21:49Z","closed_at":null,"merged_at":null,"merge_commit_sha":"8a249007bc42ad057028f760d3313e3fe22256d1","assignee":null,"assignees":[],"requested_reviewers":[],"requested_teams":[],"labels":[{"id":526233180,"node_id":"MDU6TGFiZWw1MjYyMzMxODA=","url":"https://api.github.com/repos/tikv/tikv/labels/C:%20Copr","name":"C: Copr","color":"d1fad7","default":false},{"id":1013338313,"node_id":"MDU6TGFiZWwxMDEzMzM4MzEz","url":"https://api.github.com/repos/tikv/tikv/labels/T:%20Contributor","name":"T: Contributor","color":"1d76db","default":false}],"milestone":null,"commits_url":"https://api.github.com/repos/tikv/tikv/pulls/3742/commits","review_comments_url":"https://api.github.com/repos/tikv/tikv/pulls/3742/comments","review_comment_url":"https://api.github.com/repos/tikv/tikv/pulls/comments{/number}","comments_url":"https://api.github.com/repos/tikv/tikv/issues/3742/comments","statuses_url":"https://api.github.com/repos/tikv/tikv/statuses/07653c6aa54080008dafb94a9e40d1d2a72ef1bf","head":{"label":"niedhui:niedhui/substring","ref":"niedhui/substring","sha":"07653c6aa54080008dafb94a9e40d1d2a72ef1bf","user":{"login":"niedhui","id":66329,"node_id":"MDQ6VXNlcjY2MzI5","avatar_url":"https://avatars0.githubusercontent.com/u/66329?v=4","gravatar_id":"","url":"https://api.github.com/users/niedhui","html_url":"https://github.com/niedhui","followers_url":"https://api.github.com/users/niedhui/followers","following_url":"https://api.github.com/users/niedhui/following{/other_user}","gists_url":"https://api.github.com/users/niedhui/gists{/gist_id}","starred_url":"https://api.github.com/users/niedhui/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/niedhui/subscriptions","organizations_url":"https://api.github.com/users/niedhui/orgs","repos_url":"https://api.github.com/users/niedhui/repos","events_url":"https://api.github.com/users/niedhui/events{/privacy}","received_events_url":"https://api.github.com/users/niedhui/received_events","type":"User","site_admin":false},"repo":{"id":139168713,"node_id":"MDEwOlJlcG9zaXRvcnkxMzkxNjg3MTM=","name":"tikv","full_name":"niedhui/tikv","private":false,"owner":{"login":"niedhui","id":66329,"node_id":"MDQ6VXNlcjY2MzI5","avatar_url":"https://avatars0.githubusercontent.com/u/66329?v=4","gravatar_id":"","url":"https://api.github.com/users/niedhui","html_url":"https://github.com/niedhui","followers_url":"https://api.github.com/users/niedhui/followers","following_url":"https://api.github.com/users/niedhui/following{/other_user}","gists_url":"https://api.github.com/users/niedhui/gists{/gist_id}","starred_url":"https://api.github.com/users/niedhui/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/niedhui/subscriptions","organizations_url":"https://api.github.com/users/niedhui/orgs","repos_url":"https://api.github.com/users/niedhui/repos","events_url":"https://api.github.com/users/niedhui/events{/privacy}","received_events_url":"https://api.github.com/users/niedhui/received_events","type":"User","site_admin":false},"html_url":"https://github.com/niedhui/tikv","description":"Distributed transactional key value database powered by Rust and Raft","fork":true,"url":"https://api.github.com/repos/niedhui/tikv","forks_url":"https://api.github.com/repos/niedhui/tikv/forks","keys_url":"https://api.github.com/repos/niedhui/tikv/keys{/key_id}","collaborators_url":"https://api.github.com/repos/niedhui/tikv/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/niedhui/tikv/teams","hooks_url":"https://api.github.com/repos/niedhui/tikv/hooks","issue_events_url":"https://api.github.com/repos/niedhui/tikv/issues/events{/number}","events_url":"https://api.github.com/repos/niedhui/tikv/events","assignees_url":"https://api.github.com/repos/niedhui/tikv/assignees{/user}","branches_url":"https://api.github.com/repos/niedhui/tikv/branches{/branch}","tags_url":"https://api.github.com/repos/niedhui/tikv/tags","blobs_url":"https://api.github.com/repos/niedhui/tikv/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/niedhui/tikv/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/niedhui/tikv/git/refs{/sha}","trees_url":"https://api.github.com/repos/niedhui/tikv/git/trees{/sha}","statuses_url":"https://api.github.com/repos/niedhui/tikv/statuses/{sha}","languages_url":"https://api.github.com/repos/niedhui/tikv/languages","stargazers_url":"https://api.github.com/repos/niedhui/tikv/stargazers","contributors_url":"https://api.github.com/repos/niedhui/tikv/contributors","subscribers_url":"https://api.github.com/repos/niedhui/tikv/subscribers","subscription_url":"https://api.github.com/repos/niedhui/tikv/subscription","commits_url":"https://api.github.com/repos/niedhui/tikv/commits{/sha}","git_commits_url":"https://api.github.com/repos/niedhui/tikv/git/commits{/sha}","comments_url":"https://api.github.com/repos/niedhui/tikv/comments{/number}","issue_comment_url":"https://api.github.com/repos/niedhui/tikv/issues/comments{/number}","contents_url":"https://api.github.com/repos/niedhui/tikv/contents/{+path}","compare_url":"https://api.github.com/repos/niedhui/tikv/compare/{base}...{head}","merges_url":"https://api.github.com/repos/niedhui/tikv/merges","archive_url":"https://api.github.com/repos/niedhui/tikv/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/niedhui/tikv/downloads","issues_url":"https://api.github.com/repos/niedhui/tikv/issues{/number}","pulls_url":"https://api.github.com/repos/niedhui/tikv/pulls{/number}","milestones_url":"https://api.github.com/repos/niedhui/tikv/milestones{/number}","notifications_url":"https://api.github.com/repos/niedhui/tikv/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/niedhui/tikv/labels{/name}","releases_url":"https://api.github.com/repos/niedhui/tikv/releases{/id}","deployments_url":"https://api.github.com/repos/niedhui/tikv/deployments","created_at":"2018-06-29T15:51:38Z","updated_at":"2018-08-10T08:41:50Z","pushed_at":"2018-11-06T11:17:22Z","git_url":"git://github.com/niedhui/tikv.git","ssh_url":"git@github.com:niedhui/tikv.git","clone_url":"https://github.com/niedhui/tikv.git","svn_url":"https://github.com/niedhui/tikv","homepage":"https://pingcap.com","size":16923,"stargazers_count":0,"watchers_count":0,"language":"Rust","has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"archived":false,"open_issues_count":0,"license":{"key":"apache-2.0","name":"Apache License 2.0","spdx_id":"Apache-2.0","url":"https://api.github.com/licenses/apache-2.0","node_id":"MDc6TGljZW5zZTI="},"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"tikv:master","ref":"master","sha":"f336a5e9962d49db9ee181fffcaadcde334a30ce","user":{"login":"tikv","id":41004122,"node_id":"MDEyOk9yZ2FuaXphdGlvbjQxMDA0MTIy","avatar_url":"https://avatars2.githubusercontent.com/u/41004122?v=4","gravatar_id":"","url":"https://api.github.com/users/tikv","html_url":"https://github.com/tikv","followers_url":"https://api.github.com/users/tikv/followers","following_url":"https://api.github.com/users/tikv/following{/other_user}","gists_url":"https://api.github.com/users/tikv/gists{/gist_id}","starred_url":"https://api.github.com/users/tikv/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tikv/subscriptions","organizations_url":"https://api.github.com/users/tikv/orgs","repos_url":"https://api.github.com/users/tikv/repos","events_url":"https://api.github.com/users/tikv/events{/privacy}","received_events_url":"https://api.github.com/users/tikv/received_events","type":"Organization","site_admin":false},"repo":{"id":48833910,"node_id":"MDEwOlJlcG9zaXRvcnk0ODgzMzkxMA==","name":"tikv","full_name":"tikv/tikv","private":false,"owner":{"login":"tikv","id":41004122,"node_id":"MDEyOk9yZ2FuaXphdGlvbjQxMDA0MTIy","avatar_url":"https://avatars2.githubusercontent.com/u/41004122?v=4","gravatar_id":"","url":"https://api.github.com/users/tikv","html_url":"https://github.com/tikv","followers_url":"https://api.github.com/users/tikv/followers","following_url":"https://api.github.com/users/tikv/following{/other_user}","gists_url":"https://api.github.com/users/tikv/gists{/gist_id}","starred_url":"https://api.github.com/users/tikv/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tikv/subscriptions","organizations_url":"https://api.github.com/users/tikv/orgs","repos_url":"https://api.github.com/users/tikv/repos","events_url":"https://api.github.com/users/tikv/events{/privacy}","received_events_url":"https://api.github.com/users/tikv/received_events","type":"Organization","site_admin":false},"html_url":"https://github.com/tikv/tikv","description":"Distributed transactional key-value database, originally created to complement TiDB","fork":false,"url":"https://api.github.com/repos/tikv/tikv","forks_url":"https://api.github.com/repos/tikv/tikv/forks","keys_url":"https://api.github.com/repos/tikv/tikv/keys{/key_id}","collaborators_url":"https://api.github.com/repos/tikv/tikv/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/tikv/tikv/teams","hooks_url":"https://api.github.com/repos/tikv/tikv/hooks","issue_events_url":"https://api.github.com/repos/tikv/tikv/issues/events{/number}","events_url":"https://api.github.com/repos/tikv/tikv/events","assignees_url":"https://api.github.com/repos/tikv/tikv/assignees{/user}","branches_url":"https://api.github.com/repos/tikv/tikv/branches{/branch}","tags_url":"https://api.github.com/repos/tikv/tikv/tags","blobs_url":"https://api.github.com/repos/tikv/tikv/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/tikv/tikv/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/tikv/tikv/git/refs{/sha}","trees_url":"https://api.github.com/repos/tikv/tikv/git/trees{/sha}","statuses_url":"https://api.github.com/repos/tikv/tikv/statuses/{sha}","languages_url":"https://api.github.com/repos/tikv/tikv/languages","stargazers_url":"https://api.github.com/repos/tikv/tikv/stargazers","contributors_url":"https://api.github.com/repos/tikv/tikv/contributors","subscribers_url":"https://api.github.com/repos/tikv/tikv/subscribers","subscription_url":"https://api.github.com/repos/tikv/tikv/subscription","commits_url":"https://api.github.com/repos/tikv/tikv/commits{/sha}","git_commits_url":"https://api.github.com/repos/tikv/tikv/git/commits{/sha}","comments_url":"https://api.github.com/repos/tikv/tikv/comments{/number}","issue_comment_url":"https://api.github.com/repos/tikv/tikv/issues/comments{/number}","contents_url":"https://api.github.com/repos/tikv/tikv/contents/{+path}","compare_url":"https://api.github.com/repos/tikv/tikv/compare/{base}...{head}","merges_url":"https://api.github.com/repos/tikv/tikv/merges","archive_url":"https://api.github.com/repos/tikv/tikv/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/tikv/tikv/downloads","issues_url":"https://api.github.com/repos/tikv/tikv/issues{/number}","pulls_url":"https://api.github.com/repos/tikv/tikv/pulls{/number}","milestones_url":"https://api.github.com/repos/tikv/tikv/milestones{/number}","notifications_url":"https://api.github.com/repos/tikv/tikv/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/tikv/tikv/labels{/name}","releases_url":"https://api.github.com/repos/tikv/tikv/releases{/id}","deployments_url":"https://api.github.com/repos/tikv/tikv/deployments","created_at":"2015-12-31T04:51:32Z","updated_at":"2018-11-06T08:30:58Z","pushed_at":"2018-11-06T11:17:24Z","git_url":"git://github.com/tikv/tikv.git","ssh_url":"git@github.com:tikv/tikv.git","clone_url":"https://github.com/tikv/tikv.git","svn_url":"https://github.com/tikv/tikv","homepage":"https://tikv.org","size":17074,"stargazers_count":3876,"watchers_count":3876,"language":"Rust","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":542,"mirror_url":null,"archived":false,"open_issues_count":284,"license":{"key":"apache-2.0","name":"Apache License 2.0","spdx_id":"Apache-2.0","url":"https://api.github.com/licenses/apache-2.0","node_id":"MDc6TGljZW5zZTI="},"forks":542,"open_issues":284,"watchers":3876,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/tikv/tikv/pulls/3742"},"html":{"href":"https://github.com/tikv/tikv/pull/3742"},"issue":{"href":"https://api.github.com/repos/tikv/tikv/issues/3742"},"comments":{"href":"https://api.github.com/repos/tikv/tikv/issues/3742/comments"},"review_comments":{"href":"https://api.github.com/repos/tikv/tikv/pulls/3742/comments"},"review_comment":{"href":"https://api.github.com/repos/tikv/tikv/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/tikv/tikv/pulls/3742/commits"},"statuses":{"href":"https://api.github.com/repos/tikv/tikv/statuses/07653c6aa54080008dafb94a9e40d1d2a72ef1bf"}},"author_association":"CONTRIBUTOR"}}
{"actor":{"display_login":"niedhui"}}