Skip to content

Commit

Permalink
add ut
Browse files Browse the repository at this point in the history
  • Loading branch information
jacktengg committed Sep 28, 2023
1 parent d0b6295 commit 39aa28a
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 36 deletions.
4 changes: 2 additions & 2 deletions be/src/runtime/type_limit.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ struct type_limit<vectorized::Decimal128> {
}
static vectorized::Decimal128 min() { return -max(); }
};
static Int256 MAX_DECIMAL256_INT({999999999999999999ll, 999999999999999999ll, 999999999999999999ll,
999999999999999999ll, 9999ll});
static Int256 MAX_DECIMAL256_INT({18446744073709551615ul, 8607968719199866879ul,
532749306367912313ul, 1593091911132452277ul});
template <>
struct type_limit<vectorized::Decimal256> {
static vectorized::Decimal256 max() { return vectorized::Decimal256(MAX_DECIMAL256_INT); }
Expand Down
12 changes: 8 additions & 4 deletions be/src/vec/core/decimal_comparison.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,16 @@ class DecimalComparison {
}

Shift shift;
if (scale_a < scale_b)
if (scale_a < scale_b) {
shift.a = DataTypeDecimal<B>(max_decimal_precision<B>(), scale_b)
.get_scale_multiplier(scale_b - scale_a);
if (scale_a > scale_b)
.get_scale_multiplier(scale_b - scale_a)
.value;
}
if (scale_a > scale_b) {
shift.b = DataTypeDecimal<A>(max_decimal_precision<A>(), scale_a)
.get_scale_multiplier(scale_a - scale_b);
.get_scale_multiplier(scale_a - scale_b)
.value;
}

return apply_with_scale(a, b, shift);
}
Expand Down
21 changes: 17 additions & 4 deletions be/src/vec/core/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,6 @@ struct Decimal {
}
}

