Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions clickhouse/columns/date.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ ItemView ColumnDate::GetItem(size_t index) const {

ColumnDate32::ColumnDate32()
: Column(Type::CreateDate32())
, data_(std::make_shared<ColumnUInt32>())
, data_(std::make_shared<ColumnInt32>())
{
}

void ColumnDate32::Append(const std::time_t& value) {
/// TODO: This code is fundamentally wrong.
data_->Append(static_cast<uint16_t>(value / std::time_t(86400)));
data_->Append(static_cast<int32_t>(value / std::time_t(86400)));
}

void ColumnDate32::Clear() {
Expand Down Expand Up @@ -101,7 +101,7 @@ size_t ColumnDate32::Size() const {
}

ColumnRef ColumnDate32::Slice(size_t begin, size_t len) const {
auto col = data_->Slice(begin, len)->As<ColumnUInt32>();
auto col = data_->Slice(begin, len)->As<ColumnInt32>();
auto result = std::make_shared<ColumnDate32>();

result->data_->Append(col);
Expand All @@ -119,7 +119,7 @@ void ColumnDate32::Swap(Column& other) {
}

ItemView ColumnDate32::GetItem(size_t index) const {
return data_->GetItem(index);
return ItemView{Type()->GetCode(), data_->GetItem(index)};
}


Expand Down
2 changes: 1 addition & 1 deletion clickhouse/columns/date.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class ColumnDate32 : public Column {
ItemView GetItem(size_t index) const override;

private:
std::shared_ptr<ColumnUInt32> data_;
std::shared_ptr<ColumnInt32> data_;
};


Expand Down
1 change: 1 addition & 0 deletions clickhouse/columns/itemview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ void ItemView::ValidateData(Type::Code type, DataType data) {
case Type::Code::UInt32:
case Type::Code::Float32:
case Type::Code::DateTime:
case Type::Code::Date32:
case Type::Code::IPv4:
case Type::Code::Decimal32:
return AssertSize({4});
Expand Down
3 changes: 2 additions & 1 deletion clickhouse/columns/itemview.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ struct ItemView {
if (sizeof(ValueType) == data.size()) {
return *reinterpret_cast<const T*>(data.data());
} else {
throw AssertionError("Incompatitable value type and size.");
throw AssertionError("Incompatitable value type and size. Requested size: "
+ std::to_string(sizeof(ValueType)) + " stored size: " + std::to_string(data.size()));
}
}
}
Expand Down
14 changes: 13 additions & 1 deletion ut/Column_ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ namespace {
using namespace clickhouse;
}

namespace clickhouse{

std::ostream& operator<<(std::ostream& ostr, const Type::Code& type_code) {
return ostr << Type::TypeName(type_code) << " (" << static_cast<int>(type_code) << ")";
}

}


// Generic tests for a Column subclass against basic API:
// 1. Constructor: Create, ensure that it is empty
Expand Down Expand Up @@ -60,6 +68,8 @@ class GenericColumnTest : public testing::Test {
return GenerateVector(values_size, FromVectorGenerator{MakeDateTimes()});
} else if constexpr (std::is_same_v<ColumnType, ColumnDateTime64>) {
return MakeDateTime64s(3u, values_size);
} else if constexpr (std::is_same_v<ColumnType, ColumnDate32>) {
return GenerateVector(values_size, FromVectorGenerator{MakeDates32()});
} else if constexpr (std::is_same_v<ColumnType, ColumnIPv4>) {
return GenerateVector(values_size, FromVectorGenerator{MakeIPv4s()});
} else if constexpr (std::is_same_v<ColumnType, ColumnIPv6>) {
Expand Down Expand Up @@ -100,7 +110,7 @@ using ValueColumns = ::testing::Types<
, ColumnInt8, ColumnInt16, ColumnInt32, ColumnInt64
, ColumnFloat32, ColumnFloat64
, ColumnString, ColumnFixedString
, ColumnDate, ColumnDateTime, ColumnDateTime64
, ColumnDate, ColumnDateTime, ColumnDateTime64, ColumnDate32
, ColumnIPv4, ColumnIPv6
, ColumnInt128
, ColumnDecimal
Expand Down Expand Up @@ -168,6 +178,8 @@ inline auto convertValueForGetItem(const ColumnType& col, ValueType&& t) {
return std::string_view(reinterpret_cast<const char*>(t.s6_addr), 16);
} else if constexpr (std::is_same_v<ColumnType, ColumnDate>) {
return static_cast<uint16_t>(t / std::time_t(86400));
} else if constexpr (std::is_same_v<ColumnType, ColumnDate32>) {
return static_cast<uint32_t>(t / std::time_t(86400));
} else if constexpr (std::is_same_v<ColumnType, ColumnDateTime>) {
return static_cast<uint32_t>(t);
} else {
Expand Down
17 changes: 17 additions & 0 deletions ut/value_generators.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,23 @@ std::vector<clickhouse::Int64> MakeDates() {
return result;
}

std::vector<clickhouse::Int64> 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();

// add corresponding negative values, since pre-epoch date are supported too.
const auto size = result.size();
for (size_t i = 0; i < size; ++i) {
result.push_back(result[i] * -1);
}

return result;
}

std::vector<clickhouse::Int64> MakeDateTimes() {
// in CH DateTime internally a UInt32
return {
Expand Down
1 change: 1 addition & 0 deletions ut/value_generators.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ 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<clickhouse::Int64> MakeDateTimes();
std::vector<in_addr> MakeIPv4s();
std::vector<in6_addr> MakeIPv6s();
Expand Down