Skip to content

Commit b297a6e

Browse files
authored
Merge pull request #320 from Enmk/Date_and_Date32_immediate_value_access
Implemented Append/RawAt/operator[] for ColumnDate and ColumnDate32 that work with uint16_t/int32_t
2 parents eb64b66 + 7c9d167 commit b297a6e

26 files changed

+437
-128
lines changed

clickhouse/CMakeLists.txt

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ SET ( clickhouse-cpp-lib-src
1717
columns/ip4.cpp
1818
columns/ip6.cpp
1919
columns/lowcardinality.cpp
20-
columns/lowcardinalityadaptor.h
2120
columns/nullable.cpp
2221
columns/numeric.cpp
2322
columns/map.cpp
@@ -33,6 +32,55 @@ SET ( clickhouse-cpp-lib-src
3332
block.cpp
3433
client.cpp
3534
query.cpp
35+
36+
# Headers
37+
base/buffer.h
38+
base/compressed.h
39+
base/endpoints_iterator.h
40+
base/input.h
41+
base/open_telemetry.h
42+
base/output.h
43+
base/platform.h
44+
base/projected_iterator.h
45+
base/singleton.h
46+
base/socket.h
47+
base/sslsocket.h
48+
base/string_utils.h
49+
base/string_view.h
50+
base/uuid.h
51+
base/wire_format.h
52+
53+
columns/array.h
54+
columns/column.h
55+
columns/date.h
56+
columns/decimal.h
57+
columns/enum.h
58+
columns/factory.h
59+
columns/geo.h
60+
columns/ip4.h
61+
columns/ip6.h
62+
columns/itemview.h
63+
columns/lowcardinality.h
64+
columns/lowcardinalityadaptor.h
65+
columns/map.h
66+
columns/nothing.h
67+
columns/nullable.h
68+
columns/numeric.h
69+
columns/string.h
70+
columns/tuple.h
71+
columns/utils.h
72+
columns/uuid.h
73+
74+
types/type_parser.h
75+
types/types.h
76+
77+
block.h
78+
client.h
79+
error_codes.h
80+
exceptions.h
81+
protocol.h
82+
query.h
83+
server_exception.h
3684
)
3785

3886
if (MSVC)

clickhouse/columns/date.cpp

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "date.h"
2+
#include <cstdint>
23

34
namespace clickhouse {
45

@@ -9,7 +10,7 @@ ColumnDate::ColumnDate()
910
}
1011

1112
void ColumnDate::Append(const std::time_t& value) {
12-
/// TODO: This code is fundamentally wrong.
13+
/// The implementation is fundamentally wrong, ignores timezones, leap years and daylight saving.
1314
data_->Append(static_cast<uint16_t>(value / std::time_t(86400)));
1415
}
1516

@@ -18,9 +19,18 @@ void ColumnDate::Clear() {
1819
}
1920

2021
std::time_t ColumnDate::At(size_t n) const {
22+
/// The implementation is fundamentally wrong, ignores timezones, leap years and daylight saving.
2123
return static_cast<std::time_t>(data_->At(n)) * 86400;
2224
}
2325

26+
void ColumnDate::AppendRaw(uint16_t value) {
27+
data_->Append(value);
28+
}
29+
30+
uint16_t ColumnDate::RawAt(size_t n) const {
31+
return data_->At(n);
32+
}
33+
2434
void ColumnDate::Append(ColumnRef column) {
2535
if (auto col = column->As<ColumnDate>()) {
2636
data_->Append(col->data_);
@@ -62,15 +72,14 @@ ItemView ColumnDate::GetItem(size_t index) const {
6272
}
6373

6474

65-
6675
ColumnDate32::ColumnDate32()
6776
: Column(Type::CreateDate32())
6877
, data_(std::make_shared<ColumnInt32>())
6978
{
7079
}
7180

7281
void ColumnDate32::Append(const std::time_t& value) {
73-
/// TODO: This code is fundamentally wrong.
82+
/// The implementation is fundamentally wrong, ignores timezones, leap years and daylight saving.
7483
data_->Append(static_cast<int32_t>(value / std::time_t(86400)));
7584
}
7685

@@ -79,6 +88,7 @@ void ColumnDate32::Clear() {
7988
}
8089

8190
std::time_t ColumnDate32::At(size_t n) const {
91+
/// The implementation is fundamentally wrong, ignores timezones, leap years and daylight saving.
8292
return static_cast<std::time_t>(data_->At(n)) * 86400;
8393
}
8494

@@ -88,6 +98,14 @@ void ColumnDate32::Append(ColumnRef column) {
8898
}
8999
}
90100

101+
void ColumnDate32::AppendRaw(int32_t value) {
102+
data_->Append(value);
103+
}
104+
105+
int32_t ColumnDate32::RawAt(size_t n) const {
106+
return data_->At(n);
107+
}
108+
91109
bool ColumnDate32::LoadBody(InputStream* input, size_t rows) {
92110
return data_->LoadBody(input, rows);
93111
}
@@ -122,7 +140,6 @@ ItemView ColumnDate32::GetItem(size_t index) const {
122140
return ItemView{Type()->GetCode(), data_->GetItem(index)};
123141
}
124142

125-
126143
ColumnDateTime::ColumnDateTime()
127144
: Column(Type::CreateDateTime())
128145
, data_(std::make_shared<ColumnUInt32>())
@@ -241,6 +258,7 @@ void ColumnDateTime64::SaveBody(OutputStream* output) {
241258
void ColumnDateTime64::Clear() {
242259
data_->Clear();
243260
}
261+
244262
size_t ColumnDateTime64::Size() const {
245263
return data_->Size();
246264
}

clickhouse/columns/date.h

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,17 @@ class ColumnDate : public Column {
1515
ColumnDate();
1616

1717
/// Appends one element to the end of column.
18-
/// TODO: The implementation is fundamentally wrong.
18+
/// The implementation is fundamentally wrong, ignores timezones, leap years and daylight saving.
1919
void Append(const std::time_t& value);
2020

2121
/// Returns element at given row number.
22-
/// TODO: The implementation is fundamentally wrong.
22+
/// The implementation is fundamentally wrong, ignores timezones, leap years and daylight saving.
2323
std::time_t At(size_t n) const;
24+
inline std::time_t operator [] (size_t n) const { return At(n); }
25+
26+
/// Do append data as is -- number of day in Unix epoch, no conversions performed.
27+
void AppendRaw(uint16_t value);
28+
uint16_t RawAt(size_t n) const;
2429

2530
/// Appends content of given column to the end of current one.
2631
void Append(ColumnRef column) override;
@@ -56,16 +61,22 @@ class ColumnDate32 : public Column {
5661
ColumnDate32();
5762

5863
/// Appends one element to the end of column.
59-
/// TODO: The implementation is fundamentally wrong.
64+
/// The implementation is fundamentally wrong, ignores timezones, leap years and daylight saving.
6065
void Append(const std::time_t& value);
6166

6267
/// Returns element at given row number.
63-
/// TODO: The implementation is fundamentally wrong.
68+
/// The implementation is fundamentally wrong, ignores timezones, leap years and daylight saving.
6469
std::time_t At(size_t n) const;
6570

6671
/// Appends content of given column to the end of current one.
6772
void Append(ColumnRef column) override;
6873

74+
inline std::time_t operator [] (size_t n) const { return At(n); }
75+
76+
/// Do append data as is -- number of day in Unix epoch (32bit signed), no conversions performed.
77+
void AppendRaw(int32_t value);
78+
int32_t RawAt(size_t n) const;
79+
6980
/// Loads column data from input stream.
7081
bool LoadBody(InputStream* input, size_t rows) override;
7182

@@ -90,7 +101,7 @@ class ColumnDate32 : public Column {
90101
};
91102

92103

93-
/** */
104+
/** DateTime64 supports date-time values (number of seconds since UNIX epoch), from 1970 up to 2130. */
94105
class ColumnDateTime : public Column {
95106
public:
96107
using ValueType = std::time_t;
@@ -103,6 +114,7 @@ class ColumnDateTime : public Column {
103114

104115
/// Returns element at given row number.
105116
std::time_t At(size_t n) const;
117+
inline std::time_t operator [] (size_t n) const { return At(n); }
106118

107119
/// Timezone associated with a data column.
108120
std::string Timezone() const;
@@ -135,7 +147,7 @@ class ColumnDateTime : public Column {
135147
};
136148

137149

138-
/** */
150+
/** DateTime64 supports date-time values of arbitrary sub-second precision, from 1900 up to 2300. */
139151
class ColumnDateTime64 : public Column {
140152
public:
141153
using ValueType = Int64;
@@ -152,6 +164,8 @@ class ColumnDateTime64 : public Column {
152164
/// Returns element at given row number.
153165
Int64 At(size_t n) const;
154166

167+
inline Int64 operator[](size_t n) const { return At(n); }
168+
155169
/// Timezone associated with a data column.
156170
std::string Timezone() const;
157171

clickhouse/columns/decimal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class ColumnDecimal : public Column {
1818
void Append(const std::string& value);
1919

2020
Int128 At(size_t i) const;
21+
inline auto operator[](size_t i) const { return At(i); }
2122

2223
public:
2324
void Append(ColumnRef column) override;

clickhouse/columns/enum.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,6 @@ std::string_view ColumnEnum<T>::NameAt(size_t n) const {
5555
return type_->As<EnumType>()->GetEnumName(data_.at(n));
5656
}
5757

58-
template <typename T>
59-
const T& ColumnEnum<T>::operator[] (size_t n) const {
60-
return data_[n];
61-
}
62-
6358
template <typename T>
6459
void ColumnEnum<T>::SetAt(size_t n, const T& value, bool checkValue) {
6560
if (checkValue) {

clickhouse/columns/enum.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class ColumnEnum : public Column {
2323
std::string_view NameAt(size_t n) const;
2424

2525
/// Returns element at given row number.
26-
const T& operator[] (size_t n) const;
26+
inline const T& operator[] (size_t n) const { return At(n); }
2727

2828
/// Set element at given row number.
2929
void SetAt(size_t n, const T& value, bool checkValue = false);

clickhouse/columns/geo.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,6 @@ const typename ColumnGeo<NestedColumnType, type_code>::ValueType ColumnGeo<Neste
5454
return data_->At(n);
5555
}
5656

57-
template <typename NestedColumnType, Type::Code type_code>
58-
const typename ColumnGeo<NestedColumnType, type_code>::ValueType ColumnGeo<NestedColumnType, type_code>::operator[](size_t n) const {
59-
return data_->At(n);
60-
}
61-
6257
template <typename NestedColumnType, Type::Code type_code>
6358
void ColumnGeo<NestedColumnType, type_code>::Append(ColumnRef column) {
6459
if (auto col = column->template As<ColumnGeo>()) {

clickhouse/columns/geo.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class ColumnGeo : public Column {
2626
const ValueType At(size_t n) const;
2727

2828
/// Returns element at given row number.
29-
const ValueType operator[](size_t n) const;
29+
inline const ValueType operator[](size_t n) const { return At(n); }
3030

3131
public:
3232
/// Appends content of given column to the end of current one.

clickhouse/columns/map.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ class ColumnMapT : public ColumnMap {
212212

213213
inline auto At(size_t index) const { return MapValueView{typed_data_->At(index)}; }
214214

215-
inline auto operator[](size_t index) const { return MapValueView{typed_data_->At(index)}; }
215+
inline auto operator[](size_t index) const { return At(index); }
216216

217217
using ColumnMap::Append;
218218

clickhouse/columns/nothing.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class ColumnNothing : public Column {
3333
std::nullptr_t At(size_t) const { return nullptr; };
3434

3535
/// Returns element at given row number.
36-
std::nullptr_t operator [] (size_t) const { return nullptr; };
36+
inline std::nullptr_t operator [] (size_t) const { return nullptr; };
3737

3838
/// Makes slice of the current column.
3939
ColumnRef Slice(size_t, size_t len) const override {

0 commit comments

Comments
 (0)