Skip to content

Commit d5efcc7

Browse files
committed
Added Block::Clear() and some tests
1 parent 9c3c857 commit d5efcc7

File tree

3 files changed

+58
-2
lines changed

3 files changed

+58
-2
lines changed

clickhouse/block.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,7 @@ size_t Block::GetRowCount() const {
8181
return rows_;
8282
}
8383

84-
size_t Block::RefreshRowCount()
85-
{
84+
size_t Block::RefreshRowCount() {
8685
size_t rows = 0UL;
8786

8887
for (size_t idx = 0UL; idx < columns_.size(); ++idx)
@@ -100,6 +99,15 @@ size_t Block::RefreshRowCount()
10099
return rows_;
101100
}
102101

102+
void Block::Clear() {
103+
for (auto & c : columns_) {
104+
c.column->Clear();
105+
}
106+
107+
RefreshRowCount();
108+
}
109+
110+
103111
ColumnRef Block::operator [] (size_t idx) const {
104112
if (idx < columns_.size()) {
105113
return columns_[idx].column;

clickhouse/block.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ class Block {
8585
return columns_.at(idx).name;
8686
}
8787

88+
/// Convinience method to wipe out all rows from all columns
89+
void Clear();
90+
8891
/// Reference to column by index in the block.
8992
ColumnRef operator [] (size_t idx) const;
9093

ut/block_ut.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
#include <clickhouse/client.h>
2+
#include <clickhouse/columns/tuple.h>
3+
#include <clickhouse/types/types.h>
4+
5+
#include "clickhouse/columns/column.h"
6+
#include "gtest/gtest-message.h"
27
#include "readonly_client_test.h"
38
#include "connection_failed_client_test.h"
49
#include "utils.h"
@@ -83,3 +88,43 @@ TEST(BlockTest, Iterators) {
8388
ASSERT_NE(block.cbegin(), block.cend());
8489
}
8590

91+
TEST(BlockTest, Clear) {
92+
// Test that Block::Clear removes all rows from all of the columns,
93+
// without changing column instances, types, names, etc.
94+
95+
auto block = MakeBlock({
96+
{"foo", std::make_shared<ColumnUInt8>(std::vector<uint8_t>{1, 2, 3, 4, 5})},
97+
{"bar", std::make_shared<ColumnString>(std::vector<std::string>{"1", "2", "3", "4", "5"})},
98+
});
99+
100+
std::vector<std::tuple<std::string, Column*>> expected_columns_description;
101+
for (const auto & c : block) {
102+
expected_columns_description.emplace_back(c.Name(), c.Column().get());
103+
}
104+
105+
block.Clear();
106+
107+
// Block must report empty after being cleared
108+
EXPECT_EQ(0u, block.GetRowCount());
109+
110+
size_t i = 0;
111+
for (const auto & c : block) {
112+
const auto & [expected_name, expected_column] = expected_columns_description[i];
113+
SCOPED_TRACE(testing::Message("col #") << c.ColumnIndex() << " \"" << c.Name() << "\"");
114+
115+
// MUST be same column object
116+
EXPECT_EQ(expected_column, c.Column().get());
117+
118+
// MUST have same column name
119+
EXPECT_EQ(expected_name, c.Name());
120+
121+
// column MUST be empty
122+
EXPECT_EQ(0u, c.Column()->Size())
123+
<< c.ColumnIndex() << " : " << c.Name();
124+
125+
++i;
126+
EXPECT_FALSE(true);
127+
}
128+
}
129+
130+

0 commit comments

Comments
 (0)