diff --git a/clickhouse/columns/date.cpp b/clickhouse/columns/date.cpp index f4d08cb4..b93386c8 100644 --- a/clickhouse/columns/date.cpp +++ b/clickhouse/columns/date.cpp @@ -37,6 +37,18 @@ void ColumnDate::Append(ColumnRef column) { } } +std::vector &ColumnDate::GetRawVector() { + return data_->GetRawVector(); +} + +void ColumnDate::Reserve(size_t new_cap) { + data_->Reserve(new_cap); +} + +size_t ColumnDate::Capacity() const { + return data_->Capacity(); +} + bool ColumnDate::LoadBody(InputStream* input, size_t rows) { return data_->LoadBody(input, rows); } @@ -98,6 +110,18 @@ void ColumnDate32::Append(ColumnRef column) { } } +std::vector & ColumnDate32::GetRawVector() { + return data_->GetRawVector(); +} + +void ColumnDate32::Reserve(size_t new_cap) { + data_->Reserve(new_cap); +} + +size_t ColumnDate32::Capacity() const { + return data_->Capacity(); +} + void ColumnDate32::AppendRaw(int32_t value) { data_->Append(value); } @@ -160,6 +184,10 @@ std::time_t ColumnDateTime::At(size_t n) const { return data_->At(n); } +void ColumnDateTime::AppendRaw(uint32_t value) { + data_->Append(value); +} + std::string ColumnDateTime::Timezone() const { return type_->As()->Timezone(); } @@ -170,6 +198,18 @@ void ColumnDateTime::Append(ColumnRef column) { } } +std::vector & ColumnDateTime::GetRawVector() { + return data_->GetRawVector(); +} + +void ColumnDateTime::Reserve(size_t new_cap) { + data_->Reserve(new_cap); +} + +size_t ColumnDateTime::Capacity() const { + return data_->Capacity(); +} + bool ColumnDateTime::LoadBody(InputStream* input, size_t rows) { return data_->LoadBody(input, rows); } diff --git a/clickhouse/columns/date.h b/clickhouse/columns/date.h index 9b170001..a17cf26b 100644 --- a/clickhouse/columns/date.h +++ b/clickhouse/columns/date.h @@ -30,6 +30,15 @@ class ColumnDate : public Column { /// Appends content of given column to the end of current one. void Append(ColumnRef column) override; + /// Get Raw Vector Contents + std::vector & GetRawVector(); + + /// Increase the capacity of the column + void Reserve(size_t new_cap); + + /// Returns the capacity of the column + size_t Capacity() const; + /// Loads column data from input stream. bool LoadBody(InputStream* input, size_t rows) override; @@ -77,6 +86,15 @@ class ColumnDate32 : public Column { void AppendRaw(int32_t value); int32_t RawAt(size_t n) const; + /// Get Raw Vector Contents + std::vector & GetRawVector(); + + /// Increase the capacity of the column + void Reserve(size_t new_cap); + + /// Returns the capacity of the column + size_t Capacity() const; + /// Loads column data from input stream. bool LoadBody(InputStream* input, size_t rows) override; @@ -116,9 +134,21 @@ class ColumnDateTime : public Column { std::time_t At(size_t n) const; inline std::time_t operator [] (size_t n) const { return At(n); } + /// Append raw as UNIX epoch seconds in uint32 + void AppendRaw(uint32_t value); + /// Timezone associated with a data column. std::string Timezone() const; + /// Get Raw Vector Contents + std::vector & GetRawVector(); + + /// Increase the capacity of the column + void Reserve(size_t new_cap); + + /// Returns the capacity of the column + size_t Capacity() const; + public: /// Appends content of given column to the end of current one. void Append(ColumnRef column) override; diff --git a/clickhouse/columns/numeric.cpp b/clickhouse/columns/numeric.cpp index 81d6c721..57a32dd0 100644 --- a/clickhouse/columns/numeric.cpp +++ b/clickhouse/columns/numeric.cpp @@ -38,6 +38,21 @@ void ColumnVector::Erase(size_t pos, size_t count) { data_.erase(data_.begin() + begin, data_.begin() + last); } +template +std::vector & ColumnVector::GetRawVector() { + return data_; +} + +template +void ColumnVector::Reserve(size_t new_cap) { + data_.reserve(new_cap); +} + +template +size_t ColumnVector::Capacity() const { + return data_.capacity(); +} + template void ColumnVector::Clear() { data_.clear(); diff --git a/clickhouse/columns/numeric.h b/clickhouse/columns/numeric.h index dcc344bb..d3523614 100644 --- a/clickhouse/columns/numeric.h +++ b/clickhouse/columns/numeric.h @@ -30,6 +30,15 @@ class ColumnVector : public Column { void Erase(size_t pos, size_t count = 1); + /// Get Raw Vector Contents + std::vector & GetRawVector(); + + /// Increase the capacity of the column + void Reserve(size_t new_cap); + + /// Returns the capacity of the column + size_t Capacity() const; + public: /// Appends content of given column to the end of current one. void Append(ColumnRef column) override; diff --git a/ut/columns_ut.cpp b/ut/columns_ut.cpp index ce477554..fb0d4147 100644 --- a/ut/columns_ut.cpp +++ b/ut/columns_ut.cpp @@ -916,3 +916,93 @@ TEST(ColumnsCase, ColumnMapT_Wrap) { EXPECT_EQ("123", map_view.At(1)); EXPECT_EQ("abc", map_view.At(2)); } + +TEST(ColumnsCase, ReservedAndCapacity) { + std::vector> columns; + + #define RaC_TEST_CASE(test_id_in, column_type) \ + case test_id_in: { \ + columns.push_back(std::make_shared()); \ + auto column = std::static_pointer_cast(columns[test_id_in]); \ + column->Reserve(100u); \ + ASSERT_EQ(column->Capacity(), 100u); \ + ASSERT_EQ(columns[test_id_in]->Size(), 0u); \ + break; \ + } + + for (uint8_t rac_test_id = 0; rac_test_id < 14; ++rac_test_id) { + switch (rac_test_id) { + RaC_TEST_CASE( 0, ColumnUInt8); + RaC_TEST_CASE( 1, ColumnUInt16); + RaC_TEST_CASE( 2, ColumnUInt32); + RaC_TEST_CASE( 3, ColumnUInt64); + RaC_TEST_CASE( 4, ColumnInt8); + RaC_TEST_CASE( 5, ColumnInt16); + RaC_TEST_CASE( 6, ColumnInt32); + RaC_TEST_CASE( 7, ColumnInt64); + RaC_TEST_CASE( 8, ColumnInt128); + RaC_TEST_CASE( 9, ColumnFloat32); + RaC_TEST_CASE(10, ColumnFloat64); + RaC_TEST_CASE(11, ColumnDate); + RaC_TEST_CASE(12, ColumnDate32); + RaC_TEST_CASE(13, ColumnDateTime); + default: { + EXPECT_NE(0, 0); + break; + } + } + } +} + +TEST(ColumnsCase, RawVector) { + std::vector> columns; + + #define RV_TEST_CASE(test_id_in, column_type) \ + case test_id_in: { \ + columns.push_back(std::make_shared()); \ + auto column = std::static_pointer_cast(columns[test_id_in]); \ + column->Append(10u); \ + column->Append(20u); \ + ASSERT_EQ(columns[test_id_in]->Size(), 2u); \ + auto column_v = column->GetRawVector(); \ + ASSERT_EQ(static_cast(column_v[0]), 10u); \ + ASSERT_EQ(static_cast(column_v[1]), 20u); \ + break; \ + } + + #define RV_TEST_CASE_D(test_id_in, column_type) \ + case test_id_in: { \ + columns.push_back(std::make_shared()); \ + auto column = std::static_pointer_cast(columns[test_id_in]); \ + column->AppendRaw(10u); \ + column->AppendRaw(20u); \ + ASSERT_EQ(columns[test_id_in]->Size(), 2u); \ + auto column_v = column->GetRawVector(); \ + ASSERT_EQ(static_cast(column_v[0]), 10u); \ + ASSERT_EQ(static_cast(column_v[1]), 20u); \ + break; \ + } + + for (uint8_t rv_test_id = 0; rv_test_id < 14; ++rv_test_id) { + switch (rv_test_id) { + RV_TEST_CASE( 0, ColumnUInt8); + RV_TEST_CASE( 1, ColumnUInt16); + RV_TEST_CASE( 2, ColumnUInt32); + RV_TEST_CASE( 3, ColumnUInt64); + RV_TEST_CASE( 4, ColumnInt8); + RV_TEST_CASE( 5, ColumnInt16); + RV_TEST_CASE( 6, ColumnInt32); + RV_TEST_CASE( 7, ColumnInt64); + RV_TEST_CASE( 8, ColumnInt128); + RV_TEST_CASE( 9, ColumnFloat32); + RV_TEST_CASE( 10, ColumnFloat64); + RV_TEST_CASE_D( 11, ColumnDate); + RV_TEST_CASE_D( 12, ColumnDate32); + RV_TEST_CASE_D( 13, ColumnDateTime); + default: { + EXPECT_NE(0, 0); + break; + } + } + } +}