2016-07-05 16:23:37 +00:00
|
|
|
#pragma once
|
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Columns/IColumn.h>
|
2019-10-07 18:56:03 +00:00
|
|
|
#include <Columns/IColumnImpl.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Columns/ColumnsNumber.h>
|
2019-03-14 23:10:51 +00:00
|
|
|
#include <Common/typeid_cast.h>
|
2019-08-21 02:28:04 +00:00
|
|
|
#include <Common/assert_cast.h>
|
2019-03-14 23:10:51 +00:00
|
|
|
|
2022-02-09 16:32:52 +00:00
|
|
|
#include "config_core.h"
|
|
|
|
|
|
|
|
|
2020-10-22 20:23:44 +00:00
|
|
|
class Collator;
|
2016-07-05 16:23:37 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2017-12-15 21:32:25 +00:00
|
|
|
using NullMap = ColumnUInt8::Container;
|
2017-03-29 11:33:07 +00:00
|
|
|
using ConstNullMapPtr = const NullMap *;
|
2016-08-10 19:12:29 +00:00
|
|
|
|
2016-08-16 13:31:42 +00:00
|
|
|
/// Class that specifies nullable columns. A nullable column represents
|
|
|
|
/// a column, which may have any type, provided with the possibility of
|
|
|
|
/// storing NULL values. For this purpose, a ColumNullable object stores
|
|
|
|
/// an ordinary column along with a special column, namely a byte map,
|
|
|
|
/// whose type is ColumnUInt8. The latter column indicates whether the
|
|
|
|
/// value of a given row is a NULL or not. Such a design is preferred
|
|
|
|
/// over a bitmap because columns are usually stored on disk as compressed
|
|
|
|
/// files. In this regard, using a bitmap instead of a byte map would
|
|
|
|
/// greatly complicate the implementation with little to no benefits.
|
2019-04-19 20:21:17 +00:00
|
|
|
class ColumnNullable final : public COWHelper<IColumn, ColumnNullable>
|
2016-07-05 16:23:37 +00:00
|
|
|
{
|
2017-12-14 04:25:22 +00:00
|
|
|
private:
|
2019-04-19 20:21:17 +00:00
|
|
|
friend class COWHelper<IColumn, ColumnNullable>;
|
2017-12-14 04:25:22 +00:00
|
|
|
|
2018-03-20 14:17:09 +00:00
|
|
|
ColumnNullable(MutableColumnPtr && nested_column_, MutableColumnPtr && null_map_);
|
2017-12-15 19:53:11 +00:00
|
|
|
ColumnNullable(const ColumnNullable &) = default;
|
2017-12-14 04:25:22 +00:00
|
|
|
|
2018-03-21 19:39:14 +00:00
|
|
|
public:
|
|
|
|
/** Create immutable column using immutable arguments. This arguments may be shared with other columns.
|
|
|
|
* Use IColumn::mutate in order to make mutable column and mutate shared nested columns.
|
|
|
|
*/
|
2019-04-19 20:21:17 +00:00
|
|
|
using Base = COWHelper<IColumn, ColumnNullable>;
|
2018-03-21 19:39:14 +00:00
|
|
|
static Ptr create(const ColumnPtr & nested_column_, const ColumnPtr & null_map_)
|
2018-03-20 14:17:09 +00:00
|
|
|
{
|
|
|
|
return ColumnNullable::create(nested_column_->assumeMutable(), null_map_->assumeMutable());
|
|
|
|
}
|
|
|
|
|
2022-03-17 18:06:50 +00:00
|
|
|
template <typename ... Args>
|
|
|
|
requires (IsMutableColumns<Args ...>::value)
|
2018-03-21 19:39:14 +00:00
|
|
|
static MutablePtr create(Args &&... args) { return Base::create(std::forward<Args>(args)...); }
|
|
|
|
|
2017-12-07 22:11:51 +00:00
|
|
|
const char * getFamilyName() const override { return "Nullable"; }
|
|
|
|
std::string getName() const override { return "Nullable(" + nested_column->getName() + ")"; }
|
2020-06-04 22:02:59 +00:00
|
|
|
TypeIndex getDataType() const override { return TypeIndex::Nullable; }
|
2017-12-14 03:56:56 +00:00
|
|
|
MutableColumnPtr cloneResized(size_t size) const override;
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t size() const override { return nested_column->size(); }
|
2019-08-21 02:28:04 +00:00
|
|
|
bool isNullAt(size_t n) const override { return assert_cast<const ColumnUInt8 &>(*null_map).getData()[n] != 0;}
|
2017-04-01 07:20:54 +00:00
|
|
|
Field operator[](size_t n) const override;
|
|
|
|
void get(size_t n, Field & res) const override;
|
2020-07-17 16:57:26 +00:00
|
|
|
bool getBool(size_t n) const override { return isNullAt(n) ? false : nested_column->getBool(n); }
|
2017-04-01 07:20:54 +00:00
|
|
|
UInt64 get64(size_t n) const override { return nested_column->get64(n); }
|
2021-04-15 02:16:09 +00:00
|
|
|
bool isDefaultAt(size_t n) const override { return isNullAt(n); }
|
2020-07-20 15:49:06 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* If isNullAt(n) returns false, returns the nested column's getDataAt(n), otherwise returns a special value
|
2020-07-22 13:18:21 +00:00
|
|
|
* EMPTY_STRING_REF indicating that data is not present.
|
2020-07-20 15:49:06 +00:00
|
|
|
*/
|
|
|
|
StringRef getDataAt(size_t n) const override
|
|
|
|
{
|
|
|
|
if (isNullAt(n))
|
2020-07-22 13:18:21 +00:00
|
|
|
return EMPTY_STRING_REF;
|
2020-07-20 15:49:06 +00:00
|
|
|
|
|
|
|
return getNestedColumn().getDataAt(n);
|
|
|
|
}
|
2019-05-23 13:35:26 +00:00
|
|
|
|
|
|
|
/// Will insert null value if pos=nullptr
|
2017-04-01 07:20:54 +00:00
|
|
|
void insertData(const char * pos, size_t length) override;
|
|
|
|
StringRef serializeValueIntoArena(size_t n, Arena & arena, char const *& begin) const override;
|
|
|
|
const char * deserializeAndInsertFromArena(const char * pos) override;
|
2021-02-16 21:26:06 +00:00
|
|
|
const char * skipSerializedInArena(const char * pos) const override;
|
2018-06-03 17:43:56 +00:00
|
|
|
void insertRangeFrom(const IColumn & src, size_t start, size_t length) override;
|
2017-04-01 07:20:54 +00:00
|
|
|
void insert(const Field & x) override;
|
|
|
|
void insertFrom(const IColumn & src, size_t n) override;
|
2017-01-30 05:22:49 +00:00
|
|
|
|
2019-09-17 16:55:11 +00:00
|
|
|
void insertFromNotNullable(const IColumn & src, size_t n);
|
|
|
|
void insertRangeFromNotNullable(const IColumn & src, size_t start, size_t length);
|
2019-09-18 18:44:44 +00:00
|
|
|
void insertManyFromNotNullable(const IColumn & src, size_t position, size_t length);
|
2019-09-17 16:55:11 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
void insertDefault() override
|
|
|
|
{
|
2017-12-14 03:56:56 +00:00
|
|
|
getNestedColumn().insertDefault();
|
|
|
|
getNullMapData().push_back(1);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2017-01-30 05:22:49 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
void popBack(size_t n) override;
|
2018-03-20 14:17:09 +00:00
|
|
|
ColumnPtr filter(const Filter & filt, ssize_t result_size_hint) const override;
|
2021-06-07 10:55:55 +00:00
|
|
|
void expand(const Filter & mask, bool inverted) override;
|
2019-02-18 19:44:26 +00:00
|
|
|
ColumnPtr permute(const Permutation & perm, size_t limit) const override;
|
2019-02-18 17:28:53 +00:00
|
|
|
ColumnPtr index(const IColumn & indexes, size_t limit) const override;
|
2017-04-01 07:20:54 +00:00
|
|
|
int compareAt(size_t n, size_t m, const IColumn & rhs_, int null_direction_hint) const override;
|
2022-02-09 16:32:52 +00:00
|
|
|
|
|
|
|
#if USE_EMBEDDED_COMPILER
|
|
|
|
|
|
|
|
bool isComparatorCompilable() const override;
|
|
|
|
|
|
|
|
llvm::Value * compileComparator(llvm::IRBuilderBase & /*builder*/, llvm::Value * /*lhs*/, llvm::Value * /*rhs*/, llvm::Value * /*nan_direction_hint*/) const override;
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2020-06-02 00:23:41 +00:00
|
|
|
void compareColumn(const IColumn & rhs, size_t rhs_row_num,
|
2020-06-17 11:43:55 +00:00
|
|
|
PaddedPODArray<UInt64> * row_indexes, PaddedPODArray<Int8> & compare_results,
|
2020-06-02 00:23:41 +00:00
|
|
|
int direction, int nan_direction_hint) const override;
|
2020-10-29 11:24:01 +00:00
|
|
|
int compareAtWithCollation(size_t n, size_t m, const IColumn & rhs, int null_direction_hint, const Collator &) const override;
|
2021-02-26 04:50:04 +00:00
|
|
|
bool hasEqualValues() const override;
|
2022-02-23 17:34:19 +00:00
|
|
|
void getPermutation(IColumn::PermutationSortDirection direction, IColumn::PermutationSortStability stability,
|
|
|
|
size_t limit, int null_direction_hint, Permutation & res) const override;
|
|
|
|
void updatePermutation(IColumn::PermutationSortDirection direction, IColumn::PermutationSortStability stability,
|
|
|
|
size_t limit, int null_direction_hint, Permutation & res, EqualRanges & equal_ranges) const override;
|
|
|
|
void getPermutationWithCollation(const Collator & collator, IColumn::PermutationSortDirection direction, IColumn::PermutationSortStability stability,
|
|
|
|
size_t limit, int null_direction_hint, Permutation & res) const override;
|
|
|
|
void updatePermutationWithCollation(const Collator & collator, IColumn::PermutationSortDirection direction, IColumn::PermutationSortStability stability,
|
|
|
|
size_t limit, int null_direction_hint, Permutation & res, EqualRanges& equal_ranges) const override;
|
2017-04-01 07:20:54 +00:00
|
|
|
void reserve(size_t n) override;
|
2022-03-05 12:50:47 +00:00
|
|
|
void ensureOwnership() override;
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t byteSize() const override;
|
2021-01-02 22:58:10 +00:00
|
|
|
size_t byteSizeAt(size_t n) const override;
|
2017-07-13 16:49:09 +00:00
|
|
|
size_t allocatedBytes() const override;
|
2019-03-10 03:16:51 +00:00
|
|
|
void protect() override;
|
2018-03-20 14:17:09 +00:00
|
|
|
ColumnPtr replicate(const Offsets & replicate_offsets) const override;
|
2017-04-01 07:20:54 +00:00
|
|
|
void updateHashWithValue(size_t n, SipHash & hash) const override;
|
2020-03-18 16:03:55 +00:00
|
|
|
void updateWeakHash32(WeakHash32 & hash) const override;
|
2020-05-20 22:16:08 +00:00
|
|
|
void updateHashFast(SipHash & hash) const override;
|
2017-04-01 07:20:54 +00:00
|
|
|
void getExtremes(Field & min, Field & max) const override;
|
2021-01-02 09:47:38 +00:00
|
|
|
// Special function for nullable minmax index
|
|
|
|
void getExtremesNullLast(Field & min, Field & max) const;
|
2016-07-05 16:23:37 +00:00
|
|
|
|
2017-12-14 03:56:56 +00:00
|
|
|
MutableColumns scatter(ColumnIndex num_columns, const Selector & selector) const override
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
return scatterImpl<ColumnNullable>(num_columns, selector);
|
|
|
|
}
|
2017-02-11 20:20:57 +00:00
|
|
|
|
2017-07-06 13:54:55 +00:00
|
|
|
void gather(ColumnGathererStream & gatherer_stream) override;
|
|
|
|
|
2021-02-17 23:09:46 +00:00
|
|
|
ColumnPtr compress() const override;
|
|
|
|
|
2017-12-07 12:09:55 +00:00
|
|
|
void forEachSubcolumn(ColumnCallback callback) override
|
|
|
|
{
|
|
|
|
callback(nested_column);
|
|
|
|
callback(null_map);
|
2019-03-14 23:10:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool structureEquals(const IColumn & rhs) const override
|
|
|
|
{
|
2022-03-11 18:16:49 +00:00
|
|
|
if (const auto * rhs_nullable = typeid_cast<const ColumnNullable *>(&rhs))
|
2019-03-14 23:10:51 +00:00
|
|
|
return nested_column->structureEquals(*rhs_nullable->nested_column);
|
|
|
|
return false;
|
2017-12-07 12:09:55 +00:00
|
|
|
}
|
|
|
|
|
2021-06-07 13:50:27 +00:00
|
|
|
double getRatioOfDefaultRows(double sample_ratio) const override
|
|
|
|
{
|
2021-07-23 23:15:44 +00:00
|
|
|
return getRatioOfDefaultRowsImpl<ColumnNullable>(sample_ratio);
|
2021-06-07 13:50:27 +00:00
|
|
|
}
|
|
|
|
|
2021-06-07 15:34:22 +00:00
|
|
|
void getIndicesOfNonDefaultRows(Offsets & indices, size_t from, size_t limit) const override
|
|
|
|
{
|
2021-07-23 23:15:44 +00:00
|
|
|
getIndicesOfNonDefaultRowsImpl<ColumnNullable>(indices, from, limit);
|
2021-06-07 15:34:22 +00:00
|
|
|
}
|
|
|
|
|
2021-06-07 16:38:22 +00:00
|
|
|
ColumnPtr createWithOffsets(const Offsets & offsets, const Field & default_field, size_t total_rows, size_t shift) const override;
|
2021-06-07 13:50:27 +00:00
|
|
|
|
2019-07-01 11:44:19 +00:00
|
|
|
bool isNullable() const override { return true; }
|
2017-12-09 10:14:45 +00:00
|
|
|
bool isFixedAndContiguous() const override { return false; }
|
|
|
|
bool valuesHaveFixedSize() const override { return nested_column->valuesHaveFixedSize(); }
|
|
|
|
size_t sizeOfValueIfFixed() const override { return null_map->sizeOfValueIfFixed() + nested_column->sizeOfValueIfFixed(); }
|
2018-02-28 19:47:33 +00:00
|
|
|
bool onlyNull() const override { return nested_column->isDummy(); }
|
2020-10-29 11:24:01 +00:00
|
|
|
bool isCollationSupported() const override { return nested_column->isCollationSupported(); }
|
2017-12-09 10:14:45 +00:00
|
|
|
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Return the column that represents values.
|
2019-03-25 01:43:54 +00:00
|
|
|
IColumn & getNestedColumn() { return *nested_column; }
|
2017-12-14 03:56:56 +00:00
|
|
|
const IColumn & getNestedColumn() const { return *nested_column; }
|
|
|
|
|
|
|
|
const ColumnPtr & getNestedColumnPtr() const { return nested_column; }
|
2020-11-10 17:32:00 +00:00
|
|
|
ColumnPtr & getNestedColumnPtr() { return nested_column; }
|
2016-08-16 13:31:42 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Return the column that represents the byte map.
|
2017-12-14 03:56:56 +00:00
|
|
|
const ColumnPtr & getNullMapColumnPtr() const { return null_map; }
|
2020-11-10 17:32:00 +00:00
|
|
|
ColumnPtr & getNullMapColumnPtr() { return null_map; }
|
2016-12-30 05:13:14 +00:00
|
|
|
|
2019-08-21 02:28:04 +00:00
|
|
|
ColumnUInt8 & getNullMapColumn() { return assert_cast<ColumnUInt8 &>(*null_map); }
|
|
|
|
const ColumnUInt8 & getNullMapColumn() const { return assert_cast<const ColumnUInt8 &>(*null_map); }
|
2016-12-30 05:13:14 +00:00
|
|
|
|
2017-12-14 03:56:56 +00:00
|
|
|
NullMap & getNullMapData() { return getNullMapColumn().getData(); }
|
|
|
|
const NullMap & getNullMapData() const { return getNullMapColumn().getData(); }
|
2016-07-05 16:23:37 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Apply the null byte map of a specified nullable column onto the
|
|
|
|
/// null byte map of the current column by performing an element-wise OR
|
|
|
|
/// between both byte maps. This method is used to determine the null byte
|
|
|
|
/// map of the result column of a function taking one or more nullable
|
|
|
|
/// columns.
|
|
|
|
void applyNullMap(const ColumnNullable & other);
|
|
|
|
void applyNullMap(const ColumnUInt8 & map);
|
2022-03-21 18:08:35 +00:00
|
|
|
void applyNullMap(const NullMap & map);
|
2017-04-01 07:20:54 +00:00
|
|
|
void applyNegatedNullMap(const ColumnUInt8 & map);
|
2022-03-21 18:08:35 +00:00
|
|
|
void applyNegatedNullMap(const NullMap & map);
|
2016-07-05 16:23:37 +00:00
|
|
|
|
2017-04-17 20:19:09 +00:00
|
|
|
/// Check that size of null map equals to size of nested column.
|
|
|
|
void checkConsistency() const;
|
|
|
|
|
2016-07-05 16:23:37 +00:00
|
|
|
private:
|
2019-03-25 01:43:54 +00:00
|
|
|
WrappedPtr nested_column;
|
|
|
|
WrappedPtr null_map;
|
2017-03-07 20:52:09 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
template <bool negative>
|
2022-03-21 18:08:35 +00:00
|
|
|
void applyNullMapImpl(const NullMap & map);
|
2020-10-29 11:24:01 +00:00
|
|
|
|
|
|
|
int compareAtImpl(size_t n, size_t m, const IColumn & rhs_, int null_direction_hint, const Collator * collator=nullptr) const;
|
|
|
|
|
2022-02-23 17:34:19 +00:00
|
|
|
void getPermutationImpl(IColumn::PermutationSortDirection direction, IColumn::PermutationSortStability stability,
|
|
|
|
size_t limit, int null_direction_hint, Permutation & res, const Collator * collator = nullptr) const;
|
2020-10-29 11:24:01 +00:00
|
|
|
|
2022-02-23 17:34:19 +00:00
|
|
|
void updatePermutationImpl(IColumn::PermutationSortDirection direction, IColumn::PermutationSortStability stability,
|
|
|
|
size_t limit, int null_direction_hint, Permutation & res, EqualRanges & equal_ranges, const Collator * collator = nullptr) const;
|
2016-07-05 16:23:37 +00:00
|
|
|
};
|
|
|
|
|
2017-12-10 22:44:04 +00:00
|
|
|
ColumnPtr makeNullable(const ColumnPtr & column);
|
2022-07-07 18:53:20 +00:00
|
|
|
ColumnPtr makeNullableSafe(const ColumnPtr & column);
|
2019-06-26 17:20:33 +00:00
|
|
|
|
2016-07-05 16:23:37 +00:00
|
|
|
}
|