From 20a86ec0fa27fd885ef61a7089459b6abba4e229 Mon Sep 17 00:00:00 2001 From: xufei Date: Mon, 24 Apr 2023 11:00:27 +0800 Subject: [PATCH 1/4] truncate should always be treated as warning in cast decimal as decimal Signed-off-by: xufei --- dbms/src/Functions/FunctionsTiDBConversion.h | 4 +++- .../Functions/tests/gtest_tidb_conversion.cpp | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/dbms/src/Functions/FunctionsTiDBConversion.h b/dbms/src/Functions/FunctionsTiDBConversion.h index e1ba48f696e..9b2cab4994b 100755 --- a/dbms/src/Functions/FunctionsTiDBConversion.h +++ b/dbms/src/Functions/FunctionsTiDBConversion.h @@ -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) { @@ -961,6 +961,8 @@ struct TiDBConvertToDecimal else ++value; } + if (old_value != value * scale_mul) + context.getDAGContext()->appendWarning("Truncate in cast decimal as decimal"); } else { diff --git a/dbms/src/Functions/tests/gtest_tidb_conversion.cpp b/dbms/src/Functions/tests/gtest_tidb_conversion.cpp index 941ac240640..95f58229e71 100644 --- a/dbms/src/Functions/tests/gtest_tidb_conversion.cpp +++ b/dbms/src/Functions/tests/gtest_tidb_conversion.cpp @@ -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(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(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 { From d4482c4df2b699c1b853c8d85d8ad661c520e584 Mon Sep 17 00:00:00 2001 From: xufei Date: Tue, 25 Apr 2023 18:09:37 +0800 Subject: [PATCH 2/4] Update dbms/src/Functions/FunctionsTiDBConversion.h --- dbms/src/Functions/FunctionsTiDBConversion.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dbms/src/Functions/FunctionsTiDBConversion.h b/dbms/src/Functions/FunctionsTiDBConversion.h index 9b2cab4994b..3db85738838 100755 --- a/dbms/src/Functions/FunctionsTiDBConversion.h +++ b/dbms/src/Functions/FunctionsTiDBConversion.h @@ -962,7 +962,7 @@ struct TiDBConvertToDecimal ++value; } if (old_value != value * scale_mul) - context.getDAGContext()->appendWarning("Truncate in cast decimal as decimal"); + context.getDAGContext().appendWarning("Truncate in cast decimal as decimal"); } else { From c19129340fad92e54ce4e4b0c2e46fbd53250477 Mon Sep 17 00:00:00 2001 From: xufei Date: Tue, 25 Apr 2023 18:31:23 +0800 Subject: [PATCH 3/4] Update dbms/src/Functions/FunctionsTiDBConversion.h --- dbms/src/Functions/FunctionsTiDBConversion.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dbms/src/Functions/FunctionsTiDBConversion.h b/dbms/src/Functions/FunctionsTiDBConversion.h index 3db85738838..9b2cab4994b 100755 --- a/dbms/src/Functions/FunctionsTiDBConversion.h +++ b/dbms/src/Functions/FunctionsTiDBConversion.h @@ -962,7 +962,7 @@ struct TiDBConvertToDecimal ++value; } if (old_value != value * scale_mul) - context.getDAGContext().appendWarning("Truncate in cast decimal as decimal"); + context.getDAGContext()->appendWarning("Truncate in cast decimal as decimal"); } else { From 957840abc0a436eb43dc785c289f486053135d2c Mon Sep 17 00:00:00 2001 From: xufei Date: Tue, 25 Apr 2023 18:31:29 +0800 Subject: [PATCH 4/4] Update dbms/src/Functions/tests/gtest_tidb_conversion.cpp --- dbms/src/Functions/tests/gtest_tidb_conversion.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dbms/src/Functions/tests/gtest_tidb_conversion.cpp b/dbms/src/Functions/tests/gtest_tidb_conversion.cpp index 95f58229e71..be8e6eae77e 100644 --- a/dbms/src/Functions/tests/gtest_tidb_conversion.cpp +++ b/dbms/src/Functions/tests/gtest_tidb_conversion.cpp @@ -1311,7 +1311,7 @@ CATCH TEST_F(TestTidbConversion, truncateInCastDecimalAsDecimal) try { - DAGContext * dag_context = context->getDAGContext(); + 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();