From 4e880288c65c07424ac64c6273fd9ee73fe327c8 Mon Sep 17 00:00:00 2001 From: flynn Date: Sat, 12 Aug 2023 15:10:30 +0800 Subject: [PATCH] [refactor]use clear concept to replace std::enable_if_t (#22801) --------- Signed-off-by: flynn --- be/src/vec/columns/column_const.h | 4 ++- be/src/vec/common/columns_hashing.h | 11 +++--- be/src/vec/common/columns_hashing_impl.h | 22 ++++++------ be/src/vec/common/hash_table/hash.h | 3 +- be/src/vec/common/nan_utils.h | 25 ++++++++----- be/src/vec/common/sip_hash.h | 9 +++-- be/src/vec/common/typeid_cast.h | 3 +- be/src/vec/core/decimal_comparison.h | 12 +++---- be/src/vec/core/field.h | 12 ++++--- be/src/vec/data_types/data_type_decimal.h | 21 ++++++----- .../vec/functions/array/function_array_map.h | 5 +-- .../vec/functions/array/function_array_set.h | 6 ++-- be/src/vec/functions/function_cast.h | 3 +- be/src/vec/functions/function_helpers.h | 6 ++-- be/src/vec/functions/function_json.cpp | 9 +++-- be/src/vec/functions/function_totype.h | 16 ++++----- be/test/vec/exprs/vexpr_test.cpp | 36 +++++++++---------- 17 files changed, 112 insertions(+), 91 deletions(-) diff --git a/be/src/vec/columns/column_const.h b/be/src/vec/columns/column_const.h index 3f33ee5792..e06902972a 100644 --- a/be/src/vec/columns/column_const.h +++ b/be/src/vec/columns/column_const.h @@ -24,6 +24,7 @@ #include #include +#include #include #include #include @@ -279,7 +280,8 @@ std::pair check_column_const_set_readability(const IColumn& c /* * @warning use this function sometimes cause performance problem in GCC. */ -template , T> = 0> +template + requires std::is_integral_v T index_check_const(T arg, bool constancy) noexcept { return constancy ? 0 : arg; } diff --git a/be/src/vec/common/columns_hashing.h b/be/src/vec/common/columns_hashing.h index 84d486a443..2c28e34b75 100644 --- a/be/src/vec/common/columns_hashing.h +++ b/be/src/vec/common/columns_hashing.h @@ -315,8 +315,9 @@ struct HashMethodSingleLowNullableColumn : public SingleColumnMethod { } template - ALWAYS_INLINE typename std::enable_if_t& lazy_emplace_key( - Data& data, size_t row, Arena& pool, Func&& f, CreatorForNull&& null_creator) { + requires has_mapped + ALWAYS_INLINE Mapped& lazy_emplace_key(Data& data, size_t row, Arena& pool, Func&& f, + CreatorForNull&& null_creator) { if (key_column->is_null_at(row)) { bool has_null_key = data.has_null_key_data(); data.has_null_key_data() = true; @@ -330,9 +331,9 @@ struct HashMethodSingleLowNullableColumn : public SingleColumnMethod { } template - ALWAYS_INLINE typename std::enable_if_t& lazy_emplace_key( - Data& data, size_t row, Arena& pool, size_t hash_value, Func&& f, - CreatorForNull&& null_creator) { + requires has_mapped + ALWAYS_INLINE Mapped& lazy_emplace_key(Data& data, size_t row, Arena& pool, size_t hash_value, + Func&& f, CreatorForNull&& null_creator) { if (key_column->is_null_at(row)) { bool has_null_key = data.has_null_key_data(); data.has_null_key_data() = true; diff --git a/be/src/vec/common/columns_hashing_impl.h b/be/src/vec/common/columns_hashing_impl.h index bfd69c35a9..e8ce4efed6 100644 --- a/be/src/vec/common/columns_hashing_impl.h +++ b/be/src/vec/common/columns_hashing_impl.h @@ -121,7 +121,7 @@ class HashMethodBase { public: using EmplaceResult = EmplaceResultImpl; using FindResult = FindResultImpl; - static constexpr bool has_mapped = !std::is_same::value; + static constexpr bool has_mapped = !std::is_same_v; using Cache = LastElementCache; static HashMethodContextPtr createContext(const HashMethodContext::Settings&) { @@ -147,17 +147,16 @@ public: } template - ALWAYS_INLINE typename std::enable_if_t& lazy_emplace_key(Data& data, - size_t row, - Arena& pool, - Func&& f) { + requires has_mapped + ALWAYS_INLINE Mapped& lazy_emplace_key(Data& data, size_t row, Arena& pool, Func&& f) { auto key_holder = static_cast(*this).get_key_holder(row, pool); return lazy_emplace_impl(key_holder, data, std::forward(f)); } template - ALWAYS_INLINE typename std::enable_if_t& lazy_emplace_key( - Data& data, size_t hash_value, size_t row, Arena& pool, Func&& f) { + requires has_mapped + ALWAYS_INLINE Mapped& lazy_emplace_key(Data& data, size_t hash_value, size_t row, Arena& pool, + Func&& f) { auto key_holder = static_cast(*this).get_key_holder(row, pool); return lazy_emplace_impl(key_holder, hash_value, data, std::forward(f)); } @@ -314,16 +313,17 @@ protected: } template - ALWAYS_INLINE typename std::enable_if_t& lazy_emplace_impl( - KeyHolder& key_holder, Data& data, Func&& f) { + requires has_mapped + ALWAYS_INLINE Mapped& lazy_emplace_impl(KeyHolder& key_holder, Data& data, Func&& f) { typename Data::LookupResult it; data.lazy_emplace(key_holder, it, std::forward(f)); return *lookup_result_get_mapped(it); } template - ALWAYS_INLINE typename std::enable_if_t& lazy_emplace_impl( - KeyHolder& key_holder, size_t hash_value, Data& data, Func&& f) { + requires has_mapped + ALWAYS_INLINE Mapped& lazy_emplace_impl(KeyHolder& key_holder, size_t hash_value, Data& data, + Func&& f) { typename Data::LookupResult it; data.lazy_emplace(key_holder, it, hash_value, std::forward(f)); diff --git a/be/src/vec/common/hash_table/hash.h b/be/src/vec/common/hash_table/hash.h index 85888644a9..9856be0a3f 100644 --- a/be/src/vec/common/hash_table/hash.h +++ b/be/src/vec/common/hash_table/hash.h @@ -86,7 +86,8 @@ template struct DefaultHash; template -struct DefaultHash>> { + requires std::is_arithmetic_v +struct DefaultHash { size_t operator()(T key) const { return default_hash64(key); } }; diff --git a/be/src/vec/common/nan_utils.h b/be/src/vec/common/nan_utils.h index 8cdf8d78a9..66be6b72a2 100644 --- a/be/src/vec/common/nan_utils.h +++ b/be/src/vec/common/nan_utils.h @@ -26,44 +26,51 @@ /// To be sure, that this function is zero-cost for non-floating point types. template -std::enable_if_t, bool> is_nan(T x) { + requires std::is_floating_point_v +bool is_nan(T x) { return std::isnan(x); } template -std::enable_if_t, bool> is_nan(T) { + requires(!std::is_floating_point_v) +bool is_nan(T) { return false; } template -std::enable_if_t, bool> is_finite(T x) { + requires std::is_floating_point_v +bool is_finite(T x) { return std::isfinite(x); } template -std::enable_if_t, bool> is_finite(T) { + requires(!std::is_floating_point_v) +bool is_finite(T) { return true; } template -std::enable_if_t, T> nan_or_zero() { + requires std::is_floating_point_v +T nan_or_zero() { return std::numeric_limits::quiet_NaN(); } template -std::enable_if_t::is_integer, T> nan_or_zero() { + requires std::numeric_limits::is_integer +T nan_or_zero() { return 0; } template -std::enable_if_t, T> nan_or_zero() { + requires std::is_class_v +T nan_or_zero() { return T {}; } #if 1 /// __int128 template -std::enable_if_t && !std::numeric_limits::is_integer, __int128> -nan_or_zero() { + requires(std::is_same_v && !std::numeric_limits::is_integer) +__int128 nan_or_zero() { return __int128(0); } #endif diff --git a/be/src/vec/common/sip_hash.h b/be/src/vec/common/sip_hash.h index b9416ef34a..bc20e66357 100644 --- a/be/src/vec/common/sip_hash.h +++ b/be/src/vec/common/sip_hash.h @@ -171,8 +171,8 @@ public: /// NOTE: std::has_unique_object_representations is only available since clang 6. As of Mar 2017 we still use clang 5 sometimes. template - std::enable_if_t, void> - update(const T& x) { + requires std::is_standard_layout_v + void update(const T& x) { update(reinterpret_cast(&x), sizeof(x)); } @@ -225,9 +225,8 @@ inline doris::vectorized::UInt64 sip_hash64(const char* data, const size_t size) } template -std::enable_if_t, - doris::vectorized::UInt64> -sip_hash64(const T& x) { + requires std::is_standard_layout_v +doris::vectorized::UInt64 sip_hash64(const T& x) { SipHash hash; hash.update(x); return hash.get64(); diff --git a/be/src/vec/common/typeid_cast.h b/be/src/vec/common/typeid_cast.h index 06c5004f04..fefd38409f 100644 --- a/be/src/vec/common/typeid_cast.h +++ b/be/src/vec/common/typeid_cast.h @@ -42,7 +42,8 @@ * In the rest, behaves like a dynamic_cast. */ template -std::enable_if_t, To> typeid_cast(From& from) { + requires std::is_reference_v +To typeid_cast(From& from) { try { if (typeid(from) == typeid(To)) { return static_cast(from); diff --git a/be/src/vec/core/decimal_comparison.h b/be/src/vec/core/decimal_comparison.h index 4c7cfcf765..68a083dc15 100644 --- a/be/src/vec/core/decimal_comparison.h +++ b/be/src/vec/core/decimal_comparison.h @@ -135,8 +135,8 @@ private: } template - static std::enable_if_t && IsDecimalNumber, Shift> getScales( - const DataTypePtr& left_type, const DataTypePtr& right_type) { + requires IsDecimalNumber && IsDecimalNumber + static Shift getScales(const DataTypePtr& left_type, const DataTypePtr& right_type) { const DataTypeDecimal* decimal0 = check_decimal(*left_type); const DataTypeDecimal* decimal1 = check_decimal(*right_type); @@ -157,8 +157,8 @@ private: } template - static std::enable_if_t && !IsDecimalNumber, Shift> getScales( - const DataTypePtr& left_type, const DataTypePtr&) { + requires(IsDecimalNumber && !IsDecimalNumber) + static Shift getScales(const DataTypePtr& left_type, const DataTypePtr&) { Shift shift; const DataTypeDecimal* decimal0 = check_decimal(*left_type); if (decimal0) shift.b = decimal0->get_scale_multiplier(); @@ -166,8 +166,8 @@ private: } template - static std::enable_if_t && IsDecimalNumber, Shift> getScales( - const DataTypePtr&, const DataTypePtr& right_type) { + requires(!IsDecimalNumber && IsDecimalNumber) + static Shift getScales(const DataTypePtr&, const DataTypePtr& right_type) { Shift shift; const DataTypeDecimal* decimal1 = check_decimal(*right_type); if (decimal1) shift.a = decimal1->get_scale_multiplier(); diff --git a/be/src/vec/core/field.h b/be/src/vec/core/field.h index 9cc07669ec..89c7d79934 100644 --- a/be/src/vec/core/field.h +++ b/be/src/vec/core/field.h @@ -415,7 +415,8 @@ public: Field(Field&& rhs) { create(std::move(rhs)); } template - Field(T&& rhs, std::enable_if_t, Field>, void*> = nullptr); + requires(!std::is_same_v, Field>) + Field(T&& rhs); /// Create a string inplace. Field(const char* data, size_t size) { create(data, size); } @@ -466,7 +467,8 @@ public: } template - std::enable_if_t, Field>, Field&> operator=(T&& rhs); + requires(!std::is_same_v, Field>) + Field& operator=(T&& rhs); ~Field() { destroy(); } @@ -1218,13 +1220,15 @@ decltype(auto) cast_to_nearest_field_type(T&& x) { /// 1. float <--> int needs explicit cast /// 2. customized types needs explicit cast template -Field::Field(T&& rhs, std::enable_if_t, Field>, void*>) { + requires(!std::is_same_v, Field>) +Field::Field(T&& rhs) { auto&& val = cast_to_nearest_field_type(std::forward(rhs)); create_concrete(std::forward(val)); } template -std::enable_if_t, Field>, Field&> Field::operator=(T&& rhs) { + requires(!std::is_same_v, Field>) +Field& Field::operator=(T&& rhs) { auto&& val = cast_to_nearest_field_type(std::forward(rhs)); using U = decltype(val); if (which != TypeToEnum>::value) { diff --git a/be/src/vec/data_types/data_type_decimal.h b/be/src/vec/data_types/data_type_decimal.h index 7417d884c0..138f277c3f 100644 --- a/be/src/vec/data_types/data_type_decimal.h +++ b/be/src/vec/data_types/data_type_decimal.h @@ -407,10 +407,10 @@ constexpr bool IsDataTypeDecimalOrNumber = IsDataTypeDecimal || IsDataTypeNumber; template -std::enable_if_t && IsDataTypeDecimal, - typename ToDataType::FieldType> -convert_decimals(const typename FromDataType::FieldType& value, UInt32 scale_from, UInt32 scale_to, - UInt8* overflow_flag = nullptr) { + requires IsDataTypeDecimal && IsDataTypeDecimal +ToDataType::FieldType convert_decimals(const typename FromDataType::FieldType& value, + UInt32 scale_from, UInt32 scale_to, + UInt8* overflow_flag = nullptr) { using FromFieldType = typename FromDataType::FieldType; using ToFieldType = typename ToDataType::FieldType; using MaxFieldType = @@ -534,9 +534,9 @@ void convert_decimal_cols( } template -std::enable_if_t && IsDataTypeNumber, - typename ToDataType::FieldType> -convert_from_decimal(const typename FromDataType::FieldType& value, UInt32 scale) { + requires IsDataTypeDecimal && IsDataTypeNumber +ToDataType::FieldType convert_from_decimal(const typename FromDataType::FieldType& value, + UInt32 scale) { using FromFieldType = typename FromDataType::FieldType; using ToFieldType = typename ToDataType::FieldType; @@ -578,10 +578,9 @@ convert_from_decimal(const typename FromDataType::FieldType& value, UInt32 scale } template -std::enable_if_t && IsDataTypeDecimal, - typename ToDataType::FieldType> -convert_to_decimal(const typename FromDataType::FieldType& value, UInt32 scale, - UInt8* overflow_flag) { + requires IsDataTypeNumber && IsDataTypeDecimal +ToDataType::FieldType convert_to_decimal(const typename FromDataType::FieldType& value, + UInt32 scale, UInt8* overflow_flag) { using FromFieldType = typename FromDataType::FieldType; using ToNativeType = typename ToDataType::FieldType::NativeType; diff --git a/be/src/vec/functions/array/function_array_map.h b/be/src/vec/functions/array/function_array_map.h index ccf5603942..be9cd1be3d 100644 --- a/be/src/vec/functions/array/function_array_map.h +++ b/be/src/vec/functions/array/function_array_map.h @@ -182,7 +182,8 @@ private: return true; } - template 0), int> = 0> + template + requires(sizeof...(Ts) > 0) static bool _execute_internal(ColumnArrayMutableData& dst, ColumnArrayExecutionDatas datas, std::vector& col_const, int start_row, int end_row) { return _execute_internal(dst, datas, col_const, start_row, end_row) || @@ -190,4 +191,4 @@ private: } }; -} // namespace doris::vectorized \ No newline at end of file +} // namespace doris::vectorized diff --git a/be/src/vec/functions/array/function_array_set.h b/be/src/vec/functions/array/function_array_set.h index d8e0a156ba..1012fd055e 100644 --- a/be/src/vec/functions/array/function_array_set.h +++ b/be/src/vec/functions/array/function_array_set.h @@ -206,8 +206,8 @@ private: } return true; } - template 0), int> = 0> + template + requires(sizeof...(Ts) > 0) static bool _execute_internal(ColumnArrayMutableData& dst, const ColumnArrayExecutionData& left_data, const ColumnArrayExecutionData& right_data) { @@ -216,4 +216,4 @@ private: } }; -} // namespace doris::vectorized \ No newline at end of file +} // namespace doris::vectorized diff --git a/be/src/vec/functions/function_cast.h b/be/src/vec/functions/function_cast.h index 7f6a2ebec4..93fdd1f108 100644 --- a/be/src/vec/functions/function_cast.h +++ b/be/src/vec/functions/function_cast.h @@ -442,7 +442,8 @@ struct ConvertImpl { /** If types are identical, just take reference to column. */ template -struct ConvertImpl, T, Name> { + requires(!T::is_parametric) +struct ConvertImpl { static Status execute(FunctionContext* context, Block& block, const ColumnNumbers& arguments, size_t result, size_t /*input_rows_count*/) { block.get_by_position(result).column = block.get_by_position(arguments[0]).column; diff --git a/be/src/vec/functions/function_helpers.h b/be/src/vec/functions/function_helpers.h index 8ac921d9a3..dce507f656 100644 --- a/be/src/vec/functions/function_helpers.h +++ b/be/src/vec/functions/function_helpers.h @@ -81,12 +81,14 @@ const ColumnConst* check_and_get_column_const_string_or_fixedstring(const IColum /// Transform anything to Field. template -std::enable_if_t, Field> to_field(const T& x) { + requires(!IsDecimalNumber) +Field to_field(const T& x) { return Field(NearestFieldType(x)); } template -std::enable_if_t, Field> to_field(const T& x, UInt32 scale) { + requires IsDecimalNumber +Field to_field(const T& x, UInt32 scale) { return Field(NearestFieldType(x, scale)); } diff --git a/be/src/vec/functions/function_json.cpp b/be/src/vec/functions/function_json.cpp index 4d2f0f3614..12af7f4bf1 100644 --- a/be/src/vec/functions/function_json.cpp +++ b/be/src/vec/functions/function_json.cpp @@ -355,7 +355,8 @@ struct GetJsonNumberType { } } - template , T>* = nullptr> + template + requires std::is_same_v static void handle_result(rapidjson::Value* root, T& res, uint8_t& res_null) { if (root == nullptr || root->IsNull()) { res = 0; @@ -371,7 +372,8 @@ struct GetJsonNumberType { } } - template , T>* = nullptr> + template + requires std::is_same_v static void handle_result(rapidjson::Value* root, int32_t& res, uint8_t& res_null) { if (root != nullptr && root->IsInt()) { res = root->GetInt(); @@ -380,7 +382,8 @@ struct GetJsonNumberType { } } - template , T>* = nullptr> + template + requires std::is_same_v static void handle_result(rapidjson::Value* root, int64_t& res, uint8_t& res_null) { if (root != nullptr && root->IsInt64()) { res = root->GetInt64(); diff --git a/be/src/vec/functions/function_totype.h b/be/src/vec/functions/function_totype.h index 3c2eb8f9d4..2dc72cbec6 100644 --- a/be/src/vec/functions/function_totype.h +++ b/be/src/vec/functions/function_totype.h @@ -65,7 +65,8 @@ public: private: // handle result == DataTypeString - template , T>* = nullptr> + template + requires std::is_same_v Status execute_impl(Block& block, const ColumnNumbers& arguments, size_t result, size_t input_rows_count) { const ColumnPtr column = block.get_by_position(arguments[0]).column; @@ -92,7 +93,8 @@ private: block.get_by_position(arguments[0]).column->get_name(), get_name()); } - template , T>* = nullptr> + template + requires(!std::is_same_v) Status execute_impl(Block& block, const ColumnNumbers& arguments, size_t result, size_t input_rows_count) { const ColumnPtr column = block.get_by_position(arguments[0]).column; @@ -223,9 +225,8 @@ public: } private: - template , ReturnDataType>* = - nullptr> + template + requires(!std::is_same_v) Status execute_inner_impl(const ColumnWithTypeAndName& left, const ColumnWithTypeAndName& right, Block& block, const ColumnNumbers& arguments, size_t result) { const auto& [lcol, left_const] = unpack_if_const(left.column); @@ -261,9 +262,8 @@ private: return Status::RuntimeError("unimplements function {}", get_name()); } - template , ReturnDataType>* = - nullptr> + template + requires std::is_same_v Status execute_inner_impl(const ColumnWithTypeAndName& left, const ColumnWithTypeAndName& right, Block& block, const ColumnNumbers& arguments, size_t result) { const auto& [lcol, left_const] = unpack_if_const(left.column); diff --git a/be/test/vec/exprs/vexpr_test.cpp b/be/test/vec/exprs/vexpr_test.cpp index 6fedf5e8e3..8c90debda6 100644 --- a/be/test/vec/exprs/vexpr_test.cpp +++ b/be/test/vec/exprs/vexpr_test.cpp @@ -267,8 +267,8 @@ struct literal_traits { }; //======================== set literal =================================== -template ::CXXType, - std::enable_if_t::value, bool> = true> +template ::CXXType> + requires std::is_integral_v void set_literal(TExprNode& node, const U& value) { TIntLiteral int_literal; int_literal.__set_value(value); @@ -289,64 +289,64 @@ void set_literal(TExprNode& node, const __int128_t& v node.__set_large_int_literal(largeIntLiteral); } // std::is_same::value -template ::CXXType, - std::enable_if_t = true> +template ::CXXType> + requires(T == TYPE_DATETIME) void set_literal(TExprNode& node, const U& value) { TDateLiteral date_literal; date_literal.__set_value(value); node.__set_date_literal(date_literal); } -template ::CXXType, - std::enable_if_t = true> +template ::CXXType> + requires(T == TYPE_DATETIMEV2) void set_literal(TExprNode& node, const U& value) { TDateLiteral date_literal; date_literal.__set_value(value); node.__set_date_literal(date_literal); } -template ::CXXType, - std::enable_if_t = true> +template ::CXXType> + requires(T == TYPE_DATE) void set_literal(TExprNode& node, const U& value) { TDateLiteral date_literal; date_literal.__set_value(value); node.__set_date_literal(date_literal); } -template ::CXXType, - std::enable_if_t = true> +template ::CXXType> + requires(T == TYPE_DATEV2) void set_literal(TExprNode& node, const U& value) { TDateLiteral date_literal; date_literal.__set_value(value); node.__set_date_literal(date_literal); } -template ::CXXType, - std::enable_if_t = true> +template ::CXXType> + requires(T == TYPE_JSONB) void set_literal(TExprNode& node, const U& value) { TJsonLiteral jsonb_literal; jsonb_literal.__set_value(value); node.__set_json_literal(jsonb_literal); } -template ::CXXType, - std::enable_if_t = true> +template ::CXXType> + requires(T == TYPE_STRING) void set_literal(TExprNode& node, const U& value) { TStringLiteral string_literal; string_literal.__set_value(value); node.__set_string_literal(string_literal); } -template ::CXXType, - std::enable_if_t::is_iec559, bool> = true> +template ::CXXType> + requires std::numeric_limits::is_iec559 void set_literal(TExprNode& node, const U& value) { TFloatLiteral floatLiteral; floatLiteral.__set_value(value); node.__set_float_literal(floatLiteral); } -template ::CXXType, - std::enable_if_t = true> +template ::CXXType> + requires(T == TYPE_DECIMALV2) void set_literal(TExprNode& node, const U& value) { TDecimalLiteral decimal_literal; decimal_literal.__set_value(value);