Skip to content

Commit

Permalink
Added Capacity() as pure virtual and added implementations of it to c…
Browse files Browse the repository at this point in the history
…oncrete columns
  • Loading branch information
Enmk committed Feb 29, 2024
1 parent a72ad04 commit 6aa60bd
Show file tree
Hide file tree
Showing 34 changed files with 200 additions and 121 deletions.
14 changes: 9 additions & 5 deletions clickhouse/columns/array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,6 @@ ColumnRef ColumnArray::CloneEmpty() const {
return std::make_shared<ColumnArray>(data_->CloneEmpty());
}

void ColumnArray::Reserve(size_t new_cap) {
data_->Reserve(new_cap);
offsets_->Reserve(new_cap);
}

void ColumnArray::Append(ColumnRef column) {
if (auto col = column->As<ColumnArray>()) {
for (size_t i = 0; i < col->Size(); ++i) {
Expand All @@ -65,6 +60,15 @@ void ColumnArray::Append(ColumnRef column) {
}
}

void ColumnArray::Reserve(size_t new_cap) {
data_->Reserve(new_cap);
offsets_->Reserve(new_cap);
}

size_t ColumnArray::Capacity() const {
return data_->Capacity();
}

bool ColumnArray::LoadPrefix(InputStream* input, size_t rows) {
if (!rows) {
return true;
Expand Down
6 changes: 3 additions & 3 deletions clickhouse/columns/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ class ColumnArray : public Column {
}

public:
/// Increase the capacity of the column for large block insertion.
void Reserve(size_t new_cap) override;

/// Appends content of given column to the end of current one.
void Append(ColumnRef column) override;
/// Increase the capacity of the column for large block insertion.
void Reserve(size_t new_cap) override;
size_t Capacity() const override;

/// Loads column prefix from input stream.
bool LoadPrefix(InputStream* input, size_t rows) override;
Expand Down
1 change: 1 addition & 0 deletions clickhouse/columns/column.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class Column : public std::enable_shared_from_this<Column> {

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

/// Template method to load column data from input stream. It'll call LoadPrefix and LoadBody.
/// Should be called only once from the client. Derived classes should not call it.
Expand Down
15 changes: 9 additions & 6 deletions clickhouse/columns/date.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@ uint16_t ColumnDate::RawAt(size_t n) const {
return data_->At(n);
}

std::vector<uint16_t>& ColumnDate::GetWritableData() {
return data_->GetWritableData();
}

void ColumnDate::Append(ColumnRef column) {
if (auto col = column->As<ColumnDate>()) {
data_->Append(col->data_);
}
}

std::vector<uint16_t>& ColumnDate::GetWritableData() {
return data_->GetWritableData();
}

void ColumnDate::Reserve(size_t new_cap) {
data_->Reserve(new_cap);
}
Expand Down Expand Up @@ -315,11 +315,14 @@ std::string ColumnDateTime64::Timezone() const {
return type_->As<DateTime64Type>()->Timezone();
}

void ColumnDateTime64::Reserve(size_t new_cap)
{
void ColumnDateTime64::Reserve(size_t new_cap) {
data_->Reserve(new_cap);
}

size_t ColumnDateTime64::Capacity() const {
return data_->Capacity();
}

void ColumnDateTime64::Append(ColumnRef column) {
if (auto col = column->As<ColumnDateTime64>()) {
data_->Append(col->data_);
Expand Down
34 changes: 15 additions & 19 deletions clickhouse/columns/date.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,18 @@ class ColumnDate : public Column {
/// Do append data as is -- number of day in Unix epoch, no conversions performed.
void AppendRaw(uint16_t value);
uint16_t RawAt(size_t n) const;
/// Get Raw Vector Contents
std::vector<uint16_t>& GetWritableData();

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

/// Get Raw Vector Contents
std::vector<uint16_t>& GetWritableData();

/// Increase the capacity of the column for large block insertion.
void Reserve(size_t new_cap) override;

/// Returns the capacity of the column
size_t Capacity() const;
size_t Capacity() const override;

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

/// Returns the capacity of the column
size_t Capacity() const;

public:
/// Increase the capacity of the column for large block insertion.
void Reserve(size_t new_cap) override;

/// Appends content of given column to the end of current one.
void Append(ColumnRef column) override;
/// Increase the capacity of the column for large block insertion.
void Reserve(size_t new_cap) override;
/// Returns the capacity of the column
size_t Capacity() const override;

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

/// Returns the capacity of the column
size_t Capacity() const;

public:
/// Increase the capacity of the column for large block insertion.
void Reserve(size_t new_cap) override;

/// Appends content of given column to the end of current one.
void Append(ColumnRef column) override;
/// Increase the capacity of the column for large block insertion.
void Reserve(size_t new_cap) override;
/// Returns the capacity of the column
size_t Capacity() const override;

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

public:
/// Increase the capacity of the column for large block insertion.
void Reserve(size_t new_cap) override;

/// Appends content of given column to the end of current one.
void Append(ColumnRef column) override;
/// Increase the capacity of the column for large block insertion.
void Reserve(size_t new_cap) override;
size_t Capacity() const override;

/// Loads column data from input stream.
bool LoadBody(InputStream* input, size_t rows) override;
Expand Down
19 changes: 16 additions & 3 deletions clickhouse/columns/decimal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,13 +191,26 @@ Int128 ColumnDecimal::At(size_t i) const {
}
}

void ColumnDecimal::Append(ColumnRef column) {
if (auto col = column->As<ColumnDecimal>()) {
data_->Append(col->data_);
}
}

void ColumnDecimal::Reserve(size_t new_cap) {
data_->Reserve(new_cap);
}

void ColumnDecimal::Append(ColumnRef column) {
if (auto col = column->As<ColumnDecimal>()) {
data_->Append(col->data_);
size_t ColumnDecimal::Capacity() const {
switch (data_->Type()->GetCode()) {
case Type::Int32:
return data_->As<ColumnInt32>()->Capacity();
case Type::Int64:
return data_->As<ColumnInt64>()->Capacity();
case Type::Int128:
return data_->As<ColumnInt128>()->Capacity();
default:
throw ValidationError("Invalid data_ column type in ColumnDecimal");
}
}

Expand Down
3 changes: 2 additions & 1 deletion clickhouse/columns/decimal.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ class ColumnDecimal : public Column {

public:
/// Increase the capacity of the column for large block insertion.
void Reserve(size_t new_cap) override;
void Append(ColumnRef column) override;
void Reserve(size_t new_cap) override;
size_t Capacity() const override;
bool LoadBody(InputStream* input, size_t rows) override;
void SaveBody(OutputStream* output) override;
void Clear() override;
Expand Down
15 changes: 11 additions & 4 deletions clickhouse/columns/enum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,6 @@ void ColumnEnum<T>::SetNameAt(size_t n, const std::string& name) {
data_.at(n) = static_cast<T>(type_->As<EnumType>()->GetEnumValue(name));
}

template<typename T>
void ColumnEnum<T>::Reserve(size_t new_cap) {
data_.reserve(new_cap);
}

template <typename T>
void ColumnEnum<T>::Append(ColumnRef column) {
Expand All @@ -80,6 +76,17 @@ void ColumnEnum<T>::Append(ColumnRef column) {
}
}

template<typename T>
void ColumnEnum<T>::Reserve(size_t new_cap) {
data_.reserve(new_cap);
}

template<typename T>
size_t ColumnEnum<T>::Capacity() const {
return data_.capacity();
}


template <typename T>
bool ColumnEnum<T>::LoadBody(InputStream* input, size_t rows) {
data_.resize(rows);
Expand Down
6 changes: 3 additions & 3 deletions clickhouse/columns/enum.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ class ColumnEnum : public Column {
void SetNameAt(size_t n, const std::string& name);

public:
/// Increase the capacity of the column for large block insertion.
void Reserve(size_t new_cap) override;

/// Appends content of given column to the end of current one.
void Append(ColumnRef column) override;
/// Increase the capacity of the column for large block insertion.
void Reserve(size_t new_cap) override;
size_t Capacity() const override;

/// Loads column data from input stream.
bool LoadBody(InputStream* input, size_t rows) override;
Expand Down
14 changes: 9 additions & 5 deletions clickhouse/columns/geo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,22 @@ const typename ColumnGeo<NestedColumnType, type_code>::ValueType ColumnGeo<Neste
return data_->At(n);
}

template<typename NestedColumnType, Type::Code type_code>
void ColumnGeo<NestedColumnType, type_code>::Reserve(size_t new_cap) {
data_->Reserve(new_cap);
}

template <typename NestedColumnType, Type::Code type_code>
void ColumnGeo<NestedColumnType, type_code>::Append(ColumnRef column) {
if (auto col = column->template As<ColumnGeo>()) {
data_->Append(col->data_->template As<Column>());
}
}

template<typename NestedColumnType, Type::Code type_code>
void ColumnGeo<NestedColumnType, type_code>::Reserve(size_t new_cap) {
data_->Reserve(new_cap);
}
template<typename NestedColumnType, Type::Code type_code>
size_t ColumnGeo<NestedColumnType, type_code>::Capacity() const {
return data_->Capacity();
}

template <typename NestedColumnType, Type::Code type_code>
bool ColumnGeo<NestedColumnType, type_code>::LoadBody(InputStream* input, size_t rows) {
return data_->LoadBody(input, rows);
Expand Down
6 changes: 3 additions & 3 deletions clickhouse/columns/geo.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ class ColumnGeo : public Column {
inline const ValueType operator[](size_t n) const { return At(n); }

public:
/// Increase the capacity of the column for large block insertion.
void Reserve(size_t new_cap) override;

/// Appends content of given column to the end of current one.
void Append(ColumnRef column) override;
/// Increase the capacity of the column for large block insertion.
void Reserve(size_t new_cap) override;
size_t Capacity() const override;

/// Loads column data from input stream.
bool LoadBody(InputStream* input, size_t rows) override;
Expand Down
12 changes: 8 additions & 4 deletions clickhouse/columns/ip4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,20 @@ std::string ColumnIPv4::AsString(size_t n) const {
return ip_str;
}

void ColumnIPv4::Reserve(size_t new_cap) {
data_->Reserve(new_cap);
}

void ColumnIPv4::Append(ColumnRef column) {
if (auto col = column->As<ColumnIPv4>()) {
data_->Append(col->data_);
}
}

void ColumnIPv4::Reserve(size_t new_cap) {
data_->Reserve(new_cap);
}

size_t ColumnIPv4::Capacity() const {
return data_->Capacity();
}

bool ColumnIPv4::LoadBody(InputStream * input, size_t rows) {
return data_->LoadBody(input, rows);
}
Expand Down
6 changes: 3 additions & 3 deletions clickhouse/columns/ip4.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ class ColumnIPv4 : public Column {
std::string AsString(size_t n) const;

public:
/// Increase the capacity of the column for large block insertion.
void Reserve(size_t new_cap) override;

/// Appends content of given column to the end of current one.
void Append(ColumnRef column) override;
/// Increase the capacity of the column for large block insertion.
void Reserve(size_t new_cap) override;
size_t Capacity() const override;

/// Loads column data from input stream.
bool LoadBody(InputStream* input, size_t rows) override;
Expand Down
12 changes: 8 additions & 4 deletions clickhouse/columns/ip6.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,20 @@ in6_addr ColumnIPv6::operator [] (size_t n) const {
return *reinterpret_cast<const in6_addr*>(data_->At(n).data());
}

void ColumnIPv6::Reserve(size_t new_cap) {
data_->Reserve(new_cap);
}

void ColumnIPv6::Append(ColumnRef column) {
if (auto col = column->As<ColumnIPv6>()) {
data_->Append(col->data_);
}
}

void ColumnIPv6::Reserve(size_t new_cap) {
data_->Reserve(new_cap);
}

size_t ColumnIPv6::Capacity() const {
return data_->Capacity();
}

bool ColumnIPv6::LoadBody(InputStream* input, size_t rows) {
return data_->LoadBody(input, rows);
}
Expand Down
6 changes: 3 additions & 3 deletions clickhouse/columns/ip6.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ class ColumnIPv6 : public Column {
std::string AsString(size_t n) const;

public:
/// Increase the capacity of the column for large block insertion.
void Reserve(size_t new_cap) override;

/// Appends content of given column to the end of current one.
void Append(ColumnRef column) override;
/// Increase the capacity of the column for large block insertion.
void Reserve(size_t new_cap) override;
size_t Capacity() const override;

/// Loads column data from input stream.
bool LoadBody(InputStream* input, size_t rows) override;
Expand Down
6 changes: 6 additions & 0 deletions clickhouse/columns/lowcardinality.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,12 @@ void ColumnLowCardinality::Reserve(size_t new_cap) {
dictionary_column_->Reserve(EstimateDictionaryCapacity(new_cap));
}

size_t ColumnLowCardinality::Capacity() const {
return VisitIndexColumn([](auto & index_column) {
return index_column.Capacity();
}, *index_column_);
}

void ColumnLowCardinality::Setup(ColumnRef dictionary_column) {
AppendDefaultItem();

Expand Down
Loading

0 comments on commit 6aa60bd

Please sign in to comment.