diff --git a/docs/en/sql-reference/functions/string-search-functions.md b/docs/en/sql-reference/functions/string-search-functions.md index 716280dc544..7b14b0e96de 100644 --- a/docs/en/sql-reference/functions/string-search-functions.md +++ b/docs/en/sql-reference/functions/string-search-functions.md @@ -95,11 +95,11 @@ Result: └───────────────────────────────┘ ``` -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 +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` diff --git a/src/Functions/PositionImpl.h b/src/Functions/PositionImpl.h index 3fb9eca04e1..eeb9d8b6a59 100644 --- a/src/Functions/PositionImpl.h +++ b/src/Functions/PositionImpl.h @@ -202,14 +202,14 @@ struct PositionImpl const UInt8 * const end = haystack_data.data() + haystack_data.size(); const UInt8 * pos = begin; - /// Fastpath when needle is empty + /// Fast path when needle is empty if (needle.empty()) { - /// When needle is empty and start_pos doesn't exist, always return 1 + /// Needle is empty and start_pos doesn't exist --> always return 1 if (start_pos == nullptr) { - for (auto & x : res) - x = 1; + for (auto & r : res) + r = 1; return; } @@ -218,13 +218,13 @@ struct PositionImpl if (const ColumnConst * start_pos_const = typeid_cast(&*start_pos)) { - /// When needle is empty and start_pos is constant - UInt64 start = std::max(start_pos_const->getUInt(0), UInt64(1)); + /// Needle is empty and start_pos is constant + UInt64 start = std::max(start_pos_const->getUInt(0), static_cast(1)); for (size_t i = 0; i < rows; ++i) { size_t haystack_size = Impl::countChars( reinterpret_cast(pos), reinterpret_cast(pos + haystack_offsets[i] - prev_offset - 1)); - res[i] = start <= haystack_size + 1 ? start : 0; + res[i] = (start <= haystack_size + 1) ? start : 0; pos = begin + haystack_offsets[i]; prev_offset = haystack_offsets[i]; @@ -233,14 +233,14 @@ struct PositionImpl } else { - /// When needle is empty and start_pos is not constant + /// 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(pos), reinterpret_cast(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; + start = std::max(static_cast(1), start); + res[i] = (start <= haystack_size + 1) ? start : 0; pos = begin + haystack_offsets[i]; prev_offset = haystack_offsets[i];