diff --git a/src/Columns/ColumnLowCardinality.cpp b/src/Columns/ColumnLowCardinality.cpp index 11d02b023d6..4f9ab8215be 100644 --- a/src/Columns/ColumnLowCardinality.cpp +++ b/src/Columns/ColumnLowCardinality.cpp @@ -485,13 +485,8 @@ void ColumnLowCardinality::setSharedDictionary(const ColumnPtr & column_unique) ColumnLowCardinality::MutablePtr ColumnLowCardinality::cutAndCompact(size_t start, size_t length) const { auto sub_positions = IColumn::mutate(idx.getPositions()->cut(start, length)); - /// Create column with new indexes and old dictionary. - /// Dictionary is shared, but will be recreated after compactInplace call. - auto column = ColumnLowCardinality::create(getDictionary().assumeMutable(), std::move(sub_positions)); - /// Will create new dictionary. - column->compactInplace(); - - return column; + auto new_column_unique = Dictionary::compact(dictionary.getColumnUnique(), sub_positions); + return ColumnLowCardinality::create(std::move(new_column_unique), std::move(sub_positions)); } void ColumnLowCardinality::compactInplace() @@ -589,7 +584,7 @@ size_t ColumnLowCardinality::Index::getSizeOfIndexType(const IColumn & column, s column.getName()); } -void ColumnLowCardinality::Index::attachPositions(ColumnPtr positions_) +void ColumnLowCardinality::Index::attachPositions(MutableColumnPtr positions_) { positions = std::move(positions_); updateSizeOfType(); @@ -820,21 +815,23 @@ void ColumnLowCardinality::Dictionary::setShared(const ColumnPtr & column_unique shared = true; } -void ColumnLowCardinality::Dictionary::compact(ColumnPtr & positions) +void ColumnLowCardinality::Dictionary::compact(MutableColumnPtr & positions) { - auto new_column_unique = column_unique->cloneEmpty(); + column_unique = compact(getColumnUnique(), positions); + shared = false; +} - auto & unique = getColumnUnique(); +MutableColumnPtr ColumnLowCardinality::Dictionary::compact(const IColumnUnique & unique, MutableColumnPtr & positions) +{ + auto new_column_unique = unique.cloneEmpty(); auto & new_unique = static_cast(*new_column_unique); - auto indexes = mapUniqueIndex(positions->assumeMutableRef()); + auto indexes = mapUniqueIndex(*positions); auto sub_keys = unique.getNestedColumn()->index(*indexes, 0); auto new_indexes = new_unique.uniqueInsertRangeFrom(*sub_keys, 0, sub_keys->size()); positions = IColumn::mutate(new_indexes->index(*positions, 0)); - column_unique = std::move(new_column_unique); - - shared = false; + return new_column_unique; } ColumnPtr ColumnLowCardinality::cloneWithDefaultOnNull() const diff --git a/src/Columns/ColumnLowCardinality.h b/src/Columns/ColumnLowCardinality.h index f9bf7290601..a5c056153bb 100644 --- a/src/Columns/ColumnLowCardinality.h +++ b/src/Columns/ColumnLowCardinality.h @@ -303,8 +303,8 @@ public: void checkSizeOfType(); - ColumnPtr detachPositions() { return std::move(positions); } - void attachPositions(ColumnPtr positions_); + MutableColumnPtr detachPositions() { return IColumn::mutate(std::move(positions)); } + void attachPositions(MutableColumnPtr positions_); void countKeys(ColumnUInt64::Container & counts) const; @@ -352,7 +352,9 @@ private: bool isShared() const { return shared; } /// Create new dictionary with only keys that are mentioned in positions. - void compact(ColumnPtr & positions); + void compact(MutableColumnPtr & positions); + + static MutableColumnPtr compact(const IColumnUnique & column_unique, MutableColumnPtr & positions); private: WrappedPtr column_unique;