Skip to content

Commit 0f8b396

Browse files
authored
Merge pull request #341 from 1261385937/column-construction
add Reserve for column. Optimize large block insertion
2 parents c5225bd + d343428 commit 0f8b396

31 files changed

+161
-55
lines changed

clickhouse/columns/array.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ 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+
5560
void ColumnArray::Append(ColumnRef column) {
5661
if (auto col = column->As<ColumnArray>()) {
5762
for (size_t i = 0; i < col->Size(); ++i) {

clickhouse/columns/array.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ 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+
5053
/// Appends content of given column to the end of current one.
5154
void Append(ColumnRef column) override;
5255

clickhouse/columns/column.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ class Column : public std::enable_shared_from_this<Column> {
5252
/// Appends content of given column to the end of current one.
5353
virtual void Append(ColumnRef column) = 0;
5454

55+
/// Increase the capacity of the column for large block insertion.
56+
virtual void Reserve(size_t new_cap) = 0;
57+
5558
/// Template method to load column data from input stream. It'll call LoadPrefix and LoadBody.
5659
/// Should be called only once from the client. Derived classes should not call it.
5760
bool Load(InputStream* input, size_t rows);

clickhouse/columns/date.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,11 @@ std::string ColumnDateTime64::Timezone() const {
303303
return type_->As<DateTime64Type>()->Timezone();
304304
}
305305

306+
void ColumnDateTime64::Reserve(size_t new_cap)
307+
{
308+
data_->Reserve(new_cap);
309+
}
310+
306311
void ColumnDateTime64::Append(ColumnRef column) {
307312
if (auto col = column->As<ColumnDateTime64>()) {
308313
data_->Append(col->data_);

clickhouse/columns/date.h

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ class ColumnDate : public Column {
3434
/// Get Raw Vector Contents
3535
std::vector<uint16_t>& GetWritableData();
3636

37-
/// Increase the capacity of the column
38-
void Reserve(size_t new_cap);
37+
/// Increase the capacity of the column for large block insertion.
38+
void Reserve(size_t new_cap) override;
3939

4040
/// Returns the capacity of the column
4141
size_t Capacity() const;
@@ -79,9 +79,6 @@ class ColumnDate32 : public Column {
7979
/// The implementation is fundamentally wrong, ignores timezones, leap years and daylight saving.
8080
std::time_t At(size_t n) const;
8181

82-
/// Appends content of given column to the end of current one.
83-
void Append(ColumnRef column) override;
84-
8582
inline std::time_t operator [] (size_t n) const { return At(n); }
8683

8784
/// Do append data as is -- number of day in Unix epoch (32bit signed), no conversions performed.
@@ -91,12 +88,16 @@ class ColumnDate32 : public Column {
9188
/// Get Raw Vector Contents
9289
std::vector<int32_t>& GetWritableData();
9390

94-
/// Increase the capacity of the column
95-
void Reserve(size_t new_cap);
96-
9791
/// Returns the capacity of the column
9892
size_t Capacity() const;
9993

94+
public:
95+
/// Increase the capacity of the column for large block insertion.
96+
void Reserve(size_t new_cap) override;
97+
98+
/// Appends content of given column to the end of current one.
99+
void Append(ColumnRef column) override;
100+
100101
/// Loads column data from input stream.
101102
bool LoadBody(InputStream* input, size_t rows) override;
102103

@@ -148,13 +149,13 @@ class ColumnDateTime : public Column {
148149
/// Get Raw Vector Contents
149150
std::vector<uint32_t>& GetWritableData();
150151

151-
/// Increase the capacity of the column
152-
void Reserve(size_t new_cap);
153-
154152
/// Returns the capacity of the column
155153
size_t Capacity() const;
156154

157155
public:
156+
/// Increase the capacity of the column for large block insertion.
157+
void Reserve(size_t new_cap) override;
158+
158159
/// Appends content of given column to the end of current one.
159160
void Append(ColumnRef column) override;
160161

@@ -205,6 +206,9 @@ class ColumnDateTime64 : public Column {
205206
std::string Timezone() const;
206207

207208
public:
209+
/// Increase the capacity of the column for large block insertion.
210+
void Reserve(size_t new_cap) override;
211+
208212
/// Appends content of given column to the end of current one.
209213
void Append(ColumnRef column) override;
210214

clickhouse/columns/decimal.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,10 @@ Int128 ColumnDecimal::At(size_t i) const {
191191
}
192192
}
193193

194+
void ColumnDecimal::Reserve(size_t new_cap) {
195+
data_->Reserve(new_cap);
196+
}
197+
194198
void ColumnDecimal::Append(ColumnRef column) {
195199
if (auto col = column->As<ColumnDecimal>()) {
196200
data_->Append(col->data_);

clickhouse/columns/decimal.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ class ColumnDecimal : public Column {
2121
inline auto operator[](size_t i) const { return At(i); }
2222

2323
public:
24+
/// Increase the capacity of the column for large block insertion.
25+
void Reserve(size_t new_cap) override;
2426
void Append(ColumnRef column) override;
2527
bool LoadBody(InputStream* input, size_t rows) override;
2628
void SaveBody(OutputStream* output) override;

clickhouse/columns/enum.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ 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+
}
75+
7176
template <typename T>
7277
void ColumnEnum<T>::Append(ColumnRef column) {
7378
if (auto col = column->As<ColumnEnum<T>>()) {

clickhouse/columns/enum.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ 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+
3336
/// Appends content of given column to the end of current one.
3437
void Append(ColumnRef column) override;
3538

clickhouse/columns/geo.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ 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+
5762
template <typename NestedColumnType, Type::Code type_code>
5863
void ColumnGeo<NestedColumnType, type_code>::Append(ColumnRef column) {
5964
if (auto col = column->template As<ColumnGeo>()) {

0 commit comments

Comments
 (0)