Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix clang-tidy warnings for a batch of files #3251

Merged
merged 10 commits into from
Oct 20, 2021
49 changes: 25 additions & 24 deletions dbms/src/Common/Decimal.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ struct Decimal

Decimal(const Decimal<T> & d) = default;
Decimal() = default;
Decimal(T v_)
Decimal(T v_) // NOLINT(google-explicit-constructor)
: value(v_)
{}

Expand All @@ -156,18 +156,18 @@ struct Decimal
String toString(ScaleType) const;

template <typename U, std::enable_if_t<std::is_same_v<U, Int256> || std::is_same_v<U, Int512> || std::is_integral_v<U> || std::is_same_v<U, Int128>> * = nullptr>
operator U() const
operator U() const // NOLINT(google-explicit-constructor)
{
return static_cast<U>(value);
}

template <typename U, std::enable_if_t<sizeof(U) >= sizeof(T)> * = nullptr>
operator Decimal<U>() const
operator Decimal<U>() const // NOLINT(google-explicit-constructor)
{
return static_cast<U>(value);
}

operator T() const
operator T() const // NOLINT(google-explicit-constructor)
{
return value;
}
Expand Down Expand Up @@ -356,41 +356,42 @@ class DecimalMaxValue final : public ext::singleton<DecimalMaxValue>
Int256 number[decimal_max_prec + 1];

public:
DecimalMaxValue()
static Int256 get(PrecType idx)
{
for (PrecType i = 1; i <= decimal_max_prec; i++)
{
number[i] = number[i - 1] * 10 + 9;
}
return instance().getInternal(idx);
}

Int256 get(PrecType idx) const
static Int256 maxValue()
{
return number[idx];
return get(maxDecimalPrecision<Decimal256>());
}

static Int256 Get(PrecType idx)
private:
DecimalMaxValue()
{
return instance().get(idx);
for (PrecType i = 1; i <= decimal_max_prec; i++)
{
number[i] = number[i - 1] * 10 + 9;
}
}

static Int256 MaxValue()
Int256 getInternal(PrecType idx) const
{
return Get(maxDecimalPrecision<Decimal256>());
return number[idx];
}
};

template <typename T>
inline typename T::NativeType getScaleMultiplier(ScaleType scale)
{
return static_cast<typename T::NativeType>(DecimalMaxValue::Get(scale) + 1);
return static_cast<typename T::NativeType>(DecimalMaxValue::get(scale) + 1);
}

