From 2981ec0a6a77a9fec9f542ddca1c52ff1f37eb0d Mon Sep 17 00:00:00 2001 From: Nikolai Kochetov Date: Fri, 23 Nov 2018 11:06:27 +0300 Subject: [PATCH] Remove null map cache from ColumnUnique. --- dbms/src/Columns/ColumnUnique.h | 65 ++++++++++++++++++++++----------- 1 file changed, 43 insertions(+), 22 deletions(-) diff --git a/dbms/src/Columns/ColumnUnique.h b/dbms/src/Columns/ColumnUnique.h index be5d71c3e29..46e09af4a8e 100644 --- a/dbms/src/Columns/ColumnUnique.h +++ b/dbms/src/Columns/ColumnUnique.h @@ -81,7 +81,7 @@ public: { return column_holder->allocatedBytes() + index.allocatedBytes() - + (cached_null_mask ? cached_null_mask->allocatedBytes() : 0); + + (nested_null_mask ? nested_null_mask->allocatedBytes() : 0); } void forEachSubcolumn(IColumn::ColumnCallback callback) override { @@ -100,8 +100,8 @@ private: ReverseIndex index; /// For DataTypeNullable, stores null map. - mutable ColumnPtr cached_null_mask; - mutable ColumnPtr cached_column_nullable; + ColumnPtr nested_null_mask; + ColumnPtr nested_column_nullable; class IncrementalHash { @@ -118,6 +118,8 @@ private: mutable IncrementalHash hash; + void updateNullMask(); + static size_t numSpecialValues(bool is_nullable) { return is_nullable ? 2 : 1; } size_t numSpecialValues() const { return numSpecialValues(is_nullable); } @@ -148,6 +150,7 @@ ColumnUnique::ColumnUnique(const ColumnUnique & other) , index(numSpecialValues(is_nullable), 0) { index.setColumn(getRawColumnPtr()); + updateNullMask(); } template @@ -158,6 +161,7 @@ ColumnUnique::ColumnUnique(const IDataType & type) const auto & holder_type = is_nullable ? *static_cast(type).getNestedType() : type; column_holder = holder_type.createColumn()->cloneResized(numSpecialValues()); index.setColumn(getRawColumnPtr()); + updateNullMask(); } template @@ -172,32 +176,37 @@ ColumnUnique::ColumnUnique(MutableColumnPtr && holder, bool is_nulla throw Exception("Holder column for ColumnUnique can't be nullable.", ErrorCodes::ILLEGAL_COLUMN); index.setColumn(getRawColumnPtr()); + updateNullMask(); +} + +template +void ColumnUnique::updateNullMask() +{ + if (is_nullable) + { + size_t size = getRawColumnPtr()->size(); + if (!nested_null_mask) + { + ColumnUInt8::MutablePtr null_mask = ColumnUInt8::create(size, UInt8(0)); + null_mask->getData()[getNullValueIndex()] = 1; + nested_null_mask = std::move(null_mask); + nested_column_nullable = ColumnNullable::create(column_holder, nested_null_mask); + } + + if (nested_null_mask->size() != size) + { + IColumn & null_mask = nested_null_mask->assumeMutableRef(); + static_cast(null_mask).getData().resize_fill(size); + } + } } template const ColumnPtr & ColumnUnique::getNestedColumn() const { if (is_nullable) - { - size_t size = getRawColumnPtr()->size(); - if (!cached_null_mask) - { - ColumnUInt8::MutablePtr null_mask = ColumnUInt8::create(size, UInt8(0)); - null_mask->getData()[getNullValueIndex()] = 1; - cached_null_mask = std::move(null_mask); - cached_column_nullable = ColumnNullable::create(column_holder, cached_null_mask); - } + return nested_column_nullable; - if (cached_null_mask->size() != size) - { - MutableColumnPtr null_mask = (*std::move(cached_null_mask)).mutate(); - static_cast(*null_mask).getData().resize_fill(size); - cached_null_mask = std::move(null_mask); - cached_column_nullable = ColumnNullable::create(column_holder, cached_null_mask); - } - - return cached_column_nullable; - } return column_holder; } @@ -227,6 +236,8 @@ size_t ColumnUnique::uniqueInsert(const Field & x) if (pos != prev_size) column->popBack(1); + updateNullMask(); + return pos; } @@ -260,6 +271,8 @@ size_t ColumnUnique::uniqueInsertData(const char * pos, size_t lengt index.insertFromLastRow(); } + updateNullMask(); + return insertion_point; } @@ -288,6 +301,8 @@ size_t ColumnUnique::uniqueInsertDataWithTerminatingZero(const char if (position != prev_size) column->popBack(1); + updateNullMask(); + return static_cast(position); } @@ -343,6 +358,8 @@ size_t ColumnUnique::uniqueDeserializeAndInsertFromArena(const char if (index_pos != prev_size) column->popBack(1); + updateNullMask(); + return static_cast(index_pos); } @@ -533,6 +550,8 @@ MutableColumnPtr ColumnUnique::uniqueInsertRangeFrom(const IColumn & if (!positions_column) throw Exception("Can't find index type for ColumnUnique", ErrorCodes::LOGICAL_ERROR); + updateNullMask(); + return positions_column; } @@ -577,6 +596,8 @@ IColumnUnique::IndexesWithOverflow ColumnUnique::uniqueInsertRangeWi if (!positions_column) throw Exception("Can't find index type for ColumnUnique", ErrorCodes::LOGICAL_ERROR); + updateNullMask(); + IColumnUnique::IndexesWithOverflow indexes_with_overflow; indexes_with_overflow.indexes = std::move(positions_column); indexes_with_overflow.overflowed_keys = std::move(overflowed_keys);