Skip to content

Commit abc3327

Browse files
committed
Support Date32 data type.
1 parent 87fc118 commit abc3327

File tree

6 files changed

+113
-0
lines changed

6 files changed

+113
-0
lines changed

clickhouse/columns/date.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,63 @@ ItemView ColumnDate::GetItem(size_t index) const {
5959

6060

6161

62+
ColumnDate32::ColumnDate32()
63+
: Column(Type::CreateDate32())
64+
, data_(std::make_shared<ColumnUInt32>())
65+
{
66+
}
67+
68+
void ColumnDate32::Append(const std::time_t& value) {
69+
/// TODO: This code is fundamentally wrong.
70+
data_->Append(static_cast<uint16_t>(value / std::time_t(86400)));
71+
}
72+
73+
void ColumnDate32::Clear() {
74+
data_->Clear();
75+
}
76+
77+
std::time_t ColumnDate32::At(size_t n) const {
78+
return static_cast<std::time_t>(data_->At(n)) * 86400;
79+
}
80+
81+
void ColumnDate32::Append(ColumnRef column) {
82+
if (auto col = column->As<ColumnDate32>()) {
83+
data_->Append(col->data_);
84+
}
85+
}
86+
87+
bool ColumnDate32::Load(InputStream* input, size_t rows) {
88+
return data_->Load(input, rows);
89+
}
90+
91+
void ColumnDate32::Save(OutputStream* output) {
92+
data_->Save(output);
93+
}
94+
95+
size_t ColumnDate32::Size() const {
96+
return data_->Size();
97+
}
98+
99+
ColumnRef ColumnDate32::Slice(size_t begin, size_t len) const {
100+
auto col = data_->Slice(begin, len)->As<ColumnUInt16>();
101+
auto result = std::make_shared<ColumnDate32>();
102+
103+
result->data_->Append(col);
104+
105+
return result;
106+
}
107+
108+
void ColumnDate32::Swap(Column& other) {
109+
auto & col = dynamic_cast<ColumnDate32 &>(other);
110+
data_.swap(col.data_);
111+
}
112+
113+
ItemView ColumnDate32::GetItem(size_t index) const {
114+
return data_->GetItem(index);
115+
}
116+
117+
118+
62119
ColumnDateTime::ColumnDateTime()
63120
: Column(Type::CreateDateTime())
64121
, data_(std::make_shared<ColumnUInt32>())

clickhouse/columns/date.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,49 @@ class ColumnDate : public Column {
4848
std::shared_ptr<ColumnUInt16> data_;
4949
};
5050

51+
52+
/** */
53+
class ColumnDate32 : public Column {
54+
public:
55+
using ValueType = std::time_t;
56+
57+
ColumnDate32();
58+
59+
/// Appends one element to the end of column.
60+
/// TODO: The implementation is fundamentally wrong.
61+
void Append(const std::time_t& value);
62+
63+
/// Returns element at given row number.
64+
/// TODO: The implementation is fundamentally wrong.
65+
std::time_t At(size_t n) const;
66+
67+
/// Appends content of given column to the end of current one.
68+
void Append(ColumnRef column) override;
69+
70+
/// Loads column data from input stream.
71+
bool Load(InputStream* input, size_t rows) override;
72+
73+
/// Saves column data to output stream.
74+
void Save(OutputStream* output) override;
75+
76+
/// Clear column data .
77+
void Clear() override;
78+
79+
/// Returns count of rows in the column.
80+
size_t Size() const override;
81+
82+
/// Makes slice of the current column.
83+
ColumnRef Slice(size_t begin, size_t len) const override;
84+
85+
void Swap(Column& other) override;
86+
87+
ItemView GetItem(size_t index) const override;
88+
89+
private:
90+
std::shared_ptr<ColumnUInt32> data_;
91+
};
92+
93+
5194
/** */
5295
class ColumnDateTime : public Column {
5396
public:

clickhouse/columns/factory.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ static ColumnRef CreateTerminalColumn(const TypeAst& ast) {
8585
}
8686
case Type::Date:
8787
return std::make_shared<ColumnDate>();
88+
case Type::Date32:
89+
return std::make_shared<ColumnDate32>();
8890

8991
case Type::IPv4:
9092
return std::make_shared<ColumnIPv4>();

clickhouse/types/type_parser.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ static const std::unordered_map<std::string, Type::Code> kTypeCode = {
3232
{ "DateTime", Type::DateTime },
3333
{ "DateTime64", Type::DateTime64 },
3434
{ "Date", Type::Date },
35+
{ "Date32", Type::Date32 },
3536
{ "Array", Type::Array },
3637
{ "Nullable", Type::Nullable },
3738
{ "Tuple", Type::Tuple },

clickhouse/types/types.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ std::string Type::GetName() const {
5757
return As<DateTime64Type>()->GetName();
5858
case Date:
5959
return "Date";
60+
case Date32:
61+
return "Date32";
6062
case Array:
6163
return As<ArrayType>()->GetName();
6264
case Nullable:
@@ -101,6 +103,7 @@ uint64_t Type::GetTypeUniqueId() const {
101103
case IPv4:
102104
case IPv6:
103105
case Date:
106+
case Date32:
104107
// For simple types, unique ID is the same as Type::Code
105108
return code_;
106109

@@ -143,6 +146,10 @@ TypeRef Type::CreateDate() {
143146
return TypeRef(new Type(Type::Date));
144147
}
145148

149+
TypeRef Type::CreateDate32() {
150+
return TypeRef(new Type(Type::Date32));
151+
}
152+
146153
TypeRef Type::CreateDateTime(std::string timezone) {
147154
return TypeRef(new DateTimeType(std::move(timezone)));
148155
}

clickhouse/types/types.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class Type {
3434
FixedString,
3535
DateTime,
3636
Date,
37+
Date32,
3738
Array,
3839
Nullable,
3940
Tuple,
@@ -88,6 +89,8 @@ class Type {
8889

8990
static TypeRef CreateDate();
9091

92+
static TypeRef CreateDate32();
93+
9194
static TypeRef CreateDateTime(std::string timezone = std::string());
9295

9396
static TypeRef CreateDateTime64(size_t precision, std::string timezone = std::string());

0 commit comments

Comments
 (0)