template <typename T>
inline void checkDecimalOverflow(Decimal<T> v, PrecType prec)
{
auto maxValue = DecimalMaxValue::Get(prec);
if (v.value > maxValue || v.value < -maxValue)
auto max_value = DecimalMaxValue::get(prec);
if (v.value > max_value || v.value < -max_value)
{
throw TiFlashException("Decimal value overflow", Errors::Decimal::Overflow);
}
Expand Down Expand Up @@ -460,15 +461,15 @@ std::enable_if_t<std::is_floating_point_v<T>, U> ToDecimal(T value, ScaleType sc
{
value *= 10;
}
if (std::abs(value) > static_cast<T>(DecimalMaxValue::Get(decimal_max_prec)))
if (std::abs(value) > static_cast<T>(DecimalMaxValue::get(decimal_max_prec)))
{
throw TiFlashException("Decimal value overflow", Errors::Decimal::Overflow);
}
// rounding
T tenTimesValue = value * 10;
T ten_times_value = value * 10;
using UType = typename U::NativeType;
UType v(value);
if (Int256(tenTimesValue) % 10 >= 5)
if (static_cast<Int256>(ten_times_value) % 10 >= 5)
{
v++;
}
Expand Down Expand Up @@ -496,13 +497,13 @@ std::enable_if_t<IsDecimal<T>, U> ToDecimal(const T & v, ScaleType v_scale, Scal
}
else
{
bool need2Round = false;
bool need_to_round = false;
for (ScaleType i = scale; i < v_scale; i++)
{
need2Round = (value < 0 ? -value : value) % 10 >= 5;
need_to_round = (value < 0 ? -value : value) % 10 >= 5;
value /= 10;
}
if (need2Round)
if (need_to_round)
{
if (value < 0)
value--;
Expand Down
1 change: 0 additions & 1 deletion dbms/src/Common/ErrorExporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ void ErrorExporter::writeError(const TiFlashError & error)
error.description.data(),
error.workaround.data());
DB::writeString(buffer, wb);
return;
}

void ErrorExporter::flush()
Expand Down
1 change: 0 additions & 1 deletion dbms/src/Common/EventRecorder.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ class EventRecorder
EventRecorder(ProfileEvents::Event event_, ProfileEvents::Event event_elapsed_)
: event(event_)
, event_elapsed(event_elapsed_)
, watch()
{
watch.start();
}
Expand Down
6 changes: 3 additions & 3 deletions dbms/src/Common/Exception.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,9 @@ int getCurrentExceptionCode()

void rethrowFirstException(const Exceptions & exceptions)
{
for (size_t i = 0, size = exceptions.size(); i < size; ++i)
if (exceptions[i])
std::rethrow_exception(exceptions[i]);
for (const auto & exception : exceptions)
if (exception)
std::rethrow_exception(exception);
}


Expand Down
8 changes: 4 additions & 4 deletions dbms/src/Common/Exception.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ namespace DB
class Exception : public Poco::Exception
{
public:
Exception() {} /// For deferred initialization.
Exception(const std::string & msg, int code = 0)
Exception() = default; /// For deferred initialization.
explicit Exception(const std::string & msg, int code = 0)
: Poco::Exception(msg, code)
{}
Exception(const std::string & msg, const std::string & arg, int code = 0)
Expand Down Expand Up @@ -52,7 +52,7 @@ class Exception : public Poco::Exception
class ErrnoException : public Exception
{
public:
ErrnoException(const std::string & msg, int code = 0, int saved_errno_ = 0)
explicit ErrnoException(const std::string & msg, int code = 0, int saved_errno_ = 0)
: Exception(msg, code)
, saved_errno(saved_errno_)
{}
Expand All @@ -75,7 +75,7 @@ class ErrnoException : public Exception
using Exceptions = std::vector<std::exception_ptr>;


[[noreturn]] void throwFromErrno(const std::string & s, int code = 0, int the_errno = errno);
[[noreturn]] void throwFromErrno(const std::string & s, int code = 0, int e = errno);


/** Try to write an exception to the log (and forget about it).
Expand Down
8 changes: 4 additions & 4 deletions dbms/src/Functions/FunctionBinaryArithmetic.h
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ struct DecimalBinaryOperation
res = res / scale;
if constexpr (check_overflow)
{
if (res > DecimalMaxValue::MaxValue())
if (res > DecimalMaxValue::maxValue())
{
throw Exception("Decimal math overflow", ErrorCodes::DECIMAL_OVERFLOW);
}
Expand All @@ -503,7 +503,7 @@ struct DecimalBinaryOperation
auto res = Op::template apply<PromoteResultType>(a, b);
if constexpr (check_overflow)
{
if (res > DecimalMaxValue::MaxValue())
if (res > DecimalMaxValue::maxValue())
{
throw Exception("Decimal math overflow", ErrorCodes::DECIMAL_OVERFLOW);
}
Expand Down Expand Up @@ -532,7 +532,7 @@ struct DecimalBinaryOperation

if constexpr (check_overflow)
{
if (res > DecimalMaxValue::MaxValue())
if (res > DecimalMaxValue::maxValue())
{
throw Exception("Decimal math overflow", ErrorCodes::DECIMAL_OVERFLOW);
}
Expand All @@ -558,7 +558,7 @@ struct DecimalBinaryOperation

if constexpr (check_overflow)
{
if (res > DecimalMaxValue::MaxValue())
if (res > DecimalMaxValue::maxValue())
{
throw Exception("Decimal math overflow", ErrorCodes::DECIMAL_OVERFLOW);
}
Expand Down
2 changes: 1 addition & 1 deletion dbms/src/Functions/FunctionsRound.h
Original file line number Diff line number Diff line change
Expand Up @@ -1176,7 +1176,7 @@ struct TiDBDecimalRound
scaled_value *= PowForOutput::result[-difference];

// check overflow and construct result.
if (scaled_value > DecimalMaxValue::Get(info.output_prec))
if (scaled_value > DecimalMaxValue::get(info.output_prec))
throw TiFlashException("Data truncated", Errors::Decimal::Overflow);

auto result = static_cast<typename OutputType::NativeType>(scaled_value);
Expand Down
10 changes: 5 additions & 5 deletions dbms/src/Functions/FunctionsTiDBConversion.h
Original file line number Diff line number Diff line change
Expand Up @@ -813,7 +813,7 @@ struct TiDBConvertToDecimal
static U toTiDBDecimalInternal(T value, PrecType prec, ScaleType scale, const Context & context)
{
using UType = typename U::NativeType;
auto maxValue = DecimalMaxValue::Get(prec);
auto maxValue = DecimalMaxValue::get(prec);
if (value > maxValue || value < -maxValue)
{
context.getDAGContext()->handleOverflowError("cast to decimal", Errors::Types::Truncated);
Expand Down Expand Up @@ -874,7 +874,7 @@ struct TiDBConvertToDecimal
{
value *= 10;
}
auto max_value = DecimalMaxValue::Get(prec);
auto max_value = DecimalMaxValue::get(prec);
if (value > static_cast<Float64>(max_value))
{
context.getDAGContext()->handleOverflowError("cast real to decimal", Errors::Types::Truncated);
Expand Down Expand Up @@ -934,7 +934,7 @@ struct TiDBConvertToDecimal
}
}

auto max_value = DecimalMaxValue::Get(prec);
auto max_value = DecimalMaxValue::get(prec);
if (value > max_value || value < -max_value)
{
context.getDAGContext()->handleOverflowError("cast decimal as decimal", Errors::Types::Truncated);
Expand Down Expand Up @@ -1021,7 +1021,7 @@ struct TiDBConvertToDecimal
if (decimal_parts.exp_part.data[0] == '-')
return static_cast<UType>(0);
else
return static_cast<UType>(DecimalMaxValue::Get(prec));
return static_cast<UType>(DecimalMaxValue::get(prec));
}
}
Int256 v = 0;
Expand All @@ -1033,7 +1033,7 @@ struct TiDBConvertToDecimal
if (decimal_parts.int_part.data[pos] == '-')
is_negative = true;
}
Int256 max_value = DecimalMaxValue::Get(prec);
Int256 max_value = DecimalMaxValue::get(prec);

Int64 current_scale = frac_offset_by_exponent >= 0
? -(decimal_parts.int_part.size - pos + frac_offset_by_exponent)
Expand Down
3 changes: 1 addition & 2 deletions dbms/src/Functions/tests/gtest_arithmetic_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -762,9 +762,8 @@ try
using NativeType = Decimal::NativeType; \
using FieldType = DecimalField<Decimal>; \
auto prec = (precision); \
auto & builder = DecimalMaxValue::instance(); \
auto max_scale = std::min(decimal_max_scale, static_cast<ScaleType>(prec) - 1); \
auto exp10_x = static_cast<NativeType>(builder.Get(max_scale)) + 1; /* exp10_x: 10^x */ \
auto exp10_x = static_cast<NativeType>(DecimalMaxValue::get(max_scale)) + 1; /* exp10_x: 10^x */ \
auto decimal_max = exp10_x * 10 - 1; \
auto zero = static_cast<NativeType>(0); /* for Int256 */ \
ASSERT_COLUMN_EQ( \
Expand Down