change as request

This commit is contained in:
taiyang-li 2023-01-31 10:48:02 +08:00
parent e3a5318739
commit 4cf11d9a84
2 changed files with 23 additions and 21 deletions

View File

@ -95,13 +95,13 @@ 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.
Note: If argument needle is empty the following rules apply:
- if no start_pos was specified: return 1
- if start_pos = 0: return 1
- if start_pos >= 1 and start_pos <= length(haystack) + 1: return start_pos
- otherwise: return 0
The same rules also apply to functions `positionCaseInsensitive`, `positionUTF8` and `positionCaseInsensitiveUTF8`
``` sql
SELECT

View File

@ -214,9 +214,9 @@ struct PositionImpl
}
ColumnString::Offset prev_offset = 0;
auto rows = haystack_offsets.size();
const ColumnConst * start_pos_const = typeid_cast<const ColumnConst *>(&*start_pos);
if (start_pos_const)
size_t rows = haystack_offsets.size();
if (const ColumnConst * start_pos_const = typeid_cast<const ColumnConst *>(&*start_pos))
{
/// When needle is empty and start_pos is constant
UInt64 start = std::max(start_pos_const->getUInt(0), UInt64(1));
@ -231,20 +231,22 @@ struct PositionImpl
}
return;
}
/// When needle is empty and start_pos is not constant
for (size_t i = 0; i < rows; ++i)
else
{
size_t haystack_size = Impl::countChars(
reinterpret_cast<const char *>(pos), reinterpret_cast<const char *>(pos + haystack_offsets[i] - prev_offset - 1));
UInt64 start = start_pos->getUInt(i);
start = std::max(UInt64(1), start);
res[i] = start <= haystack_size + 1 ? start : 0;
/// When needle is empty and start_pos is not constant
for (size_t i = 0; i < rows; ++i)
{
size_t haystack_size = Impl::countChars(
reinterpret_cast<const char *>(pos), reinterpret_cast<const char *>(pos + haystack_offsets[i] - prev_offset - 1));
UInt64 start = start_pos->getUInt(i);
start = std::max(UInt64(1), start);
res[i] = start <= haystack_size + 1 ? start : 0;
pos = begin + haystack_offsets[i];
prev_offset = haystack_offsets[i];
pos = begin + haystack_offsets[i];
prev_offset = haystack_offsets[i];
}
return;
}
return;
}
/// Current index in the array of strings.