Skip to content

Commit

Permalink
Fixed tests to generate time_t values to avoid ambiguity
Browse files Browse the repository at this point in the history
  • Loading branch information
Enmk committed Aug 3, 2023
1 parent dc62ca9 commit ad07a79
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 21 deletions.
10 changes: 8 additions & 2 deletions clickhouse/columns/date.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@ std::time_t ColumnDate::At(size_t n) const {
return static_cast<std::time_t>(data_->At(n)) * 86400;
}


void ColumnDate::Append(uint16_t value) {
data_->Append(value);
}

void ColumnDate::AppendRaw(uint16_t value) {
data_->Append(value);
}

uint16_t ColumnDate::RawAt(size_t n) const {
return data_->At(n);
}
Expand Down Expand Up @@ -77,7 +80,6 @@ ItemView ColumnDate::GetItem(size_t index) const {
}



ColumnDate32::ColumnDate32()
: Column(Type::CreateDate32())
, data_(std::make_shared<ColumnInt32>())
Expand Down Expand Up @@ -108,6 +110,10 @@ void ColumnDate32::Append(int32_t value) {
data_->Append(value);
}

void ColumnDate32::AppendRaw(int32_t value) {
data_->Append(value);
}

int32_t ColumnDate32::RawAt(size_t n) const {
return data_->At(n);
}
Expand Down
4 changes: 2 additions & 2 deletions clickhouse/columns/date.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class ColumnDate : public Column {

/// Do data as is -- number of day in Unix epoch, no conversions performed
void Append(uint16_t value);
void AppendRaw(uint16_t value) { Append(value); }
void AppendRaw(uint16_t value);
uint16_t RawAt(size_t n) const;
uint16_t operator [] (size_t n) const;

Expand Down Expand Up @@ -74,7 +74,7 @@ class ColumnDate32 : public Column {

/// Do data as is -- number of day in Unix epoch, no conversions performed
void Append(int32_t value);
void AppendRaw(int32_t value) { Append(value); }
void AppendRaw(int32_t value);
int32_t RawAt(size_t n) const;
int32_t operator [] (size_t n) const;

Expand Down
10 changes: 9 additions & 1 deletion ut/Column_ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class GenericColumnTest : public testing::Test {
} else if constexpr (std::is_same_v<ColumnType, ColumnFixedString>) {
return GenerateVector(values_size, FromVectorGenerator{MakeFixedStrings(12)});
} else if constexpr (std::is_same_v<ColumnType, ColumnDate>) {
return GenerateVector(values_size, FromVectorGenerator{MakeDates()});
return GenerateVector(values_size, FromVectorGenerator{MakeDates<time_t>()});
} else if constexpr (std::is_same_v<ColumnType, ColumnDateTime>) {
return GenerateVector(values_size, FromVectorGenerator{MakeDateTimes()});
} else if constexpr (std::is_same_v<ColumnType, ColumnDateTime64>) {
Expand Down Expand Up @@ -143,6 +143,14 @@ using ValueColumns = ::testing::Types<
>;
TYPED_TEST_SUITE(GenericColumnTest, ValueColumns);

//TestCase(TypeCreatorFunc, ValuesGeneratorFunc)


//class GenericColumnTestCase
//{
// virutual
//};

TYPED_TEST(GenericColumnTest, Construct) {
auto column = this->MakeColumn();
ASSERT_EQ(0u, column->Size());
Expand Down
17 changes: 3 additions & 14 deletions ut/value_generators.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "value_generators.h"

#include <algorithm>
#include <type_traits>
#include <math.h>

namespace {
Expand Down Expand Up @@ -53,25 +54,13 @@ std::vector<Int64> MakeDateTime64s(size_t scale, size_t values_size) {
});
}

std::vector<clickhouse::Int64> MakeDates() {
// in CH Date internally a UInt16 and stores a day number
// ColumnDate expects values to be seconds, which is then
// converted to day number internally, hence the `* 86400`.
std::vector<clickhouse::Int64> result {0, 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536 - 1};
std::for_each(result.begin(), result.end(), [](auto& value) {
value *= 86400;
});

return result;
}

std::vector<clickhouse::Int64> MakeDates32() {
std::vector<int32_t> MakeDates32() {
// in CH Date32 internally a UInt32 and stores a day number
// ColumnDate expects values to be seconds, which is then
// converted to day number internally, hence the `* 86400`.
// 114634 * 86400 is 2282-11-10, last integer that fits into DateTime32 range
// (max is 2283-11-11)
std::vector<clickhouse::Int64> result = MakeDates();
std::vector<int32_t> result = MakeDates<int32_t>();

// add corresponding negative values, since pre-epoch date are supported too.
const auto size = result.size();
Expand Down
20 changes: 18 additions & 2 deletions ut/value_generators.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,31 @@ std::vector<uint8_t> MakeBools();
std::vector<std::string> MakeFixedStrings(size_t string_size);
std::vector<std::string> MakeStrings();
std::vector<clickhouse::Int64> MakeDateTime64s(size_t scale, size_t values_size = 200);
std::vector<clickhouse::Int64> MakeDates();
std::vector<clickhouse::Int64> MakeDates32();
std::vector<int32_t> MakeDates32();
std::vector<clickhouse::Int64> MakeDateTimes();
std::vector<in_addr> MakeIPv4s();
std::vector<in6_addr> MakeIPv6s();
std::vector<clickhouse::UUID> MakeUUIDs();
std::vector<clickhouse::Int128> MakeInt128s();
std::vector<clickhouse::Int128> MakeDecimals(size_t precision, size_t scale);

template <typename ResultType>
std::vector<ResultType> MakeDates() {
std::vector<ResultType> result {0, 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536 - 1};

if constexpr (std::is_same_v<time_t, ResultType>) {
// in CH Date internally a UInt16 and stores a day number
// ColumnDate expects values to be seconds, which is then
// converted to day number internally, hence the `* 86400`.
std::for_each(result.begin(), result.end(), [](auto& value) {
value *= 86400;
});
}

return result;
}


std::string FooBarGenerator(size_t i);

template <typename ValueType = void, typename Generator>
Expand Down

0 comments on commit ad07a79

Please sign in to comment.