Skip to content
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

Extend splitByString for empty separators #9742

Merged
merged 2 commits into from
Mar 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 16 additions & 7 deletions dbms/src/Functions/FunctionsStringArray.h
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,6 @@ class SplitByStringImpl
ErrorCodes::ILLEGAL_COLUMN);

sep = col->getValue<String>();

if (sep.empty())
throw Exception("Illegal separator for function " + getName() + ". Must be not empty.", ErrorCodes::BAD_ARGUMENTS);
}

/// Returns the position of the argument that is the column of strings
Expand All @@ -239,15 +236,27 @@ class SplitByStringImpl
return false;

token_begin = pos;
pos = reinterpret_cast<Pos>(memmem(pos, end - pos, sep.data(), sep.size()));

if (pos)
if (sep.empty())
{
pos += 1;
token_end = pos;
pos += sep.size();

if (pos == end)
pos = nullptr;
}
else
token_end = end;
{
pos = reinterpret_cast<Pos>(memmem(pos, end - pos, sep.data(), sep.size()));

if (pos)
{
token_end = pos;
pos += sep.size();
}
else
token_end = end;
}

return true;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
['cde','cde']
['','cde','cde','']
['','','','']
['','']
['a','b','c','d','e']
['hello','world']
['gbye','bug']
6 changes: 6 additions & 0 deletions dbms/tests/queries/0_stateless/01100_split_by_string.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
select splitByString('ab', 'cdeabcde');
select splitByString('ab', 'abcdeabcdeab');
select splitByString('ab', 'ababab');
select splitByString('ababab', 'ababab');
select splitByString('', 'abcde');
select splitByString(', ', x) from (select arrayJoin(['hello, world', 'gbye, bug']) x);
33 changes: 32 additions & 1 deletion docs/en/query_language/functions/splitting_merging_functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,40 @@
Splits a string into substrings separated by 'separator'.'separator' must be a string constant 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:**

```sql
SELECT splitByChar(',', '1,2,3,abcde')
```
```text
┌─splitByChar(',', '1,2,3,abcde')─┐
│ ['1','2','3','abcde'] │
└─────────────────────────────────┘
```

## splitByString(separator, s)

The same as above, but it uses a string of multiple characters as the separator. The string must be non-empty.
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.

**Example:**

```sql
SELECT splitByString(', ', '1, 2 3, 4,5, abcde')
```
```text
┌─splitByString(', ', '1, 2 3, 4,5, abcde')─┐
│ ['1','2 3','4,5','abcde'] │
└───────────────────────────────────────────┘
```

```sql
SELECT splitByString('', 'abcde')
```
```text
┌─splitByString('', 'abcde')─┐
│ ['a','b','c','d','e'] │
└────────────────────────────┘
```

## arrayStringConcat(arr\[, separator\])

Expand Down