Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/rel/rel_runner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -152,6 +153,9 @@ const Byte *DecodeValue<const rel::op::Agg *>(const rel::op::Agg *&value, const
case rel::AGG_COUNT | TYPE_STRING:
p = DecodeAgg<rel::op::CountAgg<expr::String>>(value, p);
break;
case rel::AGG_COUNT | TYPE_DECIMAL:
p = DecodeAgg<rel::op::CountAgg<dingodb::types::DecimalP>>(value, p);
break;
case rel::AGG_COUNT | TYPE_DATE:
p = DecodeAgg<rel::op::CountAgg<expr::Date>>(value, p);
break;
Expand All @@ -170,6 +174,9 @@ const Byte *DecodeValue<const rel::op::Agg *>(const rel::op::Agg *&value, const
case rel::AGG_SUM | TYPE_DOUBLE:
p = DecodeAgg<rel::op::SumAgg<double>>(value, p);
break;
case rel::AGG_SUM | TYPE_DECIMAL:
p = DecodeAgg<rel::op::SumAgg<dingodb::types::DecimalP>>(value, p);
break;
case rel::AGG_MAX | TYPE_INT32:
p = DecodeAgg<rel::op::MaxAgg<int32_t>>(value, p);
break;
Expand Down
10 changes: 8 additions & 2 deletions src/types/decimal/decimal_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
#ifndef DINGO_LIBEXPR_DECIMAL_P_H
#define DINGO_LIBEXPR_DECIMAL_P_H

#include <cmath>
#include <memory>

#include "decimal.h"

namespace dingodb {
Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 4 additions & 2 deletions test/expr/calc/test_casting.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,19 @@ TEST(TestToInt32, Cast) {
ASSERT_THROW(calc::CastCheck<int32_t>((float)std::numeric_limits<int32_t>::min() - 1000.0f), ExceedsLimits<TYPE_INT32>);
ASSERT_THROW(calc::CastCheck<int64_t>((double)std::numeric_limits<int64_t>::max() + 10000.0), ExceedsLimits<TYPE_INT64>);
ASSERT_THROW(calc::CastCheck<int64_t>((double)std::numeric_limits<int64_t>::min() - 10000.0), ExceedsLimits<TYPE_INT64>);
ASSERT_EQ((calc::Cast<int32_t>(DecimalP(std::string("9.9")))), 10);
ASSERT_EQ((calc::Cast<int32_t>(DecimalP(std::string("-9.9")))), -10);
}

TEST(TestOtherToDecimalP, Cast) {
ASSERT_EQ((calc::Cast<int32_t>(DecimalP(std::string("123456.12345678987654321112345676445342323423")))), 123456);
ASSERT_EQ((calc::Cast<int64_t>(DecimalP(std::string("123456123456.12345678987654321112345676445342323423")))), 123456123456);
ASSERT_EQ((calc::Cast<int64_t>(DecimalP(std::string("123456123456.12345678987654321112345676445342323423")))), -1097928128);

//"+1" for mpf is not allow.
ASSERT_EQ((calc::Cast<int32_t>(DecimalP(std::string("1")))), 1);
ASSERT_EQ((calc::Cast<int32_t>(DecimalP(std::string("0")))), 0);
ASSERT_EQ((calc::Cast<int32_t>(DecimalP(std::string("-123456.12345678987654321112345676445342323423")))), -123456);
ASSERT_EQ((calc::Cast<int64_t>(DecimalP(std::string("-123456123456.12345678987654321112345676445342323423")))), -123456123456);
ASSERT_EQ((calc::Cast<int64_t>(DecimalP(std::string("-123456123456.12345678987654321112345676445342323423")))), 1097928128);

//float and double should not be compared by operator =.
ASSERT_EQ((calc::Cast<float>(DecimalP(std::string("123456.123456789")))), 123456.125);
Expand Down
17 changes: 17 additions & 0 deletions test/rel/test_rel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down Expand Up @@ -301,6 +310,14 @@ INSTANTIATE_TEST_SUITE_P(
Data{
new Tuple{8LL},
}
),
std::make_tuple(
"7236000074011600",
MakeDataForCountDecimal(),
1,
Data{
new Tuple{4LL},
}
)
)
);