@@ -122,22 +122,19 @@ inline auto GetNullItemForDictionary(const ColumnRef dictionary) {
122122namespace clickhouse {
123123ColumnLowCardinality::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));
0 commit comments