From b9c42effb48d7ef15a5f8f269bbe67145d2c8cd1 Mon Sep 17 00:00:00 2001 From: taiyang-li <654010905@qq.com> Date: Sat, 5 Feb 2022 19:30:40 +0800 Subject: [PATCH] change as requested --- src/Functions/FunctionsStringArray.h | 39 +++++++++---------- .../0_stateless/02185_split_by_char.sql | 1 - 2 files changed, 18 insertions(+), 22 deletions(-) diff --git a/src/Functions/FunctionsStringArray.h b/src/Functions/FunctionsStringArray.h index dcd9198ef33f..b1de017120c6 100644 --- a/src/Functions/FunctionsStringArray.h +++ b/src/Functions/FunctionsStringArray.h @@ -244,8 +244,8 @@ class SplitByCharImpl Pos end; char sep; - Int64 max_split = -1; - Int64 curr_split = 0; + std::optional max_split; + UInt64 curr_split = 0; public: static constexpr auto name = "splitByChar"; @@ -295,15 +295,14 @@ class SplitByCharImpl if (arguments.size() > 2) { - std::optional max_split_opt = std::nullopt; - if (!((max_split_opt = getMaxSplit(arguments[2])) - || (max_split_opt = getMaxSplit(arguments[2])) - || (max_split_opt = getMaxSplit(arguments[2])) - || (max_split_opt = getMaxSplit(arguments[2])) - || (max_split_opt = getMaxSplit(arguments[2])) - || (max_split_opt = getMaxSplit(arguments[2])) - || (max_split_opt = getMaxSplit(arguments[2])) - || (max_split_opt = getMaxSplit(arguments[2])))) + if (!((max_split = getMaxSplit(arguments[2])) + || (max_split = getMaxSplit(arguments[2])) + || (max_split = getMaxSplit(arguments[2])) + || (max_split = getMaxSplit(arguments[2])) + || (max_split = getMaxSplit(arguments[2])) + || (max_split = getMaxSplit(arguments[2])) + || (max_split = getMaxSplit(arguments[2])) + || (max_split = getMaxSplit(arguments[2])))) { throw Exception( ErrorCodes::ILLEGAL_COLUMN, @@ -311,23 +310,21 @@ class SplitByCharImpl arguments[2].column->getName(), getName()); } - max_split = *max_split_opt; } } template - std::optional getMaxSplit(const ColumnWithTypeAndName & argument) + std::optional getMaxSplit(const ColumnWithTypeAndName & argument) { const auto * col = checkAndGetColumnConst>(argument.column.get()); if (!col) return std::nullopt; - Int64 result= static_cast(col->template getValue()); - if (result < 0 && result != -1) - throw Exception("Illegal column " + argument.column->getName() - + " of third argument of function " + getName() + ". Must be non-negative number or -1", - ErrorCodes::ILLEGAL_COLUMN); - return result; + auto value = col->template getValue(); + if (value < 0) + throw Exception( + ErrorCodes::ILLEGAL_COLUMN, "Illegal column {} of third argument of function {}", argument.column->getName(), getName()); + return value; } /// Returns the position of the argument, that is the column of strings @@ -348,14 +345,14 @@ class SplitByCharImpl return false; token_begin = pos; - if (unlikely(max_split >= 0 && curr_split >= max_split)) + if (unlikely(max_split && curr_split >= *max_split)) { token_end = end; pos = nullptr; return true; } - pos = reinterpret_cast(memchr(pos, sep, end - pos)); + pos = reinterpret_cast(memchr(pos, sep, end - pos)); if (pos) { token_end = pos; diff --git a/tests/queries/0_stateless/02185_split_by_char.sql b/tests/queries/0_stateless/02185_split_by_char.sql index 6b843c05144a..6c4906548137 100644 --- a/tests/queries/0_stateless/02185_split_by_char.sql +++ b/tests/queries/0_stateless/02185_split_by_char.sql @@ -1,5 +1,4 @@ select splitByChar(',', '1,2,3'); -select splitByChar(',', '1,2,3', -1); select splitByChar(',', '1,2,3', 0); select splitByChar(',', '1,2,3', 1); select splitByChar(',', '1,2,3', 2);