Skip to content

Commit e750dd5

Browse files
authored
Merge pull request #330 from rtcote/rtcote_add_numeric_methods
Add reserve, capacity methods to numeric columns
2 parents 89cd7bc + 979bb9f commit e750dd5

File tree

5 files changed

+184
-0
lines changed

5 files changed

+184
-0
lines changed

clickhouse/columns/date.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,18 @@ void ColumnDate::Append(ColumnRef column) {
4343
}
4444
}
4545

46+
std::vector<uint16_t> &ColumnDate::GetRawVector() {
47+
return data_->GetRawVector();
48+
}
49+
50+
void ColumnDate::Reserve(size_t new_cap) {
51+
data_->Reserve(new_cap);
52+
}
53+
54+
size_t ColumnDate::Capacity() const {
55+
return data_->Capacity();
56+
}
57+
4658
bool ColumnDate::LoadBody(InputStream* input, size_t rows) {
4759
return data_->LoadBody(input, rows);
4860
}
@@ -110,6 +122,18 @@ void ColumnDate32::Append(ColumnRef column) {
110122
}
111123
}
112124

125+
std::vector<int32_t> & ColumnDate32::GetRawVector() {
126+
return data_->GetRawVector();
127+
}
128+
129+
void ColumnDate32::Reserve(size_t new_cap) {
130+
data_->Reserve(new_cap);
131+
}
132+
133+
size_t ColumnDate32::Capacity() const {
134+
return data_->Capacity();
135+
}
136+
113137
void ColumnDate32::AppendRaw(int32_t value) {
114138
data_->Append(value);
115139
}
@@ -182,6 +206,10 @@ std::time_t ColumnDateTime::At(size_t n) const {
182206
return data_->At(n);
183207
}
184208

209+
void ColumnDateTime::AppendRaw(uint32_t value) {
210+
data_->Append(value);
211+
}
212+
185213
std::string ColumnDateTime::Timezone() const {
186214
return type_->As<DateTimeType>()->Timezone();
187215
}
@@ -192,6 +220,18 @@ void ColumnDateTime::Append(ColumnRef column) {
192220
}
193221
}
194222

223+
std::vector<uint32_t> & ColumnDateTime::GetRawVector() {
224+
return data_->GetRawVector();
225+
}
226+
227+
void ColumnDateTime::Reserve(size_t new_cap) {
228+
data_->Reserve(new_cap);
229+
}
230+
231+
size_t ColumnDateTime::Capacity() const {
232+
return data_->Capacity();
233+
}
234+
195235
bool ColumnDateTime::LoadBody(InputStream* input, size_t rows) {
196236
return data_->LoadBody(input, rows);
197237
}

