Skip to content

Commit 64bdd77

Browse files
tshevEnmk
authored andcommitted
[Issue-36] Add support of DateTime64
1 parent 4ceb5d1 commit 64bdd77

File tree

8 files changed

+148
-0
lines changed

8 files changed

+148
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,3 +274,7 @@ BUCKAROO_DEPS
274274

275275
# Visual Studio Code
276276
/.vscode/
277+
278+
# Vim
279+
*.swp
280+
*.swo

clickhouse/columns/date.cpp

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,69 @@ ItemView ColumnDateTime::GetItem(size_t index) const {
113113
return data_->GetItem(index);
114114
}
115115

116+
117+
ColumnDateTime64::ColumnDateTime64()
118+
: Column(Type::CreateDateTime64(3ul))
119+
, data_(std::make_shared<ColumnDecimal>(18ul, 3ul))
120+
{}
121+
122+
ColumnDateTime64::ColumnDateTime64(size_t precision)
123+
: Column(Type::CreateDateTime64(precision))
124+
, data_(std::make_shared<ColumnDecimal>(18ul, precision))
125+
{}
126+
127+
128+
void ColumnDateTime64::Append(const Int128& value) {
129+
data_->Append(value);
130+
}
131+
132+
void ColumnDateTime64::Append(const std::string& value) {
133+
data_->Append(value);
134+
}
135+
136+
137+
Int128 ColumnDateTime64::At(size_t n) const {
138+
return data_->At(n);
139+
}
140+
141+
void ColumnDateTime64::Append(ColumnRef column) {
142+
if (auto col = column->As<ColumnDateTime64>()) {
143+
data_->Append(col->data_);
144+
}
145+
}
146+
147+
bool ColumnDateTime64::Load(CodedInputStream* input, size_t rows) {
148+
return data_->Load(input, rows);
149+
}
150+
151+
void ColumnDateTime64::Save(CodedOutputStream* output) {
152+
data_->Save(output);
153+
}
154+
155+
void ColumnDateTime64::Clear() {
156+
data_->Clear();
157+
}
158+
size_t ColumnDateTime64::Size() const {
159+
return data_->Size();
160+
}
161+
162+
ItemView ColumnDateTime64::GetItem(size_t index) const {
163+
return data_->GetItem(index);
164+
}
165+
166+
void ColumnDateTime64::Swap(Column& other) {
167+
auto& col = dynamic_cast<ColumnDateTime64&>(other);
168+
data_.swap(col.data_);
169+
}
170+
171+
ColumnRef ColumnDateTime64::Slice(size_t begin, size_t len) {
172+
auto col = data_->Slice(begin, len)->As<ColumnDecimal>();
173+
size_t precision = col->Type()->As<DateTime64Type>()->GetPrecision();
174+
auto result = std::make_shared<ColumnDateTime64>(precision); // TODO FIXME
175+
176+
result->data_->Append(col);
177+
178+
return result;
179+
}
180+
116181
}

clickhouse/columns/date.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22

3+
#include "decimal.h"
34
#include "numeric.h"
45

56
#include <ctime>
@@ -82,4 +83,44 @@ class ColumnDateTime : public Column {
8283
std::shared_ptr<ColumnUInt32> data_;
8384
};
8485

86+
87+
/** */
88+
class ColumnDateTime64 : public Column {
89+
public:
90+
ColumnDateTime64();
91+
ColumnDateTime64(size_t);
92+
93+
/// Appends one element to the end of column.
94+
void Append(const Int128& value);
95+
void Append(const std::string& value);
96+
97+
/// Returns element at given row number.
98+
Int128 At(size_t n) const;
99+
100+
public:
101+
/// Appends content of given column to the end of current one.
102+
void Append(ColumnRef column) override;
103+
104+
/// Loads column data from input stream.
105+
bool Load(CodedInputStream* input, size_t rows) override;
106+
107+
/// Clear column data .
108+
void Clear() override;
109+
110+
/// Saves column data to output stream.
111+
void Save(CodedOutputStream* output) override;
112+
113+
/// Returns count of rows in the column.
114+
size_t Size() const override;
115+
116+
/// Makes slice of the current column.
117+
ColumnRef Slice(size_t begin, size_t len) override;
118+
119+
void Swap(Column& other) override;
120+
121+
ItemView GetItem(size_t index) const override;
122+
private:
123+
std::shared_ptr<ColumnDecimal> data_;
124+
};
125+
85126
}