// TODO: decimal256
static constexpr auto precision =
std::is_same_v<T, Int32>
? BeConsts::MAX_DECIMAL32_PRECISION
Expand Down Expand Up @@ -661,8 +660,11 @@ struct Decimal<Int256> {
static constexpr auto precision =
std::is_same_v<T, Int32>
? BeConsts::MAX_DECIMAL32_PRECISION
: (std::is_same_v<T, Int64> ? BeConsts::MAX_DECIMAL64_PRECISION
: BeConsts::MAX_DECIMAL128_PRECISION);
: (std::is_same_v<T, Int64>
? BeConsts::MAX_DECIMAL64_PRECISION
: (std::is_same_v<T, Int128>
? BeConsts::MAX_DECIMAL128_PRECISION
: BeConsts::MAX_DECIMAL256_PRECISION));
bool is_nagetive = value < 0;
int max_result_length = precision + (scale > 0) // Add a space for decimal place
+ (scale == precision) // Add a space for leading 0
Expand All @@ -684,7 +686,9 @@ struct Decimal<Int256> {
frac_part = abs_value % decimal_scale_multiplier<T>(scale);
}
if constexpr (std::is_same_v<T, Int256>) {
std::string res {wide::to_string(whole_part)};
std::string dec_str {wide::to_string(whole_part)};
auto end = fmt::format_to(str.data() + pos, "{}", dec_str);
pos = end - str.data();
} else {
auto end = fmt::format_to(str.data() + pos, "{}", whole_part);
pos = end - str.data();
Expand Down Expand Up @@ -713,6 +717,9 @@ struct Decimal<Int256> {
const T& scale_multiplier) const {
if (UNLIKELY(value == std::numeric_limits<T>::min())) {
if constexpr (std::is_same_v<T, Int256>) {
std::string dec_str {wide::to_string(value)};
auto end = fmt::format_to(dst, "{}", dec_str);
return end - dst;
} else {
auto end = fmt::format_to(dst, "{}", value);
return end - dst;
Expand All @@ -735,6 +742,9 @@ struct Decimal<Int256> {
frac_part = abs_value % scale_multiplier;
}
if constexpr (std::is_same_v<T, Int256>) {
std::string dec_str {wide::to_string(whole_part)};
auto end = fmt::format_to(dst + pos, "{}", dec_str);
pos = end - dst;
} else {
auto end = fmt::format_to(dst + pos, "{}", whole_part);
pos = end - dst;
Expand All @@ -759,6 +769,9 @@ struct Decimal<Int256> {
}
if (frac_part) {
if constexpr (std::is_same_v<T, Int256>) {
std::string dec_str {wide::to_string(frac_part)};
auto end = fmt::format_to(dst + pos, "{}", dec_str);
pos = end - dst;
} else {
auto end = fmt::format_to(&dst[pos], "{}", frac_part);
pos = end - dst;
Expand Down
4 changes: 2 additions & 2 deletions be/src/vec/core/wide_integer.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ class integer {
template <size_t Bits2, typename Signed2>
constexpr integer<Bits, Signed>& operator=(const integer<Bits2, Signed2>& rhs) noexcept;

// template <typename Arithmetic>
// constexpr integer<Bits, Signed>& operator=(Arithmetic rhs) noexcept;
template <typename Arithmetic>
constexpr integer<Bits, Signed>& operator=(Arithmetic rhs) noexcept;

template <typename Arithmetic>
constexpr integer<Bits, Signed>& operator*=(const Arithmetic& rhs);
Expand Down
22 changes: 11 additions & 11 deletions be/src/vec/core/wide_integer_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -950,17 +950,17 @@ constexpr integer<Bits, Signed>& integer<Bits, Signed>::operator=(
return *this;
}

// template <size_t Bits, typename Signed>
// template <typename T>
// constexpr integer<Bits, Signed>& integer<Bits, Signed>::operator=(T rhs) noexcept {
// if constexpr (IsTupleLike<T>::value)
// _impl::wide_integer_from_tuple_like(*this, rhs);
// else if constexpr (std::is_same_v<std::remove_cvref_t<T>, CityHash_v1_0_2::uint128>)
// _impl::wide_integer_from_cityhash_uint128(*this, rhs);
// else
// _impl::wide_integer_from_builtin(*this, rhs);
// return *this;
// }
template <size_t Bits, typename Signed>
template <typename T>
constexpr integer<Bits, Signed>& integer<Bits, Signed>::operator=(T rhs) noexcept {
if constexpr (IsTupleLike<T>::value)
_impl::wide_integer_from_tuple_like(*this, rhs);
else if constexpr (std::is_same_v<std::remove_cvref_t<T>, CityHash_v1_0_2::uint128>)
_impl::wide_integer_from_cityhash_uint128(*this, rhs);
else
_impl::wide_integer_from_builtin(*this, rhs);
return *this;
}

template <size_t Bits, typename Signed>
template <typename T>
Expand Down
13 changes: 0 additions & 13 deletions be/src/vec/data_types/data_type_decimal.h
Original file line number Diff line number Diff line change
Expand Up @@ -604,19 +604,6 @@ typename T::NativeType max_decimal_value(UInt32 precision) {
(UInt32)(max_decimal_precision<T>() - precision));
}

/*
mnt/disk2/tengjianping/doris-master/be/src/util/string_parser.hpp:699:43: error: no matching function for call to 'min_decimal_value'
value = is_negative ? vectorized::min_decimal_value<vectorized::Decimal<T>>(
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/mnt/disk2/tengjianping/doris-master/be/src/vec/data_types/data_type_decimal.cpp:164:32: note: in instantiation of function template specialization 'doris::StringParser::string_to_decimal<doris::TYPE_DECIMAL256, wide::integer<256, int>>' requested here
res->value = StringParser::string_to_decimal<DataTypeDecimalSerDe<T>::get_primitive_type()>(
^
/mnt/disk2/tengjianping/doris-master/be/src/vec/data_types/data_type_decimal.h:609:24: note: candidate template ignored: constraints not satisfied [with T = vectorized::Decimal<integer<256, int>>]
typename T::NativeType min_decimal_value(UInt32 precision) {
^
/mnt/disk2/tengjianping/doris-master/be/src/vec/data_types/data_type_decimal.h:608:14: note: because 'IsDecimalNumber<doris::vectorized::Decimal<wide::integer<256, int> > >' evaluated to false
requires IsDecimalNumber<T>
*/
template <typename T>
requires IsDecimalNumber<T>
typename T::NativeType min_decimal_value(UInt32 precision) {
Expand Down
81 changes: 81 additions & 0 deletions be/test/vec/data_types/decimal_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// 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.

#include <gtest/gtest-message.h>
#include <gtest/gtest-test-part.h>
#include <gtest/gtest.h>

#include <memory>

#include "gtest/gtest_pred_impl.h"
#include "runtime/type_limit.h"
#include "vec/core/types.h"
namespace doris::vectorized {

TEST(DecimalTest, Decimal256) {
// 9999999999999999999999999999999999999999999999999999999999999999999999999999
Decimal256 dec1(type_limit<vectorized::Decimal256>::max());
auto des_str = dec1.to_string(10);
EXPECT_EQ(des_str,
"999999999999999999999999999999999999999999999999999999999999999999.9999999999");
des_str = dec1.to_string(0);
EXPECT_EQ(des_str,
"9999999999999999999999999999999999999999999999999999999999999999999999999999");
des_str = dec1.to_string(76);
EXPECT_EQ(des_str,
"0.9999999999999999999999999999999999999999999999999999999999999999999999999999");

auto dec2 = type_limit<vectorized::Decimal256>::min();
des_str = dec2.to_string(10);
EXPECT_EQ(des_str,
"-999999999999999999999999999999999999999999999999999999999999999999.9999999999");
des_str = dec2.to_string(0);
EXPECT_EQ(des_str,
"-9999999999999999999999999999999999999999999999999999999999999999999999999999");
des_str = dec2.to_string(76);
EXPECT_EQ(des_str,
"-0.9999999999999999999999999999999999999999999999999999999999999999999999999999");

// plus
Decimal256 dec3 = dec1 + dec2;
des_str = dec3.to_string(10);
EXPECT_EQ(des_str, "0.0000000000");
des_str = dec3.to_string(0);
EXPECT_EQ(des_str, "0");
des_str = dec3.to_string(76);
EXPECT_EQ(des_str,
"0.0000000000000000000000000000000000000000000000000000000000000000000000000000");

// minus
dec2 = type_limit<vectorized::Decimal256>::max();
dec3 = dec1 - dec2;
des_str = dec3.to_string(10);
EXPECT_EQ(des_str, "0.0000000000");

// multiply

// divide
dec1 = type_limit<vectorized::Decimal256>::max();
dec2 = vectorized::Decimal256(10);
dec3 = dec1 / dec2;
des_str = dec3.to_string(1);
EXPECT_EQ(des_str,
"99999999999999999999999999999999999999999999999999999999999999999999999999.9");

// overflow
}
} // namespace doris::vectorized

0 comments on commit 39aa28a

Please sign in to comment.