Skip to content

Commit

Permalink
truncate should always be treated as warning in cast decimal as decim…
Browse files Browse the repository at this point in the history
…al (#7350) (#7378)

close #7348
  • Loading branch information
ti-chi-bot committed Apr 25, 2023
1 parent d49dc8b commit e96161a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
4 changes: 3 additions & 1 deletion dbms/src/Functions/FunctionsTiDBConversion.h
Expand Up @@ -951,8 +951,8 @@ struct TiDBConvertToDecimal
}
else if (v_scale > scale)
{
context.getDAGContext()->handleTruncateError("cast decimal as decimal");
const bool need_to_round = ((value < 0 ? -value : value) % scale_mul) >= (scale_mul / 2);
auto old_value = value;
value /= scale_mul;
if (need_to_round)
{
Expand All @@ -961,6 +961,8 @@ struct TiDBConvertToDecimal
else
++value;
}
if (old_value != value * scale_mul)
context.getDAGContext()->appendWarning("Truncate in cast decimal as decimal");
}
else
{
Expand Down
16 changes: 16 additions & 0 deletions dbms/src/Functions/tests/gtest_tidb_conversion.cpp
Expand Up @@ -1308,6 +1308,22 @@ try
}
CATCH

TEST_F(TestTidbConversion, truncateInCastDecimalAsDecimal)
try
{
DAGContext * dag_context = context.getDAGContext();
UInt64 ori_flags = dag_context->getFlags();
dag_context->addFlag(TiDBSQLFlags::IN_INSERT_STMT | TiDBSQLFlags::IN_UPDATE_OR_DELETE_STMT);
dag_context->clearWarnings();

ASSERT_COLUMN_EQ(createColumn<Decimal32>(std::make_tuple(5, 2), {"1.23", "1.56", "1.01", "1.00", "-1.23", "-1.56", "-1.01", "-1.00"}),
executeFunction(func_name, {createColumn<Decimal32>(std::make_tuple(5, 4), {"1.2300", "1.5600", "1.0056", "1.0023", "-1.2300", "-1.5600", "-1.0056", "-1.0023"}), createCastTypeConstColumn("Decimal(5,2)")}));
ASSERT_EQ(dag_context->getWarningCount(), 4);
dag_context->setFlags(ori_flags);
dag_context->clearWarnings();
}
CATCH

TEST_F(TestTidbConversion, castDecimalAsDecimalWithRound)
try
{
Expand Down

0 comments on commit e96161a

Please sign in to comment.