diff --git a/dbms/src/Functions/FunctionsStringMath.h b/dbms/src/Functions/FunctionsStringMath.h index 0799099d683..a12b77ffc29 100644 --- a/dbms/src/Functions/FunctionsStringMath.h +++ b/dbms/src/Functions/FunctionsStringMath.h @@ -1,15 +1,15 @@ #pragma once -#include -#include -#include #include +#include #include -#include #include +#include +#include +#include -#include #include +#include #if __has_include() #include #endif @@ -20,7 +20,7 @@ namespace DB namespace ErrorCodes { - extern const int ILLEGAL_COLUMN; +extern const int ILLEGAL_COLUMN; } struct CRC32Impl @@ -28,13 +28,13 @@ struct CRC32Impl static void execute(const StringRef & s, Int64 & res) { // zlib crc32 - res = crc32(0, reinterpret_cast(s.data), s.size); + res = crc32(0, reinterpret_cast(s.data), s.size); } template static void execute(const Column * arg_col, PaddedPODArray & res) { res.resize(arg_col->size()); - for(size_t i = 0;i < arg_col->size();++i) + for (size_t i = 0; i < arg_col->size(); ++i) { execute(arg_col->getDataAt(i), res[i]); } @@ -51,8 +51,7 @@ class FunctionCRC32 : public IFunction DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override { if (!arguments.front()->isStringOrFixedString()) - throw Exception{ - "Illegal type " + arguments.front()->getName() + " of argument of function " + getName(), + throw Exception{"Illegal type " + arguments.front()->getName() + " of argument of function " + getName(), ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT}; // tidb get int64 from crc32, so we do the same thing return std::make_shared(); @@ -66,15 +65,13 @@ class FunctionCRC32 : public IFunction { CRC32Impl::execute(col, col_res->getData()); } - else if(const auto col = checkAndGetColumn(arg)) + else if (const auto col = checkAndGetColumn(arg)) { CRC32Impl::execute(col, col_res->getData()); } else { - throw Exception{ - "Illegal column " + arg->getName() + " of argument of function " + getName(), - ErrorCodes::ILLEGAL_COLUMN}; + throw Exception{"Illegal column " + arg->getName() + " of argument of function " + getName(), ErrorCodes::ILLEGAL_COLUMN}; } block.getByPosition(result).column = std::move(col_res); } @@ -86,51 +83,51 @@ struct ConvImpl static String execute(const String & arg, int from_base, int to_base) { bool is_signed = false, is_negative = false, ignore_sign = false; - if(from_base < 0) + if (from_base < 0) { from_base = -from_base; is_signed = true; } - if(to_base < 0) + if (to_base < 0) { to_base = -to_base; ignore_sign = true; } - if(from_base > 36 || from_base < 2 || to_base > 36 || to_base < 2) + if (from_base > 36 || from_base < 2 || to_base > 36 || to_base < 2) { return arg; } auto begin_pos_iter = std::find_if_not(arg.begin(), arg.end(), isspace); - if(begin_pos_iter == arg.end()) + if (begin_pos_iter == arg.end()) { return "0"; } - if(*begin_pos_iter == '-') + if (*begin_pos_iter == '-') { is_negative = true; ++begin_pos_iter; } auto begin_pos = begin_pos_iter - arg.begin(); - #if __has_include() +#if __has_include() UInt64 value; auto from_chars_res = std::from_chars(arg.data() + begin_pos, arg.data() + arg.size(), value, from_base); - if(from_chars_res.ec != 0) + if (from_chars_res.ec != std::errc{}) { throw Exception(String("Int too big to conv: ") + (arg.c_str() + begin_pos)); } - #else +#else UInt64 value = strtoull(arg.c_str() + begin_pos, nullptr, from_base); - if(errno) + if (errno) { errno = 0; throw Exception(String("Int too big to conv: ") + (arg.c_str() + begin_pos)); } - #endif +#endif - if(is_signed) + if (is_signed) { if (is_negative && value > static_cast(std::numeric_limits::max()) + 1) { @@ -141,51 +138,55 @@ struct ConvImpl value = std::numeric_limits::max(); } } - if(is_negative) + if (is_negative) { value = -value; } - if(static_cast(value) < 0) + if (static_cast(value) < 0) { is_negative = true; } - else { + else + { is_negative = false; } - if(ignore_sign && is_negative) + if (ignore_sign && is_negative) { value = 0 - value; } - #if __has_include() +#if __has_include() char buf[100] = {0}; std::to_chars(buf, std::end(buf), value, to_base); String result(buf); - for(char& c : result) + for (char & c : result) { c = toupper(c); } - #else +#else String result; - while (value != 0) { + while (value != 0) + { int digit = value % to_base; - result += (digit > 9 ? 'A' + digit - 10 : digit +'0'); + result += (digit > 9 ? 'A' + digit - 10 : digit + '0'); value /= to_base; } std::reverse(result.begin(), result.end()); - #endif +#endif - if(is_negative && ignore_sign) { + if (is_negative && ignore_sign) + { result = "-" + result; } return result; } template - static void execute(const Column * arg_col0, const std::unique_ptr> & arg_col1, const std::unique_ptr> & arg_col2, ColumnString & res_col) + static void execute(const Column * arg_col0, const std::unique_ptr> & arg_col1, + const std::unique_ptr> & arg_col2, ColumnString & res_col) { - for(size_t i = 0;i < arg_col0->size();++i) + for (size_t i = 0; i < arg_col0->size(); ++i) { // we want some std::string operation in ConvImpl so we call toString here String result = execute(arg_col0->getDataAt(i).toString(), arg_col1->get(i), arg_col2->get(i)); @@ -195,10 +196,10 @@ struct ConvImpl }; class FunctionConv : public IFunction -{ +{ template - void executeWithIntTypes(Block & block, const ColumnNumbers & arguments, const size_t result, const FirstIntColumn * first_int_arg_typed, - const SecondIntColumn * second_int_arg_typed) + void executeWithIntTypes(Block & block, const ColumnNumbers & arguments, const size_t result, + const FirstIntColumn * first_int_arg_typed, const SecondIntColumn * second_int_arg_typed) { const auto string_arg = block.getByPosition(arguments[0]).column.get(); @@ -211,7 +212,7 @@ class FunctionConv : public IFunction ConvImpl::execute(string_col, first_int_vec_helper, second_int_vec_helper, *col_res); block.getByPosition(result).column = std::move(col_res); } - else if(const auto string_col = checkAndGetColumn(string_arg)) + else if (const auto string_col = checkAndGetColumn(string_arg)) { auto first_int_vec_helper = IGetVecHelper::getHelper(first_int_arg_typed); auto second_int_vec_helper = IGetVecHelper::getHelper(second_int_arg_typed); @@ -221,8 +222,7 @@ class FunctionConv : public IFunction else { throw Exception{ - "Illegal column " + string_arg->getName() + " of argument of function " + getName(), - ErrorCodes::ILLEGAL_COLUMN}; + "Illegal column " + string_arg->getName() + " of argument of function " + getName(), ErrorCodes::ILLEGAL_COLUMN}; } } @@ -245,29 +245,27 @@ class FunctionConv : public IFunction } template - bool executeIntLeft(Block & block, const ColumnNumbers & arguments, const size_t result, - const IColumn * first_int_arg) + bool executeIntLeft(Block & block, const ColumnNumbers & arguments, const size_t result, const IColumn * first_int_arg) { if (const auto first_int_arg_typed = checkAndGetColumn>(first_int_arg)) { const auto second_int_arg = block.getByPosition(arguments[2]).column.get(); - if (executeIntRight(block, arguments, result, first_int_arg_typed, second_int_arg) || - executeIntRight(block, arguments, result, first_int_arg_typed, second_int_arg) || - executeIntRight(block, arguments, result, first_int_arg_typed, second_int_arg) || - executeIntRight(block, arguments, result, first_int_arg_typed, second_int_arg) || - executeIntRight(block, arguments, result, first_int_arg_typed, second_int_arg) || - executeIntRight(block, arguments, result, first_int_arg_typed, second_int_arg) || - executeIntRight(block, arguments, result, first_int_arg_typed, second_int_arg) || - executeIntRight(block, arguments, result, first_int_arg_typed, second_int_arg)) + if (executeIntRight(block, arguments, result, first_int_arg_typed, second_int_arg) + || executeIntRight(block, arguments, result, first_int_arg_typed, second_int_arg) + || executeIntRight(block, arguments, result, first_int_arg_typed, second_int_arg) + || executeIntRight(block, arguments, result, first_int_arg_typed, second_int_arg) + || executeIntRight(block, arguments, result, first_int_arg_typed, second_int_arg) + || executeIntRight(block, arguments, result, first_int_arg_typed, second_int_arg) + || executeIntRight(block, arguments, result, first_int_arg_typed, second_int_arg) + || executeIntRight(block, arguments, result, first_int_arg_typed, second_int_arg)) { return true; } else { - throw Exception{ - "Illegal column " + block.getByPosition(arguments[1]).column->getName() + - " of second argument of function " + getName(), + throw Exception{"Illegal column " + block.getByPosition(arguments[1]).column->getName() + " of second argument of function " + + getName(), ErrorCodes::ILLEGAL_COLUMN}; } } @@ -275,28 +273,28 @@ class FunctionConv : public IFunction { const auto second_int_arg = block.getByPosition(arguments[2]).column.get(); - if (executeIntRight(block, arguments, result, first_int_arg_typed, second_int_arg) || - executeIntRight(block, arguments, result, first_int_arg_typed, second_int_arg) || - executeIntRight(block, arguments, result, first_int_arg_typed, second_int_arg) || - executeIntRight(block, arguments, result, first_int_arg_typed, second_int_arg) || - executeIntRight(block, arguments, result, first_int_arg_typed, second_int_arg) || - executeIntRight(block, arguments, result, first_int_arg_typed, second_int_arg) || - executeIntRight(block, arguments, result, first_int_arg_typed, second_int_arg) || - executeIntRight(block, arguments, result, first_int_arg_typed, second_int_arg)) + if (executeIntRight(block, arguments, result, first_int_arg_typed, second_int_arg) + || executeIntRight(block, arguments, result, first_int_arg_typed, second_int_arg) + || executeIntRight(block, arguments, result, first_int_arg_typed, second_int_arg) + || executeIntRight(block, arguments, result, first_int_arg_typed, second_int_arg) + || executeIntRight(block, arguments, result, first_int_arg_typed, second_int_arg) + || executeIntRight(block, arguments, result, first_int_arg_typed, second_int_arg) + || executeIntRight(block, arguments, result, first_int_arg_typed, second_int_arg) + || executeIntRight(block, arguments, result, first_int_arg_typed, second_int_arg)) { return true; } else { - throw Exception{ - "Illegal column " + block.getByPosition(arguments[1]).column->getName() + - " of second argument of function " + getName(), + throw Exception{"Illegal column " + block.getByPosition(arguments[1]).column->getName() + " of second argument of function " + + getName(), ErrorCodes::ILLEGAL_COLUMN}; } } return false; } + public: static constexpr auto name = "conv"; static FunctionPtr create(const Context &) { return std::make_shared(); } @@ -324,21 +322,19 @@ class FunctionConv : public IFunction const auto first_int_arg = block.getByPosition(arguments[1]).column.get(); - if (!executeIntLeft(block, arguments, result, first_int_arg) && - !executeIntLeft(block, arguments, result, first_int_arg) && - !executeIntLeft(block, arguments, result, first_int_arg) && - !executeIntLeft(block, arguments, result, first_int_arg) && - !executeIntLeft(block, arguments, result, first_int_arg) && - !executeIntLeft(block, arguments, result, first_int_arg) && - !executeIntLeft(block, arguments, result, first_int_arg) && - !executeIntLeft(block, arguments, result, first_int_arg)) + if (!executeIntLeft(block, arguments, result, first_int_arg) + && !executeIntLeft(block, arguments, result, first_int_arg) + && !executeIntLeft(block, arguments, result, first_int_arg) + && !executeIntLeft(block, arguments, result, first_int_arg) + && !executeIntLeft(block, arguments, result, first_int_arg) + && !executeIntLeft(block, arguments, result, first_int_arg) + && !executeIntLeft(block, arguments, result, first_int_arg) + && !executeIntLeft(block, arguments, result, first_int_arg)) { throw Exception{ - "Illegal column " + first_int_arg->getName() + " of argument of function " + getName(), - ErrorCodes::ILLEGAL_COLUMN}; + "Illegal column " + first_int_arg->getName() + " of argument of function " + getName(), ErrorCodes::ILLEGAL_COLUMN}; } } }; -} - +} // namespace DB