2016-07-05 16:23:37 +00:00
|
|
|
#include <DB/Columns/ColumnNullable.h>
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
|
|
|
|
extern const int LOGICAL_ERROR;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-07-25 18:26:45 +00:00
|
|
|
ColumnNullable::ColumnNullable(ColumnPtr nested_column_, bool fill_with_nulls)
|
2016-07-07 10:33:12 +00:00
|
|
|
: nested_column{nested_column_},
|
|
|
|
null_map{std::make_shared<ColumnUInt8>()}
|
2016-07-05 16:23:37 +00:00
|
|
|
{
|
2016-07-07 10:33:12 +00:00
|
|
|
if (nested_column.get()->isNullable())
|
2016-07-05 16:23:37 +00:00
|
|
|
throw Exception{"A nullable column cannot contain another nullable column", ErrorCodes::LOGICAL_ERROR};
|
|
|
|
|
2016-07-07 10:33:12 +00:00
|
|
|
size_t n = nested_column.get()->size();
|
2016-07-05 16:23:37 +00:00
|
|
|
if (n > 0)
|
2016-07-25 18:26:45 +00:00
|
|
|
getNullMapContent().getData().resize_fill(n, (fill_with_nulls ? 1 : 0));
|
2016-07-05 16:23:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string ColumnNullable::getName() const
|
|
|
|
{
|
2016-07-07 10:33:12 +00:00
|
|
|
return "ColumnNullable(" + nested_column.get()->getName() + ")";
|
2016-07-05 16:23:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ColumnNullable::isNumeric() const
|
|
|
|
{
|
2016-07-07 10:33:12 +00:00
|
|
|
return nested_column.get()->isNumeric();
|
2016-07-05 16:23:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ColumnNullable::isConst() const
|
|
|
|
{
|
2016-07-07 10:33:12 +00:00
|
|
|
return nested_column.get()->isConst();
|
2016-07-05 16:23:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ColumnNullable::isFixed() const
|
|
|
|
{
|
2016-07-07 10:33:12 +00:00
|
|
|
return nested_column.get()->isFixed();
|
2016-07-05 16:23:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ColumnNullable::isNullable() const
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t ColumnNullable::sizeOfField() const
|
|
|
|
{
|
2016-07-07 10:33:12 +00:00
|
|
|
return nested_column.get()->sizeOfField();
|
2016-07-05 16:23:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ColumnPtr ColumnNullable::convertToFullColumnIfConst() const
|
|
|
|
{
|
|
|
|
ColumnPtr new_col_holder;
|
|
|
|
|
2016-07-07 10:33:12 +00:00
|
|
|
if (auto full_col = nested_column.get()->convertToFullColumnIfConst())
|
2016-07-05 16:23:37 +00:00
|
|
|
{
|
|
|
|
new_col_holder = std::make_shared<ColumnNullable>(full_col);
|
|
|
|
ColumnNullable & new_col = static_cast<ColumnNullable &>(*new_col_holder.get());
|
|
|
|
|
|
|
|
/// Create a byte map.
|
|
|
|
if (new_col[0].isNull())
|
2016-07-07 10:33:12 +00:00
|
|
|
new_col.null_map = std::make_shared<ColumnUInt8>(new_col.size(), 1);
|
2016-07-05 16:23:37 +00:00
|
|
|
else
|
2016-07-07 10:33:12 +00:00
|
|
|
new_col.null_map = std::make_shared<ColumnUInt8>(new_col.size(), 0);
|
2016-07-05 16:23:37 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
new_col_holder = {};
|
|
|
|
|
|
|
|
return new_col_holder;
|
|
|
|
}
|
|
|
|
|
2016-07-11 10:09:16 +00:00
|
|
|
void ColumnNullable::updateHashWithValue(size_t n, SipHash & hash) const
|
|
|
|
{
|
|
|
|
nested_column.get()->updateHashWithValue(n, hash);
|
|
|
|
}
|
|
|
|
|
2016-07-05 16:23:37 +00:00
|
|
|
ColumnPtr ColumnNullable::cloneResized(size_t size) const
|
|
|
|
{
|
2016-07-07 10:33:12 +00:00
|
|
|
return std::make_shared<ColumnNullable>(nested_column.get()->cloneResized(size));
|
2016-07-05 16:23:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ColumnPtr ColumnNullable::cloneEmpty() const
|
|
|
|
{
|
2016-07-07 10:33:12 +00:00
|
|
|
return std::make_shared<ColumnNullable>(nested_column.get()->cloneEmpty());
|
2016-07-05 16:23:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t ColumnNullable::size() const
|
|
|
|
{
|
2016-07-07 10:33:12 +00:00
|
|
|
return nested_column.get()->size();
|
2016-07-05 16:23:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Field ColumnNullable::operator[](size_t n) const
|
|
|
|
{
|
|
|
|
if (isNullAt(n))
|
|
|
|
return Field{};
|
|
|
|
else
|
2016-07-07 10:33:12 +00:00
|
|
|
{
|
|
|
|
const IColumn & col = *(nested_column.get());
|
|
|
|
return col[n];
|
|
|
|
}
|
2016-07-05 16:23:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ColumnNullable::get(size_t n, Field & res) const
|
|
|
|
{
|
|
|
|
if (isNullAt(n))
|
|
|
|
res = Field{};
|
|
|
|
else
|
2016-07-07 10:33:12 +00:00
|
|
|
nested_column.get()->get(n, res);
|
2016-07-05 16:23:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UInt64 ColumnNullable::get64(size_t n) const
|
|
|
|
{
|
|
|
|
throw Exception{"Unsupported method", ErrorCodes::LOGICAL_ERROR};
|
|
|
|
}
|
|
|
|
|
|
|
|
StringRef ColumnNullable::getDataAt(size_t n) const
|
|
|
|
{
|
|
|
|
if (isNullAt(n))
|
|
|
|
return StringRef{};
|
|
|
|
else
|
2016-07-07 10:33:12 +00:00
|
|
|
return nested_column.get()->getDataAt(n);
|
2016-07-05 16:23:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ColumnNullable::insertData(const char * pos, size_t length)
|
|
|
|
{
|
2016-07-07 10:33:12 +00:00
|
|
|
nested_column.get()->insertData(pos, length);
|
|
|
|
getNullMapContent().getData().resize_fill(nested_column.get()->size(), 0);
|
2016-07-05 16:23:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
StringRef ColumnNullable::serializeValueIntoArena(size_t n, Arena & arena, char const *& begin) const
|
|
|
|
{
|
2016-07-07 10:33:12 +00:00
|
|
|
return nested_column.get()->serializeValueIntoArena(n, arena, begin);
|
2016-07-05 16:23:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const char * ColumnNullable::deserializeAndInsertFromArena(const char * pos)
|
|
|
|
{
|
2016-07-07 10:33:12 +00:00
|
|
|
return nested_column.get()->deserializeAndInsertFromArena(pos);
|
2016-07-05 16:23:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ColumnNullable::insertRangeFrom(const IColumn & src, size_t start, size_t length)
|
|
|
|
{
|
|
|
|
if (length == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const ColumnNullable & concrete_src = static_cast<const ColumnNullable &>(src);
|
|
|
|
|
|
|
|
if (start > (std::numeric_limits<size_t>::max() - length))
|
|
|
|
throw Exception{"ColumnNullable: overflow", ErrorCodes::LOGICAL_ERROR};
|
|
|
|
|
|
|
|
if ((start + length) > concrete_src.size())
|
|
|
|
throw Exception{"Parameter out of bound in ColumNullable::insertRangeFrom method.",
|
|
|
|
ErrorCodes::PARAMETER_OUT_OF_BOUND};
|
|
|
|
|
2016-07-07 10:33:12 +00:00
|
|
|
getNullMapContent().insertRangeFrom(*(concrete_src.null_map.get()), start, length);
|
|
|
|
nested_column.get()->insertRangeFrom(*(concrete_src.nested_column.get()), start, length);
|
2016-07-05 16:23:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ColumnNullable::insert(const Field & x)
|
|
|
|
{
|
|
|
|
if (x.isNull())
|
|
|
|
{
|
2016-07-07 10:33:12 +00:00
|
|
|
nested_column.get()->insertDefault();
|
|
|
|
getNullMapContent().insert(1);
|
2016-07-05 16:23:37 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-07-07 10:33:12 +00:00
|
|
|
nested_column.get()->insert(x);
|
|
|
|
getNullMapContent().insert(0);
|
2016-07-05 16:23:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ColumnNullable::insertDefault()
|
|
|
|
{
|
2016-07-07 10:33:12 +00:00
|
|
|
nested_column.get()->insertDefault();
|
|
|
|
getNullMapContent().insert(0);
|
2016-07-05 16:23:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ColumnNullable::popBack(size_t n)
|
|
|
|
{
|
2016-07-07 10:33:12 +00:00
|
|
|
nested_column.get()->popBack(n);
|
|
|
|
getNullMapContent().popBack(n);
|
2016-07-05 16:23:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ColumnPtr ColumnNullable::filter(const Filter & filt, ssize_t result_size_hint) const
|
|
|
|
{
|
2016-07-07 10:33:12 +00:00
|
|
|
ColumnPtr new_data = nested_column.get()->filter(filt, result_size_hint);
|
2016-07-05 16:23:37 +00:00
|
|
|
ColumnPtr filtered_col_holder = std::make_shared<ColumnNullable>(new_data);
|
|
|
|
ColumnNullable & filtered_col = static_cast<ColumnNullable &>(*(filtered_col_holder.get()));
|
2016-07-07 10:33:12 +00:00
|
|
|
filtered_col.null_map = getNullMapContent().filter(filt, result_size_hint);
|
2016-07-05 16:23:37 +00:00
|
|
|
return filtered_col_holder;
|
|
|
|
}
|
|
|
|
|
|
|
|
ColumnPtr ColumnNullable::permute(const Permutation & perm, size_t limit) const
|
|
|
|
{
|
2016-07-07 10:33:12 +00:00
|
|
|
ColumnPtr new_data = nested_column.get()->permute(perm, limit);
|
2016-07-05 16:23:37 +00:00
|
|
|
ColumnPtr permuted_col_holder = std::make_shared<ColumnNullable>(new_data);
|
|
|
|
ColumnNullable & permuted_col = static_cast<ColumnNullable &>(*permuted_col_holder);
|
2016-07-07 10:33:12 +00:00
|
|
|
permuted_col.null_map = getNullMapContent().permute(perm, limit);
|
2016-07-05 16:23:37 +00:00
|
|
|
return permuted_col_holder;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ColumnNullable::compareAt(size_t n, size_t m, const IColumn & rhs_, int nan_direction_hint) const
|
|
|
|
{
|
|
|
|
Field lval;
|
|
|
|
get(n, lval);
|
|
|
|
|
|
|
|
Field rval;
|
|
|
|
rhs_.get(m, rval);
|
|
|
|
|
|
|
|
/// Two NULL values are equal.
|
|
|
|
if (lval.isNull() && rval.isNull())
|
|
|
|
return 0;
|
|
|
|
/// The NULL value is always lesser than any value.
|
|
|
|
if (lval.isNull())
|
|
|
|
return -1;
|
|
|
|
if (rval.isNull())
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
const ColumnNullable & nullable_rhs = static_cast<const ColumnNullable &>(rhs_);
|
|
|
|
const IColumn & nested_rhs = *(nullable_rhs.getNestedColumn().get());
|
|
|
|
|
2016-07-07 10:33:12 +00:00
|
|
|
return nested_column.get()->compareAt(n, m, nested_rhs, nan_direction_hint);
|
2016-07-05 16:23:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ColumnNullable::getPermutation(bool reverse, size_t limit, Permutation & res) const
|
|
|
|
{
|
2016-07-07 10:33:12 +00:00
|
|
|
nested_column.get()->getPermutation(reverse, limit, res);
|
2016-07-05 16:23:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ColumnNullable::reserve(size_t n)
|
|
|
|
{
|
2016-07-07 10:33:12 +00:00
|
|
|
nested_column.get()->reserve(n);
|
|
|
|
getNullMapContent().reserve(n);
|
2016-07-05 16:23:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t ColumnNullable::byteSize() const
|
|
|
|
{
|
2016-07-07 10:33:12 +00:00
|
|
|
return nested_column.get()->byteSize() + getNullMapContent().byteSize();
|
2016-07-05 16:23:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ColumnNullable::getExtremesImpl(Field & min, Field & max, const NullValuesByteMap * null_map_) const
|
|
|
|
{
|
2016-07-07 10:33:12 +00:00
|
|
|
return nested_column.get()->getExtremes(min, max, &(getNullMapContent().getData()));
|
2016-07-05 16:23:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ColumnPtr ColumnNullable::replicate(const Offsets_t & offsets) const
|
|
|
|
{
|
2016-07-07 10:33:12 +00:00
|
|
|
ColumnPtr replicated_col_holder = std::make_shared<ColumnNullable>(nested_column.get()->replicate(offsets));
|
2016-07-05 16:23:37 +00:00
|
|
|
ColumnNullable & replicated_col = static_cast<ColumnNullable &>(*replicated_col_holder);
|
2016-07-07 10:33:12 +00:00
|
|
|
replicated_col.null_map = getNullMapContent().replicate(offsets);
|
2016-07-05 16:23:37 +00:00
|
|
|
return replicated_col_holder;
|
|
|
|
}
|
|
|
|
|
|
|
|
ColumnPtr & ColumnNullable::getNestedColumn()
|
|
|
|
{
|
2016-07-07 10:33:12 +00:00
|
|
|
return nested_column;
|
2016-07-05 16:23:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const ColumnPtr & ColumnNullable::getNestedColumn() const
|
|
|
|
{
|
2016-07-07 10:33:12 +00:00
|
|
|
return nested_column;
|
2016-07-05 16:23:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ColumnPtr & ColumnNullable::getNullValuesByteMap()
|
|
|
|
{
|
2016-07-07 10:33:12 +00:00
|
|
|
return null_map;
|
2016-07-05 16:23:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const ColumnPtr & ColumnNullable::getNullValuesByteMap() const
|
|
|
|
{
|
2016-07-07 10:33:12 +00:00
|
|
|
return null_map;
|
2016-07-05 16:23:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ColumnNullable::updateNullValuesByteMap(const ColumnNullable & other)
|
|
|
|
{
|
2016-07-07 10:33:12 +00:00
|
|
|
NullValuesByteMap & arr1 = getNullMapContent().getData();
|
|
|
|
const NullValuesByteMap & arr2 = other.getNullMapContent().getData();
|
2016-07-05 16:23:37 +00:00
|
|
|
|
|
|
|
if (arr1.size() != arr2.size())
|
|
|
|
throw Exception{"Inconsistent sizes", ErrorCodes::LOGICAL_ERROR};
|
|
|
|
|
|
|
|
for (size_t i = 0; i < arr1.size(); ++i)
|
|
|
|
arr1[i] |= arr2[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ColumnNullable::isNullAt(size_t n) const
|
|
|
|
{
|
2016-07-07 10:33:12 +00:00
|
|
|
auto & arr = getNullMapContent().getData();
|
2016-07-05 16:23:37 +00:00
|
|
|
return arr[n] != 0;
|
|
|
|
}
|
|
|
|
|
2016-07-07 10:33:12 +00:00
|
|
|
ColumnUInt8 & ColumnNullable::getNullMapContent()
|
|
|
|
{
|
|
|
|
return static_cast<ColumnUInt8 &>(*(null_map.get()));
|
|
|
|
}
|
|
|
|
|
|
|
|
const ColumnUInt8 & ColumnNullable::getNullMapContent() const
|
|
|
|
{
|
|
|
|
return static_cast<const ColumnUInt8 &>(*(null_map.get()));
|
|
|
|
}
|
|
|
|
|
2016-07-05 16:23:37 +00:00
|
|
|
}
|