diff --git a/be/src/vec/aggregate_functions/aggregate_function_avg.h b/be/src/vec/aggregate_functions/aggregate_function_avg.h index 955dd5644f..ae1ea26903 100644 --- a/be/src/vec/aggregate_functions/aggregate_function_avg.h +++ b/be/src/vec/aggregate_functions/aggregate_function_avg.h @@ -60,8 +60,6 @@ struct AggregateFunctionAvgData { DecimalV2Value cal_ret = decimal_val_sum / decimal_val_count; Decimal128 ret(cal_ret.value()); return ret; - } else if constexpr (IsDecimal128I && IsDecimal128I) { - return static_cast(sum).value.val / count; } else { return static_cast(sum) / count; } @@ -83,13 +81,9 @@ template class AggregateFunctionAvg final : public IAggregateFunctionDataHelper> { public: - using ResultType = - std::conditional_t, Decimal128, - std::conditional_t, Decimal128I, Float64>>; - using ResultDataType = - std::conditional_t, DataTypeDecimal, - std::conditional_t, DataTypeDecimal, - DataTypeNumber>>; + using ResultType = std::conditional_t, Decimal128, Float64>; + using ResultDataType = std::conditional_t, DataTypeDecimal, + DataTypeNumber>; using ColVecType = std::conditional_t, ColumnDecimal, ColumnVector>; using ColVecResult = std::conditional_t, ColumnDecimal, @@ -121,10 +115,8 @@ public: void add(AggregateDataPtr __restrict place, const IColumn** columns, size_t row_num, Arena*) const override { const auto& column = static_cast(*columns[0]); - if constexpr (IsDecimal128I && IsDecimal128I) { - this->data(place).sum.value.val += column.get_data()[row_num].value.val; - } else if constexpr (IsDecimal128I && IsDecimalNumber) { - this->data(place).sum.value.val += column.get_data()[row_num]; + if constexpr (IsDecimalNumber) { + this->data(place).sum += column.get_data()[row_num].value; } else { this->data(place).sum += column.get_data()[row_num]; } @@ -138,7 +130,11 @@ public: void merge(AggregateDataPtr __restrict place, ConstAggregateDataPtr rhs, Arena*) const override { - this->data(place).sum += this->data(rhs).sum; + if constexpr (IsDecimalNumber) { + this->data(place).sum += this->data(rhs).sum.value; + } else { + this->data(place).sum += this->data(rhs).sum; + } this->data(place).count += this->data(rhs).count; } diff --git a/be/src/vec/aggregate_functions/aggregate_function_avg_weighted.h b/be/src/vec/aggregate_functions/aggregate_function_avg_weighted.h index 0d86055468..c7cdb86a2c 100644 --- a/be/src/vec/aggregate_functions/aggregate_function_avg_weighted.h +++ b/be/src/vec/aggregate_functions/aggregate_function_avg_weighted.h @@ -32,8 +32,6 @@ struct AggregateFunctionAvgWeightedData { if constexpr (IsDecimalV2) { DecimalV2Value value = binary_cast(data_val); data_sum = data_sum + (static_cast(value) * weight_val); - } else if constexpr (IsDecimal128I) { - data_sum = data_sum + (data_val.value.val * weight_val); } else { data_sum = data_sum + (data_val * weight_val); } diff --git a/be/src/vec/aggregate_functions/aggregate_function_collect.h b/be/src/vec/aggregate_functions/aggregate_function_collect.h index f385aa3000..e78fdfe082 100644 --- a/be/src/vec/aggregate_functions/aggregate_function_collect.h +++ b/be/src/vec/aggregate_functions/aggregate_function_collect.h @@ -41,11 +41,7 @@ struct AggregateFunctionCollectSetData { void add(const IColumn& column, size_t row_num) { const auto& vec = assert_cast(column).get_data(); - if constexpr (IsDecimal128I) { - set.insert(vec[row_num].value); - } else { - set.insert(vec[row_num]); - } + set.insert(vec[row_num]); } void merge(const AggregateFunctionCollectSetData& rhs) { set.merge(rhs.set); } void write(BufferWritable& buf) const { set.write(buf); } diff --git a/be/src/vec/aggregate_functions/aggregate_function_min_max.h b/be/src/vec/aggregate_functions/aggregate_function_min_max.h index 08108526d4..5bb86dd271 100644 --- a/be/src/vec/aggregate_functions/aggregate_function_min_max.h +++ b/be/src/vec/aggregate_functions/aggregate_function_min_max.h @@ -206,23 +206,11 @@ public: } bool change_if_less(const IColumn& column, size_t row_num, Arena* arena) { - if constexpr (IsDecimal128I) { - if (!has() || - (assert_cast&>(column).get_data()[row_num]).value.val < - value.val) { - change(column, row_num, arena); - return true; - } else { - return false; - } + if (!has() || assert_cast&>(column).get_data()[row_num] < value) { + change(column, row_num, arena); + return true; } else { - if (!has() || - assert_cast&>(column).get_data()[row_num] < value) { - change(column, row_num, arena); - return true; - } else { - return false; - } + return false; } } @@ -236,23 +224,11 @@ public: } bool change_if_greater(const IColumn& column, size_t row_num, Arena* arena) { - if constexpr (IsDecimal128I) { - if (!has() || - (assert_cast&>(column).get_data()[row_num]).value.val > - value.val) { - change(column, row_num, arena); - return true; - } else { - return false; - } + if (!has() || assert_cast&>(column).get_data()[row_num] > value) { + change(column, row_num, arena); + return true; } else { - if (!has() || - assert_cast&>(column).get_data()[row_num] > value) { - change(column, row_num, arena); - return true; - } else { - return false; - } + return false; } } diff --git a/be/src/vec/aggregate_functions/aggregate_function_product.h b/be/src/vec/aggregate_functions/aggregate_function_product.h index d9260554be..28af3ead15 100644 --- a/be/src/vec/aggregate_functions/aggregate_function_product.h +++ b/be/src/vec/aggregate_functions/aggregate_function_product.h @@ -114,9 +114,7 @@ public: } void reset(AggregateDataPtr place) const override { - if constexpr (IsDecimal128I) { - this->data(place).reset(T(1 * ResultDataType::get_scale_multiplier(scale).value.val)); - } else if constexpr (IsDecimalNumber) { + if constexpr (IsDecimalNumber) { this->data(place).reset(T(1 * ResultDataType::get_scale_multiplier(scale))); } else { this->data(place).reset(1); diff --git a/be/src/vec/aggregate_functions/aggregate_function_sum.cpp b/be/src/vec/aggregate_functions/aggregate_function_sum.cpp index 22bf6fd80f..60313f6547 100644 --- a/be/src/vec/aggregate_functions/aggregate_function_sum.cpp +++ b/be/src/vec/aggregate_functions/aggregate_function_sum.cpp @@ -31,9 +31,7 @@ namespace doris::vectorized { template struct SumSimple { /// @note It uses slow Decimal128 (cause we need such a variant). sumWithOverflow is faster for Decimal32/64 - using ResultType = std::conditional_t< - IsDecimalV2, Decimal128, - std::conditional_t, Decimal128I, NearestFieldType>>; + using ResultType = std::conditional_t, Decimal128, NearestFieldType>; // using ResultType = NearestFieldType; using AggregateDataType = AggregateFunctionSumData; using Function = AggregateFunctionSum; diff --git a/be/src/vec/aggregate_functions/helpers.h b/be/src/vec/aggregate_functions/helpers.h index 36e11f7011..a6ada661b6 100644 --- a/be/src/vec/aggregate_functions/helpers.h +++ b/be/src/vec/aggregate_functions/helpers.h @@ -187,7 +187,7 @@ static IAggregateFunction* create_with_decimal_type(const IDataType& argument_ty return new AggregateFunctionTemplate(std::forward(args)...); } if (which.idx == TypeIndex::Decimal128I) { - return new AggregateFunctionTemplate(std::forward(args)...); + return new AggregateFunctionTemplate(std::forward(args)...); } return nullptr; } diff --git a/be/src/vec/columns/column_decimal.cpp b/be/src/vec/columns/column_decimal.cpp index a6bd40ab95..13902b54a7 100644 --- a/be/src/vec/columns/column_decimal.cpp +++ b/be/src/vec/columns/column_decimal.cpp @@ -40,12 +40,7 @@ int ColumnDecimal::compare_at(size_t n, size_t m, const IColumn& rhs_, int) c const T& a = data[n]; const T& b = other.data[m]; - if constexpr (doris::vectorized::IsDecimal128I) { - if (scale == other.scale) - return a.value.val > b.value.val ? 1 : (a.value.val < b.value.val ? -1 : 0); - } else { - if (scale == other.scale) return a > b ? 1 : (a < b ? -1 : 0); - } + if (scale == other.scale) return a > b ? 1 : (a < b ? -1 : 0); return decimal_less(b, a, other.scale, scale) ? 1 : (decimal_less(a, b, scale, other.scale) ? -1 : 0); @@ -379,20 +374,11 @@ void ColumnDecimal::get_extremes(Field& min, Field& max) const { T cur_min = data[0]; T cur_max = data[0]; - if constexpr (doris::vectorized::IsDecimal128I) { - for (const T& x : data) { - if (x.value.val < cur_min.value.val) - cur_min.value.val = x.value.val; - else if (x.value.val > cur_max.value.val) - cur_max.value.val = x.value.val; - } - } else { - for (const T& x : data) { - if (x < cur_min) - cur_min = x; - else if (x > cur_max) - cur_max = x; - } + for (const T& x : data) { + if (x < cur_min) + cur_min = x; + else if (x > cur_max) + cur_max = x; } min = NearestFieldType(cur_min, scale); @@ -421,13 +407,7 @@ void ColumnDecimal::compare_internal(size_t rhs_row_id, const IColumn& rhs, for (size_t row_id = begin; row_id < end; row_id++) { auto value_a = get_data()[row_id]; int res = 0; - if constexpr (doris::vectorized::IsDecimal128I) { - res = value_a.value.val > cmp_base.value.val - ? 1 - : (value_a.value.val < cmp_base.value.val ? -1 : 0); - } else { - res = value_a > cmp_base ? 1 : (value_a < cmp_base ? -1 : 0); - } + res = value_a > cmp_base ? 1 : (value_a < cmp_base ? -1 : 0); if (res * direction < 0) { filter[row_id] = 1; cmp_res[row_id] = 1; diff --git a/be/src/vec/columns/column_decimal.h b/be/src/vec/columns/column_decimal.h index b19e5ff31e..a3960067cb 100644 --- a/be/src/vec/columns/column_decimal.h +++ b/be/src/vec/columns/column_decimal.h @@ -70,7 +70,6 @@ class ColumnDecimal final : public COWHelper; - static constexpr bool IsDecimal128I = std::is_same_v; public: using value_type = T; @@ -176,28 +175,10 @@ public: return StringRef(reinterpret_cast(&data[n]), sizeof(data[n])); } void get(size_t n, Field& res) const override { res = (*this)[n]; } - bool get_bool(size_t n) const override { - if constexpr (IsDecimal128I) { - return bool(data[n].value.val); - } else { - return bool(data[n]); - } - } - Int64 get_int(size_t n) const override { - if constexpr (IsDecimal128I) { - return Int64(data[n].value.val * scale); - } else { - return Int64(data[n] * scale); - } - } + bool get_bool(size_t n) const override { return bool(data[n]); } + Int64 get_int(size_t n) const override { return Int64(data[n] * scale); } UInt64 get64(size_t n) const override; - bool is_default_at(size_t n) const override { - if constexpr (IsDecimal128I) { - return data[n].value.val == 0; - } else { - return data[n] == 0; - } - } + bool is_default_at(size_t n) const override { return data[n] == 0; } void clear() override { data.clear(); } @@ -259,20 +240,8 @@ public: UInt32 get_scale() const { return scale; } T get_scale_multiplier() const; - T get_whole_part(size_t n) const { - if constexpr (IsDecimal128I) { - return data[n].value.val / get_scale_multiplier().value.val; - } else { - return data[n] / get_scale_multiplier(); - } - } - T get_fractional_part(size_t n) const { - if constexpr (IsDecimal128I) { - return data[n].value.val % get_scale_multiplier().value.val; - } else { - return data[n] % get_scale_multiplier(); - } - } + T get_whole_part(size_t n) const { return data[n] / get_scale_multiplier(); } + T get_fractional_part(size_t n) const { return data[n] % get_scale_multiplier(); } protected: Container data; @@ -287,23 +256,12 @@ protected: auto sort_end = res.end(); if (limit && limit < s) sort_end = res.begin() + limit; - if constexpr (doris::vectorized::IsDecimal128I) { - if (reverse) - std::partial_sort(res.begin(), sort_end, res.end(), [this](size_t a, size_t b) { - return data[a].value.val > data[b].value.val; - }); - else - std::partial_sort(res.begin(), sort_end, res.end(), [this](size_t a, size_t b) { - return data[a].value.val < data[b].value.val; - }); - } else { - if (reverse) - std::partial_sort(res.begin(), sort_end, res.end(), - [this](size_t a, size_t b) { return data[a] > data[b]; }); - else - std::partial_sort(res.begin(), sort_end, res.end(), - [this](size_t a, size_t b) { return data[a] < data[b]; }); - } + if (reverse) + std::partial_sort(res.begin(), sort_end, res.end(), + [this](size_t a, size_t b) { return data[a] > data[b]; }); + else + std::partial_sort(res.begin(), sort_end, res.end(), + [this](size_t a, size_t b) { return data[a] < data[b]; }); } }; diff --git a/be/src/vec/common/field_visitors.h b/be/src/vec/common/field_visitors.h index 42ba1bff0b..cf777a447a 100644 --- a/be/src/vec/common/field_visitors.h +++ b/be/src/vec/common/field_visitors.h @@ -243,11 +243,7 @@ public: if constexpr (std::is_floating_point_v) return static_cast(x.get_value()) / x.get_scale_multiplier(); else { - if constexpr (std::is_same_v) { - return x.get_value().value.val / x.get_scale_multiplier().value.val; - } else { - return x.get_value() / x.get_scale_multiplier(); - } + return x.get_value() / x.get_scale_multiplier(); } } diff --git a/be/src/vec/core/decimal_comparison.h b/be/src/vec/core/decimal_comparison.h index 4a4491d304..1f2bce4200 100644 --- a/be/src/vec/core/decimal_comparison.h +++ b/be/src/vec/core/decimal_comparison.h @@ -143,32 +143,12 @@ private: Shift shift; if (decimal0 && decimal1) { auto result_type = decimal_result_type(*decimal0, *decimal1, false, false); - if constexpr (sizeof(T) >= sizeof(U) && IsDecimal128I) { - shift.a = result_type.scale_factor_for(*decimal0, false).value.val; - } else if constexpr (sizeof(T) < sizeof(U) && IsDecimal128I) { - shift.a = result_type.scale_factor_for(*decimal0, false).value.val; - } else { - shift.a = result_type.scale_factor_for(*decimal0, false); - } - if constexpr (sizeof(T) >= sizeof(U) && IsDecimal128I) { - shift.b = result_type.scale_factor_for(*decimal1, false).value.val; - } else if constexpr (sizeof(T) < sizeof(U) && IsDecimal128I) { - shift.b = result_type.scale_factor_for(*decimal1, false).value.val; - } else { - shift.b = result_type.scale_factor_for(*decimal1, false); - } + shift.a = result_type.scale_factor_for(*decimal0, false); + shift.b = result_type.scale_factor_for(*decimal1, false); } else if (decimal0) { - if constexpr (IsDecimal128I) { - shift.b = decimal0->get_scale_multiplier().value.val; - } else { - shift.b = decimal0->get_scale_multiplier(); - } + shift.b = decimal0->get_scale_multiplier(); } else if (decimal1) { - if constexpr (IsDecimal128I) { - shift.a = decimal1->get_scale_multiplier().value.val; - } else { - shift.a = decimal1->get_scale_multiplier(); - } + shift.a = decimal1->get_scale_multiplier(); } return shift; @@ -179,11 +159,7 @@ private: const DataTypePtr& left_type, const DataTypePtr&) { Shift shift; const DataTypeDecimal* decimal0 = check_decimal(*left_type); - if constexpr (IsDecimal128I) { - if (decimal0) shift.b = decimal0->get_scale_multiplier().value.val; - } else { - if (decimal0) shift.b = decimal0->get_scale_multiplier(); - } + if (decimal0) shift.b = decimal0->get_scale_multiplier(); return shift; } @@ -192,11 +168,7 @@ private: const DataTypePtr&, const DataTypePtr& right_type) { Shift shift; const DataTypeDecimal* decimal1 = check_decimal(*right_type); - if constexpr (IsDecimal128I) { - if (decimal1) shift.a = decimal1->get_scale_multiplier().value.val; - } else { - if (decimal1) shift.a = decimal1->get_scale_multiplier(); - } + if (decimal1) shift.a = decimal1->get_scale_multiplier(); return shift; } @@ -256,19 +228,8 @@ private: template static NO_INLINE UInt8 apply(A a, B b, CompareInt scale [[maybe_unused]]) { - CompareInt x {}; - if constexpr (IsDecimal128I) { - x = a.value.val; - } else { - x = a; - } - - CompareInt y {}; - if constexpr (IsDecimal128I) { - y = b.value.val; - } else { - y = b; - } + CompareInt x = a; + CompareInt y = b; if constexpr (_check_overflow) { bool overflow = false; diff --git a/be/src/vec/core/field.cpp b/be/src/vec/core/field.cpp index 5d11aca10b..fb41677193 100644 --- a/be/src/vec/core/field.cpp +++ b/be/src/vec/core/field.cpp @@ -175,14 +175,14 @@ DECLARE_DECIMAL_COMPARISON(Decimal128) template <> bool decimal_equal(Decimal128I x, Decimal128I y, UInt32 xs, UInt32 ys) { - return dec_equal(Decimal128(x.value.val), Decimal128(y.value.val), xs, ys); + return dec_equal(Decimal128(x.value), Decimal128(y.value), xs, ys); } template <> bool decimal_less(Decimal128I x, Decimal128I y, UInt32 xs, UInt32 ys) { - return dec_less(Decimal128(x.value.val), Decimal128(y.value.val), xs, ys); + return dec_less(Decimal128(x.value), Decimal128(y.value), xs, ys); } template <> bool decimal_less_or_equal(Decimal128I x, Decimal128I y, UInt32 xs, UInt32 ys) { - return dec_less_or_equal(Decimal128(x.value.val), Decimal128(y.value.val), xs, ys); + return dec_less_or_equal(Decimal128(x.value), Decimal128(y.value), xs, ys); } } // namespace doris::vectorized diff --git a/be/src/vec/core/field.h b/be/src/vec/core/field.h index 89fe0de7d7..f298c43216 100644 --- a/be/src/vec/core/field.h +++ b/be/src/vec/core/field.h @@ -62,7 +62,12 @@ struct AvgNearestFieldTypeTrait { template <> struct AvgNearestFieldTypeTrait { - using Type = Decimal128I; + using Type = Decimal128; +}; + +template <> +struct AvgNearestFieldTypeTrait { + using Type = double; }; class Field; diff --git a/be/src/vec/core/sort_block.h b/be/src/vec/core/sort_block.h index 015d3f3235..9d4b9ca2b7 100644 --- a/be/src/vec/core/sort_block.h +++ b/be/src/vec/core/sort_block.h @@ -332,13 +332,7 @@ private: EqualRange& range, bool last_column) const { int new_limit = _limit; auto comparator = [&](const size_t a, const size_t b) { - if constexpr (std::is_same_v) { - auto value_a = column.get_data()[a]; - auto value_b = column.get_data()[b]; - return value_a.value.val > value_b.value.val - ? 1 - : (value_a.value.val < value_b.value.val ? -1 : 0); - } else if constexpr (!std::is_same_v) { + if constexpr (!std::is_same_v) { auto value_a = column.get_data()[a]; auto value_b = column.get_data()[b]; return value_a > value_b ? 1 : (value_a < value_b ? -1 : 0); @@ -403,11 +397,7 @@ private: _create_permutation(column, permutation_for_column.data(), perms); auto comparator = [&](const PermutationWithInlineValue& a, const PermutationWithInlineValue& b) { - if constexpr (std::is_same_v) { - return a.inline_value.value.val > b.inline_value.value.val - ? 1 - : (a.inline_value.value.val < b.inline_value.value.val ? -1 : 0); - } else if constexpr (!std::is_same_v) { + if constexpr (!std::is_same_v) { return a.inline_value > b.inline_value ? 1 : (a.inline_value < b.inline_value ? -1 : 0); } else { diff --git a/be/src/vec/core/types.h b/be/src/vec/core/types.h index 08bd469540..b61dedb686 100644 --- a/be/src/vec/core/types.h +++ b/be/src/vec/core/types.h @@ -264,61 +264,7 @@ using DateTime = Int64; using DateV2 = UInt32; using DateTimeV2 = UInt64; -struct Int128I { - Int128I() = default; - Int128I(Int128I&&) = default; - Int128I(const Int128I&) = default; - -#define DECLARE_NUMERIC_CTOR(TYPE) \ - constexpr Int128I(const TYPE& value_) : val(value_) {} \ - constexpr Int128I(const TYPE&& value_) : val(value_) {} - - DECLARE_NUMERIC_CTOR(Int128) - DECLARE_NUMERIC_CTOR(Int32) - DECLARE_NUMERIC_CTOR(Int64) - DECLARE_NUMERIC_CTOR(UInt32) - DECLARE_NUMERIC_CTOR(UInt64) - DECLARE_NUMERIC_CTOR(Float32) - DECLARE_NUMERIC_CTOR(Float64) -#undef DECLARE_NUMERIC_CTOR - constexpr Int128I& operator=(Int128I&&) = default; - constexpr Int128I& operator=(const Int128I&) = default; - - constexpr Int128I& operator=(Int128&& v) { - val = v; - return *this; - }; - - constexpr Int128I& operator=(const Int128& v) { - val = v; - return *this; - }; - - operator Int128() const { return val; } - - const Int128I& operator+=(const Int128& x) { - val += x; - return *this; - } - const Int128I& operator-=(const Int128& x) { - val -= x; - return *this; - } - const Int128I& operator*=(const Int128& x) { - val *= x; - return *this; - } - const Int128I& operator/=(const Int128& x) { - val /= x; - return *this; - } - const Int128I& operator%=(const Int128& x) { - val %= x; - return *this; - } - - Int128 val; -}; +struct Int128I {}; /// Own FieldType for Decimal. /// It is only a "storage" for decimal. To perform operations, you also have to provide a scale (number of digits after point). @@ -350,11 +296,7 @@ struct Decimal { template Decimal(const Decimal& x) { - if constexpr (std::is_same_v) { - value = x.value.val; - } else { - value = x; - } + value = x; } constexpr Decimal& operator=(Decimal&&) = default; @@ -386,6 +328,28 @@ struct Decimal { T value; }; +template <> +struct Decimal : public Decimal { + Decimal() = default; + +#define DECLARE_NUMERIC_CTOR(TYPE) \ + Decimal(const TYPE& value_) : Decimal(value_) {} + + DECLARE_NUMERIC_CTOR(Int128) + DECLARE_NUMERIC_CTOR(Int32) + DECLARE_NUMERIC_CTOR(Int64) + DECLARE_NUMERIC_CTOR(UInt32) + DECLARE_NUMERIC_CTOR(UInt64) + DECLARE_NUMERIC_CTOR(Float32) + DECLARE_NUMERIC_CTOR(Float64) +#undef DECLARE_NUMERIC_CTOR + + template + Decimal(const Decimal& x) { + value = x; + } +}; + using Decimal32 = Decimal; using Decimal64 = Decimal; using Decimal128 = Decimal; @@ -437,15 +401,18 @@ template <> inline constexpr bool IsDecimalNumber = true; template -constexpr bool IsDecimalV2 = false; +constexpr bool IsDecimal128 = false; template <> -inline constexpr bool IsDecimalV2 = true; +inline constexpr bool IsDecimal128 = true; template constexpr bool IsDecimal128I = false; template <> inline constexpr bool IsDecimal128I = true; +template +constexpr bool IsDecimalV2 = IsDecimal128 && !IsDecimal128I; + template constexpr bool IsFloatNumber = false; template <> @@ -471,7 +438,7 @@ struct NativeType { }; template <> struct NativeType { - using Type = Int128I; + using Type = Int128; }; inline const char* getTypeName(TypeIndex idx) { @@ -577,15 +544,6 @@ struct hash { } }; -template <> -struct hash { - size_t operator()(const doris::vectorized::Decimal128I& x) const { - return std::hash()(x.value.val >> 64) ^ - std::hash()( - x.value.val & std::numeric_limits::max()); - } -}; - constexpr bool is_integer(doris::vectorized::TypeIndex index) { using TypeIndex = doris::vectorized::TypeIndex; switch (index) { diff --git a/be/src/vec/data_types/data_type_decimal.cpp b/be/src/vec/data_types/data_type_decimal.cpp index 6cd9f59fbe..12dbd228b2 100644 --- a/be/src/vec/data_types/data_type_decimal.cpp +++ b/be/src/vec/data_types/data_type_decimal.cpp @@ -247,14 +247,6 @@ Int128 max_decimal_value(UInt32 precision) { DataTypeDecimal::get_scale_multiplier( (UInt64)max_decimal_precision() - precision); } -template <> -Int128I max_decimal_value(UInt32 precision) { - return (static_cast(999999999999999999ll) * 100000000000000000ll * 1000ll + - static_cast(99999999999999999ll) * 1000ll + 999ll) / - DataTypeDecimal::get_scale_multiplier( - (UInt64)max_decimal_precision() - precision) - .value.val; -} template typename T::NativeType min_decimal_value(UInt32 precision) { @@ -277,15 +269,6 @@ Int128 min_decimal_value(UInt32 precision) { DataTypeDecimal::get_scale_multiplier( (UInt64)max_decimal_precision() - precision); } -template <> -Int128I min_decimal_value(UInt32 precision) { - return -(static_cast(999999999999999999ll) * 100000000000000000ll * 1000ll + - static_cast(99999999999999999ll) * 1000ll + 999ll) / - DataTypeDecimal::get_scale_multiplier( - (UInt64)max_decimal_precision() - precision) - .value.val; -} - /// Explicit template instantiations. template class DataTypeDecimal; template class DataTypeDecimal; diff --git a/be/src/vec/data_types/data_type_decimal.h b/be/src/vec/data_types/data_type_decimal.h index e972f79fa8..d07db5f2ef 100644 --- a/be/src/vec/data_types/data_type_decimal.h +++ b/be/src/vec/data_types/data_type_decimal.h @@ -71,11 +71,6 @@ constexpr Int128 max_decimal_value() { return static_cast(999999999999999999ll) * 100000000000000000ll * 1000ll + static_cast(99999999999999999ll) * 1000ll + 999ll; } -template <> -constexpr Int128I max_decimal_value() { - return static_cast(999999999999999999ll) * 100000000000000000ll * 1000ll + - static_cast(99999999999999999ll) * 1000ll + 999ll; -} DataTypePtr create_decimal(UInt64 precision, UInt64 scale, bool use_v2); @@ -180,39 +175,20 @@ public: T whole_part(T x) const { if (scale == 0) return x; - if constexpr (IsDecimal128I) { - return x.value.val / get_scale_multiplier().value.val; - } else { - return x / get_scale_multiplier(); - } + return x / get_scale_multiplier(); } T fractional_part(T x) const { if (scale == 0) return 0; - if constexpr (IsDecimal128I) { - if (x.value.val < 0) x.value.val *= -1; - return x.value.val % get_scale_multiplier().value.val; - } else { - if (x < T(0)) x *= T(-1); - return x % get_scale_multiplier(); - } + if (x < T(0)) x *= T(-1); + return x % get_scale_multiplier(); } - T max_whole_value() const { - if constexpr (IsDecimal128I) { - return get_scale_multiplier(max_precision() - scale).value.val - 1; - } else { - return get_scale_multiplier(max_precision() - scale) - T(1); - } - } + T max_whole_value() const { return get_scale_multiplier(max_precision() - scale) - T(1); } bool can_store_whole(T x) const { T max = max_whole_value(); - if constexpr (IsDecimal128I) { - if (x.value.val > max.value.val || x.value.val < -max.value.val) return false; - } else { - if (x > max || x < -max) return false; - } + if (x > max || x < -max) return false; return true; } @@ -358,38 +334,13 @@ convert_decimals(const typename FromDataType::FieldType& value, UInt32 scale_fro if (scale_to > scale_from) { converted_value = DataTypeDecimal::get_scale_multiplier(scale_to - scale_from); - if constexpr (IsDecimal128I) { - if (common::mul_overflow(static_cast(value).val, converted_value.val, - converted_value.val)) { - LOG(WARNING) << "Decimal convert overflow"; - } - } else if constexpr (IsDecimal128I) { - if (common::mul_overflow(static_cast(value.value.val), converted_value, - converted_value)) { - LOG(WARNING) << "Decimal convert overflow"; - } - } else { - if (common::mul_overflow(static_cast(value), converted_value, - converted_value)) { - LOG(WARNING) << "Decimal convert overflow"; - } + if (common::mul_overflow(static_cast(value), converted_value, + converted_value)) { + LOG(WARNING) << "Decimal convert overflow"; } } else { - if constexpr (IsDecimal128I) { - if constexpr (IsDecimal128I) { - converted_value = - value.value.val / - DataTypeDecimal::get_scale_multiplier(scale_from - scale_to) - .value.val; - } else { - converted_value = value / DataTypeDecimal::get_scale_multiplier( - scale_from - scale_to) - .value.val; - } - } else { - converted_value = value / DataTypeDecimal::get_scale_multiplier( - scale_from - scale_to); - } + converted_value = + value / DataTypeDecimal::get_scale_multiplier(scale_from - scale_to); } if constexpr (sizeof(FromFieldType) > sizeof(ToFieldType)) { @@ -399,11 +350,7 @@ convert_decimals(const typename FromDataType::FieldType& value, UInt32 scale_fro } } - if constexpr (IsDecimal128I) { - return converted_value.val; - } else { - return converted_value; - } + return converted_value; } template @@ -416,9 +363,6 @@ convert_from_decimal(const typename FromDataType::FieldType& value, UInt32 scale if constexpr (std::is_floating_point_v) { if constexpr (IsDecimalV2) { return binary_cast(value); - } else if constexpr (IsDecimal128I) { - return *reinterpret_cast(&value) / - FromDataType::get_scale_multiplier(scale).value.val; } else { return static_cast(value) / FromDataType::get_scale_multiplier(scale); } @@ -429,41 +373,22 @@ convert_from_decimal(const typename FromDataType::FieldType& value, UInt32 scale if constexpr (sizeof(FromFieldType) > sizeof(ToFieldType) || !std::numeric_limits::is_signed) { if constexpr (std::numeric_limits::is_signed) { - if constexpr (IsDecimal128I) { - if (converted_value.value.val < std::numeric_limits::min() || - converted_value.value.val > std::numeric_limits::max()) { - LOG(WARNING) << "Decimal convert overflow"; - } - } else { - if (converted_value < std::numeric_limits::min() || - converted_value > std::numeric_limits::max()) { - LOG(WARNING) << "Decimal convert overflow"; - } + if (converted_value < std::numeric_limits::min() || + converted_value > std::numeric_limits::max()) { + LOG(WARNING) << "Decimal convert overflow"; } } else { using CastIntType = std::conditional_t, Int128, Int64>; - if constexpr (IsDecimal128I) { - if (converted_value.value.val < 0 || - converted_value.value.val > - static_cast(std::numeric_limits::max())) { - LOG(WARNING) << "Decimal convert overflow"; - } - } else { - if (converted_value < 0 || - converted_value > - static_cast(std::numeric_limits::max())) { - LOG(WARNING) << "Decimal convert overflow"; - } + if (converted_value < 0 || + converted_value > + static_cast(std::numeric_limits::max())) { + LOG(WARNING) << "Decimal convert overflow"; } } } - if constexpr (IsDecimal128I) { - return converted_value.value.val; - } else { - return converted_value; - } + return converted_value; } } @@ -480,11 +405,7 @@ convert_to_decimal(const typename FromDataType::FieldType& value, UInt32 scale) } FromFieldType out; - if constexpr (IsDataTypeDecimal128I) { - out = value * ToDataType::get_scale_multiplier(scale).value.val; - } else { - out = value * ToDataType::get_scale_multiplier(scale); - } + out = value * ToDataType::get_scale_multiplier(scale); if (out <= static_cast(std::numeric_limits::min()) || out >= static_cast(std::numeric_limits::max())) { LOG(WARNING) << "Decimal convert overflow. Float is out of Decimal range"; diff --git a/be/src/vec/exec/format/orc/vorc_reader.cpp b/be/src/vec/exec/format/orc/vorc_reader.cpp index 6b8c23c760..216aa24ec9 100644 --- a/be/src/vec/exec/format/orc/vorc_reader.cpp +++ b/be/src/vec/exec/format/orc/vorc_reader.cpp @@ -692,8 +692,8 @@ Status OrcReader::_orc_column_to_doris_column(const std::string& col_name, return _decode_decimal_column(col_name, data_column, data_type, _decimal_scale_params, cvb, num_values); case TypeIndex::Decimal128I: - return _decode_decimal_column(col_name, data_column, data_type, - _decimal_scale_params, cvb, num_values); + return _decode_decimal_column(col_name, data_column, data_type, + _decimal_scale_params, cvb, num_values); case TypeIndex::Date: return _decode_time_column( col_name, data_column, cvb, num_values); diff --git a/be/src/vec/exec/format/parquet/parquet_common.cpp b/be/src/vec/exec/format/parquet/parquet_common.cpp index fe514d76e2..1f0d8327db 100644 --- a/be/src/vec/exec/format/parquet/parquet_common.cpp +++ b/be/src/vec/exec/format/parquet/parquet_common.cpp @@ -390,13 +390,11 @@ Status FixLengthDecoder::decode_values(MutableColumnPtr& doris_column, DataTypeP break; case TypeIndex::Decimal128I: if (_physical_type == tparquet::Type::FIXED_LEN_BYTE_ARRAY) { - return _decode_binary_decimal(doris_column, data_type, select_vector); + return _decode_binary_decimal(doris_column, data_type, select_vector); } else if (_physical_type == tparquet::Type::INT32) { - return _decode_primitive_decimal(doris_column, data_type, - select_vector); + return _decode_primitive_decimal(doris_column, data_type, select_vector); } else if (_physical_type == tparquet::Type::INT64) { - return _decode_primitive_decimal(doris_column, data_type, - select_vector); + return _decode_primitive_decimal(doris_column, data_type, select_vector); } break; case TypeIndex::String: @@ -584,7 +582,7 @@ Status ByteArrayDecoder::decode_values(MutableColumnPtr& doris_column, DataTypeP case TypeIndex::Decimal128: return _decode_binary_decimal(doris_column, data_type, select_vector); case TypeIndex::Decimal128I: - return _decode_binary_decimal(doris_column, data_type, select_vector); + return _decode_binary_decimal(doris_column, data_type, select_vector); default: break; } diff --git a/be/src/vec/exec/vjdbc_connector.cpp b/be/src/vec/exec/vjdbc_connector.cpp index ad27338734..b64f69fe30 100644 --- a/be/src/vec/exec/vjdbc_connector.cpp +++ b/be/src/vec/exec/vjdbc_connector.cpp @@ -335,7 +335,7 @@ Status JdbcConnector::_convert_column_data(JNIEnv* env, jobject jobj, case TYPE_DECIMAL128I: { std::string data = _jobject_to_string(env, jobj); StringParser::ParseResult result = StringParser::PARSE_SUCCESS; - const Int128I decimal_slot = StringParser::string_to_decimal( + const Int128 decimal_slot = StringParser::string_to_decimal( data.c_str(), data.length(), slot_desc->type().precision, slot_desc->type().scale, &result); reinterpret_cast(col_ptr)->insert_data( diff --git a/be/src/vec/functions/array/function_array_aggregation.cpp b/be/src/vec/functions/array/function_array_aggregation.cpp index 9d513191f6..7db0b3d63a 100644 --- a/be/src/vec/functions/array/function_array_aggregation.cpp +++ b/be/src/vec/functions/array/function_array_aggregation.cpp @@ -55,27 +55,20 @@ struct ArrayAggregateResultImpl { template struct ArrayAggregateResultImpl { - using Result = - std::conditional_t, Decimal128, - std::conditional_t, Decimal128I, Float64>>; + using Result = std::conditional_t, Decimal128, Float64>; }; template struct ArrayAggregateResultImpl { - using Result = - std::conditional_t, Decimal128, - std::conditional_t, Decimal128I, Float64>>; + using Result = std::conditional_t, Decimal128, Float64>; }; template struct ArrayAggregateResultImpl { using Result = std::conditional_t< - IsDecimalV2, Decimal128, - std::conditional_t< - IsDecimalNumber, Decimal128I, - std::conditional_t< - IsFloatNumber, Float64, - std::conditional_t, Int128, Int64>>>>; + IsDecimalNumber, Decimal128, + std::conditional_t, Float64, + std::conditional_t, Int128, Int64>>>; }; template diff --git a/be/src/vec/functions/function_binary_arithmetic.h b/be/src/vec/functions/function_binary_arithmetic.h index 50b1ab84df..f3f43a4f75 100644 --- a/be/src/vec/functions/function_binary_arithmetic.h +++ b/be/src/vec/functions/function_binary_arithmetic.h @@ -666,10 +666,10 @@ inline constexpr bool IsIntegral = true; template constexpr bool UseLeftDecimal = false; template <> -inline constexpr bool UseLeftDecimal, DataTypeDecimal> = +inline constexpr bool UseLeftDecimal, DataTypeDecimal> = true; template <> -inline constexpr bool UseLeftDecimal, DataTypeDecimal> = +inline constexpr bool UseLeftDecimal, DataTypeDecimal> = true; template <> inline constexpr bool UseLeftDecimal, DataTypeDecimal> = true; @@ -690,6 +690,11 @@ struct BinaryOperationTraits { Case || IsDataTypeDecimal), InvalidType>, + Case<(IsDataTypeDecimalV2 && IsDataTypeDecimal && + !IsDataTypeDecimalV2) || + (IsDataTypeDecimalV2 && IsDataTypeDecimal && + !IsDataTypeDecimalV2), + InvalidType>, Case && IsDataTypeDecimal && UseLeftDecimal, LeftDataType>, @@ -850,7 +855,8 @@ class FunctionBinaryArithmetic : public IFunction { return cast_type_to_either, DataTypeDecimal, - DataTypeDecimal>(type, std::forward(f)); + DataTypeDecimal, DataTypeDecimal>( + type, std::forward(f)); } template diff --git a/be/src/vec/functions/function_case.h b/be/src/vec/functions/function_case.h index c9e001ca40..f3961c4eef 100644 --- a/be/src/vec/functions/function_case.h +++ b/be/src/vec/functions/function_case.h @@ -292,15 +292,8 @@ public: ->get_data() .data(); - if constexpr (std::is_same_v) { - for (int row_idx = 0; row_idx < rows_count; row_idx++) { - result_raw_data[row_idx] += - (then_idx[row_idx] == i) * column_raw_data[row_idx].value.val; - } - } else { - for (int row_idx = 0; row_idx < rows_count; row_idx++) { - result_raw_data[row_idx] += (then_idx[row_idx] == i) * column_raw_data[row_idx]; - } + for (int row_idx = 0; row_idx < rows_count; row_idx++) { + result_raw_data[row_idx] += (then_idx[row_idx] == i) * column_raw_data[row_idx]; } } } diff --git a/be/src/vec/functions/function_coalesce.cpp b/be/src/vec/functions/function_coalesce.cpp index 768f1e77b9..1d1ae5d593 100644 --- a/be/src/vec/functions/function_coalesce.cpp +++ b/be/src/vec/functions/function_coalesce.cpp @@ -205,13 +205,8 @@ public: // true: null_map_data[row]==0 && filled_idx[row]==0 // if true, could filled current row data into result column for (size_t row = 0; row < input_rows_count; ++row) { - if constexpr (std::is_same_v) { - result_raw_data[row] += - (!(null_map_data[row] | filled_flag[row])) * column_raw_data[row].value.val; - } else { - result_raw_data[row] += - (!(null_map_data[row] | filled_flag[row])) * column_raw_data[row]; - } + result_raw_data[row] += + (!(null_map_data[row] | filled_flag[row])) * column_raw_data[row]; filled_flag[row] += (!(null_map_data[row] | filled_flag[row])); } return Status::OK(); diff --git a/be/src/vec/io/io_helper.h b/be/src/vec/io/io_helper.h index 0c6a2b5f3a..0da73a5416 100644 --- a/be/src/vec/io/io_helper.h +++ b/be/src/vec/io/io_helper.h @@ -56,10 +56,6 @@ template <> inline Int128 decimal_scale_multiplier(UInt32 scale) { return common::exp10_i128(scale); } -template <> -inline Int128I decimal_scale_multiplier(UInt32 scale) { - return common::exp10_i128(scale); -} inline std::string int128_to_string(__int128_t value) { fmt::memory_buffer buffer; @@ -73,43 +69,27 @@ inline std::string int128_to_string(UInt128 value) { template void write_text(Decimal value, UInt32 scale, std::ostream& ostr) { - if constexpr (std::is_same_v) { - if (value.value.val < 0) { - value.value.val *= -1; - ostr << '-'; - } - } else { - if (value < Decimal(0)) { - value *= Decimal(-1); - ostr << '-'; - } + if (value < Decimal(0)) { + value *= Decimal(-1); + ostr << '-'; } - T whole_part = value; + using Type = std::conditional_t, int128_t, T>; + Type whole_part = value; if (scale) { - if constexpr (std::is_same_v) { - whole_part = value.value.val / decimal_scale_multiplier(scale); - } else { - whole_part = value / decimal_scale_multiplier(scale); - } + whole_part = value / decimal_scale_multiplier(scale); } - if constexpr (std::is_same_v) { + if constexpr (std::is_same_v || std::is_same_v) { ostr << int128_to_string(whole_part); - } else if constexpr (std::is_same_v) { - ostr << int128_to_string(whole_part.val); } else { ostr << whole_part; } if (scale) { ostr << '.'; String str_fractional(scale, '0'); - for (Int32 pos = scale - 1; pos >= 0; --pos, value /= Decimal(10)) { - if constexpr (std::is_same_v) { - str_fractional[pos] += value.value.val % 10; - } else { - str_fractional[pos] += value % Decimal(10); - } + for (Int32 pos = scale - 1; pos >= 0; --pos, value /= 10) { + str_fractional[pos] += value % 10; } ostr.write(str_fractional.data(), scale); } diff --git a/be/src/vec/runtime/vorc_writer.cpp b/be/src/vec/runtime/vorc_writer.cpp index c2c8057f25..40d2560d28 100644 --- a/be/src/vec/runtime/vorc_writer.cpp +++ b/be/src/vec/runtime/vorc_writer.cpp @@ -482,7 +482,7 @@ Status VOrcWriterWrapper::write(const Block& block) { check_and_get_column(col)) { auto col_ptr = not_null_column->get_data().data(); for (size_t row_id = 0; row_id < sz; row_id++) { - auto v = col_ptr[row_id].value.val; + auto v = col_ptr[row_id].value; orc::Int128 value(v >> 64, (uint64_t)v); cur_batch->values[row_id] = value; } diff --git a/regression-test/data/correctness_p0/test_avg.out b/regression-test/data/correctness_p0/test_avg.out new file mode 100644 index 0000000000..78a39b2a9c --- /dev/null +++ b/regression-test/data/correctness_p0/test_avg.out @@ -0,0 +1,4 @@ +-- This file is automatically generated. You should know what you did if you want to edit this +-- !select -- +9.18181818181868E14 + diff --git a/regression-test/suites/correctness_p0/test_avg.groovy b/regression-test/suites/correctness_p0/test_avg.groovy new file mode 100644 index 0000000000..88f8b54088 --- /dev/null +++ b/regression-test/suites/correctness_p0/test_avg.groovy @@ -0,0 +1,42 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +// The cases is copied from https://github.com/trinodb/trino/tree/master +// /testing/trino-product-tests/src/main/resources/sql-tests/testcases/aggregate +// and modified by Doris. + +suite("test_avg") { + def tableName = "test_avg_tbl" + + sql """ DROP TABLE IF EXISTS ${tableName} """ + sql """ + CREATE TABLE IF NOT EXISTS ${tableName} ( + c_bigint bigint + ) + DUPLICATE KEY(c_bigint) + DISTRIBUTED BY HASH(c_bigint) BUCKETS 1 + PROPERTIES ( + "replication_num" = "1" + ) + """ + + for (i in range(1, 100)) { + sql """ INSERT INTO ${tableName} values (10000000000000${i}) """ + } + qt_select """ SELECT AVG(c_bigint) FROM ${tableName} """ + sql""" DROP TABLE IF EXISTS ${tableName} """ +}