Skip to content

Commit 7b3fcdc

Browse files
committed
Some minor fixes to LC c-tor
1 parent ef0e515 commit 7b3fcdc

File tree

2 files changed

+12
-15
lines changed

2 files changed

+12
-15
lines changed

clickhouse/columns/lowcardinality.cpp

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -122,22 +122,19 @@ inline auto GetNullItemForDictionary(const ColumnRef dictionary) {
122122
namespace clickhouse {
123123
ColumnLowCardinality::ColumnLowCardinality(ColumnRef dictionary_column)
124124
: Column(Type::CreateLowCardinality(dictionary_column->Type())),
125-
dictionary_column_(dictionary_column),
125+
dictionary_column_(dictionary_column->Slice(0, 0)), // safe way to get an column of the same type.
126126
index_column_(std::make_shared<ColumnUInt32>())
127127
{
128-
if (dictionary_column_->Size() != 0) {
129-
// When dictionary column was constructed with values, re-add values by copying to update index and unique_items_map.
130-
// TODO: eliminate data copying by coming with better solution than doing AppendUnsafe() N times.
131-
132-
// Steal values into temporary column.
133-
auto values = dictionary_column_->Slice(0, 0);
134-
values->Swap(*dictionary_column_);
135-
128+
if (dictionary_column->Size() != 0) {
136129
AppendNullItemToEmptyColumn();
137130

138-
// Re-add values, updating index and unique_items_map.
139-
for (size_t i = 0; i < values->Size(); ++i)
140-
AppendUnsafe(values->GetItem(i));
131+
// Add values, updating index_column_ and unique_items_map_.
132+
for (size_t i = 0; i < dictionary_column->Size(); ++i) {
133+
// TODO: it would be possible to eliminate copying
134+
// by adding InsertUnsafe(pos, ItemView) method to a Column,
135+
// but that is too much work for now.
136+
AppendUnsafe(dictionary_column->GetItem(i));
137+
}
141138
} else {
142139
AppendNullItemToEmptyColumn();
143140
}
@@ -304,8 +301,7 @@ ColumnRef ColumnLowCardinality::Slice(size_t begin, size_t len) {
304301
begin = std::min(begin, Size());
305302
len = std::min(len, Size() - begin);
306303

307-
ColumnRef new_dictionary = dictionary_column_->Slice(0, 0);
308-
auto result = std::make_shared<ColumnLowCardinality>(new_dictionary);
304+
auto result = std::make_shared<ColumnLowCardinality>(dictionary_column_->Slice(0, 0));
309305

310306
for (size_t i = begin; i < begin + len; ++i)
311307
result->AppendUnsafe(this->GetItem(i));

clickhouse/columns/lowcardinality.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,14 @@ class ColumnLowCardinality : public Column {
4040
friend class ColumnLowCardinalityT;
4141

4242
private:
43-
// Please note that ColumnLowCardinalityT takes reference to underlying dictionary column object,
43+
// IMPLEMENTATION NOTE: ColumnLowCardinalityT takes reference to underlying dictionary column object,
4444
// so make sure to NOT change address of the dictionary object (with reset(), swap()) or with anything else.
4545
ColumnRef dictionary_column_;
4646
ColumnRef index_column_;
4747
UniqueItems unique_items_map_;
4848

4949
public:
50+
// c-tor makes a deep copy of the dictionary_column.
5051
explicit ColumnLowCardinality(ColumnRef dictionary_column);
5152
~ColumnLowCardinality();
5253

0 commit comments

Comments
 (0)