dbms: cleanup [#METR-19266]

This commit is contained in:
Alexey Arno 2016-08-17 01:40:05 +03:00
parent 4ae075c7b9
commit 821f1e6482
6 changed files with 38 additions and 64 deletions

View File

@ -20,8 +20,7 @@ using NullValuesByteMap = PaddedPODArray<UInt8>;
class ColumnNullable final : public IColumn class ColumnNullable final : public IColumn
{ {
public: public:
ColumnNullable(ColumnPtr nested_column_); ColumnNullable(ColumnPtr nested_column_, ColumnPtr null_map_);
ColumnNullable(ColumnPtr nested_column_, bool fill_with_nulls);
std::string getName() const override; std::string getName() const override;
bool isNumeric() const override; bool isNumeric() const override;
bool isConst() const override; bool isConst() const override;

View File

@ -42,7 +42,10 @@ UInt64 ColumnConst<Null>::get64(size_t n) const
template <> template <>
ColumnPtr ColumnConst<Null>::convertToFullColumn() const ColumnPtr ColumnConst<Null>::convertToFullColumn() const
{ {
return std::make_shared<ColumnNullable>(std::make_shared<ColumnUInt8>(size(), 0), true); /// We basically create a column whose rows have NULL values.
ColumnPtr full_col = std::make_shared<ColumnUInt8>(size(), 0);
ColumnPtr null_map = std::make_shared<ColumnUInt8>(size(), 1);
return std::make_shared<ColumnNullable>(full_col, null_map);
} }
template <> ColumnPtr ColumnConst<String>::convertToFullColumn() const template <> ColumnPtr ColumnConst<String>::convertToFullColumn() const

View File

@ -10,25 +10,13 @@ extern const int LOGICAL_ERROR;
} }
ColumnNullable::ColumnNullable(ColumnPtr nested_column_) ColumnNullable::ColumnNullable(ColumnPtr nested_column_, ColumnPtr null_map_)
: nested_column{nested_column_} : nested_column{nested_column_}, null_map{null_map_}
{ {
if (nested_column->isNullable()) if (nested_column->isNullable())
throw Exception{"A nullable column cannot contain another nullable column", ErrorCodes::LOGICAL_ERROR}; throw Exception{"A nullable column cannot contain another nullable column", ErrorCodes::LOGICAL_ERROR};
} }
ColumnNullable::ColumnNullable(ColumnPtr nested_column_, bool fill_with_nulls)
: nested_column{nested_column_},
null_map{std::make_shared<ColumnUInt8>()}
{
if (nested_column->isNullable())
throw Exception{"A nullable column cannot contain another nullable column", ErrorCodes::LOGICAL_ERROR};
size_t n = nested_column->size();
if (n > 0)
getNullMapContent().getData().resize_fill(n, (fill_with_nulls ? 1 : 0));
}
std::string ColumnNullable::getName() const std::string ColumnNullable::getName() const
{ {
return "ColumnNullable(" + nested_column->getName() + ")"; return "ColumnNullable(" + nested_column->getName() + ")";
@ -59,11 +47,7 @@ ColumnPtr ColumnNullable::convertToFullColumnIfConst() const
ColumnPtr new_col_holder; ColumnPtr new_col_holder;
if (auto full_col = nested_column->convertToFullColumnIfConst()) if (auto full_col = nested_column->convertToFullColumnIfConst())
{ new_col_holder = std::make_shared<ColumnNullable>(full_col, null_map);
new_col_holder = std::make_shared<ColumnNullable>(full_col);
ColumnNullable & new_col = static_cast<ColumnNullable &>(*new_col_holder);
new_col.null_map = null_map;
}
return new_col_holder; return new_col_holder;
} }
@ -78,10 +62,9 @@ void ColumnNullable::updateHashWithValue(size_t n, SipHash & hash) const
ColumnPtr ColumnNullable::cloneResized(size_t size) const ColumnPtr ColumnNullable::cloneResized(size_t size) const
{ {
ColumnPtr new_col_holder = std::make_shared<ColumnNullable>(nested_column->cloneResized(size)); ColumnPtr new_nested_col = nested_column->cloneResized(size);
auto & new_col = static_cast<ColumnNullable &>(*new_col_holder); ColumnPtr new_null_map = getNullMapContent().cloneResized(size);
new_col.null_map = getNullMapContent().cloneResized(size); return std::make_shared<ColumnNullable>(new_nested_col, new_null_map);
return new_col_holder;
} }
size_t ColumnNullable::size() const size_t ColumnNullable::size() const
@ -185,20 +168,16 @@ void ColumnNullable::popBack(size_t n)
ColumnPtr ColumnNullable::filter(const Filter & filt, ssize_t result_size_hint) const ColumnPtr ColumnNullable::filter(const Filter & filt, ssize_t result_size_hint) const
{ {
ColumnPtr new_data = nested_column->filter(filt, result_size_hint); ColumnPtr filtered_data = nested_column->filter(filt, result_size_hint);
ColumnPtr filtered_col_holder = std::make_shared<ColumnNullable>(new_data); ColumnPtr filtered_null_map = getNullMapContent().filter(filt, result_size_hint);
ColumnNullable & filtered_col = static_cast<ColumnNullable &>(*filtered_col_holder); return std::make_shared<ColumnNullable>(filtered_data, filtered_null_map);
filtered_col.null_map = getNullMapContent().filter(filt, result_size_hint);
return filtered_col_holder;
} }
ColumnPtr ColumnNullable::permute(const Permutation & perm, size_t limit) const ColumnPtr ColumnNullable::permute(const Permutation & perm, size_t limit) const
{ {
ColumnPtr new_data = nested_column->permute(perm, limit); ColumnPtr permuted_data = nested_column->permute(perm, limit);
ColumnPtr permuted_col_holder = std::make_shared<ColumnNullable>(new_data); ColumnPtr permuted_null_map = getNullMapContent().permute(perm, limit);
ColumnNullable & permuted_col = static_cast<ColumnNullable &>(*permuted_col_holder); return std::make_shared<ColumnNullable>(permuted_data, permuted_null_map);
permuted_col.null_map = getNullMapContent().permute(perm, limit);
return permuted_col_holder;
} }
int ColumnNullable::compareAt(size_t n, size_t m, const IColumn & rhs_, int null_direction_hint) const int ColumnNullable::compareAt(size_t n, size_t m, const IColumn & rhs_, int null_direction_hint) const
@ -381,10 +360,9 @@ void ColumnNullable::getExtremes(Field & min, Field & max) const
ColumnPtr ColumnNullable::replicate(const Offsets_t & offsets) const ColumnPtr ColumnNullable::replicate(const Offsets_t & offsets) const
{ {
ColumnPtr replicated_col_holder = std::make_shared<ColumnNullable>(nested_column->replicate(offsets)); ColumnPtr replicated_data = nested_column->replicate(offsets);
ColumnNullable & replicated_col = static_cast<ColumnNullable &>(*replicated_col_holder); ColumnPtr replicated_null_map = getNullMapContent().replicate(offsets);
replicated_col.null_map = getNullMapContent().replicate(offsets); return std::make_shared<ColumnNullable>(replicated_data, replicated_null_map);
return replicated_col_holder;
} }
ColumnPtr & ColumnNullable::getNestedColumn() ColumnPtr & ColumnNullable::getNestedColumn()

View File

@ -289,18 +289,14 @@ void DataTypeNullable::serializeTextXML(const IColumn & column, size_t row_num,
ColumnPtr DataTypeNullable::createColumn() const ColumnPtr DataTypeNullable::createColumn() const
{ {
ColumnPtr new_col_holder = std::make_shared<ColumnNullable>(nested_data_type->createColumn()); ColumnPtr new_col = nested_data_type->createColumn();
ColumnNullable & nullable_col = static_cast<ColumnNullable &>(*new_col_holder); return std::make_shared<ColumnNullable>(new_col, std::make_shared<ColumnUInt8>());
nullable_col.getNullValuesByteMap() = std::make_shared<ColumnUInt8>();
return new_col_holder;
} }
ColumnPtr DataTypeNullable::createConstColumn(size_t size, const Field & field) const ColumnPtr DataTypeNullable::createConstColumn(size_t size, const Field & field) const
{ {
ColumnPtr new_col_holder = std::make_shared<ColumnNullable>(nested_data_type->createConstColumn(size, field)); ColumnPtr new_col = nested_data_type->createConstColumn(size, field);
ColumnNullable & nullable_col = static_cast<ColumnNullable &>(*new_col_holder); return std::make_shared<ColumnNullable>(new_col, std::make_shared<ColumnUInt8>(size));
nullable_col.getNullValuesByteMap() = std::make_shared<ColumnUInt8>(size);
return new_col_holder;
} }
Field DataTypeNullable::getDefault() const Field DataTypeNullable::getDefault() const

View File

@ -181,7 +181,8 @@ void FunctionMultiIf::executeImpl(Block & block, const ColumnNumbers & args, siz
Block block_with_nested_cols = createBlockWithNestedColumns(block, args_to_transform); Block block_with_nested_cols = createBlockWithNestedColumns(block, args_to_transform);
/// Append a column that tracks, for each result of multiIf, the index /// Append a column that tracks, for each result of multiIf, the index
/// of the originating column. /// of the originating column. UInt16 is enough for 65536 columns.
/// A table with such a big number of columns is highly unlikely to appear.
ColumnWithTypeAndName elem; ColumnWithTypeAndName elem;
elem.type = std::make_shared<DataTypeUInt16>(); elem.type = std::make_shared<DataTypeUInt16>();
@ -202,19 +203,15 @@ void FunctionMultiIf::executeImpl(Block & block, const ColumnNumbers & args, siz
return; return;
} }
dest_col.column = std::make_shared<ColumnNullable>(source_col.column);
/// Setup the null byte map of the result column by using the branch tracker column values. /// Setup the null byte map of the result column by using the branch tracker column values.
ColumnPtr tracker_holder = block_with_nested_cols.unsafeGetByPosition(tracker).column; ColumnPtr tracker_holder = block_with_nested_cols.unsafeGetByPosition(tracker).column;
ColumnNullable & nullable_col = static_cast<ColumnNullable &>(*dest_col.column); ColumnPtr null_map;
if (auto col = typeid_cast<ColumnConstUInt16 *>(tracker_holder.get())) if (auto col = typeid_cast<ColumnConstUInt16 *>(tracker_holder.get()))
{ {
auto pos = col->getData(); auto pos = col->getData();
const IColumn & origin = *block.unsafeGetByPosition(pos).column; const IColumn & origin = *block.unsafeGetByPosition(pos).column;
ColumnPtr null_map;
if (origin.isNull()) if (origin.isNull())
null_map = std::make_shared<ColumnUInt8>(row_count, 1); null_map = std::make_shared<ColumnUInt8>(row_count, 1);
else if (origin.isNullable()) else if (origin.isNullable())
@ -224,8 +221,6 @@ void FunctionMultiIf::executeImpl(Block & block, const ColumnNumbers & args, siz
} }
else else
null_map = std::make_shared<ColumnUInt8>(row_count, 0); null_map = std::make_shared<ColumnUInt8>(row_count, 0);
nullable_col.getNullValuesByteMap() = null_map;
} }
else if (auto col = typeid_cast<ColumnUInt16 *>(tracker_holder.get())) else if (auto col = typeid_cast<ColumnUInt16 *>(tracker_holder.get()))
{ {
@ -250,9 +245,8 @@ void FunctionMultiIf::executeImpl(Block & block, const ColumnNumbers & args, siz
null_cols_map[arg] = is_null ? 1 : 0; null_cols_map[arg] = is_null ? 1 : 0;
} }
auto null_map = std::make_shared<ColumnUInt8>(row_count); null_map = std::make_shared<ColumnUInt8>(row_count);
nullable_col.getNullValuesByteMap() = null_map; auto & null_map_data = static_cast<ColumnUInt8 &>(*null_map).getData();
auto & null_map_data = null_map->getData();
const auto & data = col->getData(); const auto & data = col->getData();
for (size_t row = 0; row < row_count; ++row) for (size_t row = 0; row < row_count; ++row)
@ -276,6 +270,9 @@ void FunctionMultiIf::executeImpl(Block & block, const ColumnNumbers & args, siz
} }
else else
throw Exception{"Internal error", ErrorCodes::LOGICAL_ERROR}; throw Exception{"Internal error", ErrorCodes::LOGICAL_ERROR};
/// Store the result.
dest_col.column = std::make_shared<ColumnNullable>(source_col.column, null_map);
} }
catch (const Conditional::CondException & ex) catch (const Conditional::CondException & ex)
{ {

View File

@ -337,14 +337,15 @@ void IFunction::postProcessResult(Strategy strategy, Block & block, const Block
} }
else if (strategy == PROCESS_NULLABLE_COLUMNS) else if (strategy == PROCESS_NULLABLE_COLUMNS)
{ {
/// Initialize the result column.
const ColumnWithTypeAndName & source_col = processed_block.getByPosition(result); const ColumnWithTypeAndName & source_col = processed_block.getByPosition(result);
ColumnWithTypeAndName & dest_col = block.getByPosition(result); ColumnWithTypeAndName & dest_col = block.getByPosition(result);
dest_col.column = std::make_shared<ColumnNullable>(source_col.column);
/// Make a null map for the result. /// Initialize the result column.
ColumnNullable & nullable_col = static_cast<ColumnNullable &>(*dest_col.column); ColumnPtr null_map = std::make_shared<ColumnUInt8>(block.rowsInFirstColumn(), 0);
nullable_col.getNullValuesByteMap() = std::make_shared<ColumnUInt8>(dest_col.column->size(), 0); dest_col.column = std::make_shared<ColumnNullable>(source_col.column, null_map);
/// Deduce the null map of the result from the null maps of the
/// nullable columns.
createNullValuesByteMap(block, args, result); createNullValuesByteMap(block, args, result);
} }
else else