Better semantic of sharing columns: development [#CLICKHOUSE-2].

This commit is contained in:
Alexey Milovidov 2017-12-15 22:46:24 +03:00
parent 9c9896c1a9
commit 0b76b61c73
14 changed files with 87 additions and 70 deletions

View File

@ -75,7 +75,7 @@ MutableColumnPtr ColumnAggregateFunction::convertToValues() const
for (auto val : getData())
function->insertResultInto(val, *res);
return std::move(res);
return res;
}
@ -372,7 +372,7 @@ MutableColumns ColumnAggregateFunction::scatter(IColumn::ColumnIndex num_columns
for (size_t i = 0; i < num_rows; ++i)
static_cast<ColumnAggregateFunction &>(*columns[selector[i]]).data.push_back(data[i]);
return std::move(columns);
return columns;
}
void ColumnAggregateFunction::getPermutation(bool /*reverse*/, size_t /*limit*/, int /*nan_direction_hint*/, IColumn::Permutation & res) const

View File

@ -34,7 +34,7 @@ namespace ErrorCodes
ColumnArray::ColumnArray(const ColumnPtr & nested_column, const ColumnPtr & offsets_column)
: data(nested_column), offsets(offsets_column)
{
if (!typeid_cast<ColumnOffsets_t *>(offsets_column.get()))
if (!typeid_cast<const ColumnOffsets_t *>(offsets_column.get()))
throw Exception("offsets_column must be a ColumnUInt64", ErrorCodes::ILLEGAL_COLUMN);
/** NOTE
@ -43,13 +43,22 @@ ColumnArray::ColumnArray(const ColumnPtr & nested_column, const ColumnPtr & offs
*/
}
ColumnArray::ColumnArray(const ColumnPtr & nested_column)
: data(nested_column)
{
if (!data->empty())
throw Exception("Not empty data passed to ColumnArray, but no offsets passed", ErrorCodes::ILLEGAL_COLUMN);
offsets = ColumnOffsets_t::create();
}
std::string ColumnArray::getName() const { return "Array(" + getData().getName() + ")"; }
MutableColumnPtr ColumnArray::cloneResized(size_t to_size) const
{
auto res = ColumnArray::create(getData().cloneEmpty(), ColumnOffsets_t::create());
auto res = ColumnArray::create(getData().cloneEmpty());
if (to_size == 0)
return std::move(res);
@ -141,7 +150,7 @@ void ColumnArray::insertData(const char * pos, size_t length)
{
/** Similarly - only for arrays of fixed length values.
*/
IColumn * data_ = getData().get();
IColumn * data_ = &getData();
if (!data_->isFixedAndContiguous())
throw Exception("Method insertData is not supported for " + getName(), ErrorCodes::NOT_IMPLEMENTED);
@ -312,7 +321,7 @@ MutableColumnPtr ColumnArray::convertToFullColumnIfConst() const
{
ColumnPtr new_data;
if (auto full_column = getData().convertToFullColumnIfConst())
if (ColumnPtr full_column = getData().convertToFullColumnIfConst())
new_data = full_column;
else
new_data = data;
@ -361,7 +370,7 @@ void ColumnArray::insertRangeFrom(const IColumn & src, size_t start, size_t leng
size_t nested_offset = src_concrete.offsetAt(start);
size_t nested_length = src_concrete.getOffsets()[start + length - 1] - nested_offset;
data->insertRangeFrom(src_concrete.getData(), nested_offset, nested_length);
insertRangeFrom(src_concrete.getData(), nested_offset, nested_length);
Offsets_t & cur_offsets = getOffsets();
const Offsets_t & src_offsets = src_concrete.getOffsets();
@ -404,15 +413,15 @@ template <typename T>
MutableColumnPtr ColumnArray::filterNumber(const Filter & filt, ssize_t result_size_hint) const
{
if (getOffsets().size() == 0)
return ColumnArray::create(data, ColumnOffsets_t::create());
return ColumnArray::create(data);
auto res = ColumnArray::create(data->cloneEmpty(), ColumnOffsets_t::create());
auto res = ColumnArray::create(data->cloneEmpty());
auto & res_elems = static_cast<ColumnVector<T> &>(res->getData()).getData();
Offsets_t & res_offsets = res->getOffsets();
filterArraysImpl<T>(static_cast<const ColumnVector<T> &>(*data).getData(), getOffsets(), res_elems, res_offsets, filt, result_size_hint);
return res;
return std::move(res);
}
MutableColumnPtr ColumnArray::filterString(const Filter & filt, ssize_t result_size_hint) const
@ -422,9 +431,9 @@ MutableColumnPtr ColumnArray::filterString(const Filter & filt, ssize_t result_s
throw Exception("Size of filter doesn't match size of column.", ErrorCodes::SIZES_OF_COLUMNS_DOESNT_MATCH);
if (0 == col_size)
return ColumnArray::create(data, ColumnOffsets_t::create());
return ColumnArray::create(data);
auto res = ColumnArray::create(data->cloneEmpty(), ColumnOffsets_t::create());
auto res = ColumnArray::create(data->cloneEmpty());
const ColumnString & src_string = typeid_cast<const ColumnString &>(*data);
const ColumnString::Chars_t & src_chars = src_string.getChars();
@ -480,7 +489,7 @@ MutableColumnPtr ColumnArray::filterString(const Filter & filt, ssize_t result_s
}
}
return res;
return std::move(res);
}
MutableColumnPtr ColumnArray::filterGeneric(const Filter & filt, ssize_t result_size_hint) const
@ -490,7 +499,7 @@ MutableColumnPtr ColumnArray::filterGeneric(const Filter & filt, ssize_t result_
throw Exception("Size of filter doesn't match size of column.", ErrorCodes::SIZES_OF_COLUMNS_DOESNT_MATCH);
if (size == 0)
return ColumnArray::create(data, ColumnOffsets_t::create());
return ColumnArray::create(data);
Filter nested_filt(getOffsets().back());
for (size_t i = 0; i < size; ++i)
@ -501,7 +510,7 @@ MutableColumnPtr ColumnArray::filterGeneric(const Filter & filt, ssize_t result_
memset(&nested_filt[offsetAt(i)], 0, sizeAt(i));
}
auto res = ColumnArray::create(data, ColumnOffsets_t::create());
auto res = ColumnArray::create(data);
ssize_t nested_result_size_hint = 0;
if (result_size_hint < 0)
@ -525,36 +534,36 @@ MutableColumnPtr ColumnArray::filterGeneric(const Filter & filt, ssize_t result_
}
}
return res;
return std::move(res);
}
MutableColumnPtr ColumnArray::filterNullable(const Filter & filt, ssize_t result_size_hint) const
{
if (getOffsets().size() == 0)
return ColumnArray::create(data, ColumnOffsets_t::create());
return ColumnArray::create(data);
const ColumnNullable & nullable_elems = static_cast<const ColumnNullable &>(*data);
auto array_of_nested = ColumnArray::create(nullable_elems.getNestedColumnPtr(), offsets);
auto filtered_array_of_nested_owner = array_of_nested->filter(filt, result_size_hint);
auto & filtered_array_of_nested = static_cast<ColumnArray &>(*filtered_array_of_nested_owner);
auto & filtered_array_of_nested = static_cast<const ColumnArray &>(*filtered_array_of_nested_owner);
auto & filtered_offsets = filtered_array_of_nested.getOffsetsPtr();
auto res_null_map = ColumnUInt8::create();
auto res = ColumnArray::create(
filterArraysImplOnlyData(nullable_elems.getNullMapData(), getOffsets(), res_null_map->getData(), filt, result_size_hint);
return ColumnArray::create(
ColumnNullable::create(
filtered_array_of_nested.getDataPtr(),
res_null_map),
filtered_offsets);
filterArraysImplOnlyData(nullable_elems.getNullMapData(), getOffsets(), res_null_map->getData(), filt, result_size_hint);
return res;
}
MutableColumnPtr ColumnArray::filterTuple(const Filter & filt, ssize_t result_size_hint) const
{
if (getOffsets().size() == 0)
return ColumnArray::create(data, ColumnOffsets_t::create());
return ColumnArray::create(data);
const ColumnTuple & tuple = static_cast<const ColumnTuple &>(*data);
@ -571,11 +580,11 @@ MutableColumnPtr ColumnArray::filterTuple(const Filter & filt, ssize_t result_si
Columns tuple_columns(tuple_size);
for (size_t i = 0; i < tuple_size; ++i)
tuple_columns[i] = static_cast<ColumnArray &>(*temporary_arrays[i]).getDataPtr();
tuple_columns[i] = static_cast<const ColumnArray &>(*temporary_arrays[i]).getDataPtr();
return ColumnArray::create(
ColumnTuple::create(tuple_columns),
static_cast<ColumnArray &>(*temporary_arrays.front()).getOffsetsPtr());
static_cast<const ColumnArray &>(*temporary_arrays.front()).getOffsetsPtr());
}
@ -592,11 +601,11 @@ MutableColumnPtr ColumnArray::permute(const Permutation & perm, size_t limit) co
throw Exception("Size of permutation is less than required.", ErrorCodes::SIZES_OF_COLUMNS_DOESNT_MATCH);
if (limit == 0)
return ColumnArray::create(data, ColumnOffsets_t::create());
return ColumnArray::create(data);
Permutation nested_perm(getOffsets().back());
auto res = ColumnArray::create(data->cloneEmpty(), ColumnOffsets_t::create());
auto res = ColumnArray::create(data->cloneEmpty());
Offsets_t & res_offsets = res->getOffsets();
res_offsets.resize(limit);
@ -613,7 +622,7 @@ MutableColumnPtr ColumnArray::permute(const Permutation & perm, size_t limit) co
if (current_offset != 0)
res->data = data->permute(nested_perm, current_offset);
return res;
return std::move(res);
}
void ColumnArray::getPermutation(bool reverse, size_t limit, int nan_direction_hint, Permutation & res) const
@ -886,11 +895,11 @@ MutableColumnPtr ColumnArray::replicateTuple(const Offsets_t & replicate_offsets
Columns tuple_columns(tuple_size);
for (size_t i = 0; i < tuple_size; ++i)
tuple_columns[i] = static_cast<ColumnArray &>(*temporary_arrays[i]).getDataPtr();
tuple_columns[i] = static_cast<const ColumnArray &>(*temporary_arrays[i]).getDataPtr();
return ColumnArray::create(
ColumnTuple::create(tuple_columns),
static_cast<ColumnArray &>(*temporary_arrays.front()).getOffsetsPtr());
static_cast<const ColumnArray &>(*temporary_arrays.front()).getOffsetsPtr());
}

View File

@ -23,9 +23,12 @@ class ColumnArray final : public COWPtrHelper<IColumn, ColumnArray>
private:
friend class COWPtrHelper<IColumn, ColumnArray>;
/** Create an empty column of arrays with the type of values as in the column `nested_column` */
/** Create an array column with specified values and offsets. */
ColumnArray(const ColumnPtr & nested_column, const ColumnPtr & offsets_column);
/** Create an empty column of arrays with the type of values as in the column `nested_column` */
ColumnArray(const ColumnPtr & nested_column);
ColumnArray(const ColumnArray & src) = default;
public:

View File

@ -10,7 +10,7 @@
namespace DB
{
ColumnConst::ColumnConst(ColumnPtr data_, size_t s)
ColumnConst::ColumnConst(const ColumnPtr & data_, size_t s)
: data(data_), s(s)
{
/// Squash Const of Const.

View File

@ -27,7 +27,7 @@ private:
ColumnPtr data;
size_t s;
ColumnConst(ColumnPtr data, size_t s);
ColumnConst(const ColumnPtr & data, size_t s);
ColumnConst(const ColumnConst & src) = default;
public:

View File

@ -6,18 +6,20 @@ namespace DB
{
ColumnExpression::ColumnExpression(
size_t s_, ExpressionActionsPtr expression_, const NamesAndTypes & arguments_, DataTypePtr return_type_, std::string return_name_)
: IColumnDummy(s_), expression(expression_), arguments(arguments_), return_type(return_type_), return_name(return_name_)
size_t s_, const ExpressionActionsPtr & expression_, const NamesAndTypes & arguments_, const DataTypePtr & return_type_, const String & return_name_)
: expression(expression_), arguments(arguments_), return_type(return_type_), return_name(return_name_)
{
s = s_;
}
ColumnExpression::ColumnExpression(
size_t s_, ExpressionActionsPtr expression_, const NamesAndTypesList & arguments_, DataTypePtr return_type_, std::string return_name_)
: IColumnDummy(s_), expression(expression_), arguments(arguments_.begin(), arguments_.end()), return_type(return_type_), return_name(return_name_)
size_t s_, const ExpressionActionsPtr & expression_, const NamesAndTypesList & arguments_, const DataTypePtr & return_type_, const String & return_name_)
: expression(expression_), arguments(arguments_.begin(), arguments_.end()), return_type(return_type_), return_name(return_name_)
{
s = s_;
}
ColumnPtr ColumnExpression::cloneDummy(size_t s_) const
MutableColumnPtr ColumnExpression::cloneDummy(size_t s_) const
{
return ColumnExpression::create(s_, expression, arguments, return_type, return_name);
}

View File

@ -19,8 +19,8 @@ private:
using ExpressionActionsPtr = std::shared_ptr<ExpressionActions>;
ColumnExpression(size_t s_, ExpressionActionsPtr expression_, const NamesAndTypes & arguments_, DataTypePtr return_type_, String return_name_);
ColumnExpression(size_t s_, ExpressionActionsPtr expression_, const NamesAndTypesList & arguments_, DataTypePtr return_type_, String return_name_);
ColumnExpression(size_t s_, const ExpressionActionsPtr & expression_, const NamesAndTypes & arguments_, const DataTypePtr & return_type_, const String & return_name_);
ColumnExpression(size_t s_, const ExpressionActionsPtr & expression_, const NamesAndTypesList & arguments_, const DataTypePtr & return_type_, const String & return_name_);
ColumnExpression(const ColumnExpression &) = default;

View File

@ -227,7 +227,7 @@ MutableColumnPtr ColumnFixedString::filter(const IColumn::Filter & filt, ssize_t
data_pos += n;
}
return res;
return std::move(res);
}
MutableColumnPtr ColumnFixedString::permute(const Permutation & perm, size_t limit) const
@ -255,7 +255,7 @@ MutableColumnPtr ColumnFixedString::permute(const Permutation & perm, size_t lim
for (size_t i = 0; i < limit; ++i, offset += n)
memcpySmallAllowReadWriteOverflow15(&res_chars[offset], &chars[perm[i] * n], n);
return res;
return std::move(res);
}
MutableColumnPtr ColumnFixedString::replicate(const Offsets_t & offsets) const
@ -267,7 +267,7 @@ MutableColumnPtr ColumnFixedString::replicate(const Offsets_t & offsets) const
auto res = ColumnFixedString::create(n);
if (0 == col_size)
return res;
return std::move(res);
Chars_t & res_chars = res->chars;
res_chars.resize(n * offsets.back());
@ -277,7 +277,7 @@ MutableColumnPtr ColumnFixedString::replicate(const Offsets_t & offsets) const
for (size_t next_offset = offsets[i]; curr_offset < next_offset; ++curr_offset)
memcpySmallAllowReadWriteOverflow15(&res->chars[curr_offset * n], &chars[i * n], n);
return res;
return std::move(res);
}
void ColumnFixedString::gather(ColumnGathererStream & gatherer)

