Skip to content

Commit a162509

Browse files
committed
Add new methods for date types
1 parent 27e7126 commit a162509

File tree

9 files changed

+500
-5
lines changed

9 files changed

+500
-5
lines changed

clickhouse/columns/column.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,10 @@ class Column : public std::enable_shared_from_this<Column> {
8080
/// Returns count of rows in the column.
8181
virtual size_t Size() const = 0;
8282

83-
/// Increase the capacity of the column (currently only supported by numeric types at this time)
83+
/// Increase the capacity of the column (currently only supported by numeric/date types at this time)
8484
virtual void Reserve([[maybe_unused]]size_t new_cap) {}
8585

86-
/// Returns the capacity of the column (currently only supported by numeric types at this time)
86+
/// Returns the capacity of the column (currently only supported by numeric/date types at this time)
8787
virtual size_t Capacity() const { return Size(); }
8888

8989
/// Makes slice of the current column.

clickhouse/columns/date.cpp

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,18 @@ uint16_t ColumnDate::RawAt(size_t n) const {
3131
return data_->At(n);
3232
}
3333

34+
void ColumnDate::SwapElements(size_t pos1, size_t pos2) {
35+
data_->SwapElements(pos1, pos2);
36+
}
37+
38+
bool ColumnDate::CompareElementsGT(size_t pos1, size_t pos2) const {
39+
return data_->CompareElementsGT(pos1, pos2);
40+
}
41+
42+
bool ColumnDate::CompareElementsLT(size_t pos1, size_t pos2) const {
43+
return data_->CompareElementsLT(pos1, pos2);
44+
}
45+
3446
void ColumnDate::Append(ColumnRef column) {
3547
if (auto col = column->As<ColumnDate>()) {
3648
data_->Append(col->data_);
@@ -49,6 +61,14 @@ size_t ColumnDate::Size() const {
4961
return data_->Size();
5062
}
5163

64+
void ColumnDate::Reserve(size_t new_cap) {
65+
data_->Reserve(new_cap);
66+
}
67+
68+
size_t ColumnDate::Capacity() const {
69+
return data_->Capacity();
70+
}
71+
5272
ColumnRef ColumnDate::Slice(size_t begin, size_t len) const {
5373
auto col = data_->Slice(begin, len)->As<ColumnUInt16>();
5474
auto result = std::make_shared<ColumnDate>();
@@ -106,6 +126,18 @@ int32_t ColumnDate32::RawAt(size_t n) const {
106126
return data_->At(n);
107127
}
108128

129+
void ColumnDate32::SwapElements(size_t pos1, size_t pos2) {
130+
data_->SwapElements(pos1, pos2);
131+
}
132+
133+
bool ColumnDate32::CompareElementsGT(size_t pos1, size_t pos2) const {
134+
return data_->CompareElementsGT(pos1, pos2);
135+
}
136+
137+
bool ColumnDate32::CompareElementsLT(size_t pos1, size_t pos2) const {
138+
return data_->CompareElementsLT(pos1, pos2);
139+
}
140+
109141
bool ColumnDate32::LoadBody(InputStream* input, size_t rows) {
110142
return data_->LoadBody(input, rows);
111143
}
@@ -118,6 +150,14 @@ size_t ColumnDate32::Size() const {
118150
return data_->Size();
119151
}
120152

153+
void ColumnDate32::Reserve(size_t new_cap) {
154+
data_->Reserve(new_cap);
155+
}
156+
157+
size_t ColumnDate32::Capacity() const {
158+
return data_->Capacity();
159+
}
160+
121161
ColumnRef ColumnDate32::Slice(size_t begin, size_t len) const {
122162
auto col = data_->Slice(begin, len)->As<ColumnInt32>();
123163
auto result = std::make_shared<ColumnDate32>();
@@ -164,6 +204,18 @@ std::string ColumnDateTime::Timezone() const {
164204
return type_->As<DateTimeType>()->Timezone();
165205
}
166206

207+
void ColumnDateTime::SwapElements(size_t pos1, size_t pos2) {
208+
data_->SwapElements(pos1, pos2);
209+
}
210+
211+
bool ColumnDateTime::CompareElementsGT(size_t pos1, size_t pos2) const {
212+
return data_->CompareElementsGT(pos1, pos2);
213+
}
214+
215+
bool ColumnDateTime::CompareElementsLT(size_t pos1, size_t pos2) const {
216+
return data_->CompareElementsLT(pos1, pos2);
217+
}
218+
167219
void ColumnDateTime::Append(ColumnRef column) {
168220
if (auto col = column->As<ColumnDateTime>()) {
169221
data_->Append(col->data_);
@@ -182,6 +234,14 @@ size_t ColumnDateTime::Size() const {
182234
return data_->Size();
183235
}
184236

237+
void ColumnDateTime::Reserve(size_t new_cap) {
238+
data_->Reserve(new_cap);
239+
}
240+
241+
size_t ColumnDateTime::Capacity() const {
242+
return data_->Capacity();
243+
}
244+
185245
void ColumnDateTime::Clear() {
186246
data_->Clear();
187247
}
@@ -237,6 +297,18 @@ Int64 ColumnDateTime64::At(size_t n) const {
237297
return static_cast<Int64>(data_->At(n));
238298
}
239299

300+
void ColumnDateTime64::SwapElements(size_t pos1, size_t pos2) {
301+
data_->SwapElements(pos1, pos2);
302+
}
303+
304+
bool ColumnDateTime64::CompareElementsGT(size_t pos1, size_t pos2) const {
305+
return data_->CompareElementsGT(pos1, pos2);
306+
}
307+
308+
bool ColumnDateTime64::CompareElementsLT(size_t pos1, size_t pos2) const {
309+
return data_->CompareElementsLT(pos1, pos2);
310+
}
311+
240312
std::string ColumnDateTime64::Timezone() const {
241313
return type_->As<DateTime64Type>()->Timezone();
242314
}
@@ -263,6 +335,14 @@ size_t ColumnDateTime64::Size() const {
263335
return data_->Size();
264336
}
265337

338+
void ColumnDateTime64::Reserve(size_t new_cap) {
339+
data_->Reserve(new_cap);
340+
}
341+
342+
size_t ColumnDateTime64::Capacity() const {
343+
return data_->Capacity();
344+
}
345+
266346
ItemView ColumnDateTime64::GetItem(size_t index) const {
267347
return ItemView(Type::DateTime64, data_->GetItem(index));
268348
}

clickhouse/columns/date.h

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,17 @@ class ColumnDate : public Column {
2727
void AppendRaw(uint16_t value);
2828
uint16_t RawAt(size_t n) const;
2929

30+
/// Swap two Elements/rows in the column
31+
void SwapElements(size_t pos1, size_t pos2);
32+
33+
/// Test if the value at position 1 is greater than the value at position 2
34+
/// No range checking is performed for performance
35+
bool CompareElementsGT(size_t pos1, size_t pos2) const;
36+
37+
/// Test if the value at position 1 is less than the value at position 2
38+
/// No range checking is performed for performance
39+
bool CompareElementsLT(size_t pos1, size_t pos2) const;
40+
3041
/// Appends content of given column to the end of current one.
3142
void Append(ColumnRef column) override;
3243

@@ -42,6 +53,12 @@ class ColumnDate : public Column {
4253
/// Returns count of rows in the column.
4354
size_t Size() const override;
4455

56+
/// Increase the capacity of the column
57+
void Reserve(size_t new_cap) override;
58+
59+
/// Returns the capacity of the column
60+
size_t Capacity() const override;
61+
4562
/// Makes slice of the current column.
4663
ColumnRef Slice(size_t begin, size_t len) const override;
4764
ColumnRef CloneEmpty() const override;
@@ -77,6 +94,17 @@ class ColumnDate32 : public Column {
7794
void AppendRaw(int32_t value);
7895
int32_t RawAt(size_t n) const;
7996

97+
/// Swap two Elements/rows in the column
98+
void SwapElements(size_t pos1, size_t pos2);
99+
100+
/// Test if the value at position 1 is greater than the value at position 2
101+
/// No range checking is performed for performance
102+
bool CompareElementsGT(size_t pos1, size_t pos2) const;
103+
104+
/// Test if the value at position 1 is less than the value at position 2
105+
/// No range checking is performed for performance
106+
bool CompareElementsLT(size_t pos1, size_t pos2) const;
107+
80108
/// Loads column data from input stream.
81109
bool LoadBody(InputStream* input, size_t rows) override;
82110

@@ -89,6 +117,12 @@ class ColumnDate32 : public Column {
89117
/// Returns count of rows in the column.
90118
size_t Size() const override;
91119

120+
/// Increase the capacity of the column
121+
void Reserve(size_t new_cap) override;
122+
123+
/// Returns the capacity of the column
124+
size_t Capacity() const override;
125+
92126
/// Makes slice of the current column.
93127
ColumnRef Slice(size_t begin, size_t len) const override;
94128
ColumnRef CloneEmpty() const override;
@@ -119,6 +153,17 @@ class ColumnDateTime : public Column {
119153
/// Timezone associated with a data column.
120154
std::string Timezone() const;
121155

156+
/// Swap two Elements/rows in the column
157+
void SwapElements(size_t pos1, size_t pos2);
158+
159+
/// Test if the value at position 1 is greater than the value at position 2
160+
/// No range checking is performed for performance
161+
bool CompareElementsGT(size_t pos1, size_t pos2) const;
162+
163+
/// Test if the value at position 1 is less than the value at position 2
164+
/// No range checking is performed for performance
165+
bool CompareElementsLT(size_t pos1, size_t pos2) const;
166+
122167
public:
123168
/// Appends content of given column to the end of current one.
124169
void Append(ColumnRef column) override;
@@ -135,6 +180,12 @@ class ColumnDateTime : public Column {
135180
/// Returns count of rows in the column.
136181
size_t Size() const override;
137182

183+
/// Increase the capacity of the column
184+
void Reserve(size_t new_cap) override;
185+
186+
/// Returns the capacity of the column
187+
size_t Capacity() const override;
188+
138189
/// Makes slice of the current column.
139190
ColumnRef Slice(size_t begin, size_t len) const override;
140191
ColumnRef CloneEmpty() const override;
@@ -169,6 +220,17 @@ class ColumnDateTime64 : public Column {
169220
/// Timezone associated with a data column.
170221
std::string Timezone() const;
171222

223+
/// Swap two Elements/rows in the column
224+
void SwapElements(size_t pos1, size_t pos2);
225+
226+
/// Test if the value at position 1 is greater than the value at position 2
227+
/// No range checking is performed for performance
228+
bool CompareElementsGT(size_t pos1, size_t pos2) const;
229+
230+
/// Test if the value at position 1 is less than the value at position 2
231+
/// No range checking is performed for performance
232+
bool CompareElementsLT(size_t pos1, size_t pos2) const;
233+
172234
public:
173235
/// Appends content of given column to the end of current one.
174236
void Append(ColumnRef column) override;
@@ -185,6 +247,12 @@ class ColumnDateTime64 : public Column {
185247
/// Returns count of rows in the column.
186248
size_t Size() const override;
187249

250+
/// Increase the capacity of the column
251+
void Reserve(size_t new_cap) override;
252+
253+
/// Returns the capacity of the column
254+
size_t Capacity() const override;
255+
188256
/// Makes slice of the current column.
189257
ColumnRef Slice(size_t begin, size_t len) const override;
190258
ColumnRef CloneEmpty() const override;

clickhouse/columns/decimal.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,36 @@ void ColumnDecimal::SaveBody(OutputStream* output) {
205205
data_->SaveBody(output);
206206
}
207207

208+
void ColumnDecimal::SwapElements(size_t pos1, size_t pos2) {
209+
if (data_->Type()->GetCode() == Type::Int32) {
210+
data_->As<ColumnInt32>()->SwapElements(pos1, pos2);
211+
} else if (data_->Type()->GetCode() == Type::Int64) {
212+
data_->As<ColumnInt64>()->SwapElements(pos1, pos2);
213+
} else {
214+
data_->As<ColumnInt128>()->SwapElements(pos1, pos2);
215+
}
216+
}
217+
218+
bool ColumnDecimal::CompareElementsGT(size_t pos1, size_t pos2) const {
219+
if (data_->Type()->GetCode() == Type::Int32) {
220+
return data_->As<ColumnInt32>()->CompareElementsGT(pos1, pos2);
221+
} else if (data_->Type()->GetCode() == Type::Int64) {
222+
return data_->As<ColumnInt64>()->CompareElementsGT(pos1, pos2);
223+
} else {
224+
return data_->As<ColumnInt128>()->CompareElementsGT(pos1, pos2);
225+
}
226+
}
227+
228+
bool ColumnDecimal::CompareElementsLT(size_t pos1, size_t pos2) const {
229+
if (data_->Type()->GetCode() == Type::Int32) {
230+
return data_->As<ColumnInt32>()->CompareElementsLT(pos1, pos2);
231+
} else if (data_->Type()->GetCode() == Type::Int64) {
232+
return data_->As<ColumnInt64>()->CompareElementsLT(pos1, pos2);
233+
} else {
234+
return data_->As<ColumnInt128>()->CompareElementsLT(pos1, pos2);
235+
}
236+
}
237+
208238
void ColumnDecimal::Clear() {
209239
data_->Clear();
210240
}
@@ -213,6 +243,14 @@ size_t ColumnDecimal::Size() const {
213243
return data_->Size();
214244
}
215245

246+
void ColumnDecimal::Reserve(size_t new_cap) {
247+
data_->Reserve(new_cap);
248+
}
249+
250+
size_t ColumnDecimal::Capacity() const {
251+
return data_->Capacity();
252+
}
253+
216254
ColumnRef ColumnDecimal::Slice(size_t begin, size_t len) const {
217255
// coundn't use std::make_shared since this c-tor is private
218256
return ColumnRef{new ColumnDecimal(type_, data_->Slice(begin, len))};

clickhouse/columns/decimal.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,30 @@ class ColumnDecimal : public Column {
2020
Int128 At(size_t i) const;
2121
inline auto operator[](size_t i) const { return At(i); }
2222

23+
/// Swap two Elements/rows in the column
24+
void SwapElements(size_t pos1, size_t pos2);
25+
26+
/// Test if the value at position 1 is greater than the value at position 2
27+
/// No range checking is performed for performance
28+
bool CompareElementsGT(size_t pos1, size_t pos2) const;
29+
30+
/// Test if the value at position 1 is less than the value at position 2
31+
/// No range checking is performed for performance
32+
bool CompareElementsLT(size_t pos1, size_t pos2) const;
33+
2334
public:
2435
void Append(ColumnRef column) override;
2536
bool LoadBody(InputStream* input, size_t rows) override;
2637
void SaveBody(OutputStream* output) override;
2738
void Clear() override;
2839
size_t Size() const override;
40+
41+
/// Increase the capacity of the column
42+
void Reserve(size_t new_cap) override;
43+
44+
/// Returns the capacity of the column
45+
size_t Capacity() const override;
46+
2947
ColumnRef Slice(size_t begin, size_t len) const override;
3048
ColumnRef CloneEmpty() const override;
3149
void Swap(Column& other) override;

clickhouse/columns/numeric.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,16 @@ void ColumnVector<T>::SwapElements(size_t pos1, size_t pos2) {
4444
std::iter_swap(data_it + pos1, data_it + pos2);
4545
}
4646

47+
template <typename T>
48+
bool ColumnVector<T>::CompareElementsGT(size_t pos1, size_t pos2) const {
49+
return data_[pos1] > data_[pos2];
50+
}
51+
52+
template <typename T>
53+
bool ColumnVector<T>::CompareElementsLT(size_t pos1, size_t pos2) const {
54+
return data_[pos1] < data_[pos2];
55+
}
56+
4757
template <typename T>
4858
void ColumnVector<T>::Clear() {
4959
data_.clear();

clickhouse/columns/numeric.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,17 @@ class ColumnVector : public Column {
3030

3131
void Erase(size_t pos, size_t count = 1);
3232

33+
/// Swap two Elements/rows in the column
3334
void SwapElements(size_t pos1, size_t pos2);
3435

36+
/// Test if the value at position 1 is greater than the value at position 2
37+
/// No range checking is performed for performance
38+
bool CompareElementsGT(size_t pos1, size_t pos2) const;
39+
40+
/// Test if the value at position 1 is less than the value at position 2
41+
/// No range checking is performed for performance
42+
bool CompareElementsLT(size_t pos1, size_t pos2) const;
43+
3544
public:
3645
/// Appends content of given column to the end of current one.
3746
void Append(ColumnRef column) override;

0 commit comments

Comments
 (0)