From 2db4c7d528e03fbb42aadadea3c3827e6c99f40c Mon Sep 17 00:00:00 2001 From: SeaRise Date: Wed, 5 Jan 2022 11:50:02 +0800 Subject: [PATCH 01/12] init what need to test --- .../Functions/tests/gtest_tidb_conversion.cpp | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/dbms/src/Functions/tests/gtest_tidb_conversion.cpp b/dbms/src/Functions/tests/gtest_tidb_conversion.cpp index fad33ea2db6..438318f3364 100644 --- a/dbms/src/Functions/tests/gtest_tidb_conversion.cpp +++ b/dbms/src/Functions/tests/gtest_tidb_conversion.cpp @@ -714,6 +714,36 @@ try } CATCH +TEST_F(TestTidbConversion, castRealAsInt) +try +{ +} +CATCH + +TEST_F(TestTidbConversion, castRealAsReal) +try +{ +} +CATCH + +TEST_F(TestTidbConversion, castRealAsString) +try +{ +} +CATCH + +TEST_F(TestTidbConversion, castRealAsDecimal) +try +{ +} +CATCH + +TEST_F(TestTidbConversion, castRealAsTime) +try +{ +} +CATCH + TEST_F(TestTidbConversion, castTimeAsReal) try { From 52a46e242353e4f7a9e260480f77c379ea64289f Mon Sep 17 00:00:00 2001 From: SeaRise Date: Wed, 5 Jan 2022 15:10:09 +0800 Subject: [PATCH 02/12] update --- .../src/Functions/tests/gtest_tidb_conversion.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/dbms/src/Functions/tests/gtest_tidb_conversion.cpp b/dbms/src/Functions/tests/gtest_tidb_conversion.cpp index 438318f3364..516ddf07470 100644 --- a/dbms/src/Functions/tests/gtest_tidb_conversion.cpp +++ b/dbms/src/Functions/tests/gtest_tidb_conversion.cpp @@ -49,6 +49,21 @@ const UInt64 MAX_UINT64 = std::numeric_limits::max(); class TestTidbConversion : public DB::tests::FunctionTest { +public: + template + void testNotOnlyNull(const std::optional & input, const std::optional & output) + { + auto inner_test = [&](bool is_const) { + ASSERT_COLUMN_EQ( + is_const ? createConstColumn>(1, output) : createColumn>({output}), + executeFunction( + func_name, + {is_const ? createConstColumn>(1, input) : createColumn>({input}), + createCastTypeConstColumn(fmt::format("Nullable({})", TypeName::get()))})); + }; + inner_test(true); + inner_test(false); + } }; using DecimalField32 = DecimalField; From 95754ea635757338563785a6a463866ca58b12ef Mon Sep 17 00:00:00 2001 From: SeaRise Date: Thu, 6 Jan 2022 14:47:47 +0800 Subject: [PATCH 03/12] add only null tests --- .../Functions/tests/gtest_tidb_conversion.cpp | 133 ++++++++++++++-- .../Functions/tests/gtest_timestampdiff.cpp | 148 +++++++++--------- dbms/src/TestUtils/FunctionTestUtils.cpp | 21 --- dbms/src/TestUtils/FunctionTestUtils.h | 55 ++++++- 4 files changed, 250 insertions(+), 107 deletions(-) diff --git a/dbms/src/Functions/tests/gtest_tidb_conversion.cpp b/dbms/src/Functions/tests/gtest_tidb_conversion.cpp index 516ddf07470..9f311b8464d 100644 --- a/dbms/src/Functions/tests/gtest_tidb_conversion.cpp +++ b/dbms/src/Functions/tests/gtest_tidb_conversion.cpp @@ -47,23 +47,118 @@ const UInt16 MAX_UINT16 = std::numeric_limits::max(); const UInt32 MAX_UINT32 = std::numeric_limits::max(); const UInt64 MAX_UINT64 = std::numeric_limits::max(); +const Float32 MAX_FLOAT32 = std::numeric_limits::max(); +const Float32 MIN_FLOAT32 = std::numeric_limits::min(); +const Float64 MAX_FLOAT64 = std::numeric_limits::max(); +const Float64 MIN_FLOAT64 = std::numeric_limits::min(); + class TestTidbConversion : public DB::tests::FunctionTest { public: - template + template void testNotOnlyNull(const std::optional & input, const std::optional & output) { auto inner_test = [&](bool is_const) { - ASSERT_COLUMN_EQ( - is_const ? createConstColumn>(1, output) : createColumn>({output}), - executeFunction( - func_name, - {is_const ? createConstColumn>(1, input) : createColumn>({input}), - createCastTypeConstColumn(fmt::format("Nullable({})", TypeName::get()))})); + if constexpr (IsDecimal) + { + if (std::is_same_v) + { + } + else if (std::is_same_v) + { + } + else if (std::is_same_v) + { + } + else + { + static_assert(std::is_same_v); + } + } + else if constexpr (std::is_same_v) + { + } + else + { + ASSERT_COLUMN_EQ( + is_const ? createConstColumn>(1, output) : createColumn>({output}), + executeFunction( + func_name, + {is_const ? createConstColumn>(1, input) : createColumn>({input}), + createCastTypeConstColumn(fmt::format("Nullable({})", TypeName::get()))})); + } }; inner_test(true); inner_test(false); } + + template + void testOnlyNull() + { + std::vector nulls = { + createOnlyNullColumnConst(1), + createOnlyNullColumn(1), + createColumn>({{}}), + createConstColumn>(1, {})}; + + auto inner_test = [&](const ColumnWithTypeAndName & null_one) { + if constexpr (IsDecimal) + { + auto precision = 0; + if constexpr (std::is_same_v) + { + precision = 9; + } + else if constexpr (std::is_same_v) + { + precision = 18; + } + else if constexpr (std::is_same_v) + { + precision = 38; + } + else + { + static_assert(std::is_same_v); + precision = 65; + } + auto meta = std::make_tuple(precision, 0); + auto res = null_one.column->isColumnConst() + ? createConstColumn>(meta, 1, std::optional>{}) + : createColumn>(meta, {std::optional>{}}); + ASSERT_COLUMN_EQ( + res, + executeFunction( + func_name, + {null_one, + createCastTypeConstColumn(fmt::format("Nullable(Decimal({},0))", precision))})); + } + else if constexpr (std::is_same_v) + { + auto res = null_one.column->isColumnConst() ? createDateTimeColumnConst(1, {}, 6) : createDateTimeColumn({{}}, 6); + ASSERT_COLUMN_EQ( + res, + executeFunction( + func_name, + {null_one, + createCastTypeConstColumn("Nullable(MyDateTime(6))")})); + } + else + { + auto res = null_one.column->isColumnConst() ? createConstColumn>(1, {}) : createColumn>({{}}); + ASSERT_COLUMN_EQ( + res, + executeFunction( + func_name, + {null_one, + createCastTypeConstColumn(fmt::format("Nullable({})", TypeName::get()))})); + } + }; + for (const auto & null_one : nulls) + { + inner_test(null_one); + } + } }; using DecimalField32 = DecimalField; @@ -692,12 +787,12 @@ TEST_F(TestTidbConversion, castIntAsTime) try { ASSERT_COLUMN_EQ( - createDateTimeColumnNullable({{}, {{2021, 10, 26, 16, 8, 59, 0}}}, 6), + createDateTimeColumn({{}, {{2021, 10, 26, 16, 8, 59, 0}}}, 6), executeFunction(func_name, {createColumn>({{}, 20211026160859}), createCastTypeConstColumn("Nullable(MyDateTime(6))")})); ASSERT_COLUMN_EQ( - createDateTimeColumnNullable({{}, {{2021, 10, 26, 16, 8, 59, 0}}}, 6), + createDateTimeColumn({{}, {{2021, 10, 26, 16, 8, 59, 0}}}, 6), executeFunction(func_name, {createColumn>({{}, 20211026160859}), createCastTypeConstColumn("Nullable(MyDateTime(6))")})); @@ -717,7 +812,7 @@ try createCastTypeConstColumn("Nullable(MyDateTime(6))")}), TiFlashException); ASSERT_COLUMN_EQ( - createDateTimeColumnNullable({{}}, 6), + createDateTimeColumn({{}}, 6), executeFunction(func_name, {createColumn>({0}), createCastTypeConstColumn("Nullable(MyDateTime(6))")})); @@ -732,30 +827,48 @@ CATCH TEST_F(TestTidbConversion, castRealAsInt) try { + testOnlyNull(); + testOnlyNull(); + testOnlyNull(); + testOnlyNull(); } CATCH TEST_F(TestTidbConversion, castRealAsReal) try { + testOnlyNull(); + testOnlyNull(); } CATCH TEST_F(TestTidbConversion, castRealAsString) try { + testOnlyNull(); + testOnlyNull(); } CATCH TEST_F(TestTidbConversion, castRealAsDecimal) try { + testOnlyNull(); + testOnlyNull(); + testOnlyNull(); + testOnlyNull(); + testOnlyNull(); + testOnlyNull(); + testOnlyNull(); + testOnlyNull(); } CATCH TEST_F(TestTidbConversion, castRealAsTime) try { + testOnlyNull(); + testOnlyNull(); } CATCH diff --git a/dbms/src/Functions/tests/gtest_timestampdiff.cpp b/dbms/src/Functions/tests/gtest_timestampdiff.cpp index 135a10d749c..a7fdfaebe6d 100644 --- a/dbms/src/Functions/tests/gtest_timestampdiff.cpp +++ b/dbms/src/Functions/tests/gtest_timestampdiff.cpp @@ -28,12 +28,12 @@ try executeFunction(func_name, {createConstColumn(1, "year"), createOnlyNullColumnConst(1), - createDateTimeColumnNullable({{{2020, 1, 1, 0, 0, 0, 0}}}, 6)})); + createDateTimeColumn({{{2020, 1, 1, 0, 0, 0, 0}}}, 6)})); ASSERT_COLUMN_EQ( createConstColumn>(1, std::nullopt), executeFunction(func_name, {createConstColumn(1, "year"), - createDateTimeColumnNullable({{{2020, 1, 1, 0, 0, 0, 0}}}, 6), + createDateTimeColumn({{{2020, 1, 1, 0, 0, 0, 0}}}, 6), createOnlyNullColumnConst(1)})); } CATCH @@ -42,76 +42,76 @@ TEST_F(TestTimestampDiff, constVector) try { ASSERT_TIMESTAMP_DIFF("year", - createDateTimeColumnConst(2, {2021, 1, 1, 0, 0, 0, 0}, 6), - createDateTimeColumnNullable({{{2022, 1, 1, 0, 0, 0, 0}}, {{2020, 1, 1, 0, 0, 0, 0}}}, 6), + createDateTimeColumnConst(2, {{2021, 1, 1, 0, 0, 0, 0}}, 6), + createDateTimeColumn({{{2022, 1, 1, 0, 0, 0, 0}}, {{2020, 1, 1, 0, 0, 0, 0}}}, 6), createColumn>({1, -1})); ASSERT_TIMESTAMP_DIFF("year", - createDateTimeColumnNullable({{{2022, 1, 1, 0, 0, 0, 0}}, {{2020, 1, 1, 0, 0, 0, 0}}}, 6), - createDateTimeColumnConst(2, {2021, 1, 1, 0, 0, 0, 0}, 6), + createDateTimeColumn({{{2022, 1, 1, 0, 0, 0, 0}}, {{2020, 1, 1, 0, 0, 0, 0}}}, 6), + createDateTimeColumnConst(2, {{2021, 1, 1, 0, 0, 0, 0}}, 6), createColumn>({-1, 1})); ASSERT_TIMESTAMP_DIFF("quarter", - createDateTimeColumnConst(2, {2021, 1, 1, 0, 0, 0, 0}, 6), - createDateTimeColumnNullable({{{2021, 4, 1, 0, 0, 0, 0}}, {{2020, 10, 1, 0, 0, 0, 0}}}, 6), + createDateTimeColumnConst(2, {{2021, 1, 1, 0, 0, 0, 0}}, 6), + createDateTimeColumn({{{2021, 4, 1, 0, 0, 0, 0}}, {{2020, 10, 1, 0, 0, 0, 0}}}, 6), createColumn>({1, -1})); ASSERT_TIMESTAMP_DIFF("quarter", - createDateTimeColumnNullable({{{2021, 4, 1, 0, 0, 0, 0}}, {{2020, 10, 1, 0, 0, 0, 0}}}, 6), - createDateTimeColumnConst(2, {2021, 1, 1, 0, 0, 0, 0}, 6), + createDateTimeColumn({{{2021, 4, 1, 0, 0, 0, 0}}, {{2020, 10, 1, 0, 0, 0, 0}}}, 6), + createDateTimeColumnConst(2, {{2021, 1, 1, 0, 0, 0, 0}}, 6), createColumn>({-1, 1})); ASSERT_TIMESTAMP_DIFF("month", - createDateTimeColumnConst(2, {2021, 1, 1, 0, 0, 0, 0}, 6), - createDateTimeColumnNullable({{{2021, 2, 1, 0, 0, 0, 0}}, {{2020, 12, 1, 0, 0, 0, 0}}}, 6), + createDateTimeColumnConst(2, {{2021, 1, 1, 0, 0, 0, 0}}, 6), + createDateTimeColumn({{{2021, 2, 1, 0, 0, 0, 0}}, {{2020, 12, 1, 0, 0, 0, 0}}}, 6), createColumn>({1, -1})); ASSERT_TIMESTAMP_DIFF("month", - createDateTimeColumnNullable({{{2021, 2, 1, 0, 0, 0, 0}}, {{2020, 12, 1, 0, 0, 0, 0}}}, 6), - createDateTimeColumnConst(2, {2021, 1, 1, 0, 0, 0, 0}, 6), + createDateTimeColumn({{{2021, 2, 1, 0, 0, 0, 0}}, {{2020, 12, 1, 0, 0, 0, 0}}}, 6), + createDateTimeColumnConst(2, {{2021, 1, 1, 0, 0, 0, 0}}, 6), createColumn>({-1, 1})); ASSERT_TIMESTAMP_DIFF("week", - createDateTimeColumnConst(2, {2021, 2, 28, 0, 0, 0, 0}, 6), - createDateTimeColumnNullable({{{2021, 3, 7, 0, 0, 0, 0}}, {{2021, 2, 21, 0, 0, 0, 0}}}, 6), + createDateTimeColumnConst(2, {{2021, 2, 28, 0, 0, 0, 0}}, 6), + createDateTimeColumn({{{2021, 3, 7, 0, 0, 0, 0}}, {{2021, 2, 21, 0, 0, 0, 0}}}, 6), createColumn>({1, -1})); ASSERT_TIMESTAMP_DIFF("week", - createDateTimeColumnNullable({{{2021, 3, 7, 0, 0, 0, 0}}, {{2021, 2, 21, 0, 0, 0, 0}}}, 6), - createDateTimeColumnConst(2, {2021, 2, 28, 0, 0, 0, 0}, 6), + createDateTimeColumn({{{2021, 3, 7, 0, 0, 0, 0}}, {{2021, 2, 21, 0, 0, 0, 0}}}, 6), + createDateTimeColumnConst(2, {{2021, 2, 28, 0, 0, 0, 0}}, 6), createColumn>({-1, 1})); ASSERT_TIMESTAMP_DIFF("day", - createDateTimeColumnConst(2, {2021, 2, 28, 0, 0, 0, 0}, 6), - createDateTimeColumnNullable({{{2021, 3, 1, 0, 0, 0, 0}}, {{2021, 2, 27, 0, 0, 0, 0}}}, 6), + createDateTimeColumnConst(2, {{2021, 2, 28, 0, 0, 0, 0}}, 6), + createDateTimeColumn({{{2021, 3, 1, 0, 0, 0, 0}}, {{2021, 2, 27, 0, 0, 0, 0}}}, 6), createColumn>({1, -1})); ASSERT_TIMESTAMP_DIFF("day", - createDateTimeColumnNullable({{{2021, 3, 1, 0, 0, 0, 0}}, {{2021, 2, 27, 0, 0, 0, 0}}}, 6), - createDateTimeColumnConst(2, {2021, 2, 28, 0, 0, 0, 0}, 6), + createDateTimeColumn({{{2021, 3, 1, 0, 0, 0, 0}}, {{2021, 2, 27, 0, 0, 0, 0}}}, 6), + createDateTimeColumnConst(2, {{2021, 2, 28, 0, 0, 0, 0}}, 6), createColumn>({-1, 1})); ASSERT_TIMESTAMP_DIFF("hour", - createDateTimeColumnConst(2, {2021, 1, 1, 23, 0, 0, 0}, 6), - createDateTimeColumnNullable({{{2021, 1, 2, 0, 0, 0, 0}}, {{2021, 1, 1, 22, 0, 0, 0}}}, 6), + createDateTimeColumnConst(2, {{2021, 1, 1, 23, 0, 0, 0}}, 6), + createDateTimeColumn({{{2021, 1, 2, 0, 0, 0, 0}}, {{2021, 1, 1, 22, 0, 0, 0}}}, 6), createColumn>({1, -1})); ASSERT_TIMESTAMP_DIFF("hour", - createDateTimeColumnNullable({{{2021, 1, 2, 0, 0, 0, 0}}, {{2021, 1, 1, 22, 0, 0, 0}}}, 6), - createDateTimeColumnConst(2, {2021, 1, 1, 23, 0, 0, 0}, 6), + createDateTimeColumn({{{2021, 1, 2, 0, 0, 0, 0}}, {{2021, 1, 1, 22, 0, 0, 0}}}, 6), + createDateTimeColumnConst(2, {{2021, 1, 1, 23, 0, 0, 0}}, 6), createColumn>({-1, 1})); ASSERT_TIMESTAMP_DIFF("minute", - createDateTimeColumnConst(2, {2021, 1, 1, 23, 59, 0, 0}, 6), - createDateTimeColumnNullable({{{2021, 1, 2, 0, 0, 0, 0}}, {{2021, 1, 1, 23, 58, 0, 0}}}, 6), + createDateTimeColumnConst(2, {{2021, 1, 1, 23, 59, 0, 0}}, 6), + createDateTimeColumn({{{2021, 1, 2, 0, 0, 0, 0}}, {{2021, 1, 1, 23, 58, 0, 0}}}, 6), createColumn>({1, -1})); ASSERT_TIMESTAMP_DIFF("minute", - createDateTimeColumnNullable({{{2021, 1, 2, 0, 0, 0, 0}}, {{2021, 1, 1, 23, 58, 0, 0}}}, 6), - createDateTimeColumnConst(2, {2021, 1, 1, 23, 59, 0, 0}, 6), + createDateTimeColumn({{{2021, 1, 2, 0, 0, 0, 0}}, {{2021, 1, 1, 23, 58, 0, 0}}}, 6), + createDateTimeColumnConst(2, {{2021, 1, 1, 23, 59, 0, 0}}, 6), createColumn>({-1, 1})); ASSERT_TIMESTAMP_DIFF("second", - createDateTimeColumnConst(2, {2021, 1, 1, 23, 59, 59, 0}, 6), - createDateTimeColumnNullable({{{2021, 1, 2, 0, 0, 0, 0}}, {{2021, 1, 1, 23, 59, 58, 0}}}, 6), + createDateTimeColumnConst(2, {{2021, 1, 1, 23, 59, 59, 0}}, 6), + createDateTimeColumn({{{2021, 1, 2, 0, 0, 0, 0}}, {{2021, 1, 1, 23, 59, 58, 0}}}, 6), createColumn>({1, -1})); ASSERT_TIMESTAMP_DIFF("second", - createDateTimeColumnNullable({{{2021, 1, 2, 0, 0, 0, 0}}, {{2021, 1, 1, 23, 59, 58, 0}}}, 6), - createDateTimeColumnConst(2, {2021, 1, 1, 23, 59, 59, 0}, 6), + createDateTimeColumn({{{2021, 1, 2, 0, 0, 0, 0}}, {{2021, 1, 1, 23, 59, 58, 0}}}, 6), + createDateTimeColumnConst(2, {{2021, 1, 1, 23, 59, 59, 0}}, 6), createColumn>({-1, 1})); ASSERT_TIMESTAMP_DIFF("microsecond", - createDateTimeColumnConst(2, {2021, 1, 1, 23, 59, 59, 999999}, 6), - createDateTimeColumnNullable({{{2021, 1, 2, 0, 0, 0, 0}}, {{2021, 1, 1, 23, 59, 59, 999998}}}, 6), + createDateTimeColumnConst(2, {{2021, 1, 1, 23, 59, 59, 999999}}, 6), + createDateTimeColumn({{{2021, 1, 2, 0, 0, 0, 0}}, {{2021, 1, 1, 23, 59, 59, 999998}}}, 6), createColumn>({1, -1})); ASSERT_TIMESTAMP_DIFF("microsecond", - createDateTimeColumnNullable({{{2021, 1, 2, 0, 0, 0, 0}}, {{2021, 1, 1, 23, 59, 59, 999998}}}, 6), - createDateTimeColumnConst(2, {2021, 1, 1, 23, 59, 59, 999999}, 6), + createDateTimeColumn({{{2021, 1, 2, 0, 0, 0, 0}}, {{2021, 1, 1, 23, 59, 59, 999998}}}, 6), + createDateTimeColumnConst(2, {{2021, 1, 1, 23, 59, 59, 999999}}, 6), createColumn>({-1, 1})); } CATCH @@ -120,76 +120,76 @@ TEST_F(TestTimestampDiff, vectorVector) try { ASSERT_TIMESTAMP_DIFF("year", - createDateTimeColumnNullable({{{2021, 1, 1, 0, 0, 0, 0}}, {{2021, 1, 1, 0, 0, 0, 0}}}, 6), - createDateTimeColumnNullable({{{2022, 1, 1, 0, 0, 0, 0}}, {{2020, 1, 1, 0, 0, 0, 0}}}, 6), + createDateTimeColumn({{{2021, 1, 1, 0, 0, 0, 0}}, {{2021, 1, 1, 0, 0, 0, 0}}}, 6), + createDateTimeColumn({{{2022, 1, 1, 0, 0, 0, 0}}, {{2020, 1, 1, 0, 0, 0, 0}}}, 6), createColumn>({1, -1})); ASSERT_TIMESTAMP_DIFF("year", - createDateTimeColumnNullable({{{2022, 1, 1, 0, 0, 0, 0}}, {{2020, 1, 1, 0, 0, 0, 0}}}, 6), - createDateTimeColumnNullable({{{2021, 1, 1, 0, 0, 0, 0}}, {{2021, 1, 1, 0, 0, 0, 0}}}, 6), + createDateTimeColumn({{{2022, 1, 1, 0, 0, 0, 0}}, {{2020, 1, 1, 0, 0, 0, 0}}}, 6), + createDateTimeColumn({{{2021, 1, 1, 0, 0, 0, 0}}, {{2021, 1, 1, 0, 0, 0, 0}}}, 6), createColumn>({-1, 1})); ASSERT_TIMESTAMP_DIFF("quarter", - createDateTimeColumnNullable({{{2021, 1, 1, 0, 0, 0, 0}}, {{2021, 1, 1, 0, 0, 0, 0}}}, 6), - createDateTimeColumnNullable({{{2021, 4, 1, 0, 0, 0, 0}}, {{2020, 10, 1, 0, 0, 0, 0}}}, 6), + createDateTimeColumn({{{2021, 1, 1, 0, 0, 0, 0}}, {{2021, 1, 1, 0, 0, 0, 0}}}, 6), + createDateTimeColumn({{{2021, 4, 1, 0, 0, 0, 0}}, {{2020, 10, 1, 0, 0, 0, 0}}}, 6), createColumn>({1, -1})); ASSERT_TIMESTAMP_DIFF("quarter", - createDateTimeColumnNullable({{{2021, 4, 1, 0, 0, 0, 0}}, {{2020, 10, 1, 0, 0, 0, 0}}}, 6), - createDateTimeColumnNullable({{{2021, 1, 1, 0, 0, 0, 0}}, {{2021, 1, 1, 0, 0, 0, 0}}}, 6), + createDateTimeColumn({{{2021, 4, 1, 0, 0, 0, 0}}, {{2020, 10, 1, 0, 0, 0, 0}}}, 6), + createDateTimeColumn({{{2021, 1, 1, 0, 0, 0, 0}}, {{2021, 1, 1, 0, 0, 0, 0}}}, 6), createColumn>({-1, 1})); ASSERT_TIMESTAMP_DIFF("month", - createDateTimeColumnNullable({{{2021, 1, 1, 0, 0, 0, 0}}, {{2021, 1, 1, 0, 0, 0, 0}}}, 6), - createDateTimeColumnNullable({{{2021, 2, 1, 0, 0, 0, 0}}, {{2020, 12, 1, 0, 0, 0, 0}}}, 6), + createDateTimeColumn({{{2021, 1, 1, 0, 0, 0, 0}}, {{2021, 1, 1, 0, 0, 0, 0}}}, 6), + createDateTimeColumn({{{2021, 2, 1, 0, 0, 0, 0}}, {{2020, 12, 1, 0, 0, 0, 0}}}, 6), createColumn>({1, -1})); ASSERT_TIMESTAMP_DIFF("month", - createDateTimeColumnNullable({{{2021, 2, 1, 0, 0, 0, 0}}, {{2020, 12, 1, 0, 0, 0, 0}}}, 6), - createDateTimeColumnNullable({{{2021, 1, 1, 0, 0, 0, 0}}, {{2021, 1, 1, 0, 0, 0, 0}}}, 6), + createDateTimeColumn({{{2021, 2, 1, 0, 0, 0, 0}}, {{2020, 12, 1, 0, 0, 0, 0}}}, 6), + createDateTimeColumn({{{2021, 1, 1, 0, 0, 0, 0}}, {{2021, 1, 1, 0, 0, 0, 0}}}, 6), createColumn>({-1, 1})); ASSERT_TIMESTAMP_DIFF("week", - createDateTimeColumnNullable({{{2021, 2, 28, 0, 0, 0, 0}}, {{2021, 2, 28, 0, 0, 0, 0}}}, 6), - createDateTimeColumnNullable({{{2021, 3, 7, 0, 0, 0, 0}}, {{2021, 2, 21, 0, 0, 0, 0}}}, 6), + createDateTimeColumn({{{2021, 2, 28, 0, 0, 0, 0}}, {{2021, 2, 28, 0, 0, 0, 0}}}, 6), + createDateTimeColumn({{{2021, 3, 7, 0, 0, 0, 0}}, {{2021, 2, 21, 0, 0, 0, 0}}}, 6), createColumn>({1, -1})); ASSERT_TIMESTAMP_DIFF("week", - createDateTimeColumnNullable({{{2021, 3, 7, 0, 0, 0, 0}}, {{2021, 2, 21, 0, 0, 0, 0}}}, 6), - createDateTimeColumnNullable({{{2021, 2, 28, 0, 0, 0, 0}}, {{2021, 2, 28, 0, 0, 0, 0}}}, 6), + createDateTimeColumn({{{2021, 3, 7, 0, 0, 0, 0}}, {{2021, 2, 21, 0, 0, 0, 0}}}, 6), + createDateTimeColumn({{{2021, 2, 28, 0, 0, 0, 0}}, {{2021, 2, 28, 0, 0, 0, 0}}}, 6), createColumn>({-1, 1})); ASSERT_TIMESTAMP_DIFF("day", - createDateTimeColumnNullable({{{2021, 2, 28, 0, 0, 0, 0}}, {{2021, 2, 28, 0, 0, 0, 0}}}, 6), - createDateTimeColumnNullable({{{2021, 3, 1, 0, 0, 0, 0}}, {{2021, 2, 27, 0, 0, 0, 0}}}, 6), + createDateTimeColumn({{{2021, 2, 28, 0, 0, 0, 0}}, {{2021, 2, 28, 0, 0, 0, 0}}}, 6), + createDateTimeColumn({{{2021, 3, 1, 0, 0, 0, 0}}, {{2021, 2, 27, 0, 0, 0, 0}}}, 6), createColumn>({1, -1})); ASSERT_TIMESTAMP_DIFF("day", - createDateTimeColumnNullable({{{2021, 3, 1, 0, 0, 0, 0}}, {{2021, 2, 27, 0, 0, 0, 0}}}, 6), - createDateTimeColumnNullable({{{2021, 2, 28, 0, 0, 0, 0}}, {{2021, 2, 28, 0, 0, 0, 0}}}, 6), + createDateTimeColumn({{{2021, 3, 1, 0, 0, 0, 0}}, {{2021, 2, 27, 0, 0, 0, 0}}}, 6), + createDateTimeColumn({{{2021, 2, 28, 0, 0, 0, 0}}, {{2021, 2, 28, 0, 0, 0, 0}}}, 6), createColumn>({-1, 1})); ASSERT_TIMESTAMP_DIFF("hour", - createDateTimeColumnNullable({{{2021, 1, 1, 23, 0, 0, 0}}, {{2021, 1, 1, 23, 0, 0, 0}}}, 6), - createDateTimeColumnNullable({{{2021, 1, 2, 0, 0, 0, 0}}, {{2021, 1, 1, 22, 0, 0, 0}}}, 6), + createDateTimeColumn({{{2021, 1, 1, 23, 0, 0, 0}}, {{2021, 1, 1, 23, 0, 0, 0}}}, 6), + createDateTimeColumn({{{2021, 1, 2, 0, 0, 0, 0}}, {{2021, 1, 1, 22, 0, 0, 0}}}, 6), createColumn>({1, -1})); ASSERT_TIMESTAMP_DIFF("hour", - createDateTimeColumnNullable({{{2021, 1, 2, 0, 0, 0, 0}}, {{2021, 1, 1, 22, 0, 0, 0}}}, 6), - createDateTimeColumnNullable({{{2021, 1, 1, 23, 0, 0, 0}}, {{2021, 1, 1, 23, 0, 0, 0}}}, 6), + createDateTimeColumn({{{2021, 1, 2, 0, 0, 0, 0}}, {{2021, 1, 1, 22, 0, 0, 0}}}, 6), + createDateTimeColumn({{{2021, 1, 1, 23, 0, 0, 0}}, {{2021, 1, 1, 23, 0, 0, 0}}}, 6), createColumn>({-1, 1})); ASSERT_TIMESTAMP_DIFF("minute", - createDateTimeColumnNullable({{{2021, 1, 1, 23, 59, 0, 0}}, {{2021, 1, 1, 23, 59, 0, 0}}}, 6), - createDateTimeColumnNullable({{{2021, 1, 2, 0, 0, 0, 0}}, {{2021, 1, 1, 23, 58, 0, 0}}}, 6), + createDateTimeColumn({{{2021, 1, 1, 23, 59, 0, 0}}, {{2021, 1, 1, 23, 59, 0, 0}}}, 6), + createDateTimeColumn({{{2021, 1, 2, 0, 0, 0, 0}}, {{2021, 1, 1, 23, 58, 0, 0}}}, 6), createColumn>({1, -1})); ASSERT_TIMESTAMP_DIFF("minute", - createDateTimeColumnNullable({{{2021, 1, 2, 0, 0, 0, 0}}, {{2021, 1, 1, 23, 58, 0, 0}}}, 6), - createDateTimeColumnNullable({{{2021, 1, 1, 23, 59, 0, 0}}, {{2021, 1, 1, 23, 59, 0, 0}}}, 6), + createDateTimeColumn({{{2021, 1, 2, 0, 0, 0, 0}}, {{2021, 1, 1, 23, 58, 0, 0}}}, 6), + createDateTimeColumn({{{2021, 1, 1, 23, 59, 0, 0}}, {{2021, 1, 1, 23, 59, 0, 0}}}, 6), createColumn>({-1, 1})); ASSERT_TIMESTAMP_DIFF("second", - createDateTimeColumnNullable({{{2021, 1, 1, 23, 59, 59, 0}}, {{2021, 1, 1, 23, 59, 59, 0}}}, 6), - createDateTimeColumnNullable({{{2021, 1, 2, 0, 0, 0, 0}}, {{2021, 1, 1, 23, 59, 58, 0}}}, 6), + createDateTimeColumn({{{2021, 1, 1, 23, 59, 59, 0}}, {{2021, 1, 1, 23, 59, 59, 0}}}, 6), + createDateTimeColumn({{{2021, 1, 2, 0, 0, 0, 0}}, {{2021, 1, 1, 23, 59, 58, 0}}}, 6), createColumn>({1, -1})); ASSERT_TIMESTAMP_DIFF("second", - createDateTimeColumnNullable({{{2021, 1, 2, 0, 0, 0, 0}}, {{2021, 1, 1, 23, 59, 58, 0}}}, 6), - createDateTimeColumnNullable({{{2021, 1, 1, 23, 59, 59, 0}}, {{2021, 1, 1, 23, 59, 59, 0}}}, 6), + createDateTimeColumn({{{2021, 1, 2, 0, 0, 0, 0}}, {{2021, 1, 1, 23, 59, 58, 0}}}, 6), + createDateTimeColumn({{{2021, 1, 1, 23, 59, 59, 0}}, {{2021, 1, 1, 23, 59, 59, 0}}}, 6), createColumn>({-1, 1})); ASSERT_TIMESTAMP_DIFF("microsecond", - createDateTimeColumnNullable({{{2021, 1, 1, 23, 59, 59, 999999}}, {{2021, 1, 1, 23, 59, 59, 999999}}}, 6), - createDateTimeColumnNullable({{{2021, 1, 2, 0, 0, 0, 0}}, {{2021, 1, 1, 23, 59, 59, 999998}}}, 6), + createDateTimeColumn({{{2021, 1, 1, 23, 59, 59, 999999}}, {{2021, 1, 1, 23, 59, 59, 999999}}}, 6), + createDateTimeColumn({{{2021, 1, 2, 0, 0, 0, 0}}, {{2021, 1, 1, 23, 59, 59, 999998}}}, 6), createColumn>({1, -1})); ASSERT_TIMESTAMP_DIFF("microsecond", - createDateTimeColumnNullable({{{2021, 1, 2, 0, 0, 0, 0}}, {{2021, 1, 1, 23, 59, 59, 999998}}}, 6), - createDateTimeColumnNullable({{{2021, 1, 1, 23, 59, 59, 999999}}, {{2021, 1, 1, 23, 59, 59, 999999}}}, 6), + createDateTimeColumn({{{2021, 1, 2, 0, 0, 0, 0}}, {{2021, 1, 1, 23, 59, 59, 999998}}}, 6), + createDateTimeColumn({{{2021, 1, 1, 23, 59, 59, 999999}}, {{2021, 1, 1, 23, 59, 59, 999999}}}, 6), createColumn>({-1, 1})); } CATCH diff --git a/dbms/src/TestUtils/FunctionTestUtils.cpp b/dbms/src/TestUtils/FunctionTestUtils.cpp index c32369ace20..4a81ef004ab 100644 --- a/dbms/src/TestUtils/FunctionTestUtils.cpp +++ b/dbms/src/TestUtils/FunctionTestUtils.cpp @@ -133,26 +133,5 @@ ColumnWithTypeAndName createOnlyNullColumn(size_t size, const String & name) return {std::move(col), data_type, name}; } -ColumnWithTypeAndName createDateTimeColumnNullable(std::initializer_list> init, int fraction) -{ - auto data_type_ptr = makeNullable(std::make_shared(fraction)); - auto col = data_type_ptr->createColumn(); - for (const auto dt : init) - { - if (dt.has_value()) - col->insert(Field(dt->toPackedUInt())); - else - col->insert(Null()); - } - return {std::move(col), data_type_ptr, "datetime"}; -} - -ColumnWithTypeAndName createDateTimeColumnConst(size_t size, const MyDateTime & dt, int fraction) -{ - auto data_type_ptr = std::make_shared(fraction); - auto col = data_type_ptr->createColumnConst(size, Field(dt.toPackedUInt())); - return {std::move(col), data_type_ptr, "datetime"}; -} - } // namespace tests } // namespace DB diff --git a/dbms/src/TestUtils/FunctionTestUtils.h b/dbms/src/TestUtils/FunctionTestUtils.h index 2f65899326e..cf077544f75 100644 --- a/dbms/src/TestUtils/FunctionTestUtils.h +++ b/dbms/src/TestUtils/FunctionTestUtils.h @@ -249,8 +249,59 @@ ColumnWithTypeAndName createConstColumn( return {makeConstColumn(data_type, size, value), data_type, name}; } -ColumnWithTypeAndName createDateTimeColumnNullable(std::initializer_list> init, int fraction); -ColumnWithTypeAndName createDateTimeColumnConst(size_t size, const MyDateTime & dt, int fraction); +template +ColumnWithTypeAndName createDateTimeColumn(std::initializer_list> init, int fraction) +{ + DataTypePtr data_type_ptr = std::make_shared(fraction); + if constexpr (is_nullable) + { + data_type_ptr = makeNullable(data_type_ptr); + } + auto col = data_type_ptr->createColumn(); + for (const auto & dt : init) + { + if (dt.has_value()) + col->insert(Field(dt->toPackedUInt())); + else + { + if constexpr (is_nullable) + { + col->insert(Null()); + } + else + { + throw Exception("Null value for not nullable DataTypeMyDateTime"); + } + } + } + return {std::move(col), data_type_ptr, "datetime"}; +} + +template +ColumnWithTypeAndName createDateTimeColumnConst(size_t size, const std::optional & dt, int fraction) +{ + DataTypePtr data_type_ptr = std::make_shared(fraction); + if constexpr (is_nullable) + { + data_type_ptr = makeNullable(data_type_ptr); + } + + ColumnPtr col; + if (dt.has_value()) + col = data_type_ptr->createColumnConst(size, Field(dt->toPackedUInt())); + else + { + if constexpr (is_nullable) + { + col = data_type_ptr->createColumnConst(size, Field(Null())); + } + else + { + throw Exception("Null value for not nullable DataTypeMyDateTime"); + } + } + return {std::move(col), data_type_ptr, "datetime"}; +} // parse a string into decimal field. template From 69fedb522902806cf8d08afac32c8d245db2125f Mon Sep 17 00:00:00 2001 From: SeaRise Date: Thu, 6 Jan 2022 17:33:26 +0800 Subject: [PATCH 04/12] add as int, float, string tests --- .../Functions/tests/gtest_tidb_conversion.cpp | 99 ++++++++++++++++++- 1 file changed, 98 insertions(+), 1 deletion(-) diff --git a/dbms/src/Functions/tests/gtest_tidb_conversion.cpp b/dbms/src/Functions/tests/gtest_tidb_conversion.cpp index 9f311b8464d..60650b6938e 100644 --- a/dbms/src/Functions/tests/gtest_tidb_conversion.cpp +++ b/dbms/src/Functions/tests/gtest_tidb_conversion.cpp @@ -56,7 +56,7 @@ class TestTidbConversion : public DB::tests::FunctionTest { public: template - void testNotOnlyNull(const std::optional & input, const std::optional & output) + void testNotOnlyNull(const Input & input, const Output & output) { auto inner_test = [&](bool is_const) { if constexpr (IsDecimal) @@ -92,6 +92,43 @@ class TestTidbConversion : public DB::tests::FunctionTest inner_test(false); } + template + void testThrowException(const Input & input) + { + auto inner_test = [&](bool is_const) { + if constexpr (IsDecimal) + { + if (std::is_same_v) + { + } + else if (std::is_same_v) + { + } + else if (std::is_same_v) + { + } + else + { + static_assert(std::is_same_v); + } + } + else if constexpr (std::is_same_v) + { + } + else + { + ASSERT_THROW( + executeFunction( + func_name, + {is_const ? createConstColumn>(1, input) : createColumn>({input}), + createCastTypeConstColumn(fmt::format("Nullable({})", TypeName::get()))}), + TiFlashException); + } + }; + inner_test(true); + inner_test(false); + } + template void testOnlyNull() { @@ -831,6 +868,28 @@ try testOnlyNull(); testOnlyNull(); testOnlyNull(); + + testNotOnlyNull(0, 0); + testThrowException(MAX_FLOAT32); + testNotOnlyNull(MIN_FLOAT32, 0); + testNotOnlyNull(12.213f, 12); + testNotOnlyNull(-12.213f, -12); + testNotOnlyNull(0, 0); + testThrowException(MAX_FLOAT32); + testNotOnlyNull(MIN_FLOAT32, 0); + testNotOnlyNull(12.213f, 12); + testThrowException(-12.213f); + + testNotOnlyNull(0, 0); + testThrowException(MAX_FLOAT64); + testNotOnlyNull(MIN_FLOAT64, 0); + testNotOnlyNull(12.213, 12); + testNotOnlyNull(-12.213, -12); + testNotOnlyNull(0, 0); + testThrowException(MAX_FLOAT64); + testNotOnlyNull(MIN_FLOAT64, 0); + testNotOnlyNull(12.213, 12); + testThrowException(-12.213); } CATCH @@ -839,6 +898,18 @@ try { testOnlyNull(); testOnlyNull(); + + testNotOnlyNull(0, 0); + testNotOnlyNull(12.213, 12.213000297546387); + testNotOnlyNull(-12.213, -12.213000297546387); + testNotOnlyNull(MIN_FLOAT32, MIN_FLOAT32); + testNotOnlyNull(MAX_FLOAT32, MAX_FLOAT32); + + testNotOnlyNull(0, 0); + testNotOnlyNull(12.213, 12.213); + testNotOnlyNull(-12.213, -12.213); + testNotOnlyNull(MIN_FLOAT64, MIN_FLOAT64); + testNotOnlyNull(MAX_FLOAT64, MAX_FLOAT64); } CATCH @@ -847,6 +918,32 @@ try { testOnlyNull(); testOnlyNull(); + + testNotOnlyNull(0, "0"); + testNotOnlyNull(12.213, "12.213"); + testNotOnlyNull(-12.213, "-12.213"); + // TODO add tests after fix results is not compatible with tidb/mysql + // tiflash: 3.4028235e38 + // tidb: 340282350000000000000000000000000000000 + // mysql: 3.40282e38 + // testNotOnlyNull(MAX_FLOAT32, "3.4028235e38"); + // tiflash: 1.1754944e-38 + // tidb: 0.000000000000000000000000000000000000011754944 + // mysql: 1.17549e-38 + // testNotOnlyNull(MIN_FLOAT32, "1.1754944e-38"); + + testNotOnlyNull(0, "0"); + testNotOnlyNull(12.213, "12.213"); + testNotOnlyNull(-12.213, "-12.213"); + // TODO add tests after fix results is not compatible with tidb/mysql + // tiflash: 1.7976931348623157e308 + // tidb: 179769313486231570000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 + // mysql: 1.7976931348623157e308 + // testNotOnlyNull(MAX_FLOAT64, "1.7976931348623157e308"); + // tiflash: 2.2250738585072014e-308 + // tidb: 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000022250738585072014 + // mysql: 2.2250738585072014e-308 + // testNotOnlyNull(MIN_FLOAT64, "2.2250738585072014e-308"); } CATCH From 31f8d5ff6f3893ef449b13f6b7363f40b3e32497 Mon Sep 17 00:00:00 2001 From: SeaRise Date: Thu, 6 Jan 2022 19:27:05 +0800 Subject: [PATCH 05/12] udpate --- .../Functions/tests/gtest_tidb_conversion.cpp | 152 +++++++++++------- 1 file changed, 96 insertions(+), 56 deletions(-) diff --git a/dbms/src/Functions/tests/gtest_tidb_conversion.cpp b/dbms/src/Functions/tests/gtest_tidb_conversion.cpp index 60650b6938e..b4d0665fcc0 100644 --- a/dbms/src/Functions/tests/gtest_tidb_conversion.cpp +++ b/dbms/src/Functions/tests/gtest_tidb_conversion.cpp @@ -58,35 +58,44 @@ class TestTidbConversion : public DB::tests::FunctionTest template void testNotOnlyNull(const Input & input, const Output & output) { + static_assert(!IsDecimal && !std::is_same_v); auto inner_test = [&](bool is_const) { - if constexpr (IsDecimal) - { - if (std::is_same_v) - { - } - else if (std::is_same_v) - { - } - else if (std::is_same_v) - { - } - else - { - static_assert(std::is_same_v); - } - } - else if constexpr (std::is_same_v) - { - } - else - { - ASSERT_COLUMN_EQ( - is_const ? createConstColumn>(1, output) : createColumn>({output}), - executeFunction( - func_name, - {is_const ? createConstColumn>(1, input) : createColumn>({input}), - createCastTypeConstColumn(fmt::format("Nullable({})", TypeName::get()))})); - } + ASSERT_COLUMN_EQ( + is_const ? createConstColumn>(1, output) : createColumn>({output}), + executeFunction( + func_name, + {is_const ? createConstColumn>(1, input) : createColumn>({input}), + createCastTypeConstColumn(fmt::format("Nullable({})", TypeName::get()))})); + }; + inner_test(true); + inner_test(false); + } + + template + typename std::enable_if, void>::type testNotOnlyNull(const Input & input, const DecimalField & output, const std::tuple & meta) + { + auto inner_test = [&](bool is_const) { + ASSERT_COLUMN_EQ( + is_const ? createConstColumn>(meta, 1, output) : createColumn>(meta, {output}), + executeFunction( + func_name, + {is_const ? createConstColumn>(1, input) : createColumn>({input}), + createCastTypeConstColumn(fmt::format("Nullable(Decimal({},{}))", std::get<0>(meta), std::get<1>(meta)))})); + }; + inner_test(true); + inner_test(false); + } + + template + typename std::enable_if, void>::type testNotOnlyNull(const Input & input, const Output & output, int fraction) + { + auto inner_test = [&](bool is_const) { + ASSERT_COLUMN_EQ( + is_const ? createDateTimeColumnConst(1, output, fraction) : createDateTimeColumnConst(1, output, fraction), + executeFunction( + func_name, + {is_const ? createConstColumn>(1, input) : createColumn>({input}), + createCastTypeConstColumn(fmt::format("Nullable(MyDateTime({}))", fraction))})); }; inner_test(true); inner_test(false); @@ -95,35 +104,14 @@ class TestTidbConversion : public DB::tests::FunctionTest template void testThrowException(const Input & input) { + static_assert(!IsDecimal && !std::is_same_v); auto inner_test = [&](bool is_const) { - if constexpr (IsDecimal) - { - if (std::is_same_v) - { - } - else if (std::is_same_v) - { - } - else if (std::is_same_v) - { - } - else - { - static_assert(std::is_same_v); - } - } - else if constexpr (std::is_same_v) - { - } - else - { - ASSERT_THROW( - executeFunction( - func_name, - {is_const ? createConstColumn>(1, input) : createColumn>({input}), - createCastTypeConstColumn(fmt::format("Nullable({})", TypeName::get()))}), - TiFlashException); - } + ASSERT_THROW( + executeFunction( + func_name, + {is_const ? createConstColumn>(1, input) : createColumn>({input}), + createCastTypeConstColumn(fmt::format("Nullable({})", TypeName::get()))}), + TiFlashException); }; inner_test(true); inner_test(false); @@ -958,6 +946,54 @@ try testOnlyNull(); testOnlyNull(); testOnlyNull(); + + testNotOnlyNull(0, DecimalField32(0, 0), std::make_tuple(9, 0)); + testNotOnlyNull(12.213, DecimalField32(12213, 3), std::make_tuple(9, 3)); + testNotOnlyNull(-12.213, DecimalField32(-12213, 3), std::make_tuple(9, 3)); +// testNotOnlyNull(MAX_FLOAT32, DecimalField32(-12213, 3), std::make_tuple(9, 3)); +// testNotOnlyNull(MIN_FLOAT32, DecimalField32(-12213, 3), std::make_tuple(9, 3)); + + testNotOnlyNull(0, DecimalField64(0, 0), std::make_tuple(18, 0)); + testNotOnlyNull(12.213, DecimalField64(12213, 3), std::make_tuple(18, 3)); + testNotOnlyNull(-12.213, DecimalField64(-12213, 3), std::make_tuple(18, 3)); +// testNotOnlyNull(MAX_FLOAT32, DecimalField64(-12213, 3), std::make_tuple(18, 3)); +// testNotOnlyNull(MIN_FLOAT32, DecimalField64(-12213, 3), std::make_tuple(18, 3)); + + testNotOnlyNull(0, DecimalField128(0, 0), std::make_tuple(38, 0)); + testNotOnlyNull(12.213, DecimalField128(12213, 3), std::make_tuple(38, 3)); + testNotOnlyNull(-12.213, DecimalField128(-12213, 3), std::make_tuple(38, 3)); +// testNotOnlyNull(MAX_FLOAT32, DecimalField128(-12213, 3), std::make_tuple(38, 3)); +// testNotOnlyNull(MIN_FLOAT32, DecimalField128(-12213, 3), std::make_tuple(38, 3)); + + testNotOnlyNull(0, DecimalField256(static_cast(0), 0), std::make_tuple(65, 0)); + testNotOnlyNull(12.213, DecimalField256(static_cast(12213), 3), std::make_tuple(65, 3)); + testNotOnlyNull(-12.213, DecimalField256(static_cast(-12213), 3), std::make_tuple(65, 3)); +// testNotOnlyNull(MAX_FLOAT32, DecimalField256(static_cast(-12213), 3), std::make_tuple(65, 3)); +// testNotOnlyNull(MIN_FLOAT32, DecimalField256(static_cast(-12213), 3), std::make_tuple(65, 3)); + + testNotOnlyNull(0, DecimalField32(0, 0), std::make_tuple(9, 0)); + testNotOnlyNull(12.213, DecimalField32(12213, 3), std::make_tuple(9, 3)); + testNotOnlyNull(-12.213, DecimalField32(-12213, 3), std::make_tuple(9, 3)); +// testNotOnlyNull(MAX_FLOAT64, DecimalField32(-12213, 3), std::make_tuple(9, 3)); +// testNotOnlyNull(MIN_FLOAT64, DecimalField32(-12213, 3), std::make_tuple(9, 3)); + + testNotOnlyNull(0, DecimalField64(0, 0), std::make_tuple(18, 0)); + testNotOnlyNull(12.213, DecimalField64(12213, 3), std::make_tuple(18, 3)); + testNotOnlyNull(-12.213, DecimalField64(-12213, 3), std::make_tuple(18, 3)); +// testNotOnlyNull(MAX_FLOAT64, DecimalField64(-12213, 3), std::make_tuple(18, 3)); +// testNotOnlyNull(MIN_FLOAT64, DecimalField64(-12213, 3), std::make_tuple(18, 3)); + + testNotOnlyNull(0, DecimalField128(0, 0), std::make_tuple(38, 0)); + testNotOnlyNull(12.213, DecimalField128(12213, 3), std::make_tuple(38, 3)); + testNotOnlyNull(-12.213, DecimalField128(-12213, 3), std::make_tuple(38, 3)); +// testNotOnlyNull(MAX_FLOAT64, DecimalField128(12213, 3), std::make_tuple(38, 3)); +// testNotOnlyNull(MIN_FLOAT64, DecimalField128(12213, 3), std::make_tuple(38, 3)); + + testNotOnlyNull(0, DecimalField256(static_cast(0), 0), std::make_tuple(65, 0)); + testNotOnlyNull(12.213, DecimalField256(static_cast(12213), 3), std::make_tuple(65, 3)); + testNotOnlyNull(-12.213, DecimalField256(static_cast(-12213), 3), std::make_tuple(65, 3)); +// testNotOnlyNull(MAX_FLOAT64, DecimalField256(static_cast(12213), 0), std::make_tuple(65, 0)); +// testNotOnlyNull(MIN_FLOAT64, DecimalField256(static_cast(12213), 3), std::make_tuple(65, 3)); } CATCH @@ -966,6 +1002,10 @@ try { testOnlyNull(); testOnlyNull(); + + testNotOnlyNull(0, {0, 0, 0, 0, 0, 0, 0}, 6); + + testNotOnlyNull(0, {0, 0, 0, 0, 0, 0, 0}, 6); } CATCH From e3ee0f890c12b7177e2d6798fcd00b915446214a Mon Sep 17 00:00:00 2001 From: SeaRise Date: Mon, 10 Jan 2022 13:03:55 +0800 Subject: [PATCH 06/12] cast real as time test --- dbms/src/Functions/FunctionsTiDBConversion.h | 11 +- .../Functions/tests/gtest_tidb_conversion.cpp | 120 ++++++++++++++---- 2 files changed, 102 insertions(+), 29 deletions(-) diff --git a/dbms/src/Functions/FunctionsTiDBConversion.h b/dbms/src/Functions/FunctionsTiDBConversion.h index 0094fe43907..5bdc67f66db 100644 --- a/dbms/src/Functions/FunctionsTiDBConversion.h +++ b/dbms/src/Functions/FunctionsTiDBConversion.h @@ -1378,6 +1378,9 @@ struct TiDBConvertToTime const typename ColumnVector::Container & vec_from = col_from->getData(); + auto handle_invalid_time = [&](const String & value_str_) { + context.getDAGContext()->handleInvalidTime("Invalid time value: '" + value_str_ + "'", Errors::Types::WrongValue); + }; for (size_t i = 0; i < size; i++) { Float64 value = vec_from[i]; @@ -1409,7 +1412,13 @@ struct TiDBConvertToTime { // Fill NULL if cannot parse (*vec_null_map_to)[i] = 1; - context.getDAGContext()->handleInvalidTime("Invalid time value: '" + value_str + "'", Errors::Types::WrongValue); + handle_invalid_time(value_str); + } + catch (const std::exception &) + { + // Fill NULL if cannot parse + (*vec_null_map_to)[i] = 1; + handle_invalid_time(value_str); } } } diff --git a/dbms/src/Functions/tests/gtest_tidb_conversion.cpp b/dbms/src/Functions/tests/gtest_tidb_conversion.cpp index b4d0665fcc0..0311b690a56 100644 --- a/dbms/src/Functions/tests/gtest_tidb_conversion.cpp +++ b/dbms/src/Functions/tests/gtest_tidb_conversion.cpp @@ -87,11 +87,11 @@ class TestTidbConversion : public DB::tests::FunctionTest } template - typename std::enable_if, void>::type testNotOnlyNull(const Input & input, const Output & output, int fraction) + typename std::enable_if, void>::type testNotOnlyNull(const Input & input, const MyDateTime & output, int fraction) { auto inner_test = [&](bool is_const) { ASSERT_COLUMN_EQ( - is_const ? createDateTimeColumnConst(1, output, fraction) : createDateTimeColumnConst(1, output, fraction), + is_const ? createDateTimeColumnConst(1, output, fraction) : createDateTimeColumn({output}, fraction), executeFunction( func_name, {is_const ? createConstColumn>(1, input) : createColumn>({input}), @@ -111,7 +111,37 @@ class TestTidbConversion : public DB::tests::FunctionTest func_name, {is_const ? createConstColumn>(1, input) : createColumn>({input}), createCastTypeConstColumn(fmt::format("Nullable({})", TypeName::get()))}), - TiFlashException); + TiFlashException); + }; + inner_test(true); + inner_test(false); + } + + template + typename std::enable_if, void>::type testThrowException(const Input & input, const std::tuple & meta) + { + auto inner_test = [&](bool is_const) { + ASSERT_THROW( + executeFunction( + func_name, + {is_const ? createConstColumn>(1, input) : createColumn>({input}), + createCastTypeConstColumn(fmt::format("Nullable(Decimal({},{}))", std::get<0>(meta), std::get<1>(meta)))}), + TiFlashException); + }; + inner_test(true); + inner_test(false); + } + + template + typename std::enable_if, void>::type testThrowException(const Input & input, int fraction) + { + auto inner_test = [&](bool is_const) { + ASSERT_THROW( + executeFunction( + func_name, + {is_const ? createConstColumn>(1, input) : createColumn>({input}), + createCastTypeConstColumn(fmt::format("Nullable(MyDateTime({}))", fraction))}), + TiFlashException); }; inner_test(true); inner_test(false); @@ -907,31 +937,31 @@ try testOnlyNull(); testOnlyNull(); + // TODO add tests after non-expected results fixed + testNotOnlyNull(0, "0"); testNotOnlyNull(12.213, "12.213"); testNotOnlyNull(-12.213, "-12.213"); - // TODO add tests after fix results is not compatible with tidb/mysql // tiflash: 3.4028235e38 // tidb: 340282350000000000000000000000000000000 // mysql: 3.40282e38 - // testNotOnlyNull(MAX_FLOAT32, "3.4028235e38"); + // testNotOnlyNull(MAX_FLOAT32, "3.4028235e38"); // tiflash: 1.1754944e-38 // tidb: 0.000000000000000000000000000000000000011754944 // mysql: 1.17549e-38 - // testNotOnlyNull(MIN_FLOAT32, "1.1754944e-38"); + // testNotOnlyNull(MIN_FLOAT32, "1.1754944e-38"); testNotOnlyNull(0, "0"); testNotOnlyNull(12.213, "12.213"); testNotOnlyNull(-12.213, "-12.213"); - // TODO add tests after fix results is not compatible with tidb/mysql // tiflash: 1.7976931348623157e308 // tidb: 179769313486231570000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 // mysql: 1.7976931348623157e308 - // testNotOnlyNull(MAX_FLOAT64, "1.7976931348623157e308"); + // testNotOnlyNull(MAX_FLOAT64, "1.7976931348623157e308"); // tiflash: 2.2250738585072014e-308 // tidb: 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000022250738585072014 // mysql: 2.2250738585072014e-308 - // testNotOnlyNull(MIN_FLOAT64, "2.2250738585072014e-308"); + // testNotOnlyNull(MIN_FLOAT64, "2.2250738585072014e-308"); } CATCH @@ -950,50 +980,50 @@ try testNotOnlyNull(0, DecimalField32(0, 0), std::make_tuple(9, 0)); testNotOnlyNull(12.213, DecimalField32(12213, 3), std::make_tuple(9, 3)); testNotOnlyNull(-12.213, DecimalField32(-12213, 3), std::make_tuple(9, 3)); -// testNotOnlyNull(MAX_FLOAT32, DecimalField32(-12213, 3), std::make_tuple(9, 3)); -// testNotOnlyNull(MIN_FLOAT32, DecimalField32(-12213, 3), std::make_tuple(9, 3)); + testNotOnlyNull(MAX_FLOAT32, DecimalField32(-12213, 3), std::make_tuple(9, 3)); + testNotOnlyNull(MIN_FLOAT32, DecimalField32(-12213, 3), std::make_tuple(9, 3)); testNotOnlyNull(0, DecimalField64(0, 0), std::make_tuple(18, 0)); testNotOnlyNull(12.213, DecimalField64(12213, 3), std::make_tuple(18, 3)); testNotOnlyNull(-12.213, DecimalField64(-12213, 3), std::make_tuple(18, 3)); -// testNotOnlyNull(MAX_FLOAT32, DecimalField64(-12213, 3), std::make_tuple(18, 3)); -// testNotOnlyNull(MIN_FLOAT32, DecimalField64(-12213, 3), std::make_tuple(18, 3)); + // testNotOnlyNull(MAX_FLOAT32, DecimalField64(-12213, 3), std::make_tuple(18, 3)); + // testNotOnlyNull(MIN_FLOAT32, DecimalField64(-12213, 3), std::make_tuple(18, 3)); testNotOnlyNull(0, DecimalField128(0, 0), std::make_tuple(38, 0)); testNotOnlyNull(12.213, DecimalField128(12213, 3), std::make_tuple(38, 3)); testNotOnlyNull(-12.213, DecimalField128(-12213, 3), std::make_tuple(38, 3)); -// testNotOnlyNull(MAX_FLOAT32, DecimalField128(-12213, 3), std::make_tuple(38, 3)); -// testNotOnlyNull(MIN_FLOAT32, DecimalField128(-12213, 3), std::make_tuple(38, 3)); + // testNotOnlyNull(MAX_FLOAT32, DecimalField128(-12213, 3), std::make_tuple(38, 3)); + // testNotOnlyNull(MIN_FLOAT32, DecimalField128(-12213, 3), std::make_tuple(38, 3)); testNotOnlyNull(0, DecimalField256(static_cast(0), 0), std::make_tuple(65, 0)); testNotOnlyNull(12.213, DecimalField256(static_cast(12213), 3), std::make_tuple(65, 3)); testNotOnlyNull(-12.213, DecimalField256(static_cast(-12213), 3), std::make_tuple(65, 3)); -// testNotOnlyNull(MAX_FLOAT32, DecimalField256(static_cast(-12213), 3), std::make_tuple(65, 3)); -// testNotOnlyNull(MIN_FLOAT32, DecimalField256(static_cast(-12213), 3), std::make_tuple(65, 3)); + // testNotOnlyNull(MAX_FLOAT32, DecimalField256(static_cast(-12213), 3), std::make_tuple(65, 3)); + // testNotOnlyNull(MIN_FLOAT32, DecimalField256(static_cast(-12213), 3), std::make_tuple(65, 3)); testNotOnlyNull(0, DecimalField32(0, 0), std::make_tuple(9, 0)); testNotOnlyNull(12.213, DecimalField32(12213, 3), std::make_tuple(9, 3)); testNotOnlyNull(-12.213, DecimalField32(-12213, 3), std::make_tuple(9, 3)); -// testNotOnlyNull(MAX_FLOAT64, DecimalField32(-12213, 3), std::make_tuple(9, 3)); -// testNotOnlyNull(MIN_FLOAT64, DecimalField32(-12213, 3), std::make_tuple(9, 3)); + // testNotOnlyNull(MAX_FLOAT64, DecimalField32(-12213, 3), std::make_tuple(9, 3)); + // testNotOnlyNull(MIN_FLOAT64, DecimalField32(-12213, 3), std::make_tuple(9, 3)); testNotOnlyNull(0, DecimalField64(0, 0), std::make_tuple(18, 0)); testNotOnlyNull(12.213, DecimalField64(12213, 3), std::make_tuple(18, 3)); testNotOnlyNull(-12.213, DecimalField64(-12213, 3), std::make_tuple(18, 3)); -// testNotOnlyNull(MAX_FLOAT64, DecimalField64(-12213, 3), std::make_tuple(18, 3)); -// testNotOnlyNull(MIN_FLOAT64, DecimalField64(-12213, 3), std::make_tuple(18, 3)); + // testNotOnlyNull(MAX_FLOAT64, DecimalField64(-12213, 3), std::make_tuple(18, 3)); + // testNotOnlyNull(MIN_FLOAT64, DecimalField64(-12213, 3), std::make_tuple(18, 3)); testNotOnlyNull(0, DecimalField128(0, 0), std::make_tuple(38, 0)); testNotOnlyNull(12.213, DecimalField128(12213, 3), std::make_tuple(38, 3)); testNotOnlyNull(-12.213, DecimalField128(-12213, 3), std::make_tuple(38, 3)); -// testNotOnlyNull(MAX_FLOAT64, DecimalField128(12213, 3), std::make_tuple(38, 3)); -// testNotOnlyNull(MIN_FLOAT64, DecimalField128(12213, 3), std::make_tuple(38, 3)); + // testNotOnlyNull(MAX_FLOAT64, DecimalField128(12213, 3), std::make_tuple(38, 3)); + // testNotOnlyNull(MIN_FLOAT64, DecimalField128(12213, 3), std::make_tuple(38, 3)); testNotOnlyNull(0, DecimalField256(static_cast(0), 0), std::make_tuple(65, 0)); testNotOnlyNull(12.213, DecimalField256(static_cast(12213), 3), std::make_tuple(65, 3)); testNotOnlyNull(-12.213, DecimalField256(static_cast(-12213), 3), std::make_tuple(65, 3)); -// testNotOnlyNull(MAX_FLOAT64, DecimalField256(static_cast(12213), 0), std::make_tuple(65, 0)); -// testNotOnlyNull(MIN_FLOAT64, DecimalField256(static_cast(12213), 3), std::make_tuple(65, 3)); + // testNotOnlyNull(MAX_FLOAT64, DecimalField256(static_cast(12213), 0), std::make_tuple(65, 0)); + // testNotOnlyNull(MIN_FLOAT64, DecimalField256(static_cast(12213), 3), std::make_tuple(65, 3)); } CATCH @@ -1003,9 +1033,43 @@ try testOnlyNull(); testOnlyNull(); - testNotOnlyNull(0, {0, 0, 0, 0, 0, 0, 0}, 6); - - testNotOnlyNull(0, {0, 0, 0, 0, 0, 0, 0}, 6); + // TODO add tests after non-expected results fixed + + // mysql: null, warning. + // tiflash: null, no warning. + // tidb: 0000-00-00 00:00:00 + // testThrowException(0, 6); + testThrowException(12.213, 6); + testThrowException(-12.213, 6); + testThrowException(MAX_FLOAT32, 6); + testThrowException(MIN_FLOAT32, 6); + // mysql: 2000-01-11 00:00:00 + // tiflash / tidb: null, warnings + // testNotOnlyNull(111, {2000, 1, 11, 0, 0, 0, 0}, 6); + testThrowException(-111, 6); + // mysql: 2000-01-11 00:00:00 + // tiflash / tidb: null, warnings + // testNotOnlyNull(111.1, {2000, 1, 11, 0, 0, 0, 0}, 6); + + // mysql: null, warning. + // tiflash: null, no warning. + // tidb: 0000-00-00 00:00:00 + // testThrowException(0, 6); + testThrowException(12.213, 6); + testThrowException(-12.213, 6); + testThrowException(MAX_FLOAT64, 6); + testThrowException(MIN_FLOAT64, 6); + // mysql: 2000-01-11 00:00:00 + // tiflash / tidb: null, warnings + // testNotOnlyNull(111, {2000, 1, 11, 0, 0, 0, 0}, 6); + testThrowException(-111, 6); + // mysql: 2000-01-11 00:00:00 + // tiflash / tidb: null, warnings + // testNotOnlyNull(111.1, {2000, 1, 11, 0, 0, 0, 0}, 6); + testNotOnlyNull(20210201, {2021, 2, 1, 0, 0, 0, 0}, 6); + // mysql: 2021-02-01 00:00:00 + // tiflash / tidb: 2021-02-01 01:00:00 + // testNotOnlyNull(20210201.1, {2021, 2, 1, 0, 0, 0, 0}, 6); } CATCH From d36331476dff8a9d712b42ba5cdf370b2d1dd831 Mon Sep 17 00:00:00 2001 From: SeaRise Date: Mon, 10 Jan 2022 15:12:27 +0800 Subject: [PATCH 07/12] decimal tests --- .../Functions/tests/gtest_tidb_conversion.cpp | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/dbms/src/Functions/tests/gtest_tidb_conversion.cpp b/dbms/src/Functions/tests/gtest_tidb_conversion.cpp index 0311b690a56..481e2c24813 100644 --- a/dbms/src/Functions/tests/gtest_tidb_conversion.cpp +++ b/dbms/src/Functions/tests/gtest_tidb_conversion.cpp @@ -980,50 +980,50 @@ try testNotOnlyNull(0, DecimalField32(0, 0), std::make_tuple(9, 0)); testNotOnlyNull(12.213, DecimalField32(12213, 3), std::make_tuple(9, 3)); testNotOnlyNull(-12.213, DecimalField32(-12213, 3), std::make_tuple(9, 3)); - testNotOnlyNull(MAX_FLOAT32, DecimalField32(-12213, 3), std::make_tuple(9, 3)); - testNotOnlyNull(MIN_FLOAT32, DecimalField32(-12213, 3), std::make_tuple(9, 3)); + testThrowException(MAX_FLOAT32, std::make_tuple(9, 0)); + testNotOnlyNull(MIN_FLOAT64, DecimalField32(0, 9), std::make_tuple(9, 9)); testNotOnlyNull(0, DecimalField64(0, 0), std::make_tuple(18, 0)); testNotOnlyNull(12.213, DecimalField64(12213, 3), std::make_tuple(18, 3)); testNotOnlyNull(-12.213, DecimalField64(-12213, 3), std::make_tuple(18, 3)); - // testNotOnlyNull(MAX_FLOAT32, DecimalField64(-12213, 3), std::make_tuple(18, 3)); - // testNotOnlyNull(MIN_FLOAT32, DecimalField64(-12213, 3), std::make_tuple(18, 3)); + testThrowException(MAX_FLOAT32, std::make_tuple(18, 0)); + testNotOnlyNull(MIN_FLOAT64, DecimalField64(0, 18), std::make_tuple(18, 18)); testNotOnlyNull(0, DecimalField128(0, 0), std::make_tuple(38, 0)); testNotOnlyNull(12.213, DecimalField128(12213, 3), std::make_tuple(38, 3)); testNotOnlyNull(-12.213, DecimalField128(-12213, 3), std::make_tuple(38, 3)); - // testNotOnlyNull(MAX_FLOAT32, DecimalField128(-12213, 3), std::make_tuple(38, 3)); - // testNotOnlyNull(MIN_FLOAT32, DecimalField128(-12213, 3), std::make_tuple(38, 3)); + testThrowException(MAX_FLOAT32, std::make_tuple(38, 0)); + testNotOnlyNull(MIN_FLOAT64, DecimalField128(0, 30), std::make_tuple(38, 30)); testNotOnlyNull(0, DecimalField256(static_cast(0), 0), std::make_tuple(65, 0)); testNotOnlyNull(12.213, DecimalField256(static_cast(12213), 3), std::make_tuple(65, 3)); testNotOnlyNull(-12.213, DecimalField256(static_cast(-12213), 3), std::make_tuple(65, 3)); - // testNotOnlyNull(MAX_FLOAT32, DecimalField256(static_cast(-12213), 3), std::make_tuple(65, 3)); - // testNotOnlyNull(MIN_FLOAT32, DecimalField256(static_cast(-12213), 3), std::make_tuple(65, 3)); + testNotOnlyNull(MAX_FLOAT32, DecimalField256(Int256("340282346638528860000000000000000000000"), 0), std::make_tuple(65, 0)); + testNotOnlyNull(MIN_FLOAT64, DecimalField256(static_cast(0), 30), std::make_tuple(65, 30)); testNotOnlyNull(0, DecimalField32(0, 0), std::make_tuple(9, 0)); testNotOnlyNull(12.213, DecimalField32(12213, 3), std::make_tuple(9, 3)); testNotOnlyNull(-12.213, DecimalField32(-12213, 3), std::make_tuple(9, 3)); - // testNotOnlyNull(MAX_FLOAT64, DecimalField32(-12213, 3), std::make_tuple(9, 3)); - // testNotOnlyNull(MIN_FLOAT64, DecimalField32(-12213, 3), std::make_tuple(9, 3)); + testThrowException(MAX_FLOAT64, std::make_tuple(9, 0)); + testNotOnlyNull(MIN_FLOAT64, DecimalField32(0, 9), std::make_tuple(9, 9)); testNotOnlyNull(0, DecimalField64(0, 0), std::make_tuple(18, 0)); testNotOnlyNull(12.213, DecimalField64(12213, 3), std::make_tuple(18, 3)); testNotOnlyNull(-12.213, DecimalField64(-12213, 3), std::make_tuple(18, 3)); - // testNotOnlyNull(MAX_FLOAT64, DecimalField64(-12213, 3), std::make_tuple(18, 3)); - // testNotOnlyNull(MIN_FLOAT64, DecimalField64(-12213, 3), std::make_tuple(18, 3)); + testThrowException(MAX_FLOAT64, std::make_tuple(18, 0)); + testNotOnlyNull(MIN_FLOAT64, DecimalField64(0, 18), std::make_tuple(18, 18)); testNotOnlyNull(0, DecimalField128(0, 0), std::make_tuple(38, 0)); testNotOnlyNull(12.213, DecimalField128(12213, 3), std::make_tuple(38, 3)); testNotOnlyNull(-12.213, DecimalField128(-12213, 3), std::make_tuple(38, 3)); - // testNotOnlyNull(MAX_FLOAT64, DecimalField128(12213, 3), std::make_tuple(38, 3)); - // testNotOnlyNull(MIN_FLOAT64, DecimalField128(12213, 3), std::make_tuple(38, 3)); + testThrowException(MAX_FLOAT64, std::make_tuple(38, 0)); + testNotOnlyNull(MIN_FLOAT64, DecimalField128(0, 30), std::make_tuple(38, 30)); testNotOnlyNull(0, DecimalField256(static_cast(0), 0), std::make_tuple(65, 0)); testNotOnlyNull(12.213, DecimalField256(static_cast(12213), 3), std::make_tuple(65, 3)); testNotOnlyNull(-12.213, DecimalField256(static_cast(-12213), 3), std::make_tuple(65, 3)); - // testNotOnlyNull(MAX_FLOAT64, DecimalField256(static_cast(12213), 0), std::make_tuple(65, 0)); - // testNotOnlyNull(MIN_FLOAT64, DecimalField256(static_cast(12213), 3), std::make_tuple(65, 3)); + testThrowException(MAX_FLOAT64, std::make_tuple(65, 0)); + testNotOnlyNull(MIN_FLOAT64, DecimalField256(static_cast(0), 30), std::make_tuple(65, 30)); } CATCH From 15e6ea2c726f7152732306cb01fcd0f28ff5bbdb Mon Sep 17 00:00:00 2001 From: SeaRise Date: Mon, 10 Jan 2022 15:15:27 +0800 Subject: [PATCH 08/12] find bug --- dbms/src/Functions/tests/gtest_tidb_conversion.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/dbms/src/Functions/tests/gtest_tidb_conversion.cpp b/dbms/src/Functions/tests/gtest_tidb_conversion.cpp index 481e2c24813..6676600bb6f 100644 --- a/dbms/src/Functions/tests/gtest_tidb_conversion.cpp +++ b/dbms/src/Functions/tests/gtest_tidb_conversion.cpp @@ -998,7 +998,9 @@ try testNotOnlyNull(0, DecimalField256(static_cast(0), 0), std::make_tuple(65, 0)); testNotOnlyNull(12.213, DecimalField256(static_cast(12213), 3), std::make_tuple(65, 3)); testNotOnlyNull(-12.213, DecimalField256(static_cast(-12213), 3), std::make_tuple(65, 3)); - testNotOnlyNull(MAX_FLOAT32, DecimalField256(Int256("340282346638528860000000000000000000000"), 0), std::make_tuple(65, 0)); + // TODO add test after bug fixed + // ERROR 1105 (HY000): other error for mpp stream: Cannot convert a non-finite number to an integer. + // testNotOnlyNull(MAX_FLOAT32, DecimalField256(Int256("340282346638528860000000000000000000000"), 0), std::make_tuple(65, 0)); testNotOnlyNull(MIN_FLOAT64, DecimalField256(static_cast(0), 30), std::make_tuple(65, 30)); testNotOnlyNull(0, DecimalField32(0, 0), std::make_tuple(9, 0)); From d6f42a9052814a19afcf446299abb527ec6f67b9 Mon Sep 17 00:00:00 2001 From: SeaRise Date: Thu, 27 Jan 2022 17:54:02 +0800 Subject: [PATCH 09/12] address comments --- dbms/src/Functions/FunctionsTiDBConversion.h | 44 +++++++----- .../Functions/tests/gtest_tidb_conversion.cpp | 72 ++++++++++++++++--- 2 files changed, 90 insertions(+), 26 deletions(-) diff --git a/dbms/src/Functions/FunctionsTiDBConversion.h b/dbms/src/Functions/FunctionsTiDBConversion.h index 65f4f182663..51a79102313 100644 --- a/dbms/src/Functions/FunctionsTiDBConversion.h +++ b/dbms/src/Functions/FunctionsTiDBConversion.h @@ -940,9 +940,9 @@ struct TiDBConvertToDecimal if (need_to_round) { if (value < 0) - value--; + --value; else - value++; + ++value; } } @@ -1217,6 +1217,7 @@ struct TiDBConvertToDecimal template struct TiDBConvertToTime { +public: static_assert(std::is_same_v || std::is_same_v); using FromFieldType = typename FromDataType::FieldType; @@ -1252,7 +1253,6 @@ struct TiDBConvertToTime col_null_map_to = ColumnUInt8::create(size, 0); vec_null_map_to = &col_null_map_to->getData(); } - if constexpr (std::is_same_v) { // cast string as time @@ -1262,7 +1262,7 @@ struct TiDBConvertToTime const ColumnString::Offsets * offsets = &col_from->getOffsets(); size_t current_offset = 0; - for (size_t i = 0; i < size; i++) + for (size_t i = 0; i < size; ++i) { size_t next_offset = (*offsets)[i]; size_t string_size = next_offset - current_offset - 1; @@ -1287,7 +1287,8 @@ struct TiDBConvertToTime { // Fill NULL if cannot parse (*vec_null_map_to)[i] = 1; - context.getDAGContext()->handleInvalidTime("Invalid time value: '" + string_value + "'", Errors::Types::WrongValue); + vec_to[i] = 0; + handleInvalidTime(context, string_value); } current_offset = next_offset; } @@ -1298,7 +1299,7 @@ struct TiDBConvertToTime const auto * col_from = checkAndGetColumn(block.getByPosition(arguments[0]).column.get()); const ColumnUInt64::Container & vec_from = col_from->getData(); - for (size_t i = 0; i < size; i++) + for (size_t i = 0; i < size; ++i) { MyDateTime datetime(vec_from[i]); @@ -1347,7 +1348,7 @@ struct TiDBConvertToTime const typename ColumnVector::Container & vec_from = col_from->getData(); - for (size_t i = 0; i < size; i++) + for (size_t i = 0; i < size; ++i) { try { @@ -1368,9 +1369,8 @@ struct TiDBConvertToTime { // Cannot cast, fill with NULL (*vec_null_map_to)[i] = 1; - context.getDAGContext()->handleInvalidTime( - "Invalid time value: '" + toString(vec_from[i]) + "'", - Errors::Types::WrongValue); + vec_to[i] = 0; + handleInvalidTime(context, vec_from[i]); } } } @@ -1384,10 +1384,7 @@ struct TiDBConvertToTime const typename ColumnVector::Container & vec_from = col_from->getData(); - auto handle_invalid_time = [&](const String & value_str_) { - context.getDAGContext()->handleInvalidTime("Invalid time value: '" + value_str_ + "'", Errors::Types::WrongValue); - }; - for (size_t i = 0; i < size; i++) + for (size_t i = 0; i < size; ++i) { Float64 value = vec_from[i]; // Convert to string and then parse to time @@ -1396,6 +1393,7 @@ struct TiDBConvertToTime if (value_str == "0") { (*vec_null_map_to)[i] = 1; + vec_to[i] = 0; } else { @@ -1418,13 +1416,15 @@ struct TiDBConvertToTime { // Fill NULL if cannot parse (*vec_null_map_to)[i] = 1; - handle_invalid_time(value_str); + vec_to[i] = 0; + handleInvalidTime(context, value_str); } catch (const std::exception &) { // Fill NULL if cannot parse (*vec_null_map_to)[i] = 1; - handle_invalid_time(value_str); + vec_to[i] = 0; + handleInvalidTime(context, value_str); } } } @@ -1455,14 +1455,15 @@ struct TiDBConvertToTime catch (const Exception &) { (*vec_null_map_to)[i] = 1; - context.getDAGContext()->handleInvalidTime("Invalid time value: '" + value_str + "'", Errors::Types::WrongValue); + vec_to[i] = 0; + handleInvalidTime(context, value_str); } } } else { throw Exception( - "Illegal column " + block.getByPosition(arguments[0]).column->getName() + " of first argument of function tidb_cast", + fmt::format("Illegal column {} of first argument of function tidb_cast", block.getByPosition(arguments[0]).column->getName()), ErrorCodes::ILLEGAL_COLUMN); } @@ -1471,6 +1472,13 @@ struct TiDBConvertToTime else block.getByPosition(result).column = std::move(col_to); } + +private: + template + static void handleInvalidTime(const Context & context, const T & value) + { + context.getDAGContext()->handleInvalidTime(fmt::format("Invalid time value: '{}'", value), Errors::Types::WrongValue); + } }; /// cast duration as duration diff --git a/dbms/src/Functions/tests/gtest_tidb_conversion.cpp b/dbms/src/Functions/tests/gtest_tidb_conversion.cpp index 149417f54c8..288cdf65b91 100644 --- a/dbms/src/Functions/tests/gtest_tidb_conversion.cpp +++ b/dbms/src/Functions/tests/gtest_tidb_conversion.cpp @@ -949,22 +949,32 @@ try testNotOnlyNull(MIN_FLOAT32, 0); testNotOnlyNull(12.213f, 12); testNotOnlyNull(-12.213f, -12); + testNotOnlyNull(12.513f, 13); + testNotOnlyNull(-12.513f, -13); + testNotOnlyNull(0, 0); testThrowException(MAX_FLOAT32); testNotOnlyNull(MIN_FLOAT32, 0); testNotOnlyNull(12.213f, 12); testThrowException(-12.213f); + testNotOnlyNull(12.513f, 13); + testThrowException(-12.513f); testNotOnlyNull(0, 0); testThrowException(MAX_FLOAT64); testNotOnlyNull(MIN_FLOAT64, 0); testNotOnlyNull(12.213, 12); testNotOnlyNull(-12.213, -12); + testNotOnlyNull(12.513, 13); + testNotOnlyNull(-12.513, -13); + testNotOnlyNull(0, 0); testThrowException(MAX_FLOAT64); testNotOnlyNull(MIN_FLOAT64, 0); testNotOnlyNull(12.213, 12); testThrowException(-12.213); + testNotOnlyNull(12.513, 13); + testNotOnlyNull(-12.513, -13); } CATCH @@ -1034,55 +1044,101 @@ try testOnlyNull(); testOnlyNull(); + // TODO fix: + // for tidb, cast(12.213f as decimal(x, x)) throw warnings: Truncated incorrect DECIMAL value: '-12.21300029754638. + // tiflash is same as mysql, don't throw warnings. + // for round test, tidb throw warnings: Truncated incorrect DECIMAL value: xxx + // tiflash is same as mysql, don't throw warnings. + testNotOnlyNull(0, DecimalField32(0, 0), std::make_tuple(9, 0)); - testNotOnlyNull(12.213, DecimalField32(12213, 3), std::make_tuple(9, 3)); - testNotOnlyNull(-12.213, DecimalField32(-12213, 3), std::make_tuple(9, 3)); + testNotOnlyNull(12.213f, DecimalField32(12213, 3), std::make_tuple(9, 3)); + testNotOnlyNull(-12.213f, DecimalField32(-12213, 3), std::make_tuple(9, 3)); testThrowException(MAX_FLOAT32, std::make_tuple(9, 0)); testNotOnlyNull(MIN_FLOAT64, DecimalField32(0, 9), std::make_tuple(9, 9)); + // test round + testNotOnlyNull(12.213f, DecimalField32(-1221, 2), std::make_tuple(9, 2)); + testNotOnlyNull(-12.213f, DecimalField32(-1221, 2), std::make_tuple(9, 2)); + testNotOnlyNull(12.215f, DecimalField32(1222, 2), std::make_tuple(9, 2)); + testNotOnlyNull(-12.215f, DecimalField32(-1222, 2), std::make_tuple(9, 2)); testNotOnlyNull(0, DecimalField64(0, 0), std::make_tuple(18, 0)); - testNotOnlyNull(12.213, DecimalField64(12213, 3), std::make_tuple(18, 3)); - testNotOnlyNull(-12.213, DecimalField64(-12213, 3), std::make_tuple(18, 3)); + testNotOnlyNull(12.213f, DecimalField64(12213, 3), std::make_tuple(18, 3)); + testNotOnlyNull(-12.213f, DecimalField64(-12213, 3), std::make_tuple(18, 3)); testThrowException(MAX_FLOAT32, std::make_tuple(18, 0)); testNotOnlyNull(MIN_FLOAT64, DecimalField64(0, 18), std::make_tuple(18, 18)); + // test round + testNotOnlyNull(12.213f, DecimalField64(-1221, 2), std::make_tuple(18, 2)); + testNotOnlyNull(-12.213f, DecimalField64(-1221, 2), std::make_tuple(18, 2)); + testNotOnlyNull(12.215f, DecimalField64(1222, 2), std::make_tuple(18, 2)); + testNotOnlyNull(-12.215f, DecimalField64(-1222, 2), std::make_tuple(18, 2)); testNotOnlyNull(0, DecimalField128(0, 0), std::make_tuple(38, 0)); - testNotOnlyNull(12.213, DecimalField128(12213, 3), std::make_tuple(38, 3)); - testNotOnlyNull(-12.213, DecimalField128(-12213, 3), std::make_tuple(38, 3)); + testNotOnlyNull(12.213f, DecimalField128(12213, 3), std::make_tuple(38, 3)); + testNotOnlyNull(-12.213f, DecimalField128(-12213, 3), std::make_tuple(38, 3)); testThrowException(MAX_FLOAT32, std::make_tuple(38, 0)); testNotOnlyNull(MIN_FLOAT64, DecimalField128(0, 30), std::make_tuple(38, 30)); + // test round + testNotOnlyNull(12.213f, DecimalField128(-1221, 2), std::make_tuple(38, 2)); + testNotOnlyNull(-12.213f, DecimalField128(-1221, 2), std::make_tuple(38, 2)); + testNotOnlyNull(12.215f, DecimalField128(1222, 2), std::make_tuple(38, 2)); + testNotOnlyNull(-12.215f, DecimalField128(-1222, 2), std::make_tuple(38, 2)); testNotOnlyNull(0, DecimalField256(static_cast(0), 0), std::make_tuple(65, 0)); - testNotOnlyNull(12.213, DecimalField256(static_cast(12213), 3), std::make_tuple(65, 3)); - testNotOnlyNull(-12.213, DecimalField256(static_cast(-12213), 3), std::make_tuple(65, 3)); + testNotOnlyNull(12.213f, DecimalField256(static_cast(12213), 3), std::make_tuple(65, 3)); + testNotOnlyNull(-12.213f, DecimalField256(static_cast(-12213), 3), std::make_tuple(65, 3)); // TODO add test after bug fixed // ERROR 1105 (HY000): other error for mpp stream: Cannot convert a non-finite number to an integer. // testNotOnlyNull(MAX_FLOAT32, DecimalField256(Int256("340282346638528860000000000000000000000"), 0), std::make_tuple(65, 0)); testNotOnlyNull(MIN_FLOAT64, DecimalField256(static_cast(0), 30), std::make_tuple(65, 30)); + // test round + testNotOnlyNull(12.213f, DecimalField256(static_cast(-1221), 2), std::make_tuple(65, 2)); + testNotOnlyNull(-12.213f, DecimalField256(static_cast(-1221), 2), std::make_tuple(65, 2)); + testNotOnlyNull(12.215f, DecimalField256(static_cast(1222), 2), std::make_tuple(65, 2)); + testNotOnlyNull(-12.215f, DecimalField256(static_cast(-1222), 2), std::make_tuple(65, 2)); testNotOnlyNull(0, DecimalField32(0, 0), std::make_tuple(9, 0)); testNotOnlyNull(12.213, DecimalField32(12213, 3), std::make_tuple(9, 3)); testNotOnlyNull(-12.213, DecimalField32(-12213, 3), std::make_tuple(9, 3)); testThrowException(MAX_FLOAT64, std::make_tuple(9, 0)); testNotOnlyNull(MIN_FLOAT64, DecimalField32(0, 9), std::make_tuple(9, 9)); + // test round + testNotOnlyNull(12.213, DecimalField32(-1221, 2), std::make_tuple(9, 2)); + testNotOnlyNull(-12.213, DecimalField32(-1221, 2), std::make_tuple(9, 2)); + testNotOnlyNull(12.215, DecimalField32(1222, 2), std::make_tuple(9, 2)); + testNotOnlyNull(-12.215, DecimalField32(-1222, 2), std::make_tuple(9, 2)); testNotOnlyNull(0, DecimalField64(0, 0), std::make_tuple(18, 0)); testNotOnlyNull(12.213, DecimalField64(12213, 3), std::make_tuple(18, 3)); testNotOnlyNull(-12.213, DecimalField64(-12213, 3), std::make_tuple(18, 3)); testThrowException(MAX_FLOAT64, std::make_tuple(18, 0)); testNotOnlyNull(MIN_FLOAT64, DecimalField64(0, 18), std::make_tuple(18, 18)); + // test round + testNotOnlyNull(12.213, DecimalField64(-1221, 2), std::make_tuple(18, 2)); + testNotOnlyNull(-12.213, DecimalField64(-1221, 2), std::make_tuple(18, 2)); + testNotOnlyNull(12.215, DecimalField64(1222, 2), std::make_tuple(18, 2)); + testNotOnlyNull(-12.215, DecimalField64(-1222, 2), std::make_tuple(18, 2)); testNotOnlyNull(0, DecimalField128(0, 0), std::make_tuple(38, 0)); testNotOnlyNull(12.213, DecimalField128(12213, 3), std::make_tuple(38, 3)); testNotOnlyNull(-12.213, DecimalField128(-12213, 3), std::make_tuple(38, 3)); testThrowException(MAX_FLOAT64, std::make_tuple(38, 0)); testNotOnlyNull(MIN_FLOAT64, DecimalField128(0, 30), std::make_tuple(38, 30)); + // test round + testNotOnlyNull(12.213, DecimalField128(-1221, 2), std::make_tuple(38, 2)); + testNotOnlyNull(-12.213, DecimalField128(-1221, 2), std::make_tuple(38, 2)); + testNotOnlyNull(12.215, DecimalField128(1222, 2), std::make_tuple(38, 2)); + testNotOnlyNull(-12.215, DecimalField128(-1222, 2), std::make_tuple(38, 2)); testNotOnlyNull(0, DecimalField256(static_cast(0), 0), std::make_tuple(65, 0)); testNotOnlyNull(12.213, DecimalField256(static_cast(12213), 3), std::make_tuple(65, 3)); testNotOnlyNull(-12.213, DecimalField256(static_cast(-12213), 3), std::make_tuple(65, 3)); testThrowException(MAX_FLOAT64, std::make_tuple(65, 0)); testNotOnlyNull(MIN_FLOAT64, DecimalField256(static_cast(0), 30), std::make_tuple(65, 30)); + // test round + testNotOnlyNull(12.213, DecimalField256(static_cast(-1221), 2), std::make_tuple(65, 2)); + testNotOnlyNull(-12.213, DecimalField256(static_cast(-1221), 2), std::make_tuple(65, 2)); + testNotOnlyNull(12.215, DecimalField256(static_cast(1222), 2), std::make_tuple(65, 2)); + testNotOnlyNull(-12.215, DecimalField256(static_cast(-1222), 2), std::make_tuple(65, 2)); } CATCH From e3c28cf984951ba634b6d931bbd9a77d5c871ce7 Mon Sep 17 00:00:00 2001 From: SeaRise Date: Thu, 27 Jan 2022 18:33:34 +0800 Subject: [PATCH 10/12] udpate --- dbms/src/Flash/Coprocessor/DAGContext.h | 6 +- .../Functions/tests/gtest_tidb_conversion.cpp | 101 +++++++++++------- 2 files changed, 69 insertions(+), 38 deletions(-) diff --git a/dbms/src/Flash/Coprocessor/DAGContext.h b/dbms/src/Flash/Coprocessor/DAGContext.h index 48227dca68b..aefd164a00c 100644 --- a/dbms/src/Flash/Coprocessor/DAGContext.h +++ b/dbms/src/Flash/Coprocessor/DAGContext.h @@ -185,7 +185,11 @@ class DAGContext warnings_.push_back(error); } } - void clearWarnings() { warnings.clear(); } + void clearWarnings() + { + warnings.clear(); + warning_count = 0; + } UInt64 getWarningCount() { return warning_count; } const mpp::TaskMeta & getMPPTaskMeta() const { return mpp_task_meta; } bool isBatchCop() const { return is_batch_cop; } diff --git a/dbms/src/Functions/tests/gtest_tidb_conversion.cpp b/dbms/src/Functions/tests/gtest_tidb_conversion.cpp index 288cdf65b91..a560249775c 100644 --- a/dbms/src/Functions/tests/gtest_tidb_conversion.cpp +++ b/dbms/src/Functions/tests/gtest_tidb_conversion.cpp @@ -1047,41 +1047,24 @@ try // TODO fix: // for tidb, cast(12.213f as decimal(x, x)) throw warnings: Truncated incorrect DECIMAL value: '-12.21300029754638. // tiflash is same as mysql, don't throw warnings. - // for round test, tidb throw warnings: Truncated incorrect DECIMAL value: xxx - // tiflash is same as mysql, don't throw warnings. testNotOnlyNull(0, DecimalField32(0, 0), std::make_tuple(9, 0)); testNotOnlyNull(12.213f, DecimalField32(12213, 3), std::make_tuple(9, 3)); testNotOnlyNull(-12.213f, DecimalField32(-12213, 3), std::make_tuple(9, 3)); testThrowException(MAX_FLOAT32, std::make_tuple(9, 0)); testNotOnlyNull(MIN_FLOAT64, DecimalField32(0, 9), std::make_tuple(9, 9)); - // test round - testNotOnlyNull(12.213f, DecimalField32(-1221, 2), std::make_tuple(9, 2)); - testNotOnlyNull(-12.213f, DecimalField32(-1221, 2), std::make_tuple(9, 2)); - testNotOnlyNull(12.215f, DecimalField32(1222, 2), std::make_tuple(9, 2)); - testNotOnlyNull(-12.215f, DecimalField32(-1222, 2), std::make_tuple(9, 2)); testNotOnlyNull(0, DecimalField64(0, 0), std::make_tuple(18, 0)); testNotOnlyNull(12.213f, DecimalField64(12213, 3), std::make_tuple(18, 3)); testNotOnlyNull(-12.213f, DecimalField64(-12213, 3), std::make_tuple(18, 3)); testThrowException(MAX_FLOAT32, std::make_tuple(18, 0)); testNotOnlyNull(MIN_FLOAT64, DecimalField64(0, 18), std::make_tuple(18, 18)); - // test round - testNotOnlyNull(12.213f, DecimalField64(-1221, 2), std::make_tuple(18, 2)); - testNotOnlyNull(-12.213f, DecimalField64(-1221, 2), std::make_tuple(18, 2)); - testNotOnlyNull(12.215f, DecimalField64(1222, 2), std::make_tuple(18, 2)); - testNotOnlyNull(-12.215f, DecimalField64(-1222, 2), std::make_tuple(18, 2)); testNotOnlyNull(0, DecimalField128(0, 0), std::make_tuple(38, 0)); testNotOnlyNull(12.213f, DecimalField128(12213, 3), std::make_tuple(38, 3)); testNotOnlyNull(-12.213f, DecimalField128(-12213, 3), std::make_tuple(38, 3)); testThrowException(MAX_FLOAT32, std::make_tuple(38, 0)); testNotOnlyNull(MIN_FLOAT64, DecimalField128(0, 30), std::make_tuple(38, 30)); - // test round - testNotOnlyNull(12.213f, DecimalField128(-1221, 2), std::make_tuple(38, 2)); - testNotOnlyNull(-12.213f, DecimalField128(-1221, 2), std::make_tuple(38, 2)); - testNotOnlyNull(12.215f, DecimalField128(1222, 2), std::make_tuple(38, 2)); - testNotOnlyNull(-12.215f, DecimalField128(-1222, 2), std::make_tuple(38, 2)); testNotOnlyNull(0, DecimalField256(static_cast(0), 0), std::make_tuple(65, 0)); testNotOnlyNull(12.213f, DecimalField256(static_cast(12213), 3), std::make_tuple(65, 3)); @@ -1090,55 +1073,99 @@ try // ERROR 1105 (HY000): other error for mpp stream: Cannot convert a non-finite number to an integer. // testNotOnlyNull(MAX_FLOAT32, DecimalField256(Int256("340282346638528860000000000000000000000"), 0), std::make_tuple(65, 0)); testNotOnlyNull(MIN_FLOAT64, DecimalField256(static_cast(0), 30), std::make_tuple(65, 30)); - // test round - testNotOnlyNull(12.213f, DecimalField256(static_cast(-1221), 2), std::make_tuple(65, 2)); - testNotOnlyNull(-12.213f, DecimalField256(static_cast(-1221), 2), std::make_tuple(65, 2)); - testNotOnlyNull(12.215f, DecimalField256(static_cast(1222), 2), std::make_tuple(65, 2)); - testNotOnlyNull(-12.215f, DecimalField256(static_cast(-1222), 2), std::make_tuple(65, 2)); testNotOnlyNull(0, DecimalField32(0, 0), std::make_tuple(9, 0)); testNotOnlyNull(12.213, DecimalField32(12213, 3), std::make_tuple(9, 3)); testNotOnlyNull(-12.213, DecimalField32(-12213, 3), std::make_tuple(9, 3)); testThrowException(MAX_FLOAT64, std::make_tuple(9, 0)); testNotOnlyNull(MIN_FLOAT64, DecimalField32(0, 9), std::make_tuple(9, 9)); - // test round - testNotOnlyNull(12.213, DecimalField32(-1221, 2), std::make_tuple(9, 2)); - testNotOnlyNull(-12.213, DecimalField32(-1221, 2), std::make_tuple(9, 2)); - testNotOnlyNull(12.215, DecimalField32(1222, 2), std::make_tuple(9, 2)); - testNotOnlyNull(-12.215, DecimalField32(-1222, 2), std::make_tuple(9, 2)); testNotOnlyNull(0, DecimalField64(0, 0), std::make_tuple(18, 0)); testNotOnlyNull(12.213, DecimalField64(12213, 3), std::make_tuple(18, 3)); testNotOnlyNull(-12.213, DecimalField64(-12213, 3), std::make_tuple(18, 3)); testThrowException(MAX_FLOAT64, std::make_tuple(18, 0)); testNotOnlyNull(MIN_FLOAT64, DecimalField64(0, 18), std::make_tuple(18, 18)); - // test round - testNotOnlyNull(12.213, DecimalField64(-1221, 2), std::make_tuple(18, 2)); - testNotOnlyNull(-12.213, DecimalField64(-1221, 2), std::make_tuple(18, 2)); - testNotOnlyNull(12.215, DecimalField64(1222, 2), std::make_tuple(18, 2)); - testNotOnlyNull(-12.215, DecimalField64(-1222, 2), std::make_tuple(18, 2)); testNotOnlyNull(0, DecimalField128(0, 0), std::make_tuple(38, 0)); testNotOnlyNull(12.213, DecimalField128(12213, 3), std::make_tuple(38, 3)); testNotOnlyNull(-12.213, DecimalField128(-12213, 3), std::make_tuple(38, 3)); testThrowException(MAX_FLOAT64, std::make_tuple(38, 0)); testNotOnlyNull(MIN_FLOAT64, DecimalField128(0, 30), std::make_tuple(38, 30)); - // test round - testNotOnlyNull(12.213, DecimalField128(-1221, 2), std::make_tuple(38, 2)); - testNotOnlyNull(-12.213, DecimalField128(-1221, 2), std::make_tuple(38, 2)); - testNotOnlyNull(12.215, DecimalField128(1222, 2), std::make_tuple(38, 2)); - testNotOnlyNull(-12.215, DecimalField128(-1222, 2), std::make_tuple(38, 2)); testNotOnlyNull(0, DecimalField256(static_cast(0), 0), std::make_tuple(65, 0)); testNotOnlyNull(12.213, DecimalField256(static_cast(12213), 3), std::make_tuple(65, 3)); testNotOnlyNull(-12.213, DecimalField256(static_cast(-12213), 3), std::make_tuple(65, 3)); testThrowException(MAX_FLOAT64, std::make_tuple(65, 0)); testNotOnlyNull(MIN_FLOAT64, DecimalField256(static_cast(0), 30), std::make_tuple(65, 30)); + + // test round + // TODO fix: + // in default mode + // for round test, tidb throw warnings: Truncated incorrect DECIMAL value: xxx + // tiflash is same as mysql, don't throw warnings. + DAGContext * dag_context = context.getDAGContext(); + UInt64 ori_flags = dag_context->getFlags(); + dag_context->addFlag(TiDBSQLFlags::TRUNCATE_AS_WARNING); + dag_context->clearWarnings(); + + testNotOnlyNull(12.213f, DecimalField32(-1221, 2), std::make_tuple(9, 2)); + testNotOnlyNull(-12.213f, DecimalField32(-1221, 2), std::make_tuple(9, 2)); + testNotOnlyNull(12.215f, DecimalField32(1222, 2), std::make_tuple(9, 2)); + testNotOnlyNull(-12.215f, DecimalField32(-1222, 2), std::make_tuple(9, 2)); + assert(dag_context->getWarningCount() == 4); + dag_context->clearWarnings(); + + testNotOnlyNull(12.213f, DecimalField64(-1221, 2), std::make_tuple(18, 2)); + testNotOnlyNull(-12.213f, DecimalField64(-1221, 2), std::make_tuple(18, 2)); + testNotOnlyNull(12.215f, DecimalField64(1222, 2), std::make_tuple(18, 2)); + testNotOnlyNull(-12.215f, DecimalField64(-1222, 2), std::make_tuple(18, 2)); + assert(dag_context->getWarningCount() == 4); + dag_context->clearWarnings(); + + testNotOnlyNull(12.213f, DecimalField128(-1221, 2), std::make_tuple(38, 2)); + testNotOnlyNull(-12.213f, DecimalField128(-1221, 2), std::make_tuple(38, 2)); + testNotOnlyNull(12.215f, DecimalField128(1222, 2), std::make_tuple(38, 2)); + testNotOnlyNull(-12.215f, DecimalField128(-1222, 2), std::make_tuple(38, 2)); + assert(dag_context->getWarningCount() == 4); + dag_context->clearWarnings(); + + testNotOnlyNull(12.213f, DecimalField256(static_cast(-1221), 2), std::make_tuple(65, 2)); + testNotOnlyNull(-12.213f, DecimalField256(static_cast(-1221), 2), std::make_tuple(65, 2)); + testNotOnlyNull(12.215f, DecimalField256(static_cast(1222), 2), std::make_tuple(65, 2)); + testNotOnlyNull(-12.215f, DecimalField256(static_cast(-1222), 2), std::make_tuple(65, 2)); + assert(dag_context->getWarningCount() == 4); + dag_context->clearWarnings(); + + testNotOnlyNull(12.213, DecimalField32(-1221, 2), std::make_tuple(9, 2)); + testNotOnlyNull(-12.213, DecimalField32(-1221, 2), std::make_tuple(9, 2)); + testNotOnlyNull(12.215, DecimalField32(1222, 2), std::make_tuple(9, 2)); + testNotOnlyNull(-12.215, DecimalField32(-1222, 2), std::make_tuple(9, 2)); + assert(dag_context->getWarningCount() == 4); + dag_context->clearWarnings(); + + testNotOnlyNull(12.213, DecimalField64(-1221, 2), std::make_tuple(18, 2)); + testNotOnlyNull(-12.213, DecimalField64(-1221, 2), std::make_tuple(18, 2)); + testNotOnlyNull(12.215, DecimalField64(1222, 2), std::make_tuple(18, 2)); + testNotOnlyNull(-12.215, DecimalField64(-1222, 2), std::make_tuple(18, 2)); + assert(dag_context->getWarningCount() == 4); + dag_context->clearWarnings(); + + testNotOnlyNull(12.213, DecimalField128(-1221, 2), std::make_tuple(38, 2)); + testNotOnlyNull(-12.213, DecimalField128(-1221, 2), std::make_tuple(38, 2)); + testNotOnlyNull(12.215, DecimalField128(1222, 2), std::make_tuple(38, 2)); + testNotOnlyNull(-12.215, DecimalField128(-1222, 2), std::make_tuple(38, 2)); + assert(dag_context->getWarningCount() == 4); + dag_context->clearWarnings(); + testNotOnlyNull(12.213, DecimalField256(static_cast(-1221), 2), std::make_tuple(65, 2)); testNotOnlyNull(-12.213, DecimalField256(static_cast(-1221), 2), std::make_tuple(65, 2)); testNotOnlyNull(12.215, DecimalField256(static_cast(1222), 2), std::make_tuple(65, 2)); testNotOnlyNull(-12.215, DecimalField256(static_cast(-1222), 2), std::make_tuple(65, 2)); + assert(dag_context->getWarningCount() == 4); + dag_context->clearWarnings(); + + dag_context->setFlags(ori_flags); } CATCH From 7368f88e4dbeea0c499d8d5751edfc9c51739365 Mon Sep 17 00:00:00 2001 From: SeaRise Date: Thu, 27 Jan 2022 18:49:58 +0800 Subject: [PATCH 11/12] fix --- .../Functions/tests/gtest_tidb_conversion.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/dbms/src/Functions/tests/gtest_tidb_conversion.cpp b/dbms/src/Functions/tests/gtest_tidb_conversion.cpp index a560249775c..713c24a8d54 100644 --- a/dbms/src/Functions/tests/gtest_tidb_conversion.cpp +++ b/dbms/src/Functions/tests/gtest_tidb_conversion.cpp @@ -1109,56 +1109,56 @@ try dag_context->addFlag(TiDBSQLFlags::TRUNCATE_AS_WARNING); dag_context->clearWarnings(); - testNotOnlyNull(12.213f, DecimalField32(-1221, 2), std::make_tuple(9, 2)); + testNotOnlyNull(12.213f, DecimalField32(1221, 2), std::make_tuple(9, 2)); testNotOnlyNull(-12.213f, DecimalField32(-1221, 2), std::make_tuple(9, 2)); testNotOnlyNull(12.215f, DecimalField32(1222, 2), std::make_tuple(9, 2)); testNotOnlyNull(-12.215f, DecimalField32(-1222, 2), std::make_tuple(9, 2)); assert(dag_context->getWarningCount() == 4); dag_context->clearWarnings(); - testNotOnlyNull(12.213f, DecimalField64(-1221, 2), std::make_tuple(18, 2)); + testNotOnlyNull(12.213f, DecimalField64(1221, 2), std::make_tuple(18, 2)); testNotOnlyNull(-12.213f, DecimalField64(-1221, 2), std::make_tuple(18, 2)); testNotOnlyNull(12.215f, DecimalField64(1222, 2), std::make_tuple(18, 2)); testNotOnlyNull(-12.215f, DecimalField64(-1222, 2), std::make_tuple(18, 2)); assert(dag_context->getWarningCount() == 4); dag_context->clearWarnings(); - testNotOnlyNull(12.213f, DecimalField128(-1221, 2), std::make_tuple(38, 2)); + testNotOnlyNull(12.213f, DecimalField128(1221, 2), std::make_tuple(38, 2)); testNotOnlyNull(-12.213f, DecimalField128(-1221, 2), std::make_tuple(38, 2)); testNotOnlyNull(12.215f, DecimalField128(1222, 2), std::make_tuple(38, 2)); testNotOnlyNull(-12.215f, DecimalField128(-1222, 2), std::make_tuple(38, 2)); assert(dag_context->getWarningCount() == 4); dag_context->clearWarnings(); - testNotOnlyNull(12.213f, DecimalField256(static_cast(-1221), 2), std::make_tuple(65, 2)); + testNotOnlyNull(12.213f, DecimalField256(static_cast(1221), 2), std::make_tuple(65, 2)); testNotOnlyNull(-12.213f, DecimalField256(static_cast(-1221), 2), std::make_tuple(65, 2)); testNotOnlyNull(12.215f, DecimalField256(static_cast(1222), 2), std::make_tuple(65, 2)); testNotOnlyNull(-12.215f, DecimalField256(static_cast(-1222), 2), std::make_tuple(65, 2)); assert(dag_context->getWarningCount() == 4); dag_context->clearWarnings(); - testNotOnlyNull(12.213, DecimalField32(-1221, 2), std::make_tuple(9, 2)); + testNotOnlyNull(12.213, DecimalField32(1221, 2), std::make_tuple(9, 2)); testNotOnlyNull(-12.213, DecimalField32(-1221, 2), std::make_tuple(9, 2)); testNotOnlyNull(12.215, DecimalField32(1222, 2), std::make_tuple(9, 2)); testNotOnlyNull(-12.215, DecimalField32(-1222, 2), std::make_tuple(9, 2)); assert(dag_context->getWarningCount() == 4); dag_context->clearWarnings(); - testNotOnlyNull(12.213, DecimalField64(-1221, 2), std::make_tuple(18, 2)); + testNotOnlyNull(12.213, DecimalField64(1221, 2), std::make_tuple(18, 2)); testNotOnlyNull(-12.213, DecimalField64(-1221, 2), std::make_tuple(18, 2)); testNotOnlyNull(12.215, DecimalField64(1222, 2), std::make_tuple(18, 2)); testNotOnlyNull(-12.215, DecimalField64(-1222, 2), std::make_tuple(18, 2)); assert(dag_context->getWarningCount() == 4); dag_context->clearWarnings(); - testNotOnlyNull(12.213, DecimalField128(-1221, 2), std::make_tuple(38, 2)); + testNotOnlyNull(12.213, DecimalField128(1221, 2), std::make_tuple(38, 2)); testNotOnlyNull(-12.213, DecimalField128(-1221, 2), std::make_tuple(38, 2)); testNotOnlyNull(12.215, DecimalField128(1222, 2), std::make_tuple(38, 2)); testNotOnlyNull(-12.215, DecimalField128(-1222, 2), std::make_tuple(38, 2)); assert(dag_context->getWarningCount() == 4); dag_context->clearWarnings(); - testNotOnlyNull(12.213, DecimalField256(static_cast(-1221), 2), std::make_tuple(65, 2)); + testNotOnlyNull(12.213, DecimalField256(static_cast(1221), 2), std::make_tuple(65, 2)); testNotOnlyNull(-12.213, DecimalField256(static_cast(-1221), 2), std::make_tuple(65, 2)); testNotOnlyNull(12.215, DecimalField256(static_cast(1222), 2), std::make_tuple(65, 2)); testNotOnlyNull(-12.215, DecimalField256(static_cast(-1222), 2), std::make_tuple(65, 2)); From b0ad315d63d6eb1fc32007d0f60642f4bff3c5b0 Mon Sep 17 00:00:00 2001 From: SeaRise Date: Fri, 28 Jan 2022 00:30:36 +0800 Subject: [PATCH 12/12] update --- .../Functions/tests/gtest_tidb_conversion.cpp | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-) diff --git a/dbms/src/Functions/tests/gtest_tidb_conversion.cpp b/dbms/src/Functions/tests/gtest_tidb_conversion.cpp index 713c24a8d54..09fb166b9df 100644 --- a/dbms/src/Functions/tests/gtest_tidb_conversion.cpp +++ b/dbms/src/Functions/tests/gtest_tidb_conversion.cpp @@ -1113,59 +1113,44 @@ try testNotOnlyNull(-12.213f, DecimalField32(-1221, 2), std::make_tuple(9, 2)); testNotOnlyNull(12.215f, DecimalField32(1222, 2), std::make_tuple(9, 2)); testNotOnlyNull(-12.215f, DecimalField32(-1222, 2), std::make_tuple(9, 2)); - assert(dag_context->getWarningCount() == 4); - dag_context->clearWarnings(); testNotOnlyNull(12.213f, DecimalField64(1221, 2), std::make_tuple(18, 2)); testNotOnlyNull(-12.213f, DecimalField64(-1221, 2), std::make_tuple(18, 2)); testNotOnlyNull(12.215f, DecimalField64(1222, 2), std::make_tuple(18, 2)); testNotOnlyNull(-12.215f, DecimalField64(-1222, 2), std::make_tuple(18, 2)); - assert(dag_context->getWarningCount() == 4); - dag_context->clearWarnings(); testNotOnlyNull(12.213f, DecimalField128(1221, 2), std::make_tuple(38, 2)); testNotOnlyNull(-12.213f, DecimalField128(-1221, 2), std::make_tuple(38, 2)); testNotOnlyNull(12.215f, DecimalField128(1222, 2), std::make_tuple(38, 2)); testNotOnlyNull(-12.215f, DecimalField128(-1222, 2), std::make_tuple(38, 2)); - assert(dag_context->getWarningCount() == 4); - dag_context->clearWarnings(); testNotOnlyNull(12.213f, DecimalField256(static_cast(1221), 2), std::make_tuple(65, 2)); testNotOnlyNull(-12.213f, DecimalField256(static_cast(-1221), 2), std::make_tuple(65, 2)); testNotOnlyNull(12.215f, DecimalField256(static_cast(1222), 2), std::make_tuple(65, 2)); testNotOnlyNull(-12.215f, DecimalField256(static_cast(-1222), 2), std::make_tuple(65, 2)); - assert(dag_context->getWarningCount() == 4); - dag_context->clearWarnings(); testNotOnlyNull(12.213, DecimalField32(1221, 2), std::make_tuple(9, 2)); testNotOnlyNull(-12.213, DecimalField32(-1221, 2), std::make_tuple(9, 2)); testNotOnlyNull(12.215, DecimalField32(1222, 2), std::make_tuple(9, 2)); testNotOnlyNull(-12.215, DecimalField32(-1222, 2), std::make_tuple(9, 2)); - assert(dag_context->getWarningCount() == 4); - dag_context->clearWarnings(); testNotOnlyNull(12.213, DecimalField64(1221, 2), std::make_tuple(18, 2)); testNotOnlyNull(-12.213, DecimalField64(-1221, 2), std::make_tuple(18, 2)); testNotOnlyNull(12.215, DecimalField64(1222, 2), std::make_tuple(18, 2)); testNotOnlyNull(-12.215, DecimalField64(-1222, 2), std::make_tuple(18, 2)); - assert(dag_context->getWarningCount() == 4); - dag_context->clearWarnings(); testNotOnlyNull(12.213, DecimalField128(1221, 2), std::make_tuple(38, 2)); testNotOnlyNull(-12.213, DecimalField128(-1221, 2), std::make_tuple(38, 2)); testNotOnlyNull(12.215, DecimalField128(1222, 2), std::make_tuple(38, 2)); testNotOnlyNull(-12.215, DecimalField128(-1222, 2), std::make_tuple(38, 2)); - assert(dag_context->getWarningCount() == 4); - dag_context->clearWarnings(); testNotOnlyNull(12.213, DecimalField256(static_cast(1221), 2), std::make_tuple(65, 2)); testNotOnlyNull(-12.213, DecimalField256(static_cast(-1221), 2), std::make_tuple(65, 2)); testNotOnlyNull(12.215, DecimalField256(static_cast(1222), 2), std::make_tuple(65, 2)); testNotOnlyNull(-12.215, DecimalField256(static_cast(-1222), 2), std::make_tuple(65, 2)); - assert(dag_context->getWarningCount() == 4); - dag_context->clearWarnings(); dag_context->setFlags(ori_flags); + dag_context->clearWarnings(); } CATCH