Skip to content

Commit cd0b27a

Browse files
authored
Merge pull request #175 from huyphams/support-date32
Support Date32 data type, tests will follow
2 parents a87ce63 + bf4b978 commit cd0b27a

File tree

6 files changed

+115
-0
lines changed

6 files changed

+115
-0
lines changed

clickhouse/columns/date.cpp

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

6464

6565

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

clickhouse/columns/date.h

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

51+
52+
class ColumnDate32 : public Column {
53+
public:
54+
using ValueType = std::time_t;
55+
56+
ColumnDate32();
57+
58+
/// Appends one element to the end of column.
59+
/// TODO: The implementation is fundamentally wrong.
60+
void Append(const std::time_t& value);
61+
62+
/// Returns element at given row number.
63+
/// TODO: The implementation is fundamentally wrong.
64+
std::time_t At(size_t n) const;
65+
66+
/// Appends content of given column to the end of current one.
67+
void Append(ColumnRef column) override;
68+
69+
/// Loads column data from input stream.
70+
bool LoadBody(InputStream* input, size_t rows) override;
71+
72+
/// Saves column data to output stream.
73+
void SaveBody(OutputStream* output) override;
74+
75+
/// Clear column data .
76+
void Clear() override;
77+
78+
/// Returns count of rows in the column.
79+
size_t Size() const override;
80+
81+
/// Makes slice of the current column.
82+
ColumnRef Slice(size_t begin, size_t len) const override;
83+
ColumnRef CloneEmpty() const override;
84+
void Swap(Column& other) override;
85+
86+
ItemView GetItem(size_t index) const override;
87+
88+
private:
89+
std::shared_ptr<ColumnUInt32> data_;
90+
};
91+
92+
5193
/** */
5294
class ColumnDateTime : public Column {
5395
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
@@ -45,6 +45,7 @@ const char* Type::TypeName(Type::Code code) {
4545
case Type::Code::Decimal128: return "Decimal128";
4646
case Type::Code::LowCardinality: return "LowCardinality";
4747
case Type::Code::DateTime64: return "DateTime64";
48+
case Type::Code::Date32: return "Date32";
4849
}
4950

5051
return "Unknown type";
@@ -69,6 +70,7 @@ std::string Type::GetName() const {
6970
case IPv4:
7071
case IPv6:
7172
case Date:
73+
case Date32:
7274
return TypeName(code_);
7375
case FixedString:
7476
return As<FixedStringType>()->GetName();
@@ -120,6 +122,7 @@ uint64_t Type::GetTypeUniqueId() const {
120122
case IPv4:
121123
case IPv6:
122124
case Date:
125+
case Date32:
123126
// For simple types, unique ID is the same as Type::Code
124127
return code_;
125128

@@ -162,6 +165,10 @@ TypeRef Type::CreateDate() {
162165
return TypeRef(new Type(Type::Date));
163166
}
164167

168+
TypeRef Type::CreateDate32() {
169+
return TypeRef(new Type(Type::Date32));
170+
}
171+
165172
TypeRef Type::CreateDateTime(std::string timezone) {
166173
return TypeRef(new DateTimeType(std::move(timezone)));
167174
}

clickhouse/types/types.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class Type {
4949
Decimal128,
5050
LowCardinality,
5151
DateTime64,
52+
Date32,
5253
};
5354

5455
using EnumItem = std::pair<std::string /* name */, int16_t /* value */>;
@@ -91,6 +92,8 @@ class Type {
9192

9293
static TypeRef CreateDate();
9394

95+
static TypeRef CreateDate32();
96+
9497
static TypeRef CreateDateTime(std::string timezone = std::string());
9598

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

0 commit comments

Comments
 (0)