Skip to content

Commit 13be4d8

Browse files
authored
Merge pull request #338 from Enmk/fix_tests_and_Column_interface_after_330
Fix tests and column interface after 330
2 parents e750dd5 + c3f4a09 commit 13be4d8

File tree

8 files changed

+108
-102
lines changed

8 files changed

+108
-102
lines changed

clickhouse/columns/date.cpp

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

46-
std::vector<uint16_t> &ColumnDate::GetRawVector() {
47-
return data_->GetRawVector();
46+
std::vector<uint16_t>& ColumnDate::GetWritableData() {
47+
return data_->GetWritableData();
4848
}
4949

5050
void ColumnDate::Reserve(size_t new_cap) {
@@ -122,8 +122,8 @@ void ColumnDate32::Append(ColumnRef column) {
122122
}
123123
}
124124

125-
std::vector<int32_t> & ColumnDate32::GetRawVector() {
126-
return data_->GetRawVector();
125+
std::vector<int32_t>& ColumnDate32::GetWritableData() {
126+
return data_->GetWritableData();
127127
}
128128

129129
void ColumnDate32::Reserve(size_t new_cap) {
@@ -220,8 +220,8 @@ void ColumnDateTime::Append(ColumnRef column) {
220220
}
221221
}
222222

223-
std::vector<uint32_t> & ColumnDateTime::GetRawVector() {
224-
return data_->GetRawVector();
223+
std::vector<uint32_t>& ColumnDateTime::GetWritableData() {
224+
return data_->GetWritableData();
225225
}
226226

227227
void ColumnDateTime::Reserve(size_t new_cap) {

clickhouse/columns/date.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class ColumnDate : public Column {
3232
void Append(ColumnRef column) override;
3333

3434
/// Get Raw Vector Contents
35-
std::vector<uint16_t> & GetRawVector();
35+
std::vector<uint16_t>& GetWritableData();
3636

3737
/// Increase the capacity of the column
3838
void Reserve(size_t new_cap);
@@ -89,7 +89,7 @@ class ColumnDate32 : public Column {
8989
int32_t RawAt(size_t n) const;
9090

9191
/// Get Raw Vector Contents
92-
std::vector<int32_t> & GetRawVector();
92+
std::vector<int32_t>& GetWritableData();
9393

9494
/// Increase the capacity of the column
9595
void Reserve(size_t new_cap);
@@ -146,7 +146,7 @@ class ColumnDateTime : public Column {
146146
std::string Timezone() const;
147147

148148
/// Get Raw Vector Contents
149-
std::vector<uint32_t> & GetRawVector();
149+
std::vector<uint32_t>& GetWritableData();
150150

151151
/// Increase the capacity of the column
152152
void Reserve(size_t new_cap);

clickhouse/columns/numeric.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ void ColumnVector<T>::Erase(size_t pos, size_t count) {
3939
}
4040

4141
template <typename T>
42-
std::vector<T> & ColumnVector<T>::GetRawVector() {
42+
std::vector<T>& ColumnVector<T>::GetWritableData() {
4343
return data_;
4444
}
4545

clickhouse/columns/numeric.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class ColumnVector : public Column {
3131
void Erase(size_t pos, size_t count = 1);
3232

3333
/// Get Raw Vector Contents
34-
std::vector<T> & GetRawVector();
34+
std::vector<T>& GetWritableData();
3535

3636
/// Increase the capacity of the column
3737
void Reserve(size_t new_cap);

ut/Column_ut.cpp

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,14 @@
1616
#include <clickhouse/client.h>
1717

1818
#include <gtest/gtest.h>
19+
#include <algorithm>
1920
#include <initializer_list>
2021
#include <memory>
2122
#include <type_traits>
2223

24+
#include "gtest/internal/gtest-internal.h"
25+
#include "ut/utils_comparison.h"
26+
#include "ut/utils_meta.h"
2327
#include "utils.h"
2428
#include "roundtrip_column.h"
2529
#include "value_generators.h"
@@ -364,6 +368,82 @@ TYPED_TEST(GenericColumnTest, Swap) {
364368
EXPECT_TRUE(CompareRecursive(values, *column_B));
365369
}
366370

371+
// GTEST_SKIP for debug builds to draw attention of developer
372+
#if !defined(NDEBUG)
373+
#define COLUMN_DOESNT_IMPLEMENT(comment) GTEST_SKIP() << this->MakeColumn()->GetType().GetName() << " doesn't implement " << comment;
374+
#else
375+
#define COLUMN_DOESNT_IMPLEMENT(comment) GTEST_SUCCEED() << this->MakeColumn()->GetType().GetName() << " doesn't implement " << comment;
376+
#endif
377+
378+
TYPED_TEST(GenericColumnTest, ReserveAndCapacity) {
379+
if constexpr (
380+
// TODO(venemkov): test that ColumnType has Reserve() and Capacity() methods
381+
is_one_of_v<typename TestFixture::ColumnType,
382+
// Only types that support Reserve() and Capacity() methods
383+
ColumnUInt8,
384+
ColumnUInt16,
385+
ColumnUInt32,
386+
ColumnUInt64,
387+
ColumnInt8,
388+
ColumnInt16,
389+
ColumnInt32,
390+
ColumnInt64,
391+
ColumnInt128,
392+
ColumnFloat32,
393+
ColumnFloat64,
394+
ColumnDate,
395+
ColumnDate32,
396+
ColumnDateTime>) {
397+
398+
auto column = this->MakeColumn();
399+
EXPECT_EQ(0u, column->Capacity());
400+
EXPECT_NO_THROW(column->Reserve(100u));
401+
EXPECT_EQ(100u, column->Capacity());
402+
EXPECT_EQ(0u, column->Size());
403+
}
404+
else {
405+
COLUMN_DOESNT_IMPLEMENT("method Reserve() and Capacity()");
406+
}
407+
}
408+
409+
410+
TYPED_TEST(GenericColumnTest, GetWritableData) {
411+
if constexpr (
412+
// TODO(venemkov): test that ColumnType has GetWritableData() method
413+
is_one_of_v<typename TestFixture::ColumnType,
414+
// Only types that support GetWritableData() method
415+
ColumnUInt8,
416+
ColumnUInt16,
417+
ColumnUInt32,
418+
ColumnUInt64,
419+
ColumnInt8,
420+
ColumnInt16,
421+
ColumnInt32,
422+
ColumnInt64,
423+
ColumnInt128,
424+
ColumnFloat32,
425+
ColumnFloat64,
426+
ColumnDate,
427+
ColumnDate32,
428+
ColumnDateTime>) {
429+
auto [column, values] = this->MakeColumnWithValues(111);
430+
// Do conversion from time_t to internal representation, similar to what ColumnDate and ColumnDate32 do
431+
if constexpr (is_one_of_v<typename TestFixture::ColumnType,
432+
ColumnDate,
433+
ColumnDate32>) {
434+
std::for_each(values.begin(), values.end(), [](auto & value) {
435+
value /= 86400;
436+
});
437+
}
438+
439+
EXPECT_TRUE(CompareRecursive(values, column->GetWritableData()));
440+
}
441+
else {
442+
COLUMN_DOESNT_IMPLEMENT("method GetWritableData()");
443+
}
444+
}
445+
446+
367447
TYPED_TEST(GenericColumnTest, LoadAndSave) {
368448
auto [column_A, values] = this->MakeColumnWithValues(100);
369449

ut/columns_ut.cpp

Lines changed: 0 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -965,93 +965,3 @@ 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-
}

ut/utils.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "utils_meta.h"
88
#include "utils_comparison.h"
99

10+
#include <iterator>
1011
#include <optional>
1112
#include <ostream>
1213
#include <ratio>
@@ -181,7 +182,7 @@ std::ostream& operator<<(std::ostream & ostr, const PrintContainer<T>& print_con
181182
}
182183
}
183184

184-
return ostr << "]";
185+
return ostr << "] (" << std::size(container) << " items)";
185186
}
186187

187188
inline uint64_t versionNumber(

ut/utils_meta.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,18 @@ struct is_instantiation_of< Template, Template<Args...> > : std::true_type {};
5151

5252
template <typename T>
5353
inline constexpr bool is_string_v = std::is_same_v<std::string, std::decay_t<T>> || std::is_same_v<std::string_view, std::decay_t<T>>;
54+
55+
// https://stackoverflow.com/a/34111095
56+
template <typename...>
57+
struct is_one_of {
58+
static constexpr bool value = false;
59+
};
60+
61+
template <typename F, typename S, typename... T>
62+
struct is_one_of<F, S, T...> {
63+
static constexpr bool value =
64+
std::is_same<F, S>::value || is_one_of<F, T...>::value;
65+
};
66+
67+
template <typename F, typename S, typename... T>
68+
inline constexpr bool is_one_of_v = is_one_of<F, S, T...>::value;

0 commit comments

Comments
 (0)