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

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

View File

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

View File

@ -4,3 +4,5 @@ select splitByString('ab', 'ababab');
select splitByString('ababab', 'ababab');
select splitByString('', 'abcde');
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}
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.
**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
SELECT splitByChar(',', '1,2,3,abcde')
@ -19,9 +40,30 @@ SELECT splitByChar(',', '1,2,3,abcde')
## 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
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.
**Example:**
**Example**
``` sql
SELECT alphaTokens('abca1abc')