View File

@ -18,11 +18,11 @@ namespace ErrorCodes
}
ColumnNullable::ColumnNullable(ColumnPtr nested_column_, ColumnPtr null_map_)
ColumnNullable::ColumnNullable(const ColumnPtr & nested_column_, const ColumnPtr & null_map_)
: nested_column{nested_column_}, null_map{null_map_}
{
/// ColumnNullable cannot have constant nested column. But constant argument could be passed. Materialize it.
if (auto nested_column_materialized = getNestedColumn().convertToFullColumnIfConst())
if (ColumnPtr nested_column_materialized = getNestedColumn().convertToFullColumnIfConst())
nested_column = nested_column_materialized;
if (!getNestedColumn().canBeInsideNullable())
@ -42,7 +42,7 @@ void ColumnNullable::updateHashWithValue(size_t n, SipHash & hash) const
}
ColumnPtr ColumnNullable::cloneResized(size_t new_size) const
MutableColumnPtr ColumnNullable::cloneResized(size_t new_size) const
{
ColumnPtr new_nested_col = getNestedColumn().cloneResized(new_size);
auto new_null_map = ColumnUInt8::create();
@ -59,7 +59,7 @@ ColumnPtr ColumnNullable::cloneResized(size_t new_size) const
memset(&new_null_map->getData()[count], 1, new_size - count);
}
return ColumnNullable::create(new_nested_col, new_null_map);
return ColumnNullable::create(new_nested_col, std::move(new_null_map));
}
@ -437,7 +437,7 @@ ColumnPtr makeNullable(const ColumnPtr & column)
return column;
if (column->isColumnConst())
return ColumnConst::create(makeNullable(static_cast<ColumnConst &>(*column).getDataColumnPtr()), column->size());
return ColumnConst::create(makeNullable(static_cast<const ColumnConst &>(*column).getDataColumnPtr()), column->size());
return ColumnNullable::create(column, ColumnUInt8::create(column->size(), 0));
}

