Skip to content

Commit 629cd32

Browse files
tshevEnmk
authored andcommitted
[Issue-36] Use Int64 for datetime, add tests for DateTime64, and getters for Decimals and DateTime64
1 parent 5014e71 commit 629cd32

File tree

7 files changed

+60
-13
lines changed

7 files changed

+60
-13
lines changed

clickhouse/columns/date.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -113,28 +113,23 @@ 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-
122116
ColumnDateTime64::ColumnDateTime64(size_t precision)
123117
: Column(Type::CreateDateTime64(precision))
124118
, data_(std::make_shared<ColumnDecimal>(18ul, precision))
125119
{}
126120

127121

128-
void ColumnDateTime64::Append(const Int128& value) {
122+
void ColumnDateTime64::Append(const Int64& value) {
123+
// TODO: we need a type, which represents datetime
124+
// The precision of Poco.DateTime is not big enough.
129125
data_->Append(value);
130126
}
131127

132128
void ColumnDateTime64::Append(const std::string& value) {
133129
data_->Append(value);
134130
}
135131

136-
137-
Int128 ColumnDateTime64::At(size_t n) const {
132+
Int64 ColumnDateTime64::At(size_t n) const {
138133
return data_->At(n);
139134
}
140135

@@ -178,4 +173,8 @@ ColumnRef ColumnDateTime64::Slice(size_t begin, size_t len) {
178173
return result;
179174
}
180175

176+
size_t ColumnDateTime64::GetPrecision() const {
177+
return data_->Type()->As<DecimalType>()->GetScale();
178+
}
179+
181180
}

clickhouse/columns/date.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,14 @@ class ColumnDateTime : public Column {
8787
/** */
8888
class ColumnDateTime64 : public Column {
8989
public:
90-
ColumnDateTime64();
91-
ColumnDateTime64(size_t);
90+
explicit ColumnDateTime64(size_t);
9291

9392
/// Appends one element to the end of column.
94-
void Append(const Int128& value);
93+
void Append(const Int64& value);
9594
void Append(const std::string& value);
9695

9796
/// Returns element at given row number.
98-
Int128 At(size_t n) const;
97+
Int64 At(size_t n) const;
9998

10099
public:
101100
/// Appends content of given column to the end of current one.
@@ -119,6 +118,8 @@ class ColumnDateTime64 : public Column {
119118
void Swap(Column& other) override;
120119

121120
ItemView GetItem(size_t index) const override;
121+
122+
size_t GetPrecision() const;
122123
private:
123124
std::shared_ptr<ColumnDecimal> data_;
124125
};

clickhouse/columns/numeric.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class ColumnVector : public Column {
5454
};
5555

5656
using Int128 = __int128;
57+
using Int64 = int64_t;
5758

5859
using ColumnUInt8 = ColumnVector<uint8_t>;
5960
using ColumnUInt16 = ColumnVector<uint16_t>;

clickhouse/types/type_parser.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
namespace clickhouse {
1010

1111
bool TypeAst::operator==(const TypeAst & other) const {
12+
// TODO: operator == must be a friend function.
1213
return meta == other.meta
1314
&& code == other.code
1415
&& name == other.name

clickhouse/types/types.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ class DecimalType : public Type {
142142
std::string GetName() const;
143143

144144
inline size_t GetScale() const { return scale_; }
145+
inline size_t GetPrecision() const { return precision_; }
145146

146147
private:
147148
const size_t precision_, scale_;

tests/simple/main.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include <stdexcept>
66
#include <iostream>
7+
#include <cmath>
78

89
#if defined(_MSC_VER)
910
# pragma warning(disable : 4996)
@@ -129,6 +130,36 @@ inline void DateExample(Client& client) {
129130
client.Execute("DROP TABLE test.date");
130131
}
131132

133+
inline void DateTime64Example(Client& client) {
134+
Block b;
135+
136+
/// Create a table.
137+
client.Execute("CREATE TABLE IF NOT EXISTS test.date (d DateTime64(6)) ENGINE = Memory");
138+
139+
size_t precision = 6ul;
140+
auto d = std::make_shared<ColumnDateTime64>(precision);
141+
assert(d->GetPrecision() == precision);
142+
Int64 precision_multiplier = std::pow(10ull, precision);
143+
Int64 datetime = Int64(std::time(nullptr)) * precision_multiplier;
144+
145+
d->Append(datetime);
146+
b.AppendColumn("d", d);
147+
client.Insert("test.date", b);
148+
149+
client.Select("SELECT d FROM test.date", [precision_multiplier](const Block& block)
150+
{
151+
for (size_t c = 0; c < block.GetRowCount(); ++c) {
152+
auto col = block[0]->As<ColumnDateTime64>();
153+
Int64 t = col->At(c) / precision_multiplier;
154+
std::cerr << std::asctime(std::localtime(&t)) << " " << std::endl;
155+
}
156+
}
157+
);
158+
159+
/// Delete table.
160+
client.Execute("DROP TABLE test.date");
161+
}
162+
132163
inline void DecimalExample(Client& client) {
133164
Block b;
134165

@@ -442,6 +473,7 @@ static void RunTests(Client& client) {
442473
ArrayExample(client);
443474
CancelableExample(client);
444475
DateExample(client);
476+
DateTime64Example(client);
445477
DecimalExample(client);
446478
EnumExample(client);
447479
ExecptionExample(client);

ut/columns_ut.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,18 @@ TEST(ColumnsCase, DateAppend) {
132132
ASSERT_EQ(col2->At(0), (now / 86400) * 86400);
133133
}
134134

135+
TEST(ColumnsCase, DateTime64Precision) {
136+
auto column = std::make_shared<ColumnDateTime64>(6ul);
137+
ASSERT_EQ(column->GetPrecision(), 6ul);
138+
}
139+
140+
TEST(ColumnsCase, DateTime64Append) {
141+
auto column = std::make_shared<ColumnDateTime64>(6ul);
142+
column->Append(Int64(1ll));
143+
ASSERT_EQ(column->Size(), 1ul);
144+
ASSERT_EQ(column->At(0), Int64(1ll));
145+
}
146+
135147
TEST(ColumnsCase, Date2038) {
136148
auto col1 = std::make_shared<ColumnDate>();
137149
std::time_t largeDate(25882ul * 86400ul);

0 commit comments

Comments
 (0)