From 0f1029a973eeb884bf797f00cc2f0cf42f15499b Mon Sep 17 00:00:00 2001 From: xzhangxian1008 Date: Tue, 28 Jun 2022 17:01:06 +0800 Subject: [PATCH 01/10] complete --- dbms/src/TestUtils/FunctionTestUtils.cpp | 48 ++++++++++++++++++++++++ dbms/src/TestUtils/FunctionTestUtils.h | 5 +++ 2 files changed, 53 insertions(+) diff --git a/dbms/src/TestUtils/FunctionTestUtils.cpp b/dbms/src/TestUtils/FunctionTestUtils.cpp index 7fb526aeb01..4b32402c9df 100644 --- a/dbms/src/TestUtils/FunctionTestUtils.cpp +++ b/dbms/src/TestUtils/FunctionTestUtils.cpp @@ -275,5 +275,53 @@ ColumnWithTypeAndName toNullableDatetimeVec(String name, const std::vector(fsp)); return {makeColumn>(data_type, vec), data_type, name, 0}; } + +void printColumns(const ColumnsWithTypeAndName & cols) +{ + if (cols.size() <= 0) return; + printColumns(cols, 0, cols[0].column->size() - 1); +} + +void printColumns(const ColumnsWithTypeAndName & cols, size_t begin, size_t end) +{ + const size_t col_num = cols.size(); + if (col_num <= 0) return; + + const size_t col_size = cols[0].column->size(); + assert(begin <= end); + assert(col_size > end); + assert(col_size > begin); + + bool is_same = true; + + for (size_t i = 1; i < col_num; ++i) + { + if (cols[i].column->size() != col_size) + is_same = false; + } + + assert(is_same); /// Ensure the sizes of columns in cols are the same + + String output; + String col_name; + for (size_t i = 0; i < col_num; ++i) + { + /// Push the column name + output = fmt::format("{}{}: (", output, cols[i].name); + for (size_t j = begin; j <= end; ++j) + { + if (j != begin) + output = fmt::format("{}, ", output); /// Add comma to seperate different values + + /// Add value + output = fmt::format("{}{}: {}", output, j, (*cols[i].column)[j].toString()); + } + + output = fmt::format("{})\n", output); + } + + std::cout << output << std::endl; +} + } // namespace tests } // namespace DB diff --git a/dbms/src/TestUtils/FunctionTestUtils.h b/dbms/src/TestUtils/FunctionTestUtils.h index d6b7351df05..8eea9629d19 100644 --- a/dbms/src/TestUtils/FunctionTestUtils.h +++ b/dbms/src/TestUtils/FunctionTestUtils.h @@ -514,6 +514,11 @@ ColumnWithTypeAndName createConstColumn( return createConstColumn(data_type_args, size, InferredFieldType(std::nullopt), name); } +void printColumns(const ColumnsWithTypeAndName & cols); + +/// We can designate the range of columns printed with begin and end. range: [begin, end] +void printColumns(const ColumnsWithTypeAndName & cols, size_t begin, size_t end); + ::testing::AssertionResult dataTypeEqual( const DataTypePtr & expected, const DataTypePtr & actual); From 95cfb9458a41d17983dc0ca5593d3441a7c36159 Mon Sep 17 00:00:00 2001 From: xzhangxian1008 Date: Tue, 28 Jun 2022 17:08:10 +0800 Subject: [PATCH 02/10] update --- dbms/src/TestUtils/FunctionTestUtils.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/dbms/src/TestUtils/FunctionTestUtils.cpp b/dbms/src/TestUtils/FunctionTestUtils.cpp index 4b32402c9df..75c9c29c309 100644 --- a/dbms/src/TestUtils/FunctionTestUtils.cpp +++ b/dbms/src/TestUtils/FunctionTestUtils.cpp @@ -278,14 +278,16 @@ ColumnWithTypeAndName toNullableDatetimeVec(String name, const std::vectorsize() - 1); } void printColumns(const ColumnsWithTypeAndName & cols, size_t begin, size_t end) { const size_t col_num = cols.size(); - if (col_num <= 0) return; + if (col_num <= 0) + return; const size_t col_size = cols[0].column->size(); assert(begin <= end); From 9e8de2bd3c56bbbe67f5578afbc03ccca5b5dca7 Mon Sep 17 00:00:00 2001 From: xzhangxian1008 Date: Tue, 28 Jun 2022 17:12:08 +0800 Subject: [PATCH 03/10] update --- dbms/src/TestUtils/FunctionTestUtils.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/dbms/src/TestUtils/FunctionTestUtils.cpp b/dbms/src/TestUtils/FunctionTestUtils.cpp index 75c9c29c309..68cd6e6eaad 100644 --- a/dbms/src/TestUtils/FunctionTestUtils.cpp +++ b/dbms/src/TestUtils/FunctionTestUtils.cpp @@ -305,7 +305,6 @@ void printColumns(const ColumnsWithTypeAndName & cols, size_t begin, size_t end) assert(is_same); /// Ensure the sizes of columns in cols are the same String output; - String col_name; for (size_t i = 0; i < col_num; ++i) { /// Push the column name From 5633cb0468112886f7dc857f02451e8554da3529 Mon Sep 17 00:00:00 2001 From: xzhangxian1008 Date: Thu, 7 Jul 2022 15:41:45 +0800 Subject: [PATCH 04/10] update --- dbms/src/TestUtils/FunctionTestUtils.cpp | 12 ++++++------ dbms/src/TestUtils/FunctionTestUtils.h | 4 ++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/dbms/src/TestUtils/FunctionTestUtils.cpp b/dbms/src/TestUtils/FunctionTestUtils.cpp index e33f7e3076e..23bfb84d732 100644 --- a/dbms/src/TestUtils/FunctionTestUtils.cpp +++ b/dbms/src/TestUtils/FunctionTestUtils.cpp @@ -375,18 +375,18 @@ ColumnWithTypeAndName toNullableDatetimeVec(String name, const std::vector>(data_type, vec), data_type, name, 0}; } -void printColumns(const ColumnsWithTypeAndName & cols) +String printColumns(const ColumnsWithTypeAndName & cols) { if (cols.size() <= 0) - return; - printColumns(cols, 0, cols[0].column->size() - 1); + return ""; + return printColumns(cols, 0, cols[0].column->size() - 1); } -void printColumns(const ColumnsWithTypeAndName & cols, size_t begin, size_t end) +String printColumns(const ColumnsWithTypeAndName & cols, size_t begin, size_t end) { const size_t col_num = cols.size(); if (col_num <= 0) - return; + return ""; const size_t col_size = cols[0].column->size(); assert(begin <= end); @@ -420,7 +420,7 @@ void printColumns(const ColumnsWithTypeAndName & cols, size_t begin, size_t end) output = fmt::format("{})\n", output); } - std::cout << output << std::endl; + return output; } ColumnsWithTypeAndName createColumns(const ColumnsWithTypeAndName & cols) diff --git a/dbms/src/TestUtils/FunctionTestUtils.h b/dbms/src/TestUtils/FunctionTestUtils.h index 755c2dd20eb..7e2c23ff4b9 100644 --- a/dbms/src/TestUtils/FunctionTestUtils.h +++ b/dbms/src/TestUtils/FunctionTestUtils.h @@ -514,10 +514,10 @@ ColumnWithTypeAndName createConstColumn( return createConstColumn(data_type_args, size, InferredFieldType(std::nullopt), name); } -void printColumns(const ColumnsWithTypeAndName & cols); +String printColumns(const ColumnsWithTypeAndName & cols); /// We can designate the range of columns printed with begin and end. range: [begin, end] -void printColumns(const ColumnsWithTypeAndName & cols, size_t begin, size_t end); +String printColumns(const ColumnsWithTypeAndName & cols, size_t begin, size_t end); // This wrapper function only serves to construct columns input for function-like macros, // since preprocessor recognizes `{col1, col2, col3}` as three arguments instead of one. From c426d28a173223c7362355a29b7438ddf5079695 Mon Sep 17 00:00:00 2001 From: xzhangxian1008 Date: Thu, 7 Jul 2022 15:43:09 +0800 Subject: [PATCH 05/10] update --- dbms/src/TestUtils/FunctionTestUtils.cpp | 6 +++--- dbms/src/TestUtils/FunctionTestUtils.h | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/dbms/src/TestUtils/FunctionTestUtils.cpp b/dbms/src/TestUtils/FunctionTestUtils.cpp index 23bfb84d732..4e4dbec38a0 100644 --- a/dbms/src/TestUtils/FunctionTestUtils.cpp +++ b/dbms/src/TestUtils/FunctionTestUtils.cpp @@ -375,14 +375,14 @@ ColumnWithTypeAndName toNullableDatetimeVec(String name, const std::vector>(data_type, vec), data_type, name, 0}; } -String printColumns(const ColumnsWithTypeAndName & cols) +String getColumnsContent(const ColumnsWithTypeAndName & cols) { if (cols.size() <= 0) return ""; - return printColumns(cols, 0, cols[0].column->size() - 1); + return getColumnsContent(cols, 0, cols[0].column->size() - 1); } -String printColumns(const ColumnsWithTypeAndName & cols, size_t begin, size_t end) +String getColumnsContent(const ColumnsWithTypeAndName & cols, size_t begin, size_t end) { const size_t col_num = cols.size(); if (col_num <= 0) diff --git a/dbms/src/TestUtils/FunctionTestUtils.h b/dbms/src/TestUtils/FunctionTestUtils.h index 7e2c23ff4b9..8680d1886b1 100644 --- a/dbms/src/TestUtils/FunctionTestUtils.h +++ b/dbms/src/TestUtils/FunctionTestUtils.h @@ -514,10 +514,10 @@ ColumnWithTypeAndName createConstColumn( return createConstColumn(data_type_args, size, InferredFieldType(std::nullopt), name); } -String printColumns(const ColumnsWithTypeAndName & cols); +String getColumnsContent(const ColumnsWithTypeAndName & cols); /// We can designate the range of columns printed with begin and end. range: [begin, end] -String printColumns(const ColumnsWithTypeAndName & cols, size_t begin, size_t end); +String getColumnsContent(const ColumnsWithTypeAndName & cols, size_t begin, size_t end); // This wrapper function only serves to construct columns input for function-like macros, // since preprocessor recognizes `{col1, col2, col3}` as three arguments instead of one. From e5a46e160c53c2f8dfc0a2a5059e512f6273a7be Mon Sep 17 00:00:00 2001 From: xzhangxian1008 Date: Thu, 7 Jul 2022 17:06:29 +0800 Subject: [PATCH 06/10] update --- dbms/src/TestUtils/FunctionTestUtils.cpp | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/dbms/src/TestUtils/FunctionTestUtils.cpp b/dbms/src/TestUtils/FunctionTestUtils.cpp index 4e4dbec38a0..53013d95dbe 100644 --- a/dbms/src/TestUtils/FunctionTestUtils.cpp +++ b/dbms/src/TestUtils/FunctionTestUtils.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #include #include @@ -120,6 +121,7 @@ ::testing::AssertionResult blockEqual( { const auto & expected_col = expected.getByPosition(i); const auto & actual_col = actual.getByPosition(i); + std::cout << "PRINT: " << getColumnsContent({expected_col, actual_col}) << std::endl; auto cmp_res = columnEqual(expected_col, actual_col); if (!cmp_res) return cmp_res; @@ -403,24 +405,25 @@ String getColumnsContent(const ColumnsWithTypeAndName & cols, size_t begin, size assert(is_same); /// Ensure the sizes of columns in cols are the same - String output; + std::vector> col_content; + FmtBuffer fmt_buf; for (size_t i = 0; i < col_num; ++i) { /// Push the column name - output = fmt::format("{}{}: (", output, cols[i].name); + fmt_buf.append(fmt::format("{}: (", cols[i].name)); for (size_t j = begin; j <= end; ++j) - { - if (j != begin) - output = fmt::format("{}, ", output); /// Add comma to seperate different values + col_content.push_back(std::make_pair(j, (*cols[i].column)[j].toString())); - /// Add value - output = fmt::format("{}{}: {}", output, j, (*cols[i].column)[j].toString()); - } + /// Add content + fmt_buf.joinStr(col_content.begin(), col_content.end(), [](const auto & content, FmtBuffer & fmt_buf) { + fmt_buf.append(fmt::format("{}: {}", content.first, content.second)); + }, ","); - output = fmt::format("{})\n", output); + fmt_buf.append(")\n"); + col_content.clear(); } - return output; + return fmt_buf.toString(); } ColumnsWithTypeAndName createColumns(const ColumnsWithTypeAndName & cols) From dabae3415a564c004ed527d8234dd0f2f70338a3 Mon Sep 17 00:00:00 2001 From: xzhangxian1008 Date: Thu, 7 Jul 2022 17:08:13 +0800 Subject: [PATCH 07/10] udpate --- dbms/src/TestUtils/FunctionTestUtils.cpp | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/dbms/src/TestUtils/FunctionTestUtils.cpp b/dbms/src/TestUtils/FunctionTestUtils.cpp index 53013d95dbe..7b34ccc28eb 100644 --- a/dbms/src/TestUtils/FunctionTestUtils.cpp +++ b/dbms/src/TestUtils/FunctionTestUtils.cpp @@ -13,6 +13,7 @@ // limitations under the License. #include +#include #include #include #include @@ -24,7 +25,6 @@ #include #include #include -#include #include #include @@ -121,7 +121,7 @@ ::testing::AssertionResult blockEqual( { const auto & expected_col = expected.getByPosition(i); const auto & actual_col = actual.getByPosition(i); - std::cout << "PRINT: " << getColumnsContent({expected_col, actual_col}) << std::endl; + auto cmp_res = columnEqual(expected_col, actual_col); if (!cmp_res) return cmp_res; @@ -415,9 +415,13 @@ String getColumnsContent(const ColumnsWithTypeAndName & cols, size_t begin, size col_content.push_back(std::make_pair(j, (*cols[i].column)[j].toString())); /// Add content - fmt_buf.joinStr(col_content.begin(), col_content.end(), [](const auto & content, FmtBuffer & fmt_buf) { - fmt_buf.append(fmt::format("{}: {}", content.first, content.second)); - }, ","); + fmt_buf.joinStr( + col_content.begin(), + col_content.end(), + [](const auto & content, FmtBuffer & fmt_buf) { + fmt_buf.append(fmt::format("{}: {}", content.first, content.second)); + }, + ","); fmt_buf.append(")\n"); col_content.clear(); From 1fc7443d424f05e8746fa6d08300bf82506e1b98 Mon Sep 17 00:00:00 2001 From: xzhangxian1008 Date: Fri, 8 Jul 2022 17:11:25 +0800 Subject: [PATCH 08/10] add test --- .../TestUtils/tests/gtest_print_columns.cpp | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 dbms/src/TestUtils/tests/gtest_print_columns.cpp diff --git a/dbms/src/TestUtils/tests/gtest_print_columns.cpp b/dbms/src/TestUtils/tests/gtest_print_columns.cpp new file mode 100644 index 00000000000..02208962f53 --- /dev/null +++ b/dbms/src/TestUtils/tests/gtest_print_columns.cpp @@ -0,0 +1,57 @@ +// Copyright 2022 PingCAP, Ltd. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include + +namespace DB +{ +namespace tests +{ + +class PrintColumnsTest : public DB::tests::ExecutorTest +{ +public: + using ColStringType = std::optional::FieldType>; + using ColInt32Type = std::optional::FieldType>; + using ColumnWithString = std::vector; + using ColumnWithInt32 = std::vector; + + void initializeContext() override + { + test_cols.push_back(toNullableVec("col1", ColumnWithInt32{36, 34, 32, 27, {}, {}})); + test_cols.push_back(toNullableVec("col2", ColumnWithString{"female", "male", "male", "female", "male", "female"})); + col_len = test_cols[0].column->size(); + } + + ColumnsWithTypeAndName test_cols; + size_t col_len; + const String result1{"col1: (0: Int64_36,1: Int64_34,2: Int64_32,3: Int64_27,4: NULL,5: NULL)\ncol2: (0: 'female',1: 'male',2: 'male',3: 'female',4: 'male',5: 'female')\n"}; + const String result2{"col1: (0: Int64_36,1: Int64_34,2: Int64_32,3: Int64_27,4: NULL,5: NULL)\ncol2: (0: 'female',1: 'male',2: 'male',3: 'female',4: 'male',5: 'female')\n"}; + const String result3{"col1: (0: Int64_36)\ncol2: (0: 'female')\n"}; + const String result4{"col1: (1: Int64_34,2: Int64_32,3: Int64_27,4: NULL)\ncol2: (1: 'male',2: 'male',3: 'female',4: 'male')\n"}; +}; + +TEST_F(PrintColumnsTest, SimpleTest) +try +{ + EXPECT_EQ(getColumnsContent(test_cols), result1); + EXPECT_EQ(getColumnsContent(test_cols, 0, col_len - 1), result2); + EXPECT_EQ(getColumnsContent(test_cols, 0, 0), result3); + EXPECT_EQ(getColumnsContent(test_cols, 1, col_len - 2), result4); +} +CATCH + +} // namespace tests +} // namespace DB From 9bd3f39755281435f18ab9b07d6313595c9aaea0 Mon Sep 17 00:00:00 2001 From: xzhangxian1008 Date: Mon, 11 Jul 2022 10:27:15 +0800 Subject: [PATCH 09/10] add space --- dbms/src/TestUtils/FunctionTestUtils.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dbms/src/TestUtils/FunctionTestUtils.cpp b/dbms/src/TestUtils/FunctionTestUtils.cpp index 7b34ccc28eb..1c8b0242bfa 100644 --- a/dbms/src/TestUtils/FunctionTestUtils.cpp +++ b/dbms/src/TestUtils/FunctionTestUtils.cpp @@ -421,7 +421,7 @@ String getColumnsContent(const ColumnsWithTypeAndName & cols, size_t begin, size [](const auto & content, FmtBuffer & fmt_buf) { fmt_buf.append(fmt::format("{}: {}", content.first, content.second)); }, - ","); + ", "); fmt_buf.append(")\n"); col_content.clear(); From 2b9446af24528097472ad356a4c996ffb190bc30 Mon Sep 17 00:00:00 2001 From: xzhangxian1008 Date: Mon, 11 Jul 2022 11:38:27 +0800 Subject: [PATCH 10/10] update --- dbms/src/TestUtils/tests/gtest_print_columns.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/dbms/src/TestUtils/tests/gtest_print_columns.cpp b/dbms/src/TestUtils/tests/gtest_print_columns.cpp index 02208962f53..50631fc4f4a 100644 --- a/dbms/src/TestUtils/tests/gtest_print_columns.cpp +++ b/dbms/src/TestUtils/tests/gtest_print_columns.cpp @@ -37,10 +37,10 @@ class PrintColumnsTest : public DB::tests::ExecutorTest ColumnsWithTypeAndName test_cols; size_t col_len; - const String result1{"col1: (0: Int64_36,1: Int64_34,2: Int64_32,3: Int64_27,4: NULL,5: NULL)\ncol2: (0: 'female',1: 'male',2: 'male',3: 'female',4: 'male',5: 'female')\n"}; - const String result2{"col1: (0: Int64_36,1: Int64_34,2: Int64_32,3: Int64_27,4: NULL,5: NULL)\ncol2: (0: 'female',1: 'male',2: 'male',3: 'female',4: 'male',5: 'female')\n"}; + const String result1{"col1: (0: Int64_36, 1: Int64_34, 2: Int64_32, 3: Int64_27, 4: NULL, 5: NULL)\ncol2: (0: 'female', 1: 'male', 2: 'male', 3: 'female', 4: 'male', 5: 'female')\n"}; + const String result2{"col1: (0: Int64_36, 1: Int64_34, 2: Int64_32, 3: Int64_27, 4: NULL, 5: NULL)\ncol2: (0: 'female', 1: 'male', 2: 'male', 3: 'female', 4: 'male', 5: 'female')\n"}; const String result3{"col1: (0: Int64_36)\ncol2: (0: 'female')\n"}; - const String result4{"col1: (1: Int64_34,2: Int64_32,3: Int64_27,4: NULL)\ncol2: (1: 'male',2: 'male',3: 'female',4: 'male')\n"}; + const String result4{"col1: (1: Int64_34, 2: Int64_32, 3: Int64_27, 4: NULL)\ncol2: (1: 'male', 2: 'male', 3: 'female', 4: 'male')\n"}; }; TEST_F(PrintColumnsTest, SimpleTest)