Skip to content

Commit 6aa60bd

Browse files
committed
Added Capacity() as pure virtual and added implementations of it to concrete columns
1 parent a72ad04 commit 6aa60bd

34 files changed

+200
-121
lines changed

clickhouse/columns/array.cpp

+9-5
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,6 @@ ColumnRef ColumnArray::CloneEmpty() const {
5252
return std::make_shared<ColumnArray>(data_->CloneEmpty());
5353
}
5454

55-
void ColumnArray::Reserve(size_t new_cap) {
56-
data_->Reserve(new_cap);
57-
offsets_->Reserve(new_cap);
58-
}
59-
6055
void ColumnArray::Append(ColumnRef column) {
6156
if (auto col = column->As<ColumnArray>()) {
6257
for (size_t i = 0; i < col->Size(); ++i) {
@@ -65,6 +60,15 @@ void ColumnArray::Append(ColumnRef column) {
6560
}
6661
}
6762

63+
void ColumnArray::Reserve(size_t new_cap) {
64+
data_->Reserve(new_cap);
65+
offsets_->Reserve(new_cap);
66+
}
67+
68+
size_t ColumnArray::Capacity() const {
69+
return data_->Capacity();
70+
}
71+
6872
bool ColumnArray::LoadPrefix(InputStream* input, size_t rows) {
6973
if (!rows) {
7074
return true;

clickhouse/columns/array.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ class ColumnArray : public Column {
4747
}
4848

4949
public:
50-
/// Increase the capacity of the column for large block insertion.
51-
void Reserve(size_t new_cap) override;
52-
5350
/// Appends content of given column to the end of current one.
5451
void Append(ColumnRef column) override;
52+
/// Increase the capacity of the column for large block insertion.
53+
void Reserve(size_t new_cap) override;
54+
size_t Capacity() const override;
5555

5656
/// Loads column prefix from input stream.
5757
bool LoadPrefix(InputStream* input, size_t rows) override;

clickhouse/columns/column.h

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class Column : public std::enable_shared_from_this<Column> {
5454

5555
/// Increase the capacity of the column for large block insertion.
5656
virtual void Reserve(size_t new_cap) = 0;
57+
virtual size_t Capacity() const = 0;
5758

5859
/// Template method to load column data from input stream. It'll call LoadPrefix and LoadBody.
5960
/// Should be called only once from the client. Derived classes should not call it.

clickhouse/columns/date.cpp

+9-6
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,16 @@ uint16_t ColumnDate::RawAt(size_t n) const {
3737
return data_->At(n);
3838
}
3939

40+
std::vector<uint16_t>& ColumnDate::GetWritableData() {
41+
return data_->GetWritableData();
42+
}
43+
4044
void ColumnDate::Append(ColumnRef column) {
4145
if (auto col = column->As<ColumnDate>()) {
4246
data_->Append(col->data_);
4347
}
4448
}
4549

46-
std::vector<uint16_t>& ColumnDate::GetWritableData() {
47-
return data_->GetWritableData();
48-
}
49-
5050
void ColumnDate::Reserve(size_t new_cap) {
5151
data_->Reserve(new_cap);
5252
}
@@ -315,11 +315,14 @@ std::string ColumnDateTime64::Timezone() const {
315315
return type_->As<DateTime64Type>()->Timezone();
316316
}
317317

318-
void ColumnDateTime64::Reserve(size_t new_cap)
319-
{
318+
void ColumnDateTime64::Reserve(size_t new_cap) {
320319
data_->Reserve(new_cap);
321320
}
322321

322+
size_t ColumnDateTime64::Capacity() const {
323+
return data_->Capacity();
324+
}
325+
323326
void ColumnDateTime64::Append(ColumnRef column) {
324327
if (auto col = column->As<ColumnDateTime64>()) {
325328
data_->Append(col->data_);

clickhouse/columns/date.h

+15-19
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,18 @@ class ColumnDate : public Column {
2727
/// Do append data as is -- number of day in Unix epoch, no conversions performed.
2828
void AppendRaw(uint16_t value);
2929
uint16_t RawAt(size_t n) const;
30+
/// Get Raw Vector Contents
31+
std::vector<uint16_t>& GetWritableData();
3032

33+
public:
3134
/// Appends content of given column to the end of current one.
3235
void Append(ColumnRef column) override;
3336

34-
/// Get Raw Vector Contents
35-
std::vector<uint16_t>& GetWritableData();
36-
3737
/// Increase the capacity of the column for large block insertion.
3838
void Reserve(size_t new_cap) override;
3939

4040
/// Returns the capacity of the column
41-
size_t Capacity() const;
41+
size_t Capacity() const override;
4242

4343
/// Loads column data from input stream.
4444
bool LoadBody(InputStream* input, size_t rows) override;
@@ -89,15 +89,13 @@ class ColumnDate32 : public Column {
8989
/// Get Raw Vector Contents
9090
std::vector<int32_t>& GetWritableData();
9191

92-
/// Returns the capacity of the column
93-
size_t Capacity() const;
94-
9592
public:
96-
/// Increase the capacity of the column for large block insertion.
97-
void Reserve(size_t new_cap) override;
98-
9993
/// Appends content of given column to the end of current one.
10094
void Append(ColumnRef column) override;
95+
/// Increase the capacity of the column for large block insertion.
96+
void Reserve(size_t new_cap) override;
97+
/// Returns the capacity of the column
98+
size_t Capacity() const override;
10199

102100
/// Loads column data from input stream.
103101
bool LoadBody(InputStream* input, size_t rows) override;
@@ -151,15 +149,13 @@ class ColumnDateTime : public Column {
151149
/// Get Raw Vector Contents
152150
std::vector<uint32_t>& GetWritableData();
153151

154-
/// Returns the capacity of the column
155-
size_t Capacity() const;
156-
157152
public:
158-
/// Increase the capacity of the column for large block insertion.
159-
void Reserve(size_t new_cap) override;
160-
161153
/// Appends content of given column to the end of current one.
162154
void Append(ColumnRef column) override;
155+
/// Increase the capacity of the column for large block insertion.
156+
void Reserve(size_t new_cap) override;
157+
/// Returns the capacity of the column
158+
size_t Capacity() const override;
163159

164160
/// Loads column data from input stream.
165161
bool LoadBody(InputStream* input, size_t rows) override;
@@ -209,11 +205,11 @@ class ColumnDateTime64 : public Column {
209205
std::string Timezone() const;
210206

211207
public:
212-
/// Increase the capacity of the column for large block insertion.
213-
void Reserve(size_t new_cap) override;
214-
215208
/// Appends content of given column to the end of current one.
216209
void Append(ColumnRef column) override;
210+
/// Increase the capacity of the column for large block insertion.
211+
void Reserve(size_t new_cap) override;
212+
size_t Capacity() const override;
217213

218214
/// Loads column data from input stream.
219215
bool LoadBody(InputStream* input, size_t rows) override;

clickhouse/columns/decimal.cpp

+16-3
Original file line numberDiff line numberDiff line change
@@ -191,13 +191,26 @@ Int128 ColumnDecimal::At(size_t i) const {
191191
}
192192
}
193193

194+
void ColumnDecimal::Append(ColumnRef column) {
195+
if (auto col = column->As<ColumnDecimal>()) {
196+
data_->Append(col->data_);
197+
}
198+
}
199+
194200
void ColumnDecimal::Reserve(size_t new_cap) {
195201
data_->Reserve(new_cap);
196202
}
197203

198-
void ColumnDecimal::Append(ColumnRef column) {
199-
if (auto col = column->As<ColumnDecimal>()) {
200-
data_->Append(col->data_);
204+
size_t ColumnDecimal::Capacity() const {
205+
switch (data_->Type()->GetCode()) {
206+
case Type::Int32:
207+
return data_->As<ColumnInt32>()->Capacity();
208+
case Type::Int64:
209+
return data_->As<ColumnInt64>()->Capacity();
210+
case Type::Int128:
211+
return data_->As<ColumnInt128>()->Capacity();
212+
default:
213+
throw ValidationError("Invalid data_ column type in ColumnDecimal");
201214
}
202215
}
203216

clickhouse/columns/decimal.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ class ColumnDecimal : public Column {
2222

2323
public:
2424
/// Increase the capacity of the column for large block insertion.
25-
void Reserve(size_t new_cap) override;
2625
void Append(ColumnRef column) override;
26+
void Reserve(size_t new_cap) override;
27+
size_t Capacity() const override;
2728
bool LoadBody(InputStream* input, size_t rows) override;
2829
void SaveBody(OutputStream* output) override;
2930
void Clear() override;

clickhouse/columns/enum.cpp

+11-4
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,6 @@ void ColumnEnum<T>::SetNameAt(size_t n, const std::string& name) {
6868
data_.at(n) = static_cast<T>(type_->As<EnumType>()->GetEnumValue(name));
6969
}
7070

71-
template<typename T>
72-
void ColumnEnum<T>::Reserve(size_t new_cap) {
73-
data_.reserve(new_cap);
74-
}
7571

7672
template <typename T>
7773
void ColumnEnum<T>::Append(ColumnRef column) {
@@ -80,6 +76,17 @@ void ColumnEnum<T>::Append(ColumnRef column) {
8076
}
8177
}
8278

79+
template<typename T>
80+
void ColumnEnum<T>::Reserve(size_t new_cap) {
81+
data_.reserve(new_cap);
82+
}
83+
84+
template<typename T>
85+
size_t ColumnEnum<T>::Capacity() const {
86+
return data_.capacity();
87+
}
88+
89+
8390
template <typename T>
8491
bool ColumnEnum<T>::LoadBody(InputStream* input, size_t rows) {
8592
data_.resize(rows);

clickhouse/columns/enum.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ class ColumnEnum : public Column {
3030
void SetNameAt(size_t n, const std::string& name);
3131

3232
public:
33-
/// Increase the capacity of the column for large block insertion.
34-
void Reserve(size_t new_cap) override;
35-
3633
/// Appends content of given column to the end of current one.
3734
void Append(ColumnRef column) override;
35+
/// Increase the capacity of the column for large block insertion.
36+
void Reserve(size_t new_cap) override;
37+
size_t Capacity() const override;
3838

3939
/// Loads column data from input stream.
4040
bool LoadBody(InputStream* input, size_t rows) override;

clickhouse/columns/geo.cpp

+9-5
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,22 @@ 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-
void ColumnGeo<NestedColumnType, type_code>::Reserve(size_t new_cap) {
59-
data_->Reserve(new_cap);
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>()) {
6560
data_->Append(col->data_->template As<Column>());
6661
}
6762
}
6863

64+
template<typename NestedColumnType, Type::Code type_code>
65+
void ColumnGeo<NestedColumnType, type_code>::Reserve(size_t new_cap) {
66+
data_->Reserve(new_cap);
67+
}
68+
template<typename NestedColumnType, Type::Code type_code>
69+
size_t ColumnGeo<NestedColumnType, type_code>::Capacity() const {
70+
return data_->Capacity();
71+
}
72+
6973
template <typename NestedColumnType, Type::Code type_code>
7074
bool ColumnGeo<NestedColumnType, type_code>::LoadBody(InputStream* input, size_t rows) {
7175
return data_->LoadBody(input, rows);

clickhouse/columns/geo.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ class ColumnGeo : public Column {
2929
inline const ValueType operator[](size_t n) const { return At(n); }
3030

3131
public:
32-
/// Increase the capacity of the column for large block insertion.
33-
void Reserve(size_t new_cap) override;
34-
3532
/// Appends content of given column to the end of current one.
3633
void Append(ColumnRef column) override;
34+
/// Increase the capacity of the column for large block insertion.
35+
void Reserve(size_t new_cap) override;
36+
size_t Capacity() const override;
3737

3838
/// Loads column data from input stream.
3939
bool LoadBody(InputStream* input, size_t rows) override;

clickhouse/columns/ip4.cpp

+8-4
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,20 @@ std::string ColumnIPv4::AsString(size_t n) const {
7474
return ip_str;
7575
}
7676

77-
void ColumnIPv4::Reserve(size_t new_cap) {
78-
data_->Reserve(new_cap);
79-
}
80-
8177
void ColumnIPv4::Append(ColumnRef column) {
8278
if (auto col = column->As<ColumnIPv4>()) {
8379
data_->Append(col->data_);
8480
}
8581
}
8682

83+
void ColumnIPv4::Reserve(size_t new_cap) {
84+
data_->Reserve(new_cap);
85+
}
86+
87+
size_t ColumnIPv4::Capacity() const {
88+
return data_->Capacity();
89+
}
90+
8791
bool ColumnIPv4::LoadBody(InputStream * input, size_t rows) {
8892
return data_->LoadBody(input, rows);
8993
}

clickhouse/columns/ip4.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ class ColumnIPv4 : public Column {
3939
std::string AsString(size_t n) const;
4040

4141
public:
42-
/// Increase the capacity of the column for large block insertion.
43-
void Reserve(size_t new_cap) override;
44-
4542
/// Appends content of given column to the end of current one.
4643
void Append(ColumnRef column) override;
44+
/// Increase the capacity of the column for large block insertion.
45+
void Reserve(size_t new_cap) override;
46+
size_t Capacity() const override;
4747

4848
/// Loads column data from input stream.
4949
bool LoadBody(InputStream* input, size_t rows) override;

clickhouse/columns/ip6.cpp

+8-4
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,20 @@ in6_addr ColumnIPv6::operator [] (size_t n) const {
6565
return *reinterpret_cast<const in6_addr*>(data_->At(n).data());
6666
}
6767

68-
void ColumnIPv6::Reserve(size_t new_cap) {
69-
data_->Reserve(new_cap);
70-
}
71-
7268
void ColumnIPv6::Append(ColumnRef column) {
7369
if (auto col = column->As<ColumnIPv6>()) {
7470
data_->Append(col->data_);
7571
}
7672
}
7773

74+
void ColumnIPv6::Reserve(size_t new_cap) {
75+
data_->Reserve(new_cap);
76+
}
77+
78+
size_t ColumnIPv6::Capacity() const {
79+
return data_->Capacity();
80+
}
81+
7882
bool ColumnIPv6::LoadBody(InputStream* input, size_t rows) {
7983
return data_->LoadBody(input, rows);
8084
}

clickhouse/columns/ip6.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ class ColumnIPv6 : public Column {
3535
std::string AsString(size_t n) const;
3636

3737
public:
38-
/// Increase the capacity of the column for large block insertion.
39-
void Reserve(size_t new_cap) override;
40-
4138
/// Appends content of given column to the end of current one.
4239
void Append(ColumnRef column) override;
40+
/// Increase the capacity of the column for large block insertion.
41+
void Reserve(size_t new_cap) override;
42+
size_t Capacity() const override;
4343

4444
/// Loads column data from input stream.
4545
bool LoadBody(InputStream* input, size_t rows) override;

clickhouse/columns/lowcardinality.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,12 @@ void ColumnLowCardinality::Reserve(size_t new_cap) {
209209
dictionary_column_->Reserve(EstimateDictionaryCapacity(new_cap));
210210
}
211211

212+
size_t ColumnLowCardinality::Capacity() const {
213+
return VisitIndexColumn([](auto & index_column) {
214+
return index_column.Capacity();
215+
}, *index_column_);
216+
}
217+
212218
void ColumnLowCardinality::Setup(ColumnRef dictionary_column) {
213219
AppendDefaultItem();
214220

0 commit comments

Comments
 (0)