-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Add string function split_part #1445
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -761,4 +761,61 @@ StringVal StringFunctions::money_format(FunctionContext *context, const LargeInt | |||||
| return do_money_format(context, ss.str()); | ||||||
| } | ||||||
|
|
||||||
| static int indexOf(const uint8_t* source, int sourceOffset, int sourceCount, | ||||||
| const uint8_t* target, int targetOffset, int targetCount, | ||||||
| int fromIndex) { | ||||||
| if (fromIndex >= sourceCount) { | ||||||
| return (targetCount == 0 ? sourceCount : -1); | ||||||
| } | ||||||
| if (fromIndex < 0) { | ||||||
| fromIndex = 0; | ||||||
| } | ||||||
| if (targetCount == 0) { | ||||||
| return fromIndex; | ||||||
| } | ||||||
| const uint8_t first = target[targetOffset]; | ||||||
| int max = sourceOffset + (sourceCount - targetCount); | ||||||
| for (int i = sourceOffset + fromIndex; i <= max; i++) { | ||||||
| if (source[i] != first) { // Look for first character | ||||||
| while (++i <= max && source[i] != first); | ||||||
| } | ||||||
| if (i <= max) { // Found first character, now look at the rest of v2 | ||||||
| int j = i + 1; | ||||||
| int end = j + targetCount - 1; | ||||||
| for (int k = targetOffset + 1; j < end && source[j] == target[k]; j++, k++); | ||||||
| if (j == end) { | ||||||
| return i - sourceOffset; // Found whole string. | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
| return -1; | ||||||
| } | ||||||
|
|
||||||
|
|
||||||
| StringVal StringFunctions::split_part(FunctionContext* context,const StringVal& content, | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| const StringVal& delimiter,const IntVal& field) { | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| if (field.val <= 0) return StringVal::null(); | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if content、delimiter or field is null? |
||||||
| int find[field.val]; //store substring position | ||||||
| for(int i=0;i<=field.val;i++) find[i] = -1; // init | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| int from = 0; | ||||||
| for(int i=1;i<=field.val;i++){ // find | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| find[i-1] = indexOf(content.ptr,0,content.len, delimiter.ptr,0,delimiter.len,from); | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
You should add one space after punctuation |
||||||
| from = find[i-1] + 1; | ||||||
| if (find[i-1] == -1) { | ||||||
| break; | ||||||
| } | ||||||
| } | ||||||
| if ((field.val>1 && find[field.val-2] == -1) || (field.val==1 && find[field.val-1] == -1)){ // not find | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| return StringVal::null(); | ||||||
| } | ||||||
| int start_pos,len; | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. define |
||||||
| if (field.val == 1) { // find need split first part | ||||||
| start_pos = 0; | ||||||
| } else { | ||||||
| start_pos = find[field.val-2] + delimiter.len; | ||||||
| } | ||||||
| len = (find[field.val - 1] == -1 ? content.len : find[field.val-1]) - start_pos; | ||||||
| return StringVal(content.ptr + start_pos, len); | ||||||
| } | ||||||
|
|
||||||
| } | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| # split_part | ||
|
|
||
| ## Syntax | ||
|
|
||
| `VARCHAR split_part(VARCHAR content, VARCHAR delimiter, INT field)` | ||
|
|
||
| ## Description | ||
|
|
||
| 根据分割符拆分字符串, 返回指定的分割部分(从一开始计数)。 | ||
|
|
||
| ## Examples | ||
|
|
||
| ``` | ||
| mysql> select split_part("hello word", " ", 1); | ||
| +----------------------------------+ | ||
| | split_part('hello word', ' ', 1) | | ||
| +----------------------------------+ | ||
| | hello | | ||
| +----------------------------------+ | ||
|
|
||
|
|
||
| mysql> select split_part("hello word", " ", 2); | ||
| +----------------------------------+ | ||
| | split_part('hello word', ' ', 2) | | ||
| +----------------------------------+ | ||
| | word | | ||
| +----------------------------------+ | ||
|
|
||
| mysql> select split_part("2019年7月8号", "月", 1); | ||
| +-----------------------------------------+ | ||
| | split_part('2019年7月8号', '月', 1) | | ||
| +-----------------------------------------+ | ||
| | 2019年7 | | ||
| +-----------------------------------------+ | ||
|
|
||
| mysql> select split_part("abca", "a", 1); | ||
| +----------------------------+ | ||
| | split_part('abca', 'a', 1) | | ||
| +----------------------------+ | ||
| | | | ||
| +----------------------------+ | ||
| ``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.