|
1 | 1 | #include <clickhouse/client.h> |
2 | | -#include "readonly_client_test.h" |
3 | | -#include "connection_failed_client_test.h" |
| 2 | +#include <clickhouse/columns/tuple.h> |
| 3 | +#include <clickhouse/types/types.h> |
| 4 | + |
| 5 | +#include "clickhouse/columns/column.h" |
| 6 | +#include "clickhouse/columns/lowcardinality.h" |
| 7 | +#include "gtest/gtest-message.h" |
| 8 | + |
| 9 | +#include "ut/utils_comparison.h" |
4 | 10 | #include "utils.h" |
5 | 11 |
|
6 | 12 | #include <gtest/gtest.h> |
| 13 | +#include <memory> |
7 | 14 |
|
8 | 15 | namespace { |
9 | 16 | using namespace clickhouse; |
10 | 17 |
|
11 | 18 | Block MakeBlock(std::vector<std::pair<std::string, ColumnRef>> columns) { |
12 | 19 | Block result; |
13 | 20 |
|
| 21 | + const size_t number_of_rows = columns.size() ? columns[0].second->Size() : 0; |
| 22 | + size_t i = 0; |
14 | 23 | for (const auto & name_and_col : columns) { |
| 24 | + EXPECT_EQ(number_of_rows, name_and_col.second->Size()) |
| 25 | + << "Column #" << i << " " << name_and_col.first << " has incorrect number of rows"; |
| 26 | + |
15 | 27 | result.AppendColumn(name_and_col.first, name_and_col.second); |
| 28 | + |
| 29 | + ++i; |
16 | 30 | } |
17 | 31 |
|
18 | 32 | result.RefreshRowCount(); |
| 33 | + EXPECT_EQ(number_of_rows, result.GetRowCount()); |
| 34 | + |
19 | 35 | return result; |
20 | 36 | } |
21 | 37 |
|
@@ -83,3 +99,83 @@ TEST(BlockTest, Iterators) { |
83 | 99 | ASSERT_NE(block.cbegin(), block.cend()); |
84 | 100 | } |
85 | 101 |
|
| 102 | +TEST(BlockTest, Clear) { |
| 103 | + // Test that Block::Clear removes all rows from all of the columns, |
| 104 | + // without changing column instances, types, names, etc. |
| 105 | + |
| 106 | + auto block = MakeBlock({ |
| 107 | + {"foo", std::make_shared<ColumnUInt8>(std::vector<uint8_t>{1, 2, 3, 4, 5})}, |
| 108 | + {"bar", std::make_shared<ColumnString>(std::vector<std::string>{"1", "2", "3", "4", "5"})}, |
| 109 | + }); |
| 110 | + |
| 111 | + std::vector<std::tuple<std::string, Column*>> expected_columns_description; |
| 112 | + for (const auto & c : block) { |
| 113 | + expected_columns_description.emplace_back(c.Name(), c.Column().get()); |
| 114 | + } |
| 115 | + |
| 116 | + block.Clear(); |
| 117 | + |
| 118 | + // Block must report empty after being cleared |
| 119 | + EXPECT_EQ(0u, block.GetRowCount()); |
| 120 | + |
| 121 | + size_t i = 0; |
| 122 | + for (const auto & c : block) { |
| 123 | + const auto & [expected_name, expected_column] = expected_columns_description[i]; |
| 124 | + SCOPED_TRACE(testing::Message("col #") << c.ColumnIndex() << " \"" << c.Name() << "\""); |
| 125 | + |
| 126 | + // MUST be same column object |
| 127 | + EXPECT_EQ(expected_column, c.Column().get()); |
| 128 | + |
| 129 | + // MUST have same column name |
| 130 | + EXPECT_EQ(expected_name, c.Name()); |
| 131 | + |
| 132 | + // column MUST be empty |
| 133 | + EXPECT_EQ(0u, c.Column()->Size()); |
| 134 | + |
| 135 | + ++i; |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +TEST(BlockTest, Reserve) { |
| 140 | + // Test that Block::Reserve reserves space in all columns (uncheckable now), |
| 141 | + // without changing column instances, names, and previously stored rows. |
| 142 | + |
| 143 | + auto block = MakeBlock({ |
| 144 | + {"foo", std::make_shared<ColumnUInt8>(std::vector<uint8_t>{1, 2, 3, 4, 5})}, |
| 145 | + {"bar", std::make_shared<ColumnString>(std::vector<std::string>{"1", "2", "3", "4", "5"})}, |
| 146 | + {"quix", std::make_shared<ColumnLowCardinalityT<ColumnString>>(std::vector<std::string>{"1", "2", "3", "4", "5"})}, |
| 147 | + }); |
| 148 | + |
| 149 | + const size_t initial_rows_count = block.GetRowCount(); |
| 150 | + |
| 151 | + std::vector<std::tuple<std::string, Column*, ColumnRef>> expected_columns_description; |
| 152 | + for (const auto & c : block) { |
| 153 | + expected_columns_description.emplace_back( |
| 154 | + c.Name(), |
| 155 | + c.Column().get(), // reference to the actual object |
| 156 | + c.Column()->Slice(0, c.Column()->Size()) // reference to the values |
| 157 | + ); |
| 158 | + } |
| 159 | + |
| 160 | + block.Reserve(1000); // 1000 is arbitrary value |
| 161 | + |
| 162 | + // Block must same number of rows as before Reserve |
| 163 | + EXPECT_EQ(initial_rows_count, block.GetRowCount()); |
| 164 | + |
| 165 | + size_t i = 0; |
| 166 | + for (const auto & c : block) { |
| 167 | + const auto & [expected_name, expected_column, expected_values] = expected_columns_description[i]; |
| 168 | + SCOPED_TRACE(testing::Message("col #") << c.ColumnIndex() << " \"" << c.Name() << "\""); |
| 169 | + |
| 170 | + // MUST have same column name |
| 171 | + EXPECT_EQ(expected_name, c.Name()); |
| 172 | + |
| 173 | + // MUST be same column object |
| 174 | + EXPECT_EQ(expected_column, c.Column().get()); |
| 175 | + |
| 176 | + // column MUST have the same values |
| 177 | + EXPECT_TRUE(CompareRecursive(*expected_values, *c.Column())); |
| 178 | + |
| 179 | + ++i; |
| 180 | + } |
| 181 | +} |
0 commit comments