Merge pull request #9767 from hczhcz/patch-0320

Fix splitByString in #9742
This commit is contained in:
alexey-milovidov 2020-03-20 21:36:22 +03:00 committed by GitHub
commit d335775208
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 60 additions and 13 deletions

View File

@ -231,22 +231,23 @@ public:
/// Get the next token, if any, or return false. /// Get the next token, if any, or return false.
bool get(Pos & token_begin, Pos & token_end) bool get(Pos & token_begin, Pos & token_end)
{
if (sep.empty())
{
if (pos == end)
return false;
token_begin = pos;
pos += 1;
token_end = pos;
}
else
{ {
if (!pos) if (!pos)
return false; return false;
token_begin = pos; token_begin = pos;
if (sep.empty())
{
pos += 1;
token_end = pos;
if (pos == end)
pos = nullptr;
}
else
{
pos = reinterpret_cast<Pos>(memmem(pos, end - pos, sep.data(), sep.size())); pos = reinterpret_cast<Pos>(memmem(pos, end - pos, sep.data(), sep.size()));
if (pos) if (pos)

View File

@ -5,3 +5,5 @@
['a','b','c','d','e'] ['a','b','c','d','e']
['hello','world'] ['hello','world']
['gbye','bug'] ['gbye','bug']
['']
[]

View File

@ -4,3 +4,5 @@ select splitByString('ab', 'ababab');
select splitByString('ababab', 'ababab'); select splitByString('ababab', 'ababab');
select splitByString('', 'abcde'); select splitByString('', 'abcde');
select splitByString(', ', x) from (select arrayJoin(['hello, world', 'gbye, bug']) x); select splitByString(', ', x) from (select arrayJoin(['hello, world', 'gbye, bug']) x);
select splitByString('ab', '');
select splitByString('', '');

View File

@ -2,10 +2,31 @@
## splitByChar(separator, s) {#splitbycharseparator-s} ## splitByChar(separator, s) {#splitbycharseparator-s}
Splits a string into substrings separated by separator.separator must be a string constant consisting of exactly one character. Splits a string into substrings separated by a specified character. It uses a constant string `separator` which consisting of exactly one character.
Returns an array of selected substrings. Empty substrings may be selected if the separator occurs at the beginning or end of the string, or if there are multiple consecutive separators. Returns an array of selected substrings. Empty substrings may be selected if the separator occurs at the beginning or end of the string, or if there are multiple consecutive separators.
**Example:** **Syntax**
```sql
splitByChar(<separator>, <s>)
```
**Parameters**
- `separator` — The separator which should contain exactly one character. [String](../../data_types/string.md).
- `s` — The string to split. [String](../../data_types/string.md).
**Returned value(s)**
Returns an array of selected substrings. Empty substrings may be selected when:
* A separator occurs at the beginning or end of the string;
* There are multiple consecutive separators;
* The original string `s` is empty.
Type: [Array](../../data_types/array.md) of [String](../../data_types/string.md).
**Example**
``` sql ``` sql
SELECT splitByChar(',', '1,2,3,abcde') SELECT splitByChar(',', '1,2,3,abcde')
@ -19,9 +40,30 @@ SELECT splitByChar(',', '1,2,3,abcde')
## splitByString(separator, s) {#splitbystringseparator-s} ## splitByString(separator, s) {#splitbystringseparator-s}
The same as above, but it uses a string of multiple characters as the separator. If the string is empty, it will split the string into an array of single characters. Splits a string into substrings separated by a string. It uses a constant string `separator` of multiple characters as the separator. If the string `separator` is empty, it will split the string `s` into an array of single characters.
**Example:** **Syntax**
```sql
splitByString(<separator>, <s>)
```
**Parameters**
- `separator` — The separator. [String](../../data_types/string.md).
- `s` — The string to split. [String](../../data_types/string.md).
**Returned value(s)**
Returns an array of selected substrings. Empty substrings may be selected when:
Type: [Array](../../data_types/array.md) of [String](../../data_types/string.md).
* A non-empty separator occurs at the beginning or end of the string;
* There are multiple consecutive non-empty separators;
* The original string `s` is empty while the separator is not empty.
**Example**
``` sql ``` sql
SELECT splitByString(', ', '1, 2 3, 4,5, abcde') SELECT splitByString(', ', '1, 2 3, 4,5, abcde')
@ -52,7 +94,7 @@ Returns the string.
Selects substrings of consecutive bytes from the ranges a-z and A-Z.Returns an array of substrings. Selects substrings of consecutive bytes from the ranges a-z and A-Z.Returns an array of substrings.
**Example:** **Example**
``` sql ``` sql
SELECT alphaTokens('abca1abc') SELECT alphaTokens('abca1abc')