View File

@ -23,7 +23,7 @@ class ColumnNullable final : public COWPtrHelper<IColumn, ColumnNullable>
private:
friend class COWPtrHelper<IColumn, ColumnNullable>;
ColumnNullable(ColumnPtr nested_column_, ColumnPtr null_map_);
ColumnNullable(const ColumnPtr & nested_column_, const ColumnPtr & null_map_);
ColumnNullable(ColumnNullable &) = default;
public:

View File

@ -24,7 +24,7 @@ MutableColumnPtr ColumnString::cloneResized(size_t to_size) const
auto res = ColumnString::create();
if (to_size == 0)
return res;
return std::move(res);
size_t from_size = size();
@ -59,7 +59,7 @@ MutableColumnPtr ColumnString::cloneResized(size_t to_size) const
}
}
return res;
return std::move(res);
}
@ -108,7 +108,7 @@ MutableColumnPtr ColumnString::filter(const Filter & filt, ssize_t result_size_h
Offsets_t & res_offsets = res->offsets;
filterArraysImpl<UInt8>(chars, offsets, res_chars, res_offsets, filt, result_size_hint);
return res;
return std::move(res);
}
@ -158,7 +158,7 @@ MutableColumnPtr ColumnString::permute(const Permutation & perm, size_t limit) c
res_offsets[i] = current_new_offset;
}
return res;
return std::move(res);
}
@ -217,7 +217,7 @@ MutableColumnPtr ColumnString::replicate(const Offsets_t & replicate_offsets) co
auto res = ColumnString::create();
if (0 == col_size)
return res;
return std::move(res);
Chars_t & res_chars = res->chars;
Offsets_t & res_offsets = res->offsets;
@ -247,7 +247,7 @@ MutableColumnPtr ColumnString::replicate(const Offsets_t & replicate_offsets) co
prev_string_offset = offsets[i];
}
return res;
return std::move(res);
}

