change as request

This commit is contained in:
taiyang-li 2023-01-30 16:13:12 +08:00
parent 5db514fb05
commit d25740da83
3 changed files with 42 additions and 0 deletions

View File

@ -95,6 +95,32 @@ Result:
└───────────────────────────────┘
```
Rules when needle is empty, which is also applied for other functions like: positionCaseInsensitive, positionUTF8, positionCaseInsensitiveUTF8
- When `needle` is empty and `start_pos` doesn't exist, always returns 1;
- When `needle` is empty and `start_pos` exists:
- When `start_pos` >= 1 and `start_pos` <= `char_length(haystack)`, returns `start_pos`;
- When `start_pos` = 0, returns 1;
- When `start_pos` = `char_length(haystack) + 1`, returns `char_length(haystack)`;
- Otherwise returns 0.
``` sql
SELECT
position('abc', ''),
position('abc', '', 0),
position('abc', '', 1),
position('abc', '', 2),
position('abc', '', 3),
position('abc', '', 4),
position('abc', '', 5)
```
``` text
┌─position('abc', '')─┬─position('abc', '', 0)─┬─position('abc', '', 1)─┬─position('abc', '', 2)─┬─position('abc', '', 3)─┬─position('abc', '', 4)─┬─position('abc', '', 5)─┐
│ 1 │ 1 │ 1 │ 2 │ 3 │ 4 │ 0 │
└─────────────────────┴────────────────────────┴────────────────────────┴────────────────────────┴────────────────────────┴────────────────────────┴────────────────────────┘
```
**Examples for POSITION(needle IN haystack) syntax**
Query:

View File

@ -205,6 +205,7 @@ struct PositionImpl
/// Fastpath when needle is empty
if (needle.empty())
{
/// When needle is empty and start_pos doesn't exist, always return 1
if (start_pos == nullptr)
{
for (auto & x : res)
@ -217,6 +218,7 @@ struct PositionImpl
const ColumnConst * start_pos_const = typeid_cast<const ColumnConst *>(&*start_pos);
if (start_pos_const)
{
/// When needle is empty and start_pos is constant
UInt64 start = std::max(start_pos_const->getUInt(0), UInt64(1));
for (size_t i = 0; i < rows; ++i)
{
@ -230,6 +232,7 @@ struct PositionImpl
return;
}
/// When needle is empty and start_pos is not constant
for (size_t i = 0; i < rows; ++i)
{
size_t haystack_size = Impl::countChars(

View File

@ -0,0 +1,13 @@
<test>
<query>select position(materialize('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'), '') from numbers(100000000) format Null</query>
<query>select position(materialize('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'), '', 10) from numbers(100000000) format Null</query>
<query>select positionCaseInsensitive(materialize('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'), '') from numbers(100000000) format Null</query>
<query>select positionCaseInsensitive(materialize('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'), '', 10) from numbers(100000000) format Null</query>
<query>select positionUTF8(materialize('xẞyyaa1ẞ1yzẞXẞẞ1ẞẞ1bctest'), '') from numbers(100000000) format Null</query>
<query>select positionUTF8(materialize('xẞyyaa1ẞ1yzẞXẞẞ1ẞẞ1bctest'), '', 10) from numbers(100000000) format Null</query>
<query>select positionCaseInsensitiveUTF8(materialize('xẞyyaa1ẞ1yzẞXẞẞ1ẞẞ1bctest'), '') from numbers(100000000) format Null</query>
<query>select positionCaseInsensitiveUTF8(materialize('xẞyyaa1ẞ1yzẞXẞẞ1ẞẞ1bctest'), '', 10) from numbers(100000000) format Null</query>
</test>