From 16e31e5103e82408b734d80403cc17649e25b326 Mon Sep 17 00:00:00 2001 From: xzhangxian1008 Date: Fri, 1 Jul 2022 17:08:08 +0800 Subject: [PATCH 01/19] fix --- dbms/src/Functions/FunctionsRound.h | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/dbms/src/Functions/FunctionsRound.h b/dbms/src/Functions/FunctionsRound.h index 7a587ffee5b..bdfaac0b59a 100644 --- a/dbms/src/Functions/FunctionsRound.h +++ b/dbms/src/Functions/FunctionsRound.h @@ -1140,6 +1140,7 @@ struct TiDBDecimalRound // rounding. auto absolute_value = toSafeUnsigned(input.value); + auto carry = 0; if (frac < info.input_scale) { FracType frac_index = info.input_scale - frac; @@ -1152,7 +1153,21 @@ struct TiDBDecimalRound if (remainder >= base / 2) { // round up. + auto absolute_before = absolute_value; absolute_value += base; + + // check if carry occurs + auto absolute_tmp = absolute_value; + while (absolute_tmp >= 10 && absolute_before >= 10) + { + absolute_tmp /= 10; + absolute_before /= 10; + } + + if (absolute_tmp >= 10) + { + carry = 1; /// carry occurs + } } } @@ -1176,7 +1191,7 @@ struct TiDBDecimalRound scaled_value *= PowForOutput::result[-difference]; // check overflow and construct result. - if (scaled_value > DecimalMaxValue::get(info.output_prec)) + if (scaled_value > DecimalMaxValue::get(info.output_prec + carry)) throw TiFlashException("Data truncated", Errors::Decimal::Overflow); auto result = static_cast(scaled_value); From d974bf0f2d3cf47e67225b148046fd6215d75f64 Mon Sep 17 00:00:00 2001 From: xzhangxian1008 Date: Fri, 1 Jul 2022 17:10:08 +0800 Subject: [PATCH 02/19] update --- dbms/src/Functions/FunctionsRound.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dbms/src/Functions/FunctionsRound.h b/dbms/src/Functions/FunctionsRound.h index bdfaac0b59a..a83f87498bb 100644 --- a/dbms/src/Functions/FunctionsRound.h +++ b/dbms/src/Functions/FunctionsRound.h @@ -1191,7 +1191,7 @@ struct TiDBDecimalRound scaled_value *= PowForOutput::result[-difference]; // check overflow and construct result. - if (scaled_value > DecimalMaxValue::get(info.output_prec + carry)) + if (scaled_value > DecimalMaxValue::get(info.output_prec + carry)) throw TiFlashException("Data truncated", Errors::Decimal::Overflow); auto result = static_cast(scaled_value); From 1e3769fcb2e7a95425fa07a6af52cd5491cd429e Mon Sep 17 00:00:00 2001 From: xzhangxian1008 Date: Tue, 5 Jul 2022 11:13:00 +0800 Subject: [PATCH 03/19] update --- dbms/src/Functions/FunctionsRound.h | 30 +++++++------------ dbms/src/Functions/FunctionsString.cpp | 15 ++++++++-- .../Functions/tests/gtest_strings_format.cpp | 14 ++++++--- 3 files changed, 34 insertions(+), 25 deletions(-) diff --git a/dbms/src/Functions/FunctionsRound.h b/dbms/src/Functions/FunctionsRound.h index a83f87498bb..8b582448e66 100644 --- a/dbms/src/Functions/FunctionsRound.h +++ b/dbms/src/Functions/FunctionsRound.h @@ -1116,6 +1116,16 @@ struct TiDBDecimalRoundInfo , output_prec(getDecimalPrecision(output_type, 0)) , output_scale(getDecimalScale(output_type, 0)) {} + + TiDBDecimalRoundInfo(const FracType & input_prec_, const FracType & input_scale_, const FracType & output_prec_, const FracType & output_scale_) + : input_prec(input_prec_) + , input_scale(input_scale_) + , input_int_prec(input_prec - input_scale) + , output_prec(output_prec_) + , output_scale(output_scale_) + {} + + void setOutputScale(const FracType & output_scale_) { output_scale = output_scale_; } }; template @@ -1140,7 +1150,6 @@ struct TiDBDecimalRound // rounding. auto absolute_value = toSafeUnsigned(input.value); - auto carry = 0; if (frac < info.input_scale) { FracType frac_index = info.input_scale - frac; @@ -1153,21 +1162,7 @@ struct TiDBDecimalRound if (remainder >= base / 2) { // round up. - auto absolute_before = absolute_value; absolute_value += base; - - // check if carry occurs - auto absolute_tmp = absolute_value; - while (absolute_tmp >= 10 && absolute_before >= 10) - { - absolute_tmp /= 10; - absolute_before /= 10; - } - - if (absolute_tmp >= 10) - { - carry = 1; /// carry occurs - } } } @@ -1187,11 +1182,8 @@ struct TiDBDecimalRound auto scaled_value = static_cast(absolute_value); - if (difference < 0) - scaled_value *= PowForOutput::result[-difference]; - // check overflow and construct result. - if (scaled_value > DecimalMaxValue::get(info.output_prec + carry)) + if (scaled_value > DecimalMaxValue::get(info.output_prec)) throw TiFlashException("Data truncated", Errors::Decimal::Overflow); auto result = static_cast(scaled_value); diff --git a/dbms/src/Functions/FunctionsString.cpp b/dbms/src/Functions/FunctionsString.cpp index 7c6709c444b..0dcc1c3472b 100644 --- a/dbms/src/Functions/FunctionsString.cpp +++ b/dbms/src/Functions/FunctionsString.cpp @@ -4342,6 +4342,9 @@ class FormatImpl : public IFunction const auto & number_base_type = block.getByPosition(arguments[0]).type; const auto & precision_base_type = block.getByPosition(arguments[1]).type; + auto precision_base_col = block.getByPosition(arguments[1]).column; + auto precision_base_col_size = precision_base_col->size(); + auto col_res = ColumnString::create(); auto val_num = block.getByPosition(arguments[0]).column->size(); @@ -4350,7 +4353,14 @@ class FormatImpl : public IFunction using NumberFieldType = typename NumberType::FieldType; using NumberColVec = std::conditional_t, ColumnDecimal, ColumnVector>; const auto * number_raw = block.getByPosition(arguments[0]).column.get(); - TiDBDecimalRoundInfo info{number_type, number_type}; + + auto prec = getDecimalPrecision(number_type, 0); + auto scale = getDecimalScale(number_type, 0); + + /// output_scale is the second parameter of the 'format' function + auto output_scale = precision_base_col_size > 0 ? (*precision_base_col)[0].get() : scale; + output_scale = output_scale < 0 ? 0 : output_scale; /// Ensure output_scale >= 0 + TiDBDecimalRoundInfo info{prec, scale, prec, output_scale}; return getPrecisionType(precision_base_type, [&](const auto & precision_type, bool) { using PrecisionType = std::decay_t; @@ -4389,6 +4399,7 @@ class FormatImpl : public IFunction for (size_t i = 0; i != val_num; ++i) { size_t max_num_decimals = getMaxNumDecimals(precision_array[i]); + info.setOutputScale(max_num_decimals); format(number_array[i], max_num_decimals, info, col_res->getChars(), col_res->getOffsets()); } } @@ -4477,7 +4488,7 @@ class FormatImpl : public IFunction static std::string number2Str(T number, const TiDBDecimalRoundInfo & info [[maybe_unused]]) { if constexpr (IsDecimal) - return number.toString(info.output_scale); + return number.toString(std::min(info.input_scale, info.output_scale)); else { static_assert(std::is_floating_point_v || std::is_integral_v); diff --git a/dbms/src/Functions/tests/gtest_strings_format.cpp b/dbms/src/Functions/tests/gtest_strings_format.cpp index a2ba731fd85..8820a55c3cc 100644 --- a/dbms/src/Functions/tests/gtest_strings_format.cpp +++ b/dbms/src/Functions/tests/gtest_strings_format.cpp @@ -20,12 +20,13 @@ class StringFormat : public DB::tests::FunctionTest using FieldType = DecimalField; using NullableDecimal = Nullable; ASSERT_COLUMN_EQ( - createColumn>({"0.0000", "-0.0120", "0.0120", "12,332.1000", "12,332", "12,332", "12,332.300000000000000000000000000000", "-12,332.30000", "-1,000.0", "-333.33", {}}), + createColumn>({"0.0000", "-0.0120", "0.0120", "12,332.1000", "12,332", "12,332", "12,332.300000000000000000000000000000", "-12,332.30000", "-1,000.0", "-333.33", {}, "99,999.9999000000", "100,000.000", "100,000"}), executeFunction( func_name, createColumn( std::make_tuple(precision, 4), - {FieldType(static_cast(0), 4), + { + FieldType(static_cast(0), 4), FieldType(static_cast(-120), 4), FieldType(static_cast(120), 4), FieldType(static_cast(123321000), 4), @@ -35,8 +36,13 @@ class StringFormat : public DB::tests::FunctionTest FieldType(static_cast(-123323000), 4), FieldType(static_cast(-9999999), 4), FieldType(static_cast(-3333330), 4), - FieldType(static_cast(0), 0)}), - createColumn>({4, 4, 4, 4, 0, -1, 31, 5, 1, 2, {}}))); + FieldType(static_cast(0), 0), + FieldType(static_cast(999999999), 4), + FieldType(static_cast(999999999), 4), + FieldType(static_cast(999999999), 4) + }), + createColumn>({4, 4, 4, 4, 0, -1, 31, 5, 1, 2, {}, 10, 3, -5}) + )); ASSERT_COLUMN_EQ( createColumn>({"12,332.100", "-12,332.300", "-1,000.000", "-333.333"}), executeFunction( From b6025891e22ff12739cc1585b7a2cadc17906155 Mon Sep 17 00:00:00 2001 From: xzhangxian1008 Date: Tue, 5 Jul 2022 11:15:52 +0800 Subject: [PATCH 04/19] format --- dbms/src/Functions/tests/gtest_strings_format.cpp | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/dbms/src/Functions/tests/gtest_strings_format.cpp b/dbms/src/Functions/tests/gtest_strings_format.cpp index 8820a55c3cc..81fd088d94b 100644 --- a/dbms/src/Functions/tests/gtest_strings_format.cpp +++ b/dbms/src/Functions/tests/gtest_strings_format.cpp @@ -25,8 +25,7 @@ class StringFormat : public DB::tests::FunctionTest func_name, createColumn( std::make_tuple(precision, 4), - { - FieldType(static_cast(0), 4), + {FieldType(static_cast(0), 4), FieldType(static_cast(-120), 4), FieldType(static_cast(120), 4), FieldType(static_cast(123321000), 4), @@ -39,10 +38,8 @@ class StringFormat : public DB::tests::FunctionTest FieldType(static_cast(0), 0), FieldType(static_cast(999999999), 4), FieldType(static_cast(999999999), 4), - FieldType(static_cast(999999999), 4) - }), - createColumn>({4, 4, 4, 4, 0, -1, 31, 5, 1, 2, {}, 10, 3, -5}) - )); + FieldType(static_cast(999999999), 4)}), + createColumn>({4, 4, 4, 4, 0, -1, 31, 5, 1, 2, {}, 10, 3, -5}))); ASSERT_COLUMN_EQ( createColumn>({"12,332.100", "-12,332.300", "-1,000.000", "-333.333"}), executeFunction( From d91dcca7109a9b56cff6d0838752bec7587cf43b Mon Sep 17 00:00:00 2001 From: xzhangxian1008 Date: Tue, 5 Jul 2022 15:14:47 +0800 Subject: [PATCH 05/19] update --- dbms/src/Functions/FunctionsRound.h | 12 +++++++----- dbms/src/Functions/FunctionsString.cpp | 18 +++++++----------- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/dbms/src/Functions/FunctionsRound.h b/dbms/src/Functions/FunctionsRound.h index 8b582448e66..c86755ee607 100644 --- a/dbms/src/Functions/FunctionsRound.h +++ b/dbms/src/Functions/FunctionsRound.h @@ -1117,15 +1117,17 @@ struct TiDBDecimalRoundInfo , output_scale(getDecimalScale(output_type, 0)) {} - TiDBDecimalRoundInfo(const FracType & input_prec_, const FracType & input_scale_, const FracType & output_prec_, const FracType & output_scale_) + TiDBDecimalRoundInfo(const FracType & input_prec_, const FracType & input_scale_) : input_prec(input_prec_) , input_scale(input_scale_) - , input_int_prec(input_prec - input_scale) - , output_prec(output_prec_) - , output_scale(output_scale_) + , input_int_prec(input_prec_ - input_scale_) {} - void setOutputScale(const FracType & output_scale_) { output_scale = output_scale_; } + void setOutputPrecAndScale(const FracType & output_prec_, const FracType & output_scale_) + { + output_prec = output_prec_; + output_scale = output_scale_; + } }; template diff --git a/dbms/src/Functions/FunctionsString.cpp b/dbms/src/Functions/FunctionsString.cpp index 0dcc1c3472b..a4ae8ab134e 100644 --- a/dbms/src/Functions/FunctionsString.cpp +++ b/dbms/src/Functions/FunctionsString.cpp @@ -4342,9 +4342,6 @@ class FormatImpl : public IFunction const auto & number_base_type = block.getByPosition(arguments[0]).type; const auto & precision_base_type = block.getByPosition(arguments[1]).type; - auto precision_base_col = block.getByPosition(arguments[1]).column; - auto precision_base_col_size = precision_base_col->size(); - auto col_res = ColumnString::create(); auto val_num = block.getByPosition(arguments[0]).column->size(); @@ -4354,14 +4351,11 @@ class FormatImpl : public IFunction using NumberColVec = std::conditional_t, ColumnDecimal, ColumnVector>; const auto * number_raw = block.getByPosition(arguments[0]).column.get(); - auto prec = getDecimalPrecision(number_type, 0); - auto scale = getDecimalScale(number_type, 0); - - /// output_scale is the second parameter of the 'format' function - auto output_scale = precision_base_col_size > 0 ? (*precision_base_col)[0].get() : scale; - output_scale = output_scale < 0 ? 0 : output_scale; /// Ensure output_scale >= 0 - TiDBDecimalRoundInfo info{prec, scale, prec, output_scale}; + auto input_prec = getDecimalPrecision(number_type, 0); + auto input_scale = getDecimalScale(number_type, 0); + TiDBDecimalRoundInfo info{input_prec, input_scale}; + return getPrecisionType(precision_base_type, [&](const auto & precision_type, bool) { using PrecisionType = std::decay_t; using PrecisionFieldType = typename PrecisionType::FieldType; @@ -4378,6 +4372,7 @@ class FormatImpl : public IFunction for (size_t i = 0; i != val_num; ++i) { size_t max_num_decimals = getMaxNumDecimals(precision_array[i]); + info.setOutputPrecAndScale(input_prec, max_num_decimals); format(const_number, max_num_decimals, info, col_res->getChars(), col_res->getOffsets()); } } @@ -4389,6 +4384,7 @@ class FormatImpl : public IFunction if (const auto * col1_const = checkAndGetColumnConst(precision_raw)) { size_t max_num_decimals = getMaxNumDecimals(col1_const->template getValue()); + info.setOutputPrecAndScale(input_prec, max_num_decimals); for (const auto & number : col0_column->getData()) format(number, max_num_decimals, info, col_res->getChars(), col_res->getOffsets()); } @@ -4399,7 +4395,7 @@ class FormatImpl : public IFunction for (size_t i = 0; i != val_num; ++i) { size_t max_num_decimals = getMaxNumDecimals(precision_array[i]); - info.setOutputScale(max_num_decimals); + info.setOutputPrecAndScale(input_prec, max_num_decimals); format(number_array[i], max_num_decimals, info, col_res->getChars(), col_res->getOffsets()); } } From 6ee59864359f7deea794d9245a20894abc191150 Mon Sep 17 00:00:00 2001 From: xzhangxian1008 Date: Tue, 5 Jul 2022 15:15:38 +0800 Subject: [PATCH 06/19] format --- dbms/src/Functions/FunctionsString.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dbms/src/Functions/FunctionsString.cpp b/dbms/src/Functions/FunctionsString.cpp index a4ae8ab134e..bb28bb81d36 100644 --- a/dbms/src/Functions/FunctionsString.cpp +++ b/dbms/src/Functions/FunctionsString.cpp @@ -4355,7 +4355,7 @@ class FormatImpl : public IFunction auto input_scale = getDecimalScale(number_type, 0); TiDBDecimalRoundInfo info{input_prec, input_scale}; - + return getPrecisionType(precision_base_type, [&](const auto & precision_type, bool) { using PrecisionType = std::decay_t; using PrecisionFieldType = typename PrecisionType::FieldType; From af0711a2e78a02d257771191eb2c90766782ba8a Mon Sep 17 00:00:00 2001 From: xzhangxian1008 Date: Fri, 8 Jul 2022 10:01:29 +0800 Subject: [PATCH 07/19] enable ut --- dbms/src/Functions/tests/gtest_strings_format.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dbms/src/Functions/tests/gtest_strings_format.cpp b/dbms/src/Functions/tests/gtest_strings_format.cpp index 81fd088d94b..3346af852e0 100644 --- a/dbms/src/Functions/tests/gtest_strings_format.cpp +++ b/dbms/src/Functions/tests/gtest_strings_format.cpp @@ -61,7 +61,7 @@ class StringFormat : public DB::tests::FunctionTest FieldType(static_cast(-9999999), 4)), createColumn>({4, 0, -1, 31, 5, 1, 2}))); ASSERT_COLUMN_EQ( - createConstColumn>(1, "-1,000.000"), + createConstColumn(1, "-1,000.000"), executeFunction( func_name, createConstColumn( @@ -104,7 +104,7 @@ class StringFormat : public DB::tests::FunctionTest FieldType(static_cast(-9999999), 4)), createColumn>({4, 0, 31, 5, 1, 2}))); ASSERT_COLUMN_EQ( - createConstColumn>(1, "-1,000.000"), + createConstColumn(1, "-1,000.000"), executeFunction( func_name, createConstColumn( From 8613092079df38c910221709d8bf49af464907f1 Mon Sep 17 00:00:00 2001 From: xzhangxian1008 Date: Sat, 9 Jul 2022 17:06:52 +0800 Subject: [PATCH 08/19] update --- dbms/src/Functions/FunctionsRound.h | 15 +++------------ dbms/src/Functions/FunctionsString.cpp | 12 ++++-------- 2 files changed, 7 insertions(+), 20 deletions(-) diff --git a/dbms/src/Functions/FunctionsRound.h b/dbms/src/Functions/FunctionsRound.h index c86755ee607..7a587ffee5b 100644 --- a/dbms/src/Functions/FunctionsRound.h +++ b/dbms/src/Functions/FunctionsRound.h @@ -1116,18 +1116,6 @@ struct TiDBDecimalRoundInfo , output_prec(getDecimalPrecision(output_type, 0)) , output_scale(getDecimalScale(output_type, 0)) {} - - TiDBDecimalRoundInfo(const FracType & input_prec_, const FracType & input_scale_) - : input_prec(input_prec_) - , input_scale(input_scale_) - , input_int_prec(input_prec_ - input_scale_) - {} - - void setOutputPrecAndScale(const FracType & output_prec_, const FracType & output_scale_) - { - output_prec = output_prec_; - output_scale = output_scale_; - } }; template @@ -1184,6 +1172,9 @@ struct TiDBDecimalRound auto scaled_value = static_cast(absolute_value); + if (difference < 0) + scaled_value *= PowForOutput::result[-difference]; + // check overflow and construct result. if (scaled_value > DecimalMaxValue::get(info.output_prec)) throw TiFlashException("Data truncated", Errors::Decimal::Overflow); diff --git a/dbms/src/Functions/FunctionsString.cpp b/dbms/src/Functions/FunctionsString.cpp index bb28bb81d36..bd636982fc3 100644 --- a/dbms/src/Functions/FunctionsString.cpp +++ b/dbms/src/Functions/FunctionsString.cpp @@ -4351,10 +4351,8 @@ class FormatImpl : public IFunction using NumberColVec = std::conditional_t, ColumnDecimal, ColumnVector>; const auto * number_raw = block.getByPosition(arguments[0]).column.get(); - auto input_prec = getDecimalPrecision(number_type, 0); - auto input_scale = getDecimalScale(number_type, 0); - - TiDBDecimalRoundInfo info{input_prec, input_scale}; + TiDBDecimalRoundInfo info{number_type, number_type}; + info.output_prec = info.output_prec < 65 ? info.output_prec + 1 : 65; return getPrecisionType(precision_base_type, [&](const auto & precision_type, bool) { using PrecisionType = std::decay_t; @@ -4372,7 +4370,6 @@ class FormatImpl : public IFunction for (size_t i = 0; i != val_num; ++i) { size_t max_num_decimals = getMaxNumDecimals(precision_array[i]); - info.setOutputPrecAndScale(input_prec, max_num_decimals); format(const_number, max_num_decimals, info, col_res->getChars(), col_res->getOffsets()); } } @@ -4384,7 +4381,6 @@ class FormatImpl : public IFunction if (const auto * col1_const = checkAndGetColumnConst(precision_raw)) { size_t max_num_decimals = getMaxNumDecimals(col1_const->template getValue()); - info.setOutputPrecAndScale(input_prec, max_num_decimals); for (const auto & number : col0_column->getData()) format(number, max_num_decimals, info, col_res->getChars(), col_res->getOffsets()); } @@ -4395,7 +4391,6 @@ class FormatImpl : public IFunction for (size_t i = 0; i != val_num; ++i) { size_t max_num_decimals = getMaxNumDecimals(precision_array[i]); - info.setOutputPrecAndScale(input_prec, max_num_decimals); format(number_array[i], max_num_decimals, info, col_res->getChars(), col_res->getOffsets()); } } @@ -4507,10 +4502,11 @@ class FormatImpl : public IFunction static void format( T number, size_t max_num_decimals, - const TiDBDecimalRoundInfo & info, + TiDBDecimalRoundInfo info, ColumnString::Chars_t & res_data, ColumnString::Offsets & res_offsets) { + info.output_scale = std::min(max_num_decimals, static_cast(info.input_scale)); auto round_number = round(number, max_num_decimals, info); std::string round_number_str = number2Str(round_number, info); std::string buffer = Format::apply(round_number_str, max_num_decimals); From 832e2bc892433c0a6e1e3e6eb171aa1ddc4fb7af Mon Sep 17 00:00:00 2001 From: xzhangxian1008 Date: Sat, 9 Jul 2022 17:19:24 +0800 Subject: [PATCH 09/19] update --- dbms/src/Functions/FunctionsString.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dbms/src/Functions/FunctionsString.cpp b/dbms/src/Functions/FunctionsString.cpp index bd636982fc3..9e8251acd63 100644 --- a/dbms/src/Functions/FunctionsString.cpp +++ b/dbms/src/Functions/FunctionsString.cpp @@ -4479,7 +4479,7 @@ class FormatImpl : public IFunction static std::string number2Str(T number, const TiDBDecimalRoundInfo & info [[maybe_unused]]) { if constexpr (IsDecimal) - return number.toString(std::min(info.input_scale, info.output_scale)); + return number.toString(info.output_scale); else { static_assert(std::is_floating_point_v || std::is_integral_v); From 08e004c60168fc287b60f92b8c82624c2b8a137c Mon Sep 17 00:00:00 2001 From: xzhangxian1008 Date: Mon, 11 Jul 2022 11:39:17 +0800 Subject: [PATCH 10/19] udpate --- dbms/src/Functions/FunctionsString.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dbms/src/Functions/FunctionsString.cpp b/dbms/src/Functions/FunctionsString.cpp index 9e8251acd63..84e9de61f0a 100644 --- a/dbms/src/Functions/FunctionsString.cpp +++ b/dbms/src/Functions/FunctionsString.cpp @@ -4502,7 +4502,7 @@ class FormatImpl : public IFunction static void format( T number, size_t max_num_decimals, - TiDBDecimalRoundInfo info, + TiDBDecimalRoundInfo & info, ColumnString::Chars_t & res_data, ColumnString::Offsets & res_offsets) { From a343c2aeb1c6500056b52aea53026a5f0451fe5a Mon Sep 17 00:00:00 2001 From: xzhangxian1008 Date: Mon, 11 Jul 2022 14:05:06 +0800 Subject: [PATCH 11/19] add fullstack test --- tests/fullstack-test/expr/format.test | 49 +++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/tests/fullstack-test/expr/format.test b/tests/fullstack-test/expr/format.test index 7b46916b345..2c30edb492f 100644 --- a/tests/fullstack-test/expr/format.test +++ b/tests/fullstack-test/expr/format.test @@ -30,3 +30,52 @@ int_val 1,234.000 mysql> drop table if exists test.t + +mysql> create table test.t(id int, value decimal(65,4)) +mysql> alter table test.t set tiflash replica 1 +mysql> insert into test.t values(1,9999999999999999999999999999999999999999999999999999999999999.9999) + +func> wait_table test t + +mysql> set tidb_enforce_mpp=1; set tidb_isolation_read_engines='tiflash'; select format(value,-3) as result from test.t +result +10,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000 + +mysql> set tidb_enforce_mpp=1; set tidb_isolation_read_engines='tiflash'; select format(value,0) as result from test.t +result +10,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000 + +mysql> set tidb_enforce_mpp=1; set tidb_isolation_read_engines='tiflash'; select format(value,3) as result from test.t +result +10,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000.000 + +mysql> set tidb_enforce_mpp=1; set tidb_isolation_read_engines='tiflash'; select format(value,10) as result from test.t +result +9,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999.9999000000 + + +mysql> drop table if exists test.t + +mysql> create table test.t(id int, value decimal(7,4)) +mysql> alter table test.t set tiflash replica 1 +mysql> insert into test.t values(1,999.9999) + +func> wait_table test t + +mysql> set tidb_enforce_mpp=1; set tidb_isolation_read_engines='tiflash'; select format(value,-2) as result from test.t +result +1,000 + +mysql> set tidb_enforce_mpp=1; set tidb_isolation_read_engines='tiflash'; select format(value,0) as result from test.t +result +1,000 + +mysql> set tidb_enforce_mpp=1; set tidb_isolation_read_engines='tiflash'; select format(value,2) as result from test.t +result +1,000.00 + +mysql> set tidb_enforce_mpp=1; set tidb_isolation_read_engines='tiflash'; select format(value,10) as result from test.t +result +999.9999000000 + +mysql> drop table if exists test.t From 4dadfcaa9cabe745b077891c88898dfeabdf4c11 Mon Sep 17 00:00:00 2001 From: xzhangxian1008 Date: Thu, 14 Jul 2022 11:24:40 +0800 Subject: [PATCH 12/19] clang-tidy --- dbms/src/Functions/FunctionsString.cpp | 210 +++++++++--------- dbms/src/Functions/FunctionsString.h | 8 +- .../Functions/tests/gtest_strings_format.cpp | 4 +- 3 files changed, 111 insertions(+), 111 deletions(-) diff --git a/dbms/src/Functions/FunctionsString.cpp b/dbms/src/Functions/FunctionsString.cpp index 84e9de61f0a..94a97286c43 100644 --- a/dbms/src/Functions/FunctionsString.cpp +++ b/dbms/src/Functions/FunctionsString.cpp @@ -53,12 +53,12 @@ struct EmptyImpl } /// Only make sense if is_fixed_to_constant. - static void vector_fixed_to_constant(const ColumnString::Chars_t & /*data*/, size_t /*n*/, UInt8 & /*res*/) + static void vectorFixedToConstant(const ColumnString::Chars_t & /*data*/, size_t /*n*/, UInt8 & /*res*/) { - throw Exception("Logical error: 'vector_fixed_to_constant method' is called", ErrorCodes::LOGICAL_ERROR); + throw Exception("Logical error: 'vectorFixedToConstant method' is called", ErrorCodes::LOGICAL_ERROR); } - static void vector_fixed_to_vector(const ColumnString::Chars_t & data, size_t n, PaddedPODArray & res) + static void vectorFixedToVector(const ColumnString::Chars_t & data, size_t n, PaddedPODArray & res) { std::vector empty_chars(n); size_t size = data.size() / n; @@ -93,12 +93,12 @@ struct LengthImpl res[i] = i == 0 ? (offsets[i] - 1) : (offsets[i] - 1 - offsets[i - 1]); } - static void vector_fixed_to_constant(const ColumnString::Chars_t & /*data*/, size_t n, UInt64 & res) + static void vectorFixedToConstant(const ColumnString::Chars_t & /*data*/, size_t n, UInt64 & res) { res = n; } - static void vector_fixed_to_vector(const ColumnString::Chars_t & /*data*/, size_t /*n*/, PaddedPODArray & /*res*/) + static void vectorFixedToVector(const ColumnString::Chars_t & /*data*/, size_t /*n*/, PaddedPODArray & /*res*/) { } @@ -132,11 +132,11 @@ struct LengthUTF8Impl } } - static void vector_fixed_to_constant(const ColumnString::Chars_t & /*data*/, size_t /*n*/, UInt64 & /*res*/) + static void vectorFixedToConstant(const ColumnString::Chars_t & /*data*/, size_t /*n*/, UInt64 & /*res*/) { } - static void vector_fixed_to_vector(const ColumnString::Chars_t & data, size_t n, PaddedPODArray & res) + static void vectorFixedToVector(const ColumnString::Chars_t & data, size_t n, PaddedPODArray & res) { size_t size = data.size() / n; @@ -210,7 +210,7 @@ struct LowerUpperImpl array(data.data(), data.data() + data.size(), res_data.data()); } - static void vector_fixed(const ColumnString::Chars_t & data, size_t /*n*/, ColumnString::Chars_t & res_data) + static void vectorFixed(const ColumnString::Chars_t & data, size_t /*n*/, ColumnString::Chars_t & res_data) { res_data.resize(data.size()); array(data.data(), data.data() + data.size(), res_data.data()); @@ -247,7 +247,7 @@ struct ReverseImpl } } - static void vector_fixed(const ColumnString::Chars_t & data, size_t n, ColumnString::Chars_t & res_data) + static void vectorFixed(const ColumnString::Chars_t & data, size_t n, ColumnString::Chars_t & res_data) { res_data.resize(data.size()); size_t size = data.size() / n; @@ -307,7 +307,7 @@ struct ReverseUTF8Impl } } - static void vector_fixed(const ColumnString::Chars_t &, size_t, ColumnString::Chars_t &) + static void vectorFixed(const ColumnString::Chars_t &, size_t, ColumnString::Chars_t &) { throw Exception("Cannot apply function reverseUTF8 to fixed string.", ErrorCodes::ILLEGAL_COLUMN); } @@ -333,7 +333,7 @@ template -void LowerUpperUTF8Impl::vector_fixed( +void LowerUpperUTF8Impl::vectorFixed( const ColumnString::Chars_t & data, size_t /*n*/, ColumnString::Chars_t & res_data) @@ -615,7 +615,7 @@ void TiDBLowerUpperUTF8Impl template -void TiDBLowerUpperUTF8Impl::vector_fixed( +void TiDBLowerUpperUTF8Impl::vectorFixed( const ColumnString::Chars_t & data, size_t /*n*/, ColumnString::Chars_t & res_data) @@ -813,7 +813,7 @@ struct RightUTF8Impl else current += 1; } - if (start_offsets.size() == 0) + if (start_offsets.empty()) { // null res_data.resize(res_data.size() + 1); @@ -889,7 +889,7 @@ class FunctionStringOrArrayToT : public IFunction if (Impl::is_fixed_to_constant) { ResultType res = 0; - Impl::vector_fixed_to_constant(col->getChars(), col->getN(), res); + Impl::vectorFixedToConstant(col->getChars(), col->getN(), res); block.getByPosition(result).column = block.getByPosition(result).type->createColumnConst(col->size(), toField(res)); } @@ -899,7 +899,7 @@ class FunctionStringOrArrayToT : public IFunction typename ColumnVector::Container & vec_res = col_res->getData(); vec_res.resize(col->size()); - Impl::vector_fixed_to_vector(col->getChars(), col->getN(), vec_res); + Impl::vectorFixedToVector(col->getChars(), col->getN(), vec_res); block.getByPosition(result).column = std::move(col_res); } @@ -972,7 +972,7 @@ class FunctionReverse : public IFunction else if (const ColumnFixedString * col = checkAndGetColumn(column.get())) { auto col_res = ColumnFixedString::create(col->getN()); - ReverseImpl::vector_fixed(col->getChars(), col->getN(), col_res->getChars()); + ReverseImpl::vectorFixed(col->getChars(), col->getN(), col_res->getChars()); block.getByPosition(result).column = std::move(col_res); } else if (checkColumn(column.get())) @@ -1043,7 +1043,7 @@ class ConcatImpl : public IFunction { public: static constexpr auto name = Name::name; - ConcatImpl(const Context & context) + explicit ConcatImpl(const Context & context) : context(context) {} static FunctionPtr create(const Context & context) @@ -1085,7 +1085,7 @@ class ConcatImpl : public IFunction for (const auto arg_idx : ext::range(0, arguments.size())) { - const auto arg = arguments[arg_idx].get(); + const auto * const arg = arguments[arg_idx].get(); if (!arg->isStringOrFixedString()) throw Exception{ "Illegal type " + arg->getName() + " of argument " + std::to_string(arg_idx + 1) + " of function " + getName(), @@ -1167,7 +1167,7 @@ class FunctionTiDBConcat : public IFunction public: static constexpr auto name = NameTiDBConcat::name; - FunctionTiDBConcat(const Context & context) + explicit FunctionTiDBConcat(const Context & context) : context(context) {} static FunctionPtr create(const Context & context) @@ -1184,7 +1184,7 @@ class FunctionTiDBConcat : public IFunction DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override { - if (arguments.size() < 1) + if (arguments.empty()) throw Exception("Number of arguments for function " + getName() + " doesn't match: passed " + toString(arguments.size()) + ", should be at least 1.", ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH); @@ -1539,7 +1539,7 @@ class FunctionSubstringUTF8 : public IFunction UInt64 u_start = start_field.get(); if (u_start >= 0x8000000000000000ULL) throw Exception("Too large values of 2nd argument provided for function substring.", ErrorCodes::ARGUMENT_OUT_OF_BOUND); - start = (Int64)u_start; + start = static_cast(u_start); } bool implicit_length = true; @@ -1658,7 +1658,7 @@ class FunctionRightUTF8 : public IFunction UInt64 u_start = length_field.get(); if (u_start >= 0x8000000000000000ULL) throw Exception("Too large values of 2nd argument provided for function substring.", ErrorCodes::ARGUMENT_OUT_OF_BOUND); - length = (Int64)u_start; + length = static_cast(u_start); } if (length <= 0) @@ -1733,7 +1733,7 @@ class FunctionAppendTrailingCharIfAbsent : public IFunction if (trailing_char_str.size() != 1) throw Exception{"Second argument of function " + getName() + " must be a one-character string", ErrorCodes::BAD_ARGUMENTS}; - if (const auto col = checkAndGetColumn(column.get())) + if (const auto * const col = checkAndGetColumn(column.get())) { auto col_res = ColumnString::create(); @@ -1757,7 +1757,7 @@ class FunctionAppendTrailingCharIfAbsent : public IFunction src_offset = src_offsets[i]; dst_offset += src_length; - if (src_length > 1 && dst_data[dst_offset - 2] != trailing_char_str.front()) + if (src_length > 1 && dst_data[dst_offset - 2] != static_cast(trailing_char_str.front())) { dst_data[dst_offset - 1] = trailing_char_str.front(); dst_data[dst_offset] = 0; @@ -1783,7 +1783,7 @@ class TrimImpl : public IFunction { public: static constexpr auto name = Name::name; - explicit TrimImpl() {} + explicit TrimImpl() = default; static FunctionPtr create(const Context &) { return std::make_shared(); @@ -1810,7 +1810,7 @@ class TrimImpl : public IFunction for (const auto arg_idx : ext::range(0, arguments.size())) { - const auto arg = arguments[arg_idx].get(); + const auto * const arg = arguments[arg_idx].get(); if (!arg->isStringOrFixedString()) throw Exception{ "Illegal type " + arg->getName() + " of argument " + std::to_string(arg_idx + 1) + " of function " + getName(), @@ -1884,7 +1884,7 @@ class TrimUTF8Impl : public IFunction { public: static constexpr auto name = Name::name; - explicit TrimUTF8Impl() {} + explicit TrimUTF8Impl() = default; static FunctionPtr create(const Context &) { return std::make_shared(); @@ -1912,7 +1912,7 @@ class TrimUTF8Impl : public IFunction for (const auto arg_idx : ext::range(0, arguments.size())) { - const auto arg = arguments[arg_idx].get(); + const auto * const arg = arguments[arg_idx].get(); if (!arg->isStringOrFixedString()) throw Exception{ "Illegal type " + arg->getName() + " of argument " + std::to_string(arg_idx + 1) + " of function " @@ -1949,7 +1949,7 @@ class TrimUTF8Impl : public IFunction vector(c0_string->getChars(), c0_string->getOffsets(), c_res->getChars(), c_res->getOffsets()); else if (c0_const_string) { - auto c0_c_string = checkAndGetColumn(c0_const_string->getDataColumnPtr().get()); + const auto * c0_c_string = checkAndGetColumn(c0_const_string->getDataColumnPtr().get()); vector(c0_c_string->getChars(), c0_c_string->getOffsets(), c0_const_string->size(), c_res->getChars(), c_res->getOffsets()); } else @@ -1966,7 +1966,7 @@ class TrimUTF8Impl : public IFunction const ColumnString * c0_string = checkAndGetColumn(c0); const ColumnConst * c0_const_string = checkAndGetColumnConst(c0); const ColumnConst * c1_const_string = checkAndGetColumnConst(c1); - auto column_trim_string = checkAndGetColumn(c1_const_string->getDataColumnPtr().get()); + const auto * column_trim_string = checkAndGetColumn(c1_const_string->getDataColumnPtr().get()); auto c_res = ColumnString::create(); @@ -1974,7 +1974,7 @@ class TrimUTF8Impl : public IFunction vectorWS(c0_string->getChars(), c0_string->getOffsets(), column_trim_string->getChars(), column_trim_string->getOffsets(), c_res->getChars(), c_res->getOffsets()); else if (c0_const_string) { - auto c0_c_string = checkAndGetColumn(c0_const_string->getDataColumnPtr().get()); + const auto * c0_c_string = checkAndGetColumn(c0_const_string->getDataColumnPtr().get()); vectorWS(c0_c_string->getChars(), c0_c_string->getOffsets(), c0_const_string->size(), column_trim_string->getChars(), column_trim_string->getOffsets(), c_res->getChars(), c_res->getOffsets()); } else @@ -2415,7 +2415,7 @@ class FunctionTiDBTrim : public IFunction ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH); for (const auto arg_idx : ext::range(0, arguments.size())) { - const auto arg = arguments[arg_idx].get(); + const auto * const arg = arguments[arg_idx].get(); if (arg_idx < 2 && !arg->isString()) throw Exception{ "Illegal type " + arg->getName() + " of argument " + std::to_string(arg_idx + 1) + " of function " @@ -2461,7 +2461,7 @@ class FunctionTiDBTrim : public IFunction const ColumnString * data_col = checkAndGetColumn(column_data.get()); static const std::string default_rem = " "; - vectorConst(ltrim, rtrim, data_col->getChars(), data_col->getOffsets(), (UInt8 *)default_rem.c_str(), default_rem.size() + 1, res_col->getChars(), res_col->getOffsets()); + vectorConst(ltrim, rtrim, data_col->getChars(), data_col->getOffsets(), reinterpret_cast(default_rem.c_str()), default_rem.size() + 1, res_col->getChars(), res_col->getOffsets()); block.getByPosition(result).column = std::move(res_col); } @@ -2482,7 +2482,7 @@ class FunctionTiDBTrim : public IFunction const ColumnString * remstr_col = checkAndGetColumn(column_remstr.get()); std::string data = data_col->getValue(); - constVector(is_ltrim, is_rtrim, (UInt8 *)data.c_str(), data.size() + 1, remstr_col->getChars(), remstr_col->getOffsets(), res_col->getChars(), res_col->getOffsets()); + constVector(is_ltrim, is_rtrim, reinterpret_cast(data.c_str()), data.size() + 1, remstr_col->getChars(), remstr_col->getOffsets(), res_col->getChars(), res_col->getOffsets()); } else if (remstr_const && !data_const) { @@ -2490,7 +2490,7 @@ class FunctionTiDBTrim : public IFunction const ColumnString * data_col = checkAndGetColumn(column_data.get()); std::string remstr = remstr_col->getValue(); - vectorConst(is_ltrim, is_rtrim, data_col->getChars(), data_col->getOffsets(), (UInt8 *)remstr.c_str(), remstr.size() + 1, res_col->getChars(), res_col->getOffsets()); + vectorConst(is_ltrim, is_rtrim, data_col->getChars(), data_col->getOffsets(), reinterpret_cast(remstr.c_str()), remstr.size() + 1, res_col->getChars(), res_col->getOffsets()); } else { @@ -2542,7 +2542,7 @@ class FunctionTiDBTrim : public IFunction { const UInt8 * left = data_begin; const UInt8 * right = data_begin + data_size - 1; - const long remstr_real_size = remstr_size - 1; + const Int64 remstr_real_size = remstr_size - 1; if (remstr_real_size > 0 && is_ltrim) { @@ -2576,7 +2576,7 @@ class FunctionTiDBTrim : public IFunction bool is_rtrim, const ColumnString::Chars_t & data, const ColumnString::Offsets & offsets, - UInt8 * remstr, + const UInt8 * remstr, size_t remstr_size, ColumnString::Chars_t & res_data, ColumnString::Offsets & res_offsets) @@ -2599,7 +2599,7 @@ class FunctionTiDBTrim : public IFunction static void constVector( bool is_ltrim, bool is_rtrim, - UInt8 * data, + const UInt8 * data, size_t data_size, const ColumnString::Chars_t & remstr, const ColumnString::Offsets & remstr_offsets, @@ -2660,7 +2660,7 @@ class FunctionTiDBTrim : public IFunction } }; -class tidbPadImpl +class TidbPadImpl { public: template @@ -2773,11 +2773,11 @@ class tidbPadImpl if (is_padding_const && is_length_const) { - const_const(str_val, column_length, target_len, padding, padding_size, vec_result_null_map, size, result_data, result_offsets); + constConst(str_val, column_length, target_len, padding, padding_size, vec_result_null_map, size, result_data, result_offsets); } else if (is_padding_const && !is_length_const) { - const_const(str_val, column_length, target_len, padding, padding_size, vec_result_null_map, size, result_data, result_offsets); + constConst(str_val, column_length, target_len, padding, padding_size, vec_result_null_map, size, result_data, result_offsets); } else if (!is_padding_const && is_length_const) { @@ -2796,26 +2796,26 @@ class tidbPadImpl if (is_padding_const && is_length_const) { - column_const(string_data, string_offsets, column_length, target_len, padding, padding_size, vec_result_null_map, size, result_data, result_offsets); + columnConst(string_data, string_offsets, column_length, target_len, padding, padding_size, vec_result_null_map, size, result_data, result_offsets); } else if (is_padding_const && !is_length_const) { - column_const(string_data, string_offsets, column_length, target_len, padding, padding_size, vec_result_null_map, size, result_data, result_offsets); + columnConst(string_data, string_offsets, column_length, target_len, padding, padding_size, vec_result_null_map, size, result_data, result_offsets); } else if (!is_padding_const && is_length_const) { - column_column(string_data, string_offsets, column_length, target_len, padding_data, padding_offsets, vec_result_null_map, size, result_data, result_offsets); + columnColumn(string_data, string_offsets, column_length, target_len, padding_data, padding_offsets, vec_result_null_map, size, result_data, result_offsets); } else if (!is_padding_const && !is_length_const) { - column_column(string_data, string_offsets, column_length, target_len, padding_data, padding_offsets, vec_result_null_map, size, result_data, result_offsets); + columnColumn(string_data, string_offsets, column_length, target_len, padding_data, padding_offsets, vec_result_null_map, size, result_data, result_offsets); } } block.getByPosition(result).column = ColumnNullable::create(std::move(column_result), std::move(result_null_map)); } template - static void column_column(const ColumnString::Chars_t & string_data, const ColumnString::Offsets & string_offsets, const ColumnVector * column_length [[maybe_unused]], IntType target_len, const ColumnString::Chars_t * padding_data, const ColumnString::Offsets * padding_offsets, ColumnUInt8::Container & vec_result_null_map, size_t size, ColumnString::Chars_t & result_data, ColumnString::Offsets & result_offsets) + static void columnColumn(const ColumnString::Chars_t & string_data, const ColumnString::Offsets & string_offsets, const ColumnVector * column_length [[maybe_unused]], IntType target_len, const ColumnString::Chars_t * padding_data, const ColumnString::Offsets * padding_offsets, ColumnUInt8::Container & vec_result_null_map, size_t size, ColumnString::Chars_t & result_data, ColumnString::Offsets & result_offsets) { ColumnString::Offset string_prev_offset = 0; ColumnString::Offset padding_prev_offset = 0; @@ -2853,7 +2853,7 @@ class tidbPadImpl } template - static void column_const(const ColumnString::Chars_t & string_data, const ColumnString::Offsets & string_offsets, const ColumnVector * column_length [[maybe_unused]], IntType target_len, const UInt8 * padding, size_t padding_size, ColumnUInt8::Container & vec_result_null_map, size_t size, ColumnString::Chars_t & result_data, ColumnString::Offsets & result_offsets) + static void columnConst(const ColumnString::Chars_t & string_data, const ColumnString::Offsets & string_offsets, const ColumnVector * column_length [[maybe_unused]], IntType target_len, const UInt8 * padding, size_t padding_size, ColumnUInt8::Container & vec_result_null_map, size_t size, ColumnString::Chars_t & result_data, ColumnString::Offsets & result_offsets) { ColumnString::Offset string_prev_offset = 0; ColumnString::Offset res_prev_offset = 0; @@ -2925,7 +2925,7 @@ class tidbPadImpl } template - static void const_const(const String & str_val, const ColumnVector * column_length [[maybe_unused]], IntType target_len, const UInt8 * padding, size_t padding_size, ColumnUInt8::Container & vec_result_null_map, size_t size, ColumnString::Chars_t & result_data, ColumnString::Offsets & result_offsets) + static void constConst(const String & str_val, const ColumnVector * column_length [[maybe_unused]], IntType target_len, const UInt8 * padding, size_t padding_size, ColumnUInt8::Container & vec_result_null_map, size_t size, ColumnString::Chars_t & result_data, ColumnString::Offsets & result_offsets) { ColumnString::Offset res_prev_offset = 0; @@ -3107,7 +3107,7 @@ class PadImpl : public IFunction { public: static constexpr auto name = Name::name; - explicit PadImpl() {} + explicit PadImpl() = default; static FunctionPtr create(const Context &) { return std::make_shared(); @@ -3209,28 +3209,28 @@ class PadImpl : public IFunction switch (type_index) { case TypeIndex::UInt8: - tidbPadImpl::tidbExecutePadImpl(block, arguments, result, getName()); + TidbPadImpl::tidbExecutePadImpl(block, arguments, result, getName()); break; case TypeIndex::UInt16: - tidbPadImpl::tidbExecutePadImpl(block, arguments, result, getName()); + TidbPadImpl::tidbExecutePadImpl(block, arguments, result, getName()); break; case TypeIndex::UInt32: - tidbPadImpl::tidbExecutePadImpl(block, arguments, result, getName()); + TidbPadImpl::tidbExecutePadImpl(block, arguments, result, getName()); break; case TypeIndex::UInt64: - tidbPadImpl::tidbExecutePadImpl(block, arguments, result, getName()); + TidbPadImpl::tidbExecutePadImpl(block, arguments, result, getName()); break; case TypeIndex::Int8: - tidbPadImpl::tidbExecutePadImpl(block, arguments, result, getName()); + TidbPadImpl::tidbExecutePadImpl(block, arguments, result, getName()); break; case TypeIndex::Int16: - tidbPadImpl::tidbExecutePadImpl(block, arguments, result, getName()); + TidbPadImpl::tidbExecutePadImpl(block, arguments, result, getName()); break; case TypeIndex::Int32: - tidbPadImpl::tidbExecutePadImpl(block, arguments, result, getName()); + TidbPadImpl::tidbExecutePadImpl(block, arguments, result, getName()); break; case TypeIndex::Int64: - tidbPadImpl::tidbExecutePadImpl(block, arguments, result, getName()); + TidbPadImpl::tidbExecutePadImpl(block, arguments, result, getName()); break; default: throw Exception(fmt::format("the second argument type of {} is invalid, expect integer, got {}", getName(), type_index), ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT); @@ -3243,7 +3243,7 @@ class PadUTF8Impl : public IFunction { public: static constexpr auto name = Name::name; - explicit PadUTF8Impl() {} + explicit PadUTF8Impl() = default; static FunctionPtr create(const Context &) { return std::make_shared(); @@ -3308,7 +3308,7 @@ class PadUTF8Impl : public IFunction } auto c_res = ColumnString::create(); - auto column_padding_string = checkAndGetColumn(column_padding_const->getDataColumnPtr().get()); + const auto * column_padding_string = checkAndGetColumn(column_padding_const->getDataColumnPtr().get()); if (const ColumnString * col = checkAndGetColumn(column_string.get())) vector(col->getChars(), col->getOffsets(), length_value, column_padding_string->getChars(), column_padding_string->getOffsets(), c_res->getChars(), c_res->getOffsets()); else if (const ColumnFixedString * col = checkAndGetColumn(column_string.get())) @@ -3316,26 +3316,26 @@ class PadUTF8Impl : public IFunction else if (const ColumnConst * col = checkAndGetColumnConst(column_string.get())) { const auto * col_string = checkAndGetColumn(col->getDataColumnPtr().get()); - vector_const(col_string->getChars(), - col_string->getOffsets(), - col->size(), - length_value, - column_padding_string->getChars(), - column_padding_string->getOffsets(), - c_res->getChars(), - c_res->getOffsets()); + vectorConst(col_string->getChars(), + col_string->getOffsets(), + col->size(), + length_value, + column_padding_string->getChars(), + column_padding_string->getOffsets(), + c_res->getChars(), + c_res->getOffsets()); } else if (const ColumnConst * col = checkAndGetColumnConst(column_string.get())) { const auto * col_string = checkAndGetColumn(col->getDataColumnPtr().get()); - vector_const(col_string->getChars(), - col_string->getN(), - col->size(), - length_value, - column_padding_string->getChars(), - column_padding_string->getOffsets(), - c_res->getChars(), - c_res->getOffsets()); + vectorConst(col_string->getChars(), + col_string->getN(), + col->size(), + length_value, + column_padding_string->getChars(), + column_padding_string->getOffsets(), + c_res->getChars(), + c_res->getOffsets()); } block.getByPosition(result).column = std::move(c_res); @@ -3576,14 +3576,14 @@ class PadUTF8Impl : public IFunction } } - static void vector_const(const ColumnString::Chars_t & data, - const ColumnString::Offsets & offsets, - size_t size, /// number of rows of const column - size_t length, - const ColumnString::Chars_t & pad_data, - const ColumnString::Offsets & pad_offsets, - ColumnString::Chars_t & res_data, - ColumnString::Offsets & res_offsets) + static void vectorConst(const ColumnString::Chars_t & data, + const ColumnString::Offsets & offsets, + size_t size, /// number of rows of const column + size_t length, + const ColumnString::Chars_t & pad_data, + const ColumnString::Offsets & pad_offsets, + ColumnString::Chars_t & res_data, + ColumnString::Offsets & res_offsets) { res_data.reserve(3 * length * size); res_offsets.resize(size); @@ -3689,14 +3689,14 @@ class PadUTF8Impl : public IFunction } } - static void vector_const(const ColumnString::Chars_t & data, - size_t, /// length of fixed colomn - size_t size, /// number of row - size_t length, - const ColumnString::Chars_t & pad_data, - const ColumnString::Offsets & pad_offsets, - ColumnString::Chars_t & res_data, - ColumnString::Offsets & res_offsets) + static void vectorConst(const ColumnString::Chars_t & data, + size_t, /// length of fixed colomn + size_t size, /// number of row + size_t length, + const ColumnString::Chars_t & pad_data, + const ColumnString::Offsets & pad_offsets, + ColumnString::Chars_t & res_data, + ColumnString::Offsets & res_offsets) { res_data.reserve(3 * length * size); res_offsets.resize(size); @@ -3813,28 +3813,28 @@ class PadUTF8Impl : public IFunction switch (type_index) { case TypeIndex::UInt8: - tidbPadImpl::tidbExecutePadImpl(block, arguments, result, getName()); + TidbPadImpl::tidbExecutePadImpl(block, arguments, result, getName()); break; case TypeIndex::UInt16: - tidbPadImpl::tidbExecutePadImpl(block, arguments, result, getName()); + TidbPadImpl::tidbExecutePadImpl(block, arguments, result, getName()); break; case TypeIndex::UInt32: - tidbPadImpl::tidbExecutePadImpl(block, arguments, result, getName()); + TidbPadImpl::tidbExecutePadImpl(block, arguments, result, getName()); break; case TypeIndex::UInt64: - tidbPadImpl::tidbExecutePadImpl(block, arguments, result, getName()); + TidbPadImpl::tidbExecutePadImpl(block, arguments, result, getName()); break; case TypeIndex::Int8: - tidbPadImpl::tidbExecutePadImpl(block, arguments, result, getName()); + TidbPadImpl::tidbExecutePadImpl(block, arguments, result, getName()); break; case TypeIndex::Int16: - tidbPadImpl::tidbExecutePadImpl(block, arguments, result, getName()); + TidbPadImpl::tidbExecutePadImpl(block, arguments, result, getName()); break; case TypeIndex::Int32: - tidbPadImpl::tidbExecutePadImpl(block, arguments, result, getName()); + TidbPadImpl::tidbExecutePadImpl(block, arguments, result, getName()); break; case TypeIndex::Int64: - tidbPadImpl::tidbExecutePadImpl(block, arguments, result, getName()); + TidbPadImpl::tidbExecutePadImpl(block, arguments, result, getName()); break; default: throw Exception(fmt::format("the second argument type of {} is invalid, expect integer, got {}", getName(), type_index), ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT); @@ -3846,7 +3846,7 @@ class FunctionASCII : public IFunction { public: static constexpr auto name = "ascii"; - FunctionASCII(const Context & context) + explicit FunctionASCII(const Context & context) : context(context) {} @@ -3886,7 +3886,7 @@ class FunctionASCII : public IFunction { c0_col->get(i, res_field); String handled_str = res_field.get(); - Int64 res = handled_str.size() == 0 ? 0 : static_cast(handled_str[0]); + Int64 res = handled_str.empty() ? 0 : static_cast(handled_str[0]); col_res->insert(res); } @@ -3901,7 +3901,7 @@ class FunctionLength : public IFunction { public: static constexpr auto name = "length"; - FunctionLength(const Context & context) + explicit FunctionLength(const Context & context) : context(context) {} @@ -3955,7 +3955,7 @@ class FunctionPosition : public IFunction { public: static constexpr auto name = "position"; - FunctionPosition(const Context & context) + explicit FunctionPosition(const Context & context) : context(context) {} @@ -4018,12 +4018,12 @@ class FunctionPosition : public IFunction } private: - Int64 getPositionUTF8(const String & c1_str, Int64 idx) const + static Int64 getPositionUTF8(const String & c1_str, Int64 idx) { if (idx == -1) return 0; - auto data = reinterpret_cast(c1_str.data()); + const auto * data = reinterpret_cast(c1_str.data()); return static_cast(UTF8::countCodePoints(data, idx) + 1); } diff --git a/dbms/src/Functions/FunctionsString.h b/dbms/src/Functions/FunctionsString.h index 88181962460..6f3bcc25ecc 100644 --- a/dbms/src/Functions/FunctionsString.h +++ b/dbms/src/Functions/FunctionsString.h @@ -116,7 +116,7 @@ struct LowerUpperUTF8Impl ColumnString::Chars_t & res_data, ColumnString::Offsets & res_offsets); - static void vector_fixed(const ColumnString::Chars_t & data, size_t n, ColumnString::Chars_t & res_data); + static void vectorFixed(const ColumnString::Chars_t & data, size_t n, ColumnString::Chars_t & res_data); static void constant(const std::string & data, std::string & res_data); @@ -183,7 +183,7 @@ class FunctionStringToString : public IFunction else if (const ColumnFixedString * col = checkAndGetColumn(column.get())) { auto col_res = ColumnFixedString::create(col->getN()); - Impl::vector_fixed(col->getChars(), col->getN(), col_res->getChars()); + Impl::vectorFixed(col->getChars(), col->getN(), col_res->getChars()); block.getByPosition(result).column = std::move(col_res); } else @@ -203,7 +203,7 @@ struct TiDBLowerUpperUTF8Impl ColumnString::Chars_t & res_data, ColumnString::Offsets & res_offsets); - static void vector_fixed(const ColumnString::Chars_t & data, size_t n, ColumnString::Chars_t & res_data); + static void vectorFixed(const ColumnString::Chars_t & data, size_t n, ColumnString::Chars_t & res_data); static void constant(const std::string & data, std::string & res_data); @@ -228,7 +228,7 @@ struct TiDBLowerUpperBinaryImpl throw Exception("the TiDB function of lower or upper for binary should do noting."); } - static void vector_fixed(const ColumnString::Chars_t &, size_t, ColumnString::Chars_t &) + static void vectorFixed(const ColumnString::Chars_t &, size_t, ColumnString::Chars_t &) { throw Exception("the TiDB function of lower or upper for binary should do noting."); } diff --git a/dbms/src/Functions/tests/gtest_strings_format.cpp b/dbms/src/Functions/tests/gtest_strings_format.cpp index 3346af852e0..81fd088d94b 100644 --- a/dbms/src/Functions/tests/gtest_strings_format.cpp +++ b/dbms/src/Functions/tests/gtest_strings_format.cpp @@ -61,7 +61,7 @@ class StringFormat : public DB::tests::FunctionTest FieldType(static_cast(-9999999), 4)), createColumn>({4, 0, -1, 31, 5, 1, 2}))); ASSERT_COLUMN_EQ( - createConstColumn(1, "-1,000.000"), + createConstColumn>(1, "-1,000.000"), executeFunction( func_name, createConstColumn( @@ -104,7 +104,7 @@ class StringFormat : public DB::tests::FunctionTest FieldType(static_cast(-9999999), 4)), createColumn>({4, 0, 31, 5, 1, 2}))); ASSERT_COLUMN_EQ( - createConstColumn(1, "-1,000.000"), + createConstColumn>(1, "-1,000.000"), executeFunction( func_name, createConstColumn( From 93707d353c71483714345f930c79f7735576431c Mon Sep 17 00:00:00 2001 From: xzhangxian1008 Date: Thu, 14 Jul 2022 11:59:09 +0800 Subject: [PATCH 13/19] fix --- dbms/src/Functions/FunctionsString.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dbms/src/Functions/FunctionsString.h b/dbms/src/Functions/FunctionsString.h index 6f3bcc25ecc..b649b6c35d6 100644 --- a/dbms/src/Functions/FunctionsString.h +++ b/dbms/src/Functions/FunctionsString.h @@ -183,7 +183,7 @@ class FunctionStringToString : public IFunction else if (const ColumnFixedString * col = checkAndGetColumn(column.get())) { auto col_res = ColumnFixedString::create(col->getN()); - Impl::vectorFixed(col->getChars(), col->getN(), col_res->getChars()); + Impl::vector_fixed(col->getChars(), col->getN(), col_res->getChars()); block.getByPosition(result).column = std::move(col_res); } else From 554f6e2685a9211586869491bccdf22e9aeeb952 Mon Sep 17 00:00:00 2001 From: xzhangxian1008 Date: Thu, 14 Jul 2022 12:30:47 +0800 Subject: [PATCH 14/19] fix --- dbms/src/Functions/FunctionsString.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dbms/src/Functions/FunctionsString.h b/dbms/src/Functions/FunctionsString.h index b649b6c35d6..6f3bcc25ecc 100644 --- a/dbms/src/Functions/FunctionsString.h +++ b/dbms/src/Functions/FunctionsString.h @@ -183,7 +183,7 @@ class FunctionStringToString : public IFunction else if (const ColumnFixedString * col = checkAndGetColumn(column.get())) { auto col_res = ColumnFixedString::create(col->getN()); - Impl::vector_fixed(col->getChars(), col->getN(), col_res->getChars()); + Impl::vectorFixed(col->getChars(), col->getN(), col_res->getChars()); block.getByPosition(result).column = std::move(col_res); } else From 372d1be95106c4640991f591ebbde15277581c8f Mon Sep 17 00:00:00 2001 From: xzhangxian1008 Date: Thu, 14 Jul 2022 12:43:21 +0800 Subject: [PATCH 15/19] update --- dbms/src/Functions/FunctionsString.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dbms/src/Functions/FunctionsString.h b/dbms/src/Functions/FunctionsString.h index 6f3bcc25ecc..b649b6c35d6 100644 --- a/dbms/src/Functions/FunctionsString.h +++ b/dbms/src/Functions/FunctionsString.h @@ -183,7 +183,7 @@ class FunctionStringToString : public IFunction else if (const ColumnFixedString * col = checkAndGetColumn(column.get())) { auto col_res = ColumnFixedString::create(col->getN()); - Impl::vectorFixed(col->getChars(), col->getN(), col_res->getChars()); + Impl::vector_fixed(col->getChars(), col->getN(), col_res->getChars()); block.getByPosition(result).column = std::move(col_res); } else From 5e1b1521c57cb538954b080ea0d2059e1dd7aa38 Mon Sep 17 00:00:00 2001 From: xzhangxian1008 Date: Thu, 14 Jul 2022 13:02:04 +0800 Subject: [PATCH 16/19] fix --- dbms/src/Functions/FunctionsString.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dbms/src/Functions/FunctionsString.h b/dbms/src/Functions/FunctionsString.h index b649b6c35d6..6f3bcc25ecc 100644 --- a/dbms/src/Functions/FunctionsString.h +++ b/dbms/src/Functions/FunctionsString.h @@ -183,7 +183,7 @@ class FunctionStringToString : public IFunction else if (const ColumnFixedString * col = checkAndGetColumn(column.get())) { auto col_res = ColumnFixedString::create(col->getN()); - Impl::vector_fixed(col->getChars(), col->getN(), col_res->getChars()); + Impl::vectorFixed(col->getChars(), col->getN(), col_res->getChars()); block.getByPosition(result).column = std::move(col_res); } else From b2db960232a5aeeb3c73d4bda2af9f34b30e34ef Mon Sep 17 00:00:00 2001 From: xzhangxian1008 Date: Thu, 14 Jul 2022 13:17:38 +0800 Subject: [PATCH 17/19] fix --- dbms/src/Functions/FunctionsURL.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/dbms/src/Functions/FunctionsURL.h b/dbms/src/Functions/FunctionsURL.h index f4d42dae9fc..aedb8039264 100644 --- a/dbms/src/Functions/FunctionsURL.h +++ b/dbms/src/Functions/FunctionsURL.h @@ -960,7 +960,7 @@ struct ExtractSubstringImpl res_data.assign(start, length); } - static void vector_fixed(const ColumnString::Chars_t &, size_t, ColumnString::Chars_t &) + static void vectorFixed(const ColumnString::Chars_t &, size_t, ColumnString::Chars_t &) { throw Exception("Column of type FixedString is not supported by URL functions", ErrorCodes::ILLEGAL_COLUMN); } @@ -1018,7 +1018,7 @@ struct CutSubstringImpl res_data.append(start + length, data.data() + data.size()); } - static void vector_fixed(const ColumnString::Chars_t &, size_t, ColumnString::Chars_t &) + static void vectorFixed(const ColumnString::Chars_t &, size_t, ColumnString::Chars_t &) { throw Exception("Column of type FixedString is not supported by URL functions", ErrorCodes::ILLEGAL_COLUMN); } @@ -1033,7 +1033,7 @@ struct DecodeURLComponentImpl static void constant(const std::string & data, std::string & res_data); - static void vector_fixed(const ColumnString::Chars_t & data, size_t n, ColumnString::Chars_t & res_data); + static void vectorFixed(const ColumnString::Chars_t & data, size_t n, ColumnString::Chars_t & res_data); }; } // namespace DB From f08c0bcb5808ef1a0ded706c41e2b5979de23e9e Mon Sep 17 00:00:00 2001 From: xzhangxian1008 Date: Thu, 14 Jul 2022 13:35:38 +0800 Subject: [PATCH 18/19] fix --- dbms/src/Functions/FunctionsURL.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dbms/src/Functions/FunctionsURL.cpp b/dbms/src/Functions/FunctionsURL.cpp index d821b20073f..e26d04ccb69 100644 --- a/dbms/src/Functions/FunctionsURL.cpp +++ b/dbms/src/Functions/FunctionsURL.cpp @@ -107,7 +107,7 @@ void DecodeURLComponentImpl::vector(const ColumnString::Chars_t & data, const Co } -void DecodeURLComponentImpl::vector_fixed(const ColumnString::Chars_t &, size_t, ColumnString::Chars_t &) +void DecodeURLComponentImpl::vectorFixed(const ColumnString::Chars_t &, size_t, ColumnString::Chars_t &) { throw Exception("Column of type FixedString is not supported by URL functions", ErrorCodes::ILLEGAL_COLUMN); } From adcfec8adc3bfdde93014b875bf30197fc04b46e Mon Sep 17 00:00:00 2001 From: xzhangxian1008 Date: Thu, 14 Jul 2022 13:52:04 +0800 Subject: [PATCH 19/19] fix --- dbms/src/Functions/FunctionsString.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/dbms/src/Functions/FunctionsString.cpp b/dbms/src/Functions/FunctionsString.cpp index 94a97286c43..5cf47842889 100644 --- a/dbms/src/Functions/FunctionsString.cpp +++ b/dbms/src/Functions/FunctionsString.cpp @@ -2781,11 +2781,11 @@ class TidbPadImpl } else if (!is_padding_const && is_length_const) { - const_column(str_val, column_length, target_len, padding_data, padding_offsets, vec_result_null_map, size, result_data, result_offsets); + constColumn(str_val, column_length, target_len, padding_data, padding_offsets, vec_result_null_map, size, result_data, result_offsets); } else if (!is_padding_const && !is_length_const) { - const_column(str_val, column_length, target_len, padding_data, padding_offsets, vec_result_null_map, size, result_data, result_offsets); + constColumn(str_val, column_length, target_len, padding_data, padding_offsets, vec_result_null_map, size, result_data, result_offsets); } } else @@ -2889,7 +2889,7 @@ class TidbPadImpl } template - static void const_column(const String & str_val, const ColumnVector * column_length [[maybe_unused]], IntType target_len, const ColumnString::Chars_t * padding_data, const ColumnString::Offsets * padding_offsets, ColumnUInt8::Container & vec_result_null_map, size_t size, ColumnString::Chars_t & result_data, ColumnString::Offsets & result_offsets) + static void constColumn(const String & str_val, const ColumnVector * column_length [[maybe_unused]], IntType target_len, const ColumnString::Chars_t * padding_data, const ColumnString::Offsets * padding_offsets, ColumnUInt8::Container & vec_result_null_map, size_t size, ColumnString::Chars_t & result_data, ColumnString::Offsets & result_offsets) { ColumnString::Offset padding_prev_offset = 0; ColumnString::Offset res_prev_offset = 0;