Skip to content

Commit

Permalink
Fix check std::errc make build failed (#2517)
Browse files Browse the repository at this point in the history
  • Loading branch information
jiaqizho authored Aug 2, 2021
1 parent 0c2fac3 commit 450708d
Showing 1 changed file with 76 additions and 80 deletions.
156 changes: 76 additions & 80 deletions dbms/src/Functions/FunctionsStringMath.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
#pragma once

#include <Functions/FunctionHelpers.h>
#include <Functions/IFunction.h>
#include <Columns/ColumnString.h>
#include <Columns/ColumnFixedString.h>
#include <Columns/ColumnString.h>
#include <Columns/ColumnVector.h>
#include <DataTypes/DataTypesNumber.h>
#include <DataTypes/DataTypeString.h>
#include <DataTypes/DataTypesNumber.h>
#include <Functions/FunctionHelpers.h>
#include <Functions/IFunction.h>

#include <string>
#include <cstdlib>
#include <string>
#if __has_include(<charconv>)
#include <charconv>
#endif
Expand All @@ -20,21 +20,21 @@ namespace DB

namespace ErrorCodes
{
extern const int ILLEGAL_COLUMN;
extern const int ILLEGAL_COLUMN;
}

struct CRC32Impl
{
static void execute(const StringRef & s, Int64 & res)
{
// zlib crc32
res = crc32(0, reinterpret_cast<const unsigned char*>(s.data), s.size);
res = crc32(0, reinterpret_cast<const unsigned char *>(s.data), s.size);
}
template <typename Column>
static void execute(const Column * arg_col, PaddedPODArray<Int64> & 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]);
}
Expand All @@ -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<DataTypeInt64>();
Expand All @@ -66,15 +65,13 @@ class FunctionCRC32 : public IFunction
{
CRC32Impl::execute(col, col_res->getData());
}
else if(const auto col = checkAndGetColumn<ColumnFixedString>(arg))
else if (const auto col = checkAndGetColumn<ColumnFixedString>(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);
}
Expand All @@ -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(<charconv>)
#if __has_include(<charconv>)
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<UInt64>(std::numeric_limits<Int64>::max()) + 1)
{
Expand All @@ -141,51 +138,55 @@ struct ConvImpl
value = std::numeric_limits<Int64>::max();
}
}
if(is_negative)
if (is_negative)
{
value = -value;
}

if(static_cast<Int64>(value) < 0)
if (static_cast<Int64>(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(<charconv>)
#if __has_include(<charconv>)
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 <typename T1, typename T2, typename Column>
static void execute(const Column * arg_col0, const std::unique_ptr<IGetVecHelper<T1>> & arg_col1, const std::unique_ptr<IGetVecHelper<T2>> & arg_col2, ColumnString & res_col)
static void execute(const Column * arg_col0, const std::unique_ptr<IGetVecHelper<T1>> & arg_col1,
const std::unique_ptr<IGetVecHelper<T2>> & 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));
Expand All @@ -195,10 +196,10 @@ struct ConvImpl
};

class FunctionConv : public IFunction
{
{
template <typename FirstIntType, typename SecondIntType, typename FirstIntColumn, typename SecondIntColumn>
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();

Expand All @@ -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<ColumnFixedString>(string_arg))
else if (const auto string_col = checkAndGetColumn<ColumnFixedString>(string_arg))
{
auto first_int_vec_helper = IGetVecHelper<FirstIntType>::getHelper(first_int_arg_typed);
auto second_int_vec_helper = IGetVecHelper<SecondIntType>::getHelper(second_int_arg_typed);
Expand All @@ -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};
}
}

Expand All @@ -245,58 +245,56 @@ class FunctionConv : public IFunction
}