clickhouse/columns/factory.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ static ColumnRef CreateTerminalColumn(const TypeAst& ast) {
6565

6666
case Type::DateTime:
6767
return std::make_shared<ColumnDateTime>();
68+
case Type::DateTime64:
69+
return std::make_shared<ColumnDateTime64>(ast.elements.front().value);
6870
case Type::Date:
6971
return std::make_shared<ColumnDate>();
7072

clickhouse/columns/itemview.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ void ItemView::ValidateData(Type::Code type, DataType data) {
2020
ANY, /*String*/
2121
ANY, /*FixedString*/
2222
4, /*DateTime*/
23+
8, /*DateTime64*/
2324
2, /*Date*/
2425
ERR, /*Array*/
2526
ERR, /*Nullable*/

clickhouse/types/type_parser.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ static const std::unordered_map<std::string, Type::Code> kTypeCode = {
3030
{ "String", Type::String },
3131
{ "FixedString", Type::FixedString },
3232
{ "DateTime", Type::DateTime },
33+
{ "DateTime64", Type::DateTime64 },
3334
{ "Date", Type::Date },
3435
{ "Array", Type::Array },
3536
{ "Nullable", Type::Nullable },

clickhouse/types/types.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ std::string Type::GetName() const {
4545
return "IPv6";
4646
case DateTime:
4747
return "DateTime";
48+
case DateTime64:
49+
return As<DateTime64Type>()->GetName();
4850
case Date:
4951
return "Date";
5052
case Array:
@@ -81,6 +83,10 @@ TypeRef Type::CreateDateTime() {
8183
return TypeRef(new Type(Type::DateTime));
8284
}
8385

86+
TypeRef Type::CreateDateTime64(size_t precision) {
87+
return TypeRef(new DateTime64Type(precision));
88+
}
89+
8490
TypeRef Type::CreateDecimal(size_t precision, size_t scale) {
8591
return TypeRef(new DecimalType(precision, scale));
8692
}
@@ -219,6 +225,19 @@ EnumType::ValueToNameIterator EnumType::EndValueToName() const {
219225
return value_to_name_.end();
220226
}
221227

228+
/// class DateTime64Type
229+
230+
DateTime64Type::DateTime64Type(size_t precision) : Type(DateTime64), precision_(precision) {}
231+
232+
std::string DateTime64Type::GetName() const {
233+
std::string datetime64_representation;
234+
datetime64_representation.reserve(14);
235+
datetime64_representation += "DateTime64(";
236+
datetime64_representation += std::to_string(precision_);
237+
datetime64_representation += ")";
238+
return datetime64_representation;
239+
}
240+
222241
/// class FixedStringType
223242

224243
FixedStringType::FixedStringType(size_t n) : Type(FixedString), size_(n) {

clickhouse/types/types.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class Type {
2626
String,
2727
FixedString,
2828
DateTime,
29+
DateTime64,
2930
Date,
3031
Array,
3132
Nullable,
@@ -76,6 +77,8 @@ class Type {
7677

7778
static TypeRef CreateDateTime();
7879

80+
static TypeRef CreateDateTime64(size_t precision);
81+
7982
static TypeRef CreateDecimal(size_t precision, size_t scale);
8083

8184
static TypeRef CreateIPv4();
@@ -144,6 +147,18 @@ class DecimalType : public Type {
144147
const size_t precision_, scale_;
145148
};
146149

150+
class DateTime64Type: public Type {
151+
public:
152+
DateTime64Type(size_t precision);
153+
154+
std::string GetName() const;
155+
156+
inline size_t GetPrecision() const { return precision_; }
157+
158+
private:
159+
size_t precision_;
160+
};
161+
147162
class EnumType : public Type {
148163
public:
149164
EnumType(Type::Code type, const std::vector<EnumItem>& items);

0 commit comments

Comments
 (0)