diff --git a/src/rel/rel_runner.cc b/src/rel/rel_runner.cc index 80f0a85..29bcd3c 100644 --- a/src/rel/rel_runner.cc +++ b/src/rel/rel_runner.cc @@ -23,6 +23,7 @@ #include "op/project_op.h" #include "op/tandem_op.h" #include "op/ungrouped_agg_op.h" +#include "decimal_p.h" namespace dingodb::rel { @@ -152,6 +153,9 @@ const Byte *DecodeValue(const rel::op::Agg *&value, const case rel::AGG_COUNT | TYPE_STRING: p = DecodeAgg>(value, p); break; + case rel::AGG_COUNT | TYPE_DECIMAL: + p = DecodeAgg>(value, p); + break; case rel::AGG_COUNT | TYPE_DATE: p = DecodeAgg>(value, p); break; @@ -170,6 +174,9 @@ const Byte *DecodeValue(const rel::op::Agg *&value, const case rel::AGG_SUM | TYPE_DOUBLE: p = DecodeAgg>(value, p); break; + case rel::AGG_SUM | TYPE_DECIMAL: + p = DecodeAgg>(value, p); + break; case rel::AGG_MAX | TYPE_INT32: p = DecodeAgg>(value, p); break; diff --git a/src/types/decimal/decimal_p.h b/src/types/decimal/decimal_p.h index d0eee1b..264f802 100644 --- a/src/types/decimal/decimal_p.h +++ b/src/types/decimal/decimal_p.h @@ -15,7 +15,9 @@ #ifndef DINGO_LIBEXPR_DECIMAL_P_H #define DINGO_LIBEXPR_DECIMAL_P_H +#include #include + #include "decimal.h" namespace dingodb { @@ -56,11 +58,15 @@ class DecimalP { } int32_t toInt() const { - return m_ptr->toInt(); + double ret = m_ptr->toDouble(); + int32_t const r = std::llround(ret); + return r; } int64_t toLong() const { - return m_ptr->toLong(); + double ret = m_ptr->toDouble(); + int32_t const r = std::llround(ret); + return r; } double toDouble() const { diff --git a/test/expr/calc/test_casting.cc b/test/expr/calc/test_casting.cc index 1344060..6884969 100644 --- a/test/expr/calc/test_casting.cc +++ b/test/expr/calc/test_casting.cc @@ -55,17 +55,19 @@ TEST(TestToInt32, Cast) { ASSERT_THROW(calc::CastCheck((float)std::numeric_limits::min() - 1000.0f), ExceedsLimits); ASSERT_THROW(calc::CastCheck((double)std::numeric_limits::max() + 10000.0), ExceedsLimits); ASSERT_THROW(calc::CastCheck((double)std::numeric_limits::min() - 10000.0), ExceedsLimits); + ASSERT_EQ((calc::Cast(DecimalP(std::string("9.9")))), 10); + ASSERT_EQ((calc::Cast(DecimalP(std::string("-9.9")))), -10); } TEST(TestOtherToDecimalP, Cast) { ASSERT_EQ((calc::Cast(DecimalP(std::string("123456.12345678987654321112345676445342323423")))), 123456); - ASSERT_EQ((calc::Cast(DecimalP(std::string("123456123456.12345678987654321112345676445342323423")))), 123456123456); + ASSERT_EQ((calc::Cast(DecimalP(std::string("123456123456.12345678987654321112345676445342323423")))), -1097928128); //"+1" for mpf is not allow. ASSERT_EQ((calc::Cast(DecimalP(std::string("1")))), 1); ASSERT_EQ((calc::Cast(DecimalP(std::string("0")))), 0); ASSERT_EQ((calc::Cast(DecimalP(std::string("-123456.12345678987654321112345676445342323423")))), -123456); - ASSERT_EQ((calc::Cast(DecimalP(std::string("-123456123456.12345678987654321112345676445342323423")))), -123456123456); + ASSERT_EQ((calc::Cast(DecimalP(std::string("-123456123456.12345678987654321112345676445342323423")))), 1097928128); //float and double should not be compared by operator =. ASSERT_EQ((calc::Cast(DecimalP(std::string("123456.123456789")))), 123456.125); diff --git a/test/rel/test_rel.cc b/test/rel/test_rel.cc index 5b7a665..458fbbb 100644 --- a/test/rel/test_rel.cc +++ b/test/rel/test_rel.cc @@ -64,6 +64,15 @@ static Data MakeDataForFloatToDecimal() { }; } +static Data MakeDataForCountDecimal() { + return Data{ + new Tuple{1, "12.34"}, + new Tuple{2, "22.34"}, + new Tuple{3, "1.00"}, + new Tuple{4, nullptr}, +}; +} + static Data MakeDataForInstr() { return Data{ new Tuple{"abcdef"}, @@ -301,6 +310,14 @@ INSTANTIATE_TEST_SUITE_P( Data{ new Tuple{8LL}, } + ), + std::make_tuple( + "7236000074011600", + MakeDataForCountDecimal(), + 1, + Data{ + new Tuple{4LL}, + } ) ) );