template <typename FirstIntType>
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<ColumnVector<FirstIntType>>(first_int_arg))
{
const auto second_int_arg = block.getByPosition(arguments[2]).column.get();

if (executeIntRight<FirstIntType, UInt8>(block, arguments, result, first_int_arg_typed, second_int_arg) ||
executeIntRight<FirstIntType, UInt16>(block, arguments, result, first_int_arg_typed, second_int_arg) ||
executeIntRight<FirstIntType, UInt32>(block, arguments, result, first_int_arg_typed, second_int_arg) ||
executeIntRight<FirstIntType, UInt64>(block, arguments, result, first_int_arg_typed, second_int_arg) ||
executeIntRight<FirstIntType, Int8>(block, arguments, result, first_int_arg_typed, second_int_arg) ||
executeIntRight<FirstIntType, Int16>(block, arguments, result, first_int_arg_typed, second_int_arg) ||
executeIntRight<FirstIntType, Int32>(block, arguments, result, first_int_arg_typed, second_int_arg) ||
executeIntRight<FirstIntType, Int64>(block, arguments, result, first_int_arg_typed, second_int_arg))
if (executeIntRight<FirstIntType, UInt8>(block, arguments, result, first_int_arg_typed, second_int_arg)
|| executeIntRight<FirstIntType, UInt16>(block, arguments, result, first_int_arg_typed, second_int_arg)
|| executeIntRight<FirstIntType, UInt32>(block, arguments, result, first_int_arg_typed, second_int_arg)
|| executeIntRight<FirstIntType, UInt64>(block, arguments, result, first_int_arg_typed, second_int_arg)
|| executeIntRight<FirstIntType, Int8>(block, arguments, result, first_int_arg_typed, second_int_arg)
|| executeIntRight<FirstIntType, Int16>(block, arguments, result, first_int_arg_typed, second_int_arg)
|| executeIntRight<FirstIntType, Int32>(block, arguments, result, first_int_arg_typed, second_int_arg)
|| executeIntRight<FirstIntType, Int64>(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};
}
}
else if (const auto first_int_arg_typed = checkAndGetColumnConst<ColumnVector<FirstIntType>>(first_int_arg))
{
const auto second_int_arg = block.getByPosition(arguments[2]).column.get();

if (executeIntRight<FirstIntType, UInt8>(block, arguments, result, first_int_arg_typed, second_int_arg) ||
executeIntRight<FirstIntType, UInt16>(block, arguments, result, first_int_arg_typed, second_int_arg) ||
executeIntRight<FirstIntType, UInt32>(block, arguments, result, first_int_arg_typed, second_int_arg) ||
executeIntRight<FirstIntType, UInt64>(block, arguments, result, first_int_arg_typed, second_int_arg) ||
executeIntRight<FirstIntType, Int8>(block, arguments, result, first_int_arg_typed, second_int_arg) ||
executeIntRight<FirstIntType, Int16>(block, arguments, result, first_int_arg_typed, second_int_arg) ||
executeIntRight<FirstIntType, Int32>(block, arguments, result, first_int_arg_typed, second_int_arg) ||
executeIntRight<FirstIntType, Int64>(block, arguments, result, first_int_arg_typed, second_int_arg))
if (executeIntRight<FirstIntType, UInt8>(block, arguments, result, first_int_arg_typed, second_int_arg)
|| executeIntRight<FirstIntType, UInt16>(block, arguments, result, first_int_arg_typed, second_int_arg)
|| executeIntRight<FirstIntType, UInt32>(block, arguments, result, first_int_arg_typed, second_int_arg)
|| executeIntRight<FirstIntType, UInt64>(block, arguments, result, first_int_arg_typed, second_int_arg)
|| executeIntRight<FirstIntType, Int8>(block, arguments, result, first_int_arg_typed, second_int_arg)
|| executeIntRight<FirstIntType, Int16>(block, arguments, result, first_int_arg_typed, second_int_arg)
|| executeIntRight<FirstIntType, Int32>(block, arguments, result, first_int_arg_typed, second_int_arg)
|| executeIntRight<FirstIntType, Int64>(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<FunctionConv>(); }
Expand Down Expand Up @@ -324,21 +322,19 @@ class FunctionConv : public IFunction

const auto first_int_arg = block.getByPosition(arguments[1]).column.get();

if (!executeIntLeft<UInt8>(block, arguments, result, first_int_arg) &&
!executeIntLeft<UInt16>(block, arguments, result, first_int_arg) &&
!executeIntLeft<UInt32>(block, arguments, result, first_int_arg) &&
!executeIntLeft<UInt64>(block, arguments, result, first_int_arg) &&
!executeIntLeft<Int8>(block, arguments, result, first_int_arg) &&
!executeIntLeft<Int16>(block, arguments, result, first_int_arg) &&
!executeIntLeft<Int32>(block, arguments, result, first_int_arg) &&
!executeIntLeft<Int64>(block, arguments, result, first_int_arg))
if (!executeIntLeft<UInt8>(block, arguments, result, first_int_arg)
&& !executeIntLeft<UInt16>(block, arguments, result, first_int_arg)
&& !executeIntLeft<UInt32>(block, arguments, result, first_int_arg)
&& !executeIntLeft<UInt64>(block, arguments, result, first_int_arg)
&& !executeIntLeft<Int8>(block, arguments, result, first_int_arg)
&& !executeIntLeft<Int16>(block, arguments, result, first_int_arg)
&& !executeIntLeft<Int32>(block, arguments, result, first_int_arg)
&& !executeIntLeft<Int64>(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

0 comments on commit 450708d

Please sign in to comment.