From dc657340599d43a3580231683e0fa1c2645a8fbb Mon Sep 17 00:00:00 2001 From: flynn Date: Sun, 27 Aug 2023 04:15:27 +0000 Subject: [PATCH] refactor some code --- src/Core/Field.h | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/src/Core/Field.h b/src/Core/Field.h index 239d28163a3..e4789d6edf9 100644 --- a/src/Core/Field.h +++ b/src/Core/Field.h @@ -15,7 +15,8 @@ #include #include #include - +#include +#include namespace DB { @@ -138,7 +139,7 @@ template bool decimalEqual(T x, T y, UInt32 x_scale, UInt32 y_scale template bool decimalLess(T x, T y, UInt32 x_scale, UInt32 y_scale); template bool decimalLessOrEqual(T x, T y, UInt32 x_scale, UInt32 y_scale); -template +template class DecimalField { public: @@ -285,6 +286,11 @@ decltype(auto) castToNearestFieldType(T && x) return U(x); } +template +concept not_field_or_bool_or_stringlike + = (!std::is_same_v, Field> && !std::is_same_v, bool> + && !std::is_same_v>, String>); + /** 32 is enough. Round number is used for alignment and for better arithmetic inside std::vector. * NOTE: Actually, sizeof(std::string) is 32 when using libc++, so Field is 40 bytes. */ @@ -347,13 +353,6 @@ public: || which == Types::Decimal256; } - /// Templates to avoid ambiguity. - template - using enable_if_not_field_or_bool_or_stringlike_t = std::enable_if_t< - !std::is_same_v, Field> && - !std::is_same_v, bool> && - !std::is_same_v>, String>, Z>; - Field() : Field(Null{}) {} /** Despite the presence of a template constructor, this constructor is still needed, @@ -370,7 +369,8 @@ public: } template - Field(T && rhs, enable_if_not_field_or_bool_or_stringlike_t = nullptr); /// NOLINT + requires not_field_or_bool_or_stringlike + Field(T && rhs); /// NOLINT Field(bool rhs) : Field(castToNearestFieldType(rhs)) /// NOLINT { @@ -425,7 +425,8 @@ public: /// 1. float <--> int needs explicit cast /// 2. customized types needs explicit cast template - enable_if_not_field_or_bool_or_stringlike_t & /// NOLINT + requires not_field_or_bool_or_stringlike + Field & /// NOLINT operator=(T && rhs); Field & operator= (bool rhs) @@ -448,7 +449,7 @@ public: Types::Which getType() const { return which; } - std::string_view getTypeName() const; + constexpr std::string_view getTypeName() const { return magic_enum::enum_name(which); } bool isNull() const { return which == Types::Null; } template @@ -838,7 +839,7 @@ template <> struct Field::EnumToType { using Type = Dec template <> struct Field::EnumToType { using Type = DecimalField; }; template <> struct Field::EnumToType { using Type = DecimalField; }; template <> struct Field::EnumToType { using Type = DecimalField; }; -template <> struct Field::EnumToType { using Type = AggregateFunctionStateData; }; +template <> struct Field::EnumToType { using Type = DecimalField; }; template <> struct Field::EnumToType { using Type = CustomType; }; template <> struct Field::EnumToType { using Type = UInt64; }; @@ -896,14 +897,16 @@ auto & Field::safeGet() template -Field::Field(T && rhs, enable_if_not_field_or_bool_or_stringlike_t) +requires not_field_or_bool_or_stringlike +Field::Field(T && rhs) { auto && val = castToNearestFieldType(std::forward(rhs)); createConcrete(std::forward(val)); } template -Field::enable_if_not_field_or_bool_or_stringlike_t & /// NOLINT +requires not_field_or_bool_or_stringlike +Field & /// NOLINT Field::operator=(T && rhs) { auto && val = castToNearestFieldType(std::forward(rhs)); @@ -1004,7 +1007,7 @@ void writeFieldText(const Field & x, WriteBuffer & buf); String toString(const Field & x); -std::string_view fieldTypeToString(Field::Types::Which type); +String fieldTypeToString(Field::Types::Which type); }