View File

@ -38,7 +38,7 @@ ColumnTuple::ColumnTuple(const Columns & columns) : columns(columns)
throw Exception{"ColumnTuple cannot have ColumnConst as its element", ErrorCodes::ILLEGAL_COLUMN};
}
ColumnPtr ColumnTuple::cloneEmpty() const
MutableColumnPtr ColumnTuple::cloneEmpty() const
{
const size_t tuple_size = columns.size();
Columns new_columns(tuple_size);
@ -176,7 +176,7 @@ MutableColumnPtr ColumnTuple::replicate(const Offsets_t & offsets) const
MutableColumns ColumnTuple::scatter(ColumnIndex num_columns, const Selector & selector) const
{
const size_t tuple_size = columns.size();
std::vector<Columns> scattered_tuple_elements(tuple_size);
std::vector<MutableColumns> scattered_tuple_elements(tuple_size);
for (size_t tuple_element_idx = 0; tuple_element_idx < tuple_size; ++tuple_element_idx)
scattered_tuple_elements[tuple_element_idx] = columns[tuple_element_idx]->scatter(num_columns, selector);
@ -187,7 +187,7 @@ MutableColumns ColumnTuple::scatter(ColumnIndex num_columns, const Selector & se
{
Columns new_columns(tuple_size);
for (size_t tuple_element_idx = 0; tuple_element_idx < tuple_size; ++tuple_element_idx)
new_columns[tuple_element_idx] = scattered_tuple_elements[tuple_element_idx][scattered_idx];
new_columns[tuple_element_idx] = std::move(scattered_tuple_elements[tuple_element_idx][scattered_idx]);
res[scattered_idx] = ColumnTuple::create(new_columns);
}
@ -264,8 +264,9 @@ void ColumnTuple::gather(ColumnGathererStream & gatherer)
void ColumnTuple::reserve(size_t n)
{
for (auto & column : columns)
column->reserve(n);
const size_t tuple_size = columns.size();
for (size_t i = 0; i < tuple_size; ++i)
getColumnPtr(i)->reserve(n);
}
size_t ColumnTuple::byteSize() const

View File

@ -62,6 +62,8 @@ public:
size_t allocatedBytes() const override;
void forEachSubcolumn(ColumnCallback callback) override;
size_t tupleSize() const { return columns.size(); }
const Columns & getColumns() const { return columns; }
const ColumnPtr & getColumnPtr(size_t idx) const { return columns[idx]; }

View File

@ -105,7 +105,7 @@ const char * ColumnVector<T>::getFamilyName() const
template <typename T>
MutableColumnPtr ColumnVector<T>::cloneResized(size_t size) const
{
auto res = create();
auto res = this->create();
if (size > 0)
{
@ -119,7 +119,7 @@ MutableColumnPtr ColumnVector<T>::cloneResized(size_t size) const
memset(&new_col.data[count], static_cast<int>(value_type()), size - count);
}
return res;
return std::move(res);
}
template <typename T>
@ -152,7 +152,7 @@ MutableColumnPtr ColumnVector<T>::filter(const IColumn::Filter & filt, ssize_t r
if (size != filt.size())
throw Exception("Size of filter doesn't match size of column.", ErrorCodes::SIZES_OF_COLUMNS_DOESNT_MATCH);
auto res = create();
auto res = this->create();
Container_t & res_data = res->getData();
if (result_size_hint)
@ -206,7 +206,7 @@ MutableColumnPtr ColumnVector<T>::filter(const IColumn::Filter & filt, ssize_t r
++data_pos;
}
return res;
return std::move(res);
}
template <typename T>
@ -222,12 +222,12 @@ MutableColumnPtr ColumnVector<T>::permute(const IColumn::Permutation & perm, siz
if (perm.size() < limit)
throw Exception("Size of permutation is less than required.", ErrorCodes::SIZES_OF_COLUMNS_DOESNT_MATCH);
auto res = create(limit);
auto res = this->create(limit);
typename Self::Container_t & res_data = res->getData();
for (size_t i = 0; i < limit; ++i)
res_data[i] = data[perm[i]];
return res;
return std::move(res);
}
template <typename T>
@ -238,9 +238,9 @@ MutableColumnPtr ColumnVector<T>::replicate(const IColumn::Offsets_t & offsets)
throw Exception("Size of offsets doesn't match size of column.", ErrorCodes::SIZES_OF_COLUMNS_DOESNT_MATCH);
if (0 == size)
return create();
return this->create();
auto res = create();
auto res = this->create();
typename Self::Container_t & res_data = res->getData();
res_data.reserve(offsets.back());
@ -254,7 +254,7 @@ MutableColumnPtr ColumnVector<T>::replicate(const IColumn::Offsets_t & offsets)
res_data.push_back(data[i]);
}
return res;
return std::move(res);
}
template <typename T>