Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print content of columns for gtest #5243

Merged
merged 18 commits into from
Jul 11, 2022
56 changes: 56 additions & 0 deletions dbms/src/TestUtils/FunctionTestUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.

#include <Columns/ColumnNullable.h>
#include <Common/FmtUtils.h>
#include <Core/ColumnNumbers.h>
#include <Core/Row.h>
#include <DataTypes/DataTypeNothing.h>
Expand Down Expand Up @@ -120,6 +121,7 @@ ::testing::AssertionResult blockEqual(
{
const auto & expected_col = expected.getByPosition(i);
const auto & actual_col = actual.getByPosition(i);

auto cmp_res = columnEqual(expected_col, actual_col);
if (!cmp_res)
return cmp_res;
Expand Down Expand Up @@ -375,9 +377,63 @@ ColumnWithTypeAndName toNullableDatetimeVec(String name, const std::vector<Strin
return {makeColumn<Nullable<MyDateTime>>(data_type, vec), data_type, name, 0};
}

String getColumnsContent(const ColumnsWithTypeAndName & cols)
{
if (cols.size() <= 0)
return "";
return getColumnsContent(cols, 0, cols[0].column->size() - 1);
}

String getColumnsContent(const ColumnsWithTypeAndName & cols, size_t begin, size_t end)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe add a unit test for the function?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe add a unit test for the function?

Done

{
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

std::vector<std::pair<size_t, String>> col_content;
FmtBuffer fmt_buf;
for (size_t i = 0; i < col_num; ++i)
{
/// Push the column name
fmt_buf.append(fmt::format("{}: (", cols[i].name));
for (size_t j = begin; j <= end; ++j)
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));
},
",");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
",");
", ");

Maybe better?


fmt_buf.append(")\n");
col_content.clear();
}

return fmt_buf.toString();
}

ColumnsWithTypeAndName createColumns(const ColumnsWithTypeAndName & cols)
{
return cols;
}

} // namespace tests
} // namespace DB
6 changes: 5 additions & 1 deletion dbms/src/TestUtils/FunctionTestUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -514,13 +514,17 @@ ColumnWithTypeAndName createConstColumn(
return createConstColumn<T>(data_type_args, size, InferredFieldType<T>(std::nullopt), name);
}

String getColumnsContent(const ColumnsWithTypeAndName & cols);

/// We can designate the range of columns printed with begin and end. range: [begin, 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.
// E.g. preprocessor does not allow us to write `ASSERT_COLUMNS_EQ_R({col1, col2, col3}, actual_cols)`,
// but with this func we can write `ASSERT_COLUMNS_EQ_R(createColumns{col1, col2, col3}, actual_cols)` instead.
ColumnsWithTypeAndName createColumns(const ColumnsWithTypeAndName & cols);


::testing::AssertionResult dataTypeEqual(
const DataTypePtr & expected,
const DataTypePtr & actual);
Expand Down