diff --git a/dbms/include/DB/Columns/ColumnNullable.h b/dbms/include/DB/Columns/ColumnNullable.h index cb6b7e5125a..810510065a1 100644 --- a/dbms/include/DB/Columns/ColumnNullable.h +++ b/dbms/include/DB/Columns/ColumnNullable.h @@ -20,8 +20,7 @@ using NullValuesByteMap = PaddedPODArray; class ColumnNullable final : public IColumn { public: - ColumnNullable(ColumnPtr nested_column_); - ColumnNullable(ColumnPtr nested_column_, bool fill_with_nulls); + ColumnNullable(ColumnPtr nested_column_, ColumnPtr null_map_); std::string getName() const override; bool isNumeric() const override; bool isConst() const override; diff --git a/dbms/src/Columns/ColumnConst.cpp b/dbms/src/Columns/ColumnConst.cpp index 91d6f281e80..a0ca47dccdc 100644 --- a/dbms/src/Columns/ColumnConst.cpp +++ b/dbms/src/Columns/ColumnConst.cpp @@ -42,7 +42,10 @@ UInt64 ColumnConst::get64(size_t n) const template <> ColumnPtr ColumnConst::convertToFullColumn() const { - return std::make_shared(std::make_shared(size(), 0), true); + /// We basically create a column whose rows have NULL values. + ColumnPtr full_col = std::make_shared(size(), 0); + ColumnPtr null_map = std::make_shared(size(), 1); + return std::make_shared(full_col, null_map); } template <> ColumnPtr ColumnConst::convertToFullColumn() const diff --git a/dbms/src/Columns/ColumnNullable.cpp b/dbms/src/Columns/ColumnNullable.cpp index 4b0ec6680b9..f86ad570218 100644 --- a/dbms/src/Columns/ColumnNullable.cpp +++ b/dbms/src/Columns/ColumnNullable.cpp @@ -10,25 +10,13 @@ extern const int LOGICAL_ERROR; } -ColumnNullable::ColumnNullable(ColumnPtr nested_column_) - : nested_column{nested_column_} +ColumnNullable::ColumnNullable(ColumnPtr nested_column_, ColumnPtr null_map_) + : nested_column{nested_column_}, null_map{null_map_} { if (nested_column->isNullable()) 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()} -{ - 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 { return "ColumnNullable(" + nested_column->getName() + ")"; @@ -59,11 +47,7 @@ ColumnPtr ColumnNullable::convertToFullColumnIfConst() const ColumnPtr new_col_holder; if (auto full_col = nested_column->convertToFullColumnIfConst()) - { - new_col_holder = std::make_shared(full_col); - ColumnNullable & new_col = static_cast(*new_col_holder); - new_col.null_map = null_map; - } + new_col_holder = std::make_shared(full_col, null_map); 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 new_col_holder = std::make_shared(nested_column->cloneResized(size)); - auto & new_col = static_cast(*new_col_holder); - new_col.null_map = getNullMapContent().cloneResized(size); - return new_col_holder; + ColumnPtr new_nested_col = nested_column->cloneResized(size); + ColumnPtr new_null_map = getNullMapContent().cloneResized(size); + return std::make_shared(new_nested_col, new_null_map); } 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 new_data = nested_column->filter(filt, result_size_hint); - ColumnPtr filtered_col_holder = std::make_shared(new_data); - ColumnNullable & filtered_col = static_cast(*filtered_col_holder); - filtered_col.null_map = getNullMapContent().filter(filt, result_size_hint); - return filtered_col_holder; + ColumnPtr filtered_data = nested_column->filter(filt, result_size_hint); + ColumnPtr filtered_null_map = getNullMapContent().filter(filt, result_size_hint); + return std::make_shared(filtered_data, filtered_null_map); } ColumnPtr ColumnNullable::permute(const Permutation & perm, size_t limit) const { - ColumnPtr new_data = nested_column->permute(perm, limit); - ColumnPtr permuted_col_holder = std::make_shared(new_data); - ColumnNullable & permuted_col = static_cast(*permuted_col_holder); - permuted_col.null_map = getNullMapContent().permute(perm, limit); - return permuted_col_holder; + ColumnPtr permuted_data = nested_column->permute(perm, limit); + ColumnPtr permuted_null_map = getNullMapContent().permute(perm, limit); + return std::make_shared(permuted_data, permuted_null_map); } 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 replicated_col_holder = std::make_shared(nested_column->replicate(offsets)); - ColumnNullable & replicated_col = static_cast(*replicated_col_holder); - replicated_col.null_map = getNullMapContent().replicate(offsets); - return replicated_col_holder; + ColumnPtr replicated_data = nested_column->replicate(offsets); + ColumnPtr replicated_null_map = getNullMapContent().replicate(offsets); + return std::make_shared(replicated_data, replicated_null_map); } ColumnPtr & ColumnNullable::getNestedColumn() diff --git a/dbms/src/DataTypes/DataTypeNullable.cpp b/dbms/src/DataTypes/DataTypeNullable.cpp index 5dd341b90f9..db9fffce5b8 100644 --- a/dbms/src/DataTypes/DataTypeNullable.cpp +++ b/dbms/src/DataTypes/DataTypeNullable.cpp @@ -289,18 +289,14 @@ void DataTypeNullable::serializeTextXML(const IColumn & column, size_t row_num, ColumnPtr DataTypeNullable::createColumn() const { - ColumnPtr new_col_holder = std::make_shared(nested_data_type->createColumn()); - ColumnNullable & nullable_col = static_cast(*new_col_holder); - nullable_col.getNullValuesByteMap() = std::make_shared(); - return new_col_holder; + ColumnPtr new_col = nested_data_type->createColumn(); + return std::make_shared(new_col, std::make_shared()); } ColumnPtr DataTypeNullable::createConstColumn(size_t size, const Field & field) const { - ColumnPtr new_col_holder = std::make_shared(nested_data_type->createConstColumn(size, field)); - ColumnNullable & nullable_col = static_cast(*new_col_holder); - nullable_col.getNullValuesByteMap() = std::make_shared(size); - return new_col_holder; + ColumnPtr new_col = nested_data_type->createConstColumn(size, field); + return std::make_shared(new_col, std::make_shared(size)); } Field DataTypeNullable::getDefault() const diff --git a/dbms/src/Functions/FunctionsConditional.cpp b/dbms/src/Functions/FunctionsConditional.cpp index b97c53585ad..ca546735ba5 100644 --- a/dbms/src/Functions/FunctionsConditional.cpp +++ b/dbms/src/Functions/FunctionsConditional.cpp @@ -181,7 +181,8 @@ void FunctionMultiIf::executeImpl(Block & block, const ColumnNumbers & args, siz Block block_with_nested_cols = createBlockWithNestedColumns(block, args_to_transform); /// 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; elem.type = std::make_shared(); @@ -202,19 +203,15 @@ void FunctionMultiIf::executeImpl(Block & block, const ColumnNumbers & args, siz return; } - dest_col.column = std::make_shared(source_col.column); - /// 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; - ColumnNullable & nullable_col = static_cast(*dest_col.column); + ColumnPtr null_map; if (auto col = typeid_cast(tracker_holder.get())) { auto pos = col->getData(); const IColumn & origin = *block.unsafeGetByPosition(pos).column; - ColumnPtr null_map; - if (origin.isNull()) null_map = std::make_shared(row_count, 1); else if (origin.isNullable()) @@ -224,8 +221,6 @@ void FunctionMultiIf::executeImpl(Block & block, const ColumnNumbers & args, siz } else null_map = std::make_shared(row_count, 0); - - nullable_col.getNullValuesByteMap() = null_map; } else if (auto col = typeid_cast(tracker_holder.get())) { @@ -250,9 +245,8 @@ void FunctionMultiIf::executeImpl(Block & block, const ColumnNumbers & args, siz null_cols_map[arg] = is_null ? 1 : 0; } - auto null_map = std::make_shared(row_count); - nullable_col.getNullValuesByteMap() = null_map; - auto & null_map_data = null_map->getData(); + null_map = std::make_shared(row_count); + auto & null_map_data = static_cast(*null_map).getData(); const auto & data = col->getData(); for (size_t row = 0; row < row_count; ++row) @@ -276,6 +270,9 @@ void FunctionMultiIf::executeImpl(Block & block, const ColumnNumbers & args, siz } else throw Exception{"Internal error", ErrorCodes::LOGICAL_ERROR}; + + /// Store the result. + dest_col.column = std::make_shared(source_col.column, null_map); } catch (const Conditional::CondException & ex) { diff --git a/dbms/src/Functions/IFunction.cpp b/dbms/src/Functions/IFunction.cpp index 5c19ff39ea7..3cd03c5b07c 100644 --- a/dbms/src/Functions/IFunction.cpp +++ b/dbms/src/Functions/IFunction.cpp @@ -337,14 +337,15 @@ void IFunction::postProcessResult(Strategy strategy, Block & block, const Block } else if (strategy == PROCESS_NULLABLE_COLUMNS) { - /// Initialize the result column. const ColumnWithTypeAndName & source_col = processed_block.getByPosition(result); ColumnWithTypeAndName & dest_col = block.getByPosition(result); - dest_col.column = std::make_shared(source_col.column); - /// Make a null map for the result. - ColumnNullable & nullable_col = static_cast(*dest_col.column); - nullable_col.getNullValuesByteMap() = std::make_shared(dest_col.column->size(), 0); + /// Initialize the result column. + ColumnPtr null_map = std::make_shared(block.rowsInFirstColumn(), 0); + dest_col.column = std::make_shared(source_col.column, null_map); + + /// Deduce the null map of the result from the null maps of the + /// nullable columns. createNullValuesByteMap(block, args, result); } else