clickhouse/columns/date.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@ class ColumnDate : public Column {
3131
/// Appends content of given column to the end of current one.
3232
void Append(ColumnRef column) override;
3333

34+
/// Get Raw Vector Contents
35+
std::vector<uint16_t> & GetRawVector();
36+
37+
/// Increase the capacity of the column
38+
void Reserve(size_t new_cap);
39+
40+
/// Returns the capacity of the column
41+
size_t Capacity() const;
42+
3443
/// Loads column data from input stream.
3544
bool LoadBody(InputStream* input, size_t rows) override;
3645

@@ -79,6 +88,15 @@ class ColumnDate32 : public Column {
7988
void AppendRaw(int32_t value);
8089
int32_t RawAt(size_t n) const;
8190

91+
/// Get Raw Vector Contents
92+
std::vector<int32_t> & GetRawVector();
93+
94+
/// Increase the capacity of the column
95+
void Reserve(size_t new_cap);
96+
97+
/// Returns the capacity of the column
98+
size_t Capacity() const;
99+
82100
/// Loads column data from input stream.
83101
bool LoadBody(InputStream* input, size_t rows) override;
84102

@@ -121,9 +139,21 @@ class ColumnDateTime : public Column {
121139
std::time_t At(size_t n) const;
122140
inline std::time_t operator [] (size_t n) const { return At(n); }
123141

142+
/// Append raw as UNIX epoch seconds in uint32
143+
void AppendRaw(uint32_t value);
144+
124145
/// Timezone associated with a data column.
125146
std::string Timezone() const;
126147

148+
/// Get Raw Vector Contents
149+
std::vector<uint32_t> & GetRawVector();
150+
151+
/// Increase the capacity of the column
152+
void Reserve(size_t new_cap);
153+
154+
/// Returns the capacity of the column
155+
size_t Capacity() const;
156+
127157
public:
128158
/// Appends content of given column to the end of current one.
129159
void Append(ColumnRef column) override;

clickhouse/columns/numeric.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,21 @@ void ColumnVector<T>::Erase(size_t pos, size_t count) {
3838
data_.erase(data_.begin() + begin, data_.begin() + last);
3939
}
4040

41+
template <typename T>
42+
std::vector<T> & ColumnVector<T>::GetRawVector() {
43+
return data_;
44+
}
45+
46+
template <typename T>
47+
void ColumnVector<T>::Reserve(size_t new_cap) {
48+
data_.reserve(new_cap);
49+
}
50+
51+
template <typename T>
52+
size_t ColumnVector<T>::Capacity() const {
53+
return data_.capacity();
54+
}
55+
4156
template <typename T>
4257
void ColumnVector<T>::Clear() {
4358
data_.clear();

clickhouse/columns/numeric.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@ class ColumnVector : public Column {
3030

3131
void Erase(size_t pos, size_t count = 1);
3232

33+
/// Get Raw Vector Contents
34+
std::vector<T> & GetRawVector();
35+
36+
/// Increase the capacity of the column
37+
void Reserve(size_t new_cap);
38+
39+
/// Returns the capacity of the column
40+
size_t Capacity() const;
41+
3342
public:
3443
/// Appends content of given column to the end of current one.
3544
void Append(ColumnRef column) override;

ut/columns_ut.cpp

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -965,3 +965,93 @@ TEST(ColumnsCase, ColumnMapT_Wrap) {
965965
EXPECT_EQ("123", map_view.At(1));
966966
EXPECT_EQ("abc", map_view.At(2));
967967
}
968+
969+
TEST(ColumnsCase, ReservedAndCapacity) {
970+
std::vector<std::shared_ptr<Column>> columns;
971+
972+
#define RaC_TEST_CASE(test_id_in, column_type) \
973+
case test_id_in: { \
974+
columns.push_back(std::make_shared<column_type>()); \
975+
auto column = std::static_pointer_cast<column_type>(columns[test_id_in]); \
976+
column->Reserve(100u); \
977+
ASSERT_EQ(column->Capacity(), 100u); \
978+
ASSERT_EQ(columns[test_id_in]->Size(), 0u); \
979+
break; \
980+
}
981+
982+
for (uint8_t rac_test_id = 0; rac_test_id < 14; ++rac_test_id) {
983+
switch (rac_test_id) {
984+
RaC_TEST_CASE( 0, ColumnUInt8);
985+
RaC_TEST_CASE( 1, ColumnUInt16);
986+
RaC_TEST_CASE( 2, ColumnUInt32);
987+
RaC_TEST_CASE( 3, ColumnUInt64);
988+
RaC_TEST_CASE( 4, ColumnInt8);
989+
RaC_TEST_CASE( 5, ColumnInt16);
990+
RaC_TEST_CASE( 6, ColumnInt32);
991+
RaC_TEST_CASE( 7, ColumnInt64);
992+
RaC_TEST_CASE( 8, ColumnInt128);
993+
RaC_TEST_CASE( 9, ColumnFloat32);
994+
RaC_TEST_CASE(10, ColumnFloat64);
995+
RaC_TEST_CASE(11, ColumnDate);
996+
RaC_TEST_CASE(12, ColumnDate32);
997+
RaC_TEST_CASE(13, ColumnDateTime);
998+
default: {
999+
EXPECT_NE(0, 0);
1000+
break;
1001+
}
1002+
}
1003+
}
1004+
}
1005+
1006+
TEST(ColumnsCase, RawVector) {
1007+
std::vector<std::shared_ptr<Column>> columns;
1008+
1009+
#define RV_TEST_CASE(test_id_in, column_type) \
1010+
case test_id_in: { \
1011+
columns.push_back(std::make_shared<column_type>()); \
1012+
auto column = std::static_pointer_cast<column_type>(columns[test_id_in]); \
1013+
column->Append(10u); \
1014+
column->Append(20u); \
1015+
ASSERT_EQ(columns[test_id_in]->Size(), 2u); \
1016+
auto column_v = column->GetRawVector(); \
1017+
ASSERT_EQ(static_cast<uint32_t>(column_v[0]), 10u); \
1018+
ASSERT_EQ(static_cast<uint32_t>(column_v[1]), 20u); \
1019+
break; \
1020+
}
1021+
1022+
#define RV_TEST_CASE_D(test_id_in, column_type) \
1023+
case test_id_in: { \
1024+
columns.push_back(std::make_shared<column_type>()); \
1025+
auto column = std::static_pointer_cast<column_type>(columns[test_id_in]); \
1026+
column->AppendRaw(10u); \
1027+
column->AppendRaw(20u); \
1028+
ASSERT_EQ(columns[test_id_in]->Size(), 2u); \
1029+
auto column_v = column->GetRawVector(); \
1030+
ASSERT_EQ(static_cast<uint32_t>(column_v[0]), 10u); \
1031+
ASSERT_EQ(static_cast<uint32_t>(column_v[1]), 20u); \
1032+
break; \
1033+
}
1034+
1035+
for (uint8_t rv_test_id = 0; rv_test_id < 14; ++rv_test_id) {
1036+
switch (rv_test_id) {
1037+
RV_TEST_CASE( 0, ColumnUInt8);
1038+
RV_TEST_CASE( 1, ColumnUInt16);
1039+
RV_TEST_CASE( 2, ColumnUInt32);
1040+
RV_TEST_CASE( 3, ColumnUInt64);
1041+
RV_TEST_CASE( 4, ColumnInt8);
1042+
RV_TEST_CASE( 5, ColumnInt16);
1043+
RV_TEST_CASE( 6, ColumnInt32);
1044+
RV_TEST_CASE( 7, ColumnInt64);
1045+
RV_TEST_CASE( 8, ColumnInt128);
1046+
RV_TEST_CASE( 9, ColumnFloat32);
1047+
RV_TEST_CASE( 10, ColumnFloat64);
1048+
RV_TEST_CASE_D( 11, ColumnDate);
1049+
RV_TEST_CASE_D( 12, ColumnDate32);
1050+
RV_TEST_CASE_D( 13, ColumnDateTime);
1051+
default: {
1052+
EXPECT_NE(0, 0);
1053+
break;
1054+
}
1055+
}
1056+
}
1057+
}

0 commit comments

Comments
 (0)