2017-04-01 09:19:00 +00:00
|
|
|
#include <Common/Arena.h>
|
|
|
|
#include <Common/SipHash.h>
|
2019-08-21 02:28:04 +00:00
|
|
|
#include <Common/assert_cast.h>
|
2020-03-18 16:03:55 +00:00
|
|
|
#include <Common/WeakHash.h>
|
2023-08-08 02:07:45 +00:00
|
|
|
#include <Columns/ColumnDecimal.h>
|
|
|
|
#include <Columns/ColumnFixedString.h>
|
|
|
|
#include <Columns/ColumnsDateTime.h>
|
|
|
|
#include <Columns/ColumnsNumber.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Columns/ColumnNullable.h>
|
2019-06-27 18:50:20 +00:00
|
|
|
#include <Columns/ColumnConst.h>
|
2020-10-22 20:23:44 +00:00
|
|
|
#include <Columns/ColumnString.h>
|
2021-02-17 23:09:46 +00:00
|
|
|
#include <Columns/ColumnCompressed.h>
|
2023-02-08 19:14:28 +00:00
|
|
|
#include <Columns/ColumnLowCardinality.h>
|
2021-10-08 14:03:54 +00:00
|
|
|
#include <Processors/Transforms/ColumnGathererTransform.h>
|
2016-07-05 16:23:37 +00:00
|
|
|
|
2022-02-09 16:32:52 +00:00
|
|
|
#if USE_EMBEDDED_COMPILER
|
|
|
|
#include <DataTypes/Native.h>
|
|
|
|
#include <llvm/IR/IRBuilder.h>
|
|
|
|
#endif
|
|
|
|
|
2017-03-11 01:25:27 +00:00
|
|
|
|
2016-07-05 16:23:37 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2017-03-11 01:25:27 +00:00
|
|
|
extern const int LOGICAL_ERROR;
|
2017-03-28 03:00:33 +00:00
|
|
|
extern const int ILLEGAL_COLUMN;
|
2022-09-12 14:29:23 +00:00
|
|
|
extern const int NOT_IMPLEMENTED;
|
2016-07-05 16:23:37 +00:00
|
|
|
}
|
|
|
|
|
2017-03-11 01:25:27 +00:00
|
|
|
|
2018-03-20 14:17:09 +00:00
|
|
|
ColumnNullable::ColumnNullable(MutableColumnPtr && nested_column_, MutableColumnPtr && null_map_)
|
|
|
|
: nested_column(std::move(nested_column_)), null_map(std::move(null_map_))
|
2016-08-10 19:12:29 +00:00
|
|
|
{
|
2017-03-09 03:57:51 +00:00
|
|
|
/// ColumnNullable cannot have constant nested column. But constant argument could be passed. Materialize it.
|
2018-12-21 16:00:07 +00:00
|
|
|
nested_column = getNestedColumn().convertToFullColumnIfConst();
|
2023-08-07 06:25:15 +00:00
|
|
|
nested_type = nested_column->getDataType();
|
2017-03-06 21:36:33 +00:00
|
|
|
|
2017-12-14 03:56:56 +00:00
|
|
|
if (!getNestedColumn().canBeInsideNullable())
|
2023-01-23 13:16:14 +00:00
|
|
|
throw Exception(ErrorCodes::ILLEGAL_COLUMN, "{} cannot be inside Nullable column", getNestedColumn().getName());
|
2017-12-09 17:32:18 +00:00
|
|
|
|
2019-06-27 19:28:52 +00:00
|
|
|
if (isColumnConst(*null_map))
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::ILLEGAL_COLUMN, "ColumnNullable cannot have constant null map");
|
2016-08-10 19:12:29 +00:00
|
|
|
}
|
|
|
|
|
2022-09-12 14:29:23 +00:00
|
|
|
StringRef ColumnNullable::getDataAt(size_t n) const
|
|
|
|
{
|
|
|
|
if (!isNullAt(n))
|
|
|
|
return getNestedColumn().getDataAt(n);
|
|
|
|
|
|
|
|
throw Exception(ErrorCodes::NOT_IMPLEMENTED, "Method getDataAt is not supported for {} in case if value is NULL", getName());
|
|
|
|
}
|
|
|
|
|
2016-07-11 10:09:16 +00:00
|
|
|
void ColumnNullable::updateHashWithValue(size_t n, SipHash & hash) const
|
|
|
|
{
|
2017-12-14 03:56:56 +00:00
|
|
|
const auto & arr = getNullMapData();
|
2018-03-03 15:36:20 +00:00
|
|
|
hash.update(arr[n]);
|
2016-08-16 12:34:54 +00:00
|
|
|
if (arr[n] == 0)
|
2017-12-14 03:56:56 +00:00
|
|
|
getNestedColumn().updateHashWithValue(n, hash);
|
2016-07-11 10:09:16 +00:00
|
|
|
}
|
|
|
|
|
2020-03-18 16:03:55 +00:00
|
|
|
void ColumnNullable::updateWeakHash32(WeakHash32 & hash) const
|
|
|
|
{
|
|
|
|
auto s = size();
|
|
|
|
|
|
|
|
if (hash.getData().size() != s)
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::LOGICAL_ERROR, "Size of WeakHash32 does not match size of column: "
|
|
|
|
"column size is {}, hash size is {}", std::to_string(s), std::to_string(hash.getData().size()));
|
2020-03-18 16:03:55 +00:00
|
|
|
|
|
|
|
WeakHash32 old_hash = hash;
|
|
|
|
nested_column->updateWeakHash32(hash);
|
|
|
|
|
2020-04-22 06:22:14 +00:00
|
|
|
const auto & null_map_data = getNullMapData();
|
2020-03-18 16:03:55 +00:00
|
|
|
auto & hash_data = hash.getData();
|
|
|
|
auto & old_hash_data = old_hash.getData();
|
|
|
|
|
|
|
|
/// Use old data for nulls.
|
|
|
|
for (size_t row = 0; row < s; ++row)
|
|
|
|
if (null_map_data[row])
|
|
|
|
hash_data[row] = old_hash_data[row];
|
|
|
|
}
|
2016-12-30 05:13:14 +00:00
|
|
|
|
2020-05-20 22:16:08 +00:00
|
|
|
void ColumnNullable::updateHashFast(SipHash & hash) const
|
|
|
|
{
|
|
|
|
null_map->updateHashFast(hash);
|
|
|
|
nested_column->updateHashFast(hash);
|
|
|
|
}
|
|
|
|
|
2017-12-15 19:46:24 +00:00
|
|
|
MutableColumnPtr ColumnNullable::cloneResized(size_t new_size) const
|
2016-07-05 16:23:37 +00:00
|
|
|
{
|
2018-03-20 14:17:09 +00:00
|
|
|
MutableColumnPtr new_nested_col = getNestedColumn().cloneResized(new_size);
|
2017-12-14 01:43:19 +00:00
|
|
|
auto new_null_map = ColumnUInt8::create();
|
2017-01-04 04:30:18 +00:00
|
|
|
|
|
|
|
if (new_size > 0)
|
|
|
|
{
|
2024-01-27 16:40:11 +00:00
|
|
|
new_null_map->getData().resize_exact(new_size);
|
2017-01-04 04:30:18 +00:00
|
|
|
|
|
|
|
size_t count = std::min(size(), new_size);
|
2017-12-14 03:56:56 +00:00
|
|
|
memcpy(new_null_map->getData().data(), getNullMapData().data(), count * sizeof(getNullMapData()[0]));
|
2017-01-04 04:30:18 +00:00
|
|
|
|
|
|
|
/// If resizing to bigger one, set all new values to NULLs.
|
|
|
|
if (new_size > count)
|
|
|
|
memset(&new_null_map->getData()[count], 1, new_size - count);
|
|
|
|
}
|
|
|
|
|
2018-03-20 14:17:09 +00:00
|
|
|
return ColumnNullable::create(std::move(new_nested_col), std::move(new_null_map));
|
2016-07-05 16:23:37 +00:00
|
|
|
}
|
|
|
|
|
2016-12-30 05:13:14 +00:00
|
|
|
|
2016-07-05 16:23:37 +00:00
|
|
|
Field ColumnNullable::operator[](size_t n) const
|
|
|
|
{
|
2017-12-14 03:56:56 +00:00
|
|
|
return isNullAt(n) ? Null() : getNestedColumn()[n];
|
2016-07-05 16:23:37 +00:00
|
|
|
}
|
|
|
|
|
2016-12-30 05:13:14 +00:00
|
|
|
|
2016-07-05 16:23:37 +00:00
|
|
|
void ColumnNullable::get(size_t n, Field & res) const
|
|
|
|
{
|
|
|
|
if (isNullAt(n))
|
2016-12-30 05:13:14 +00:00
|
|
|
res = Null();
|
2016-07-05 16:23:37 +00:00
|
|
|
else
|
2017-12-14 03:56:56 +00:00
|
|
|
getNestedColumn().get(n, res);
|
2016-07-05 16:23:37 +00:00
|
|
|
}
|
|
|
|
|
2019-05-23 13:35:26 +00:00
|
|
|
void ColumnNullable::insertData(const char * pos, size_t length)
|
2016-07-05 16:23:37 +00:00
|
|
|
{
|
2019-05-23 13:35:26 +00:00
|
|
|
if (pos == nullptr)
|
|
|
|
{
|
|
|
|
getNestedColumn().insertDefault();
|
|
|
|
getNullMapData().push_back(1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
getNestedColumn().insertData(pos, length);
|
|
|
|
getNullMapData().push_back(0);
|
|
|
|
}
|
2016-07-05 16:23:37 +00:00
|
|
|
}
|
|
|
|
|
2023-07-05 05:53:12 +00:00
|
|
|
StringRef ColumnNullable::serializeValueIntoArena(size_t n, Arena & arena, char const *& begin, const UInt8 *) const
|
2016-07-05 16:23:37 +00:00
|
|
|
{
|
2017-12-14 03:56:56 +00:00
|
|
|
const auto & arr = getNullMapData();
|
2016-08-16 12:53:22 +00:00
|
|
|
static constexpr auto s = sizeof(arr[0]);
|
2023-04-25 03:38:50 +00:00
|
|
|
char * pos;
|
2016-10-20 12:58:18 +00:00
|
|
|
|
2023-08-07 06:25:15 +00:00
|
|
|
switch (nested_type)
|
|
|
|
{
|
|
|
|
case TypeIndex::UInt8:
|
|
|
|
return static_cast<const ColumnUInt8 *>(nested_column.get())->serializeValueIntoArena(n, arena, begin, &arr[n]);
|
|
|
|
case TypeIndex::UInt16:
|
|
|
|
return static_cast<const ColumnUInt16 *>(nested_column.get())->serializeValueIntoArena(n, arena, begin, &arr[n]);
|
|
|
|
case TypeIndex::UInt32:
|
|
|
|
return static_cast<const ColumnUInt32 *>(nested_column.get())->serializeValueIntoArena(n, arena, begin, &arr[n]);
|
|
|
|
case TypeIndex::UInt64:
|
|
|
|
return static_cast<const ColumnUInt64 *>(nested_column.get())->serializeValueIntoArena(n, arena, begin, &arr[n]);
|
|
|
|
case TypeIndex::UInt128:
|
|
|
|
return static_cast<const ColumnUInt128 *>(nested_column.get())->serializeValueIntoArena(n, arena, begin, &arr[n]);
|
|
|
|
case TypeIndex::UInt256:
|
|
|
|
return static_cast<const ColumnUInt256 *>(nested_column.get())->serializeValueIntoArena(n, arena, begin, &arr[n]);
|
|
|
|
case TypeIndex::Int8:
|
|
|
|
return static_cast<const ColumnInt8 *>(nested_column.get())->serializeValueIntoArena(n, arena, begin, &arr[n]);
|
|
|
|
case TypeIndex::Int16:
|
|
|
|
return static_cast<const ColumnInt16 *>(nested_column.get())->serializeValueIntoArena(n, arena, begin, &arr[n]);
|
|
|
|
case TypeIndex::Int32:
|
|
|
|
return static_cast<const ColumnInt32 *>(nested_column.get())->serializeValueIntoArena(n, arena, begin, &arr[n]);
|
|
|
|
case TypeIndex::Int64:
|
|
|
|
return static_cast<const ColumnInt64 *>(nested_column.get())->serializeValueIntoArena(n, arena, begin, &arr[n]);
|
|
|
|
case TypeIndex::Int128:
|
|
|
|
return static_cast<const ColumnInt128 *>(nested_column.get())->serializeValueIntoArena(n, arena, begin, &arr[n]);
|
|
|
|
case TypeIndex::Int256:
|
|
|
|
return static_cast<const ColumnInt256 *>(nested_column.get())->serializeValueIntoArena(n, arena, begin, &arr[n]);
|
|
|
|
case TypeIndex::Float32:
|
|
|
|
return static_cast<const ColumnFloat32 *>(nested_column.get())->serializeValueIntoArena(n, arena, begin, &arr[n]);
|
|
|
|
case TypeIndex::Float64:
|
|
|
|
return static_cast<const ColumnFloat64 *>(nested_column.get())->serializeValueIntoArena(n, arena, begin, &arr[n]);
|
|
|
|
case TypeIndex::Date:
|
|
|
|
return static_cast<const ColumnDate *>(nested_column.get())->serializeValueIntoArena(n, arena, begin, &arr[n]);
|
|
|
|
case TypeIndex::Date32:
|
|
|
|
return static_cast<const ColumnDate32 *>(nested_column.get())->serializeValueIntoArena(n, arena, begin, &arr[n]);
|
|
|
|
case TypeIndex::DateTime:
|
|
|
|
return static_cast<const ColumnDateTime *>(nested_column.get())->serializeValueIntoArena(n, arena, begin, &arr[n]);
|
|
|
|
case TypeIndex::DateTime64:
|
|
|
|
return static_cast<const ColumnDateTime64 *>(nested_column.get())->serializeValueIntoArena(n, arena, begin, &arr[n]);
|
|
|
|
case TypeIndex::String:
|
|
|
|
return static_cast<const ColumnString *>(nested_column.get())->serializeValueIntoArena(n, arena, begin, &arr[n]);
|
|
|
|
case TypeIndex::FixedString:
|
|
|
|
return static_cast<const ColumnFixedString *>(nested_column.get())->serializeValueIntoArena(n, arena, begin, &arr[n]);
|
|
|
|
case TypeIndex::Decimal32:
|
|
|
|
return static_cast<const ColumnDecimal<Decimal32> *>(nested_column.get())->serializeValueIntoArena(n, arena, begin, &arr[n]);
|
|
|
|
case TypeIndex::Decimal64:
|
|
|
|
return static_cast<const ColumnDecimal<Decimal64> *>(nested_column.get())->serializeValueIntoArena(n, arena, begin, &arr[n]);
|
|
|
|
case TypeIndex::Decimal128:
|
|
|
|
return static_cast<const ColumnDecimal<Decimal128> *>(nested_column.get())->serializeValueIntoArena(n, arena, begin, &arr[n]);
|
|
|
|
case TypeIndex::Decimal256:
|
|
|
|
return static_cast<const ColumnDecimal<Decimal256> *>(nested_column.get())->serializeValueIntoArena(n, arena, begin, &arr[n]);
|
|
|
|
case TypeIndex::UUID:
|
|
|
|
return static_cast<const ColumnUUID *>(nested_column.get())->serializeValueIntoArena(n, arena, begin, &arr[n]);
|
|
|
|
case TypeIndex::IPv4:
|
|
|
|
return static_cast<const ColumnIPv4 *>(nested_column.get())->serializeValueIntoArena(n, arena, begin, &arr[n]);
|
|
|
|
case TypeIndex::IPv6:
|
|
|
|
return static_cast<const ColumnIPv6 *>(nested_column.get())->serializeValueIntoArena(n, arena, begin, &arr[n]);
|
|
|
|
default:
|
|
|
|
pos = arena.allocContinue(s, begin);
|
|
|
|
memcpy(pos, &arr[n], s);
|
|
|
|
if (arr[n])
|
|
|
|
return StringRef(pos, s);
|
|
|
|
auto nested_ref = getNestedColumn().serializeValueIntoArena(n, arena, begin);
|
|
|
|
/// serializeValueIntoArena may reallocate memory. Have to use ptr from nested_ref.data and move it back.
|
|
|
|
return StringRef(nested_ref.data - s, nested_ref.size + s);
|
2023-04-25 03:38:50 +00:00
|
|
|
}
|
2016-07-05 16:23:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const char * ColumnNullable::deserializeAndInsertFromArena(const char * pos)
|
|
|
|
{
|
2020-01-03 15:28:38 +00:00
|
|
|
UInt8 val = unalignedLoad<UInt8>(pos);
|
2016-10-20 12:58:18 +00:00
|
|
|
pos += sizeof(val);
|
2016-08-16 12:53:22 +00:00
|
|
|
|
2017-12-14 03:56:56 +00:00
|
|
|
getNullMapData().push_back(val);
|
2016-08-16 12:53:22 +00:00
|
|
|
|
|
|
|
if (val == 0)
|
2017-12-14 03:56:56 +00:00
|
|
|
pos = getNestedColumn().deserializeAndInsertFromArena(pos);
|
2016-08-16 12:53:22 +00:00
|
|
|
else
|
2017-12-14 03:56:56 +00:00
|
|
|
getNestedColumn().insertDefault();
|
2016-10-20 12:58:18 +00:00
|
|
|
|
|
|
|
return pos;
|
2016-07-05 16:23:37 +00:00
|
|
|
}
|
|
|
|
|
2021-02-16 21:26:06 +00:00
|
|
|
const char * ColumnNullable::skipSerializedInArena(const char * pos) const
|
|
|
|
{
|
|
|
|
UInt8 val = unalignedLoad<UInt8>(pos);
|
|
|
|
pos += sizeof(val);
|
|
|
|
|
|
|
|
if (val == 0)
|
|
|
|
return getNestedColumn().skipSerializedInArena(pos);
|
|
|
|
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
2016-07-05 16:23:37 +00:00
|
|
|
void ColumnNullable::insertRangeFrom(const IColumn & src, size_t start, size_t length)
|
|
|
|
{
|
2019-08-21 02:28:04 +00:00
|
|
|
const ColumnNullable & nullable_col = assert_cast<const ColumnNullable &>(src);
|
2017-12-14 03:56:56 +00:00
|
|
|
getNullMapColumn().insertRangeFrom(*nullable_col.null_map, start, length);
|
|
|
|
getNestedColumn().insertRangeFrom(*nullable_col.nested_column, start, length);
|
2016-07-05 16:23:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ColumnNullable::insert(const Field & x)
|
|
|
|
{
|
|
|
|
if (x.isNull())
|
|
|
|
{
|
2017-12-14 03:56:56 +00:00
|
|
|
getNestedColumn().insertDefault();
|
|
|
|
getNullMapData().push_back(1);
|
2016-07-05 16:23:37 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-12-14 03:56:56 +00:00
|
|
|
getNestedColumn().insert(x);
|
|
|
|
getNullMapData().push_back(0);
|
2016-07-05 16:23:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-20 18:58:06 +00:00
|
|
|
bool ColumnNullable::tryInsert(const Field & x)
|
|
|
|
{
|
|
|
|
if (x.isNull())
|
|
|
|
{
|
|
|
|
getNestedColumn().insertDefault();
|
|
|
|
getNullMapData().push_back(1);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!getNestedColumn().tryInsert(x))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
getNullMapData().push_back(0);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-01-17 20:47:37 +00:00
|
|
|
void ColumnNullable::insertFrom(const IColumn & src, size_t n)
|
|
|
|
{
|
2019-08-21 02:28:04 +00:00
|
|
|
const ColumnNullable & src_concrete = assert_cast<const ColumnNullable &>(src);
|
2017-12-14 03:56:56 +00:00
|
|
|
getNestedColumn().insertFrom(src_concrete.getNestedColumn(), n);
|
|
|
|
getNullMapData().push_back(src_concrete.getNullMapData()[n]);
|
2017-01-17 20:47:37 +00:00
|
|
|
}
|
|
|
|
|
2019-09-17 16:55:11 +00:00
|
|
|
void ColumnNullable::insertFromNotNullable(const IColumn & src, size_t n)
|
|
|
|
{
|
|
|
|
getNestedColumn().insertFrom(src, n);
|
|
|
|
getNullMapData().push_back(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ColumnNullable::insertRangeFromNotNullable(const IColumn & src, size_t start, size_t length)
|
|
|
|
{
|
|
|
|
getNestedColumn().insertRangeFrom(src, start, length);
|
2023-05-05 18:01:20 +00:00
|
|
|
getNullMapData().resize_fill(getNullMapData().size() + length);
|
2019-09-17 16:55:11 +00:00
|
|
|
}
|
|
|
|
|
2019-09-18 18:44:44 +00:00
|
|
|
void ColumnNullable::insertManyFromNotNullable(const IColumn & src, size_t position, size_t length)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < length; ++i)
|
|
|
|
insertFromNotNullable(src, position);
|
|
|
|
}
|
|
|
|
|
2016-07-05 16:23:37 +00:00
|
|
|
void ColumnNullable::popBack(size_t n)
|
|
|
|
{
|
2017-12-14 03:56:56 +00:00
|
|
|
getNestedColumn().popBack(n);
|
|
|
|
getNullMapColumn().popBack(n);
|
2016-07-05 16:23:37 +00:00
|
|
|
}
|
|
|
|
|
2018-03-20 14:17:09 +00:00
|
|
|
ColumnPtr ColumnNullable::filter(const Filter & filt, ssize_t result_size_hint) const
|
2016-07-05 16:23:37 +00:00
|
|
|
{
|
2017-12-14 03:56:56 +00:00
|
|
|
ColumnPtr filtered_data = getNestedColumn().filter(filt, result_size_hint);
|
|
|
|
ColumnPtr filtered_null_map = getNullMapColumn().filter(filt, result_size_hint);
|
2017-12-14 01:43:19 +00:00
|
|
|
return ColumnNullable::create(filtered_data, filtered_null_map);
|
2016-07-05 16:23:37 +00:00
|
|
|
}
|
|
|
|
|
2021-06-07 10:55:55 +00:00
|
|
|
void ColumnNullable::expand(const IColumn::Filter & mask, bool inverted)
|
2021-04-27 12:49:58 +00:00
|
|
|
{
|
2021-06-07 10:55:55 +00:00
|
|
|
nested_column->expand(mask, inverted);
|
|
|
|
null_map->expand(mask, inverted);
|
2021-04-27 12:49:58 +00:00
|
|
|
}
|
|
|
|
|
2019-02-18 19:44:26 +00:00
|
|
|
ColumnPtr ColumnNullable::permute(const Permutation & perm, size_t limit) const
|
2016-07-05 16:23:37 +00:00
|
|
|
{
|
2017-12-14 03:56:56 +00:00
|
|
|
ColumnPtr permuted_data = getNestedColumn().permute(perm, limit);
|
|
|
|
ColumnPtr permuted_null_map = getNullMapColumn().permute(perm, limit);
|
2017-12-14 01:43:19 +00:00
|
|
|
return ColumnNullable::create(permuted_data, permuted_null_map);
|
2016-07-05 16:23:37 +00:00
|
|
|
}
|
|
|
|
|
2019-02-18 17:28:53 +00:00
|
|
|
ColumnPtr ColumnNullable::index(const IColumn & indexes, size_t limit) const
|
2018-04-23 16:40:25 +00:00
|
|
|
{
|
|
|
|
ColumnPtr indexed_data = getNestedColumn().index(indexes, limit);
|
|
|
|
ColumnPtr indexed_null_map = getNullMapColumn().index(indexes, limit);
|
|
|
|
return ColumnNullable::create(indexed_data, indexed_null_map);
|
|
|
|
}
|
|
|
|
|
2022-02-09 16:32:52 +00:00
|
|
|
#if USE_EMBEDDED_COMPILER
|
|
|
|
|
|
|
|
bool ColumnNullable::isComparatorCompilable() const
|
|
|
|
{
|
|
|
|
return nested_column->isComparatorCompilable();
|
|
|
|
}
|
|
|
|
|
2022-05-11 13:35:37 +00:00
|
|
|
llvm::Value * ColumnNullable::compileComparator(llvm::IRBuilderBase & builder, llvm::Value * lhs, llvm::Value * rhs,
|
|
|
|
llvm::Value * nan_direction_hint) const
|
2022-02-09 16:32:52 +00:00
|
|
|
{
|
|
|
|
llvm::IRBuilder<> & b = static_cast<llvm::IRBuilder<> &>(builder);
|
|
|
|
auto * head = b.GetInsertBlock();
|
|
|
|
|
|
|
|
llvm::Value * lhs_unwrapped_value = b.CreateExtractValue(lhs, {0});
|
|
|
|
llvm::Value * lhs_is_null_value = b.CreateExtractValue(lhs, {1});
|
|
|
|
|
|
|
|
llvm::Value * rhs_unwrapped_value = b.CreateExtractValue(rhs, {0});
|
|
|
|
llvm::Value * rhs_is_null_value = b.CreateExtractValue(rhs, {1});
|
|
|
|
|
|
|
|
llvm::Value * lhs_or_rhs_are_null = b.CreateOr(lhs_is_null_value, rhs_is_null_value);
|
|
|
|
|
|
|
|
auto * lhs_or_rhs_are_null_block = llvm::BasicBlock::Create(head->getContext(), "lhs_or_rhs_are_null_block", head->getParent());
|
|
|
|
auto * lhs_rhs_are_not_null_block = llvm::BasicBlock::Create(head->getContext(), "lhs_and_rhs_are_not_null_block", head->getParent());
|
|
|
|
auto * join_block = llvm::BasicBlock::Create(head->getContext(), "join_block", head->getParent());
|
|
|
|
|
|
|
|
b.CreateCondBr(lhs_or_rhs_are_null, lhs_or_rhs_are_null_block, lhs_rhs_are_not_null_block);
|
|
|
|
|
|
|
|
b.SetInsertPoint(lhs_or_rhs_are_null_block);
|
|
|
|
auto * lhs_equals_rhs_result = llvm::ConstantInt::getSigned(b.getInt8Ty(), 0);
|
2022-02-18 11:58:32 +00:00
|
|
|
llvm::Value * lhs_and_rhs_are_null = b.CreateAnd(lhs_is_null_value, rhs_is_null_value);
|
2022-02-09 16:32:52 +00:00
|
|
|
llvm::Value * lhs_is_null_result = b.CreateSelect(lhs_is_null_value, nan_direction_hint, b.CreateNeg(nan_direction_hint));
|
|
|
|
llvm::Value * lhs_or_rhs_are_null_block_result = b.CreateSelect(lhs_and_rhs_are_null, lhs_equals_rhs_result, lhs_is_null_result);
|
|
|
|
b.CreateBr(join_block);
|
|
|
|
|
|
|
|
b.SetInsertPoint(lhs_rhs_are_not_null_block);
|
2022-05-11 13:35:37 +00:00
|
|
|
llvm::Value * lhs_rhs_are_not_null_block_result
|
|
|
|
= nested_column->compileComparator(builder, lhs_unwrapped_value, rhs_unwrapped_value, nan_direction_hint);
|
2022-02-09 16:32:52 +00:00
|
|
|
b.CreateBr(join_block);
|
|
|
|
|
|
|
|
b.SetInsertPoint(join_block);
|
|
|
|
|
|
|
|
auto * result = b.CreatePHI(b.getInt8Ty(), 2);
|
|
|
|
result->addIncoming(lhs_or_rhs_are_null_block_result, lhs_or_rhs_are_null_block);
|
|
|
|
result->addIncoming(lhs_rhs_are_not_null_block_result, lhs_rhs_are_not_null_block);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2020-10-29 11:24:01 +00:00
|
|
|
int ColumnNullable::compareAtImpl(size_t n, size_t m, const IColumn & rhs_, int null_direction_hint, const Collator * collator) const
|
2016-07-05 16:23:37 +00:00
|
|
|
{
|
2016-08-17 17:20:21 +00:00
|
|
|
/// NULL values share the properties of NaN values.
|
|
|
|
/// Here the last parameter of compareAt is called null_direction_hint
|
|
|
|
/// instead of the usual nan_direction_hint and is used to implement
|
|
|
|
/// the ordering specified by either NULLS FIRST or NULLS LAST in the
|
|
|
|
/// ORDER BY construction.
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2019-08-21 02:28:04 +00:00
|
|
|
const ColumnNullable & nullable_rhs = assert_cast<const ColumnNullable &>(rhs_);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2016-08-10 19:12:29 +00:00
|
|
|
bool lval_is_null = isNullAt(n);
|
|
|
|
bool rval_is_null = nullable_rhs.isNullAt(m);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2016-08-17 17:20:21 +00:00
|
|
|
if (unlikely(lval_is_null || rval_is_null))
|
|
|
|
{
|
|
|
|
if (lval_is_null && rval_is_null)
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return lval_is_null ? null_direction_hint : -null_direction_hint;
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-12-14 03:56:56 +00:00
|
|
|
const IColumn & nested_rhs = nullable_rhs.getNestedColumn();
|
2020-10-29 11:24:01 +00:00
|
|
|
if (collator)
|
|
|
|
return getNestedColumn().compareAtWithCollation(n, m, nested_rhs, null_direction_hint, *collator);
|
|
|
|
|
2017-12-14 03:56:56 +00:00
|
|
|
return getNestedColumn().compareAt(n, m, nested_rhs, null_direction_hint);
|
2016-07-05 16:23:37 +00:00
|
|
|
}
|
|
|
|
|
2020-10-29 11:24:01 +00:00
|
|
|
int ColumnNullable::compareAt(size_t n, size_t m, const IColumn & rhs_, int null_direction_hint) const
|
|
|
|
{
|
|
|
|
return compareAtImpl(n, m, rhs_, null_direction_hint);
|
|
|
|
}
|
|
|
|
|
|
|
|
int ColumnNullable::compareAtWithCollation(size_t n, size_t m, const IColumn & rhs_, int null_direction_hint, const Collator & collator) const
|
|
|
|
{
|
|
|
|
return compareAtImpl(n, m, rhs_, null_direction_hint, &collator);
|
|
|
|
}
|
|
|
|
|
2020-06-02 00:23:41 +00:00
|
|
|
void ColumnNullable::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
|
2020-06-01 12:10:32 +00:00
|
|
|
{
|
2020-06-17 11:43:55 +00:00
|
|
|
return doCompareColumn<ColumnNullable>(assert_cast<const ColumnNullable &>(rhs), rhs_row_num, row_indexes,
|
|
|
|
compare_results, direction, nan_direction_hint);
|
2020-06-01 12:10:32 +00:00
|
|
|
}
|
|
|
|
|
2021-02-26 04:50:04 +00:00
|
|
|
bool ColumnNullable::hasEqualValues() const
|
|
|
|
{
|
|
|
|
return hasEqualValuesImpl<ColumnNullable>();
|
|
|
|
}
|
|
|
|
|
2022-02-23 17:34:19 +00:00
|
|
|
void ColumnNullable::getPermutationImpl(IColumn::PermutationSortDirection direction, IColumn::PermutationSortStability stability,
|
|
|
|
size_t limit, int null_direction_hint, Permutation & res, const Collator * collator) const
|
2016-07-05 16:23:37 +00:00
|
|
|
{
|
2017-02-10 10:20:06 +00:00
|
|
|
/// Cannot pass limit because of unknown amount of NULLs.
|
2020-10-22 20:23:44 +00:00
|
|
|
|
|
|
|
if (collator)
|
2022-02-23 17:34:19 +00:00
|
|
|
getNestedColumn().getPermutationWithCollation(*collator, direction, stability, 0, null_direction_hint, res);
|
2020-10-22 20:23:44 +00:00
|
|
|
else
|
2022-02-23 17:34:19 +00:00
|
|
|
getNestedColumn().getPermutation(direction, stability, 0, null_direction_hint, res);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2022-02-23 17:34:19 +00:00
|
|
|
bool reverse = direction == IColumn::PermutationSortDirection::Descending;
|
2022-03-03 13:02:31 +00:00
|
|
|
const auto is_nulls_last = ((null_direction_hint > 0) != reverse);
|
|
|
|
|
|
|
|
size_t res_size = res.size();
|
|
|
|
|
|
|
|
if (!limit)
|
|
|
|
limit = res_size;
|
|
|
|
else
|
|
|
|
limit = std::min(res_size, limit);
|
|
|
|
|
2022-03-11 21:16:25 +00:00
|
|
|
/// For stable sort we must process all NULL values
|
|
|
|
if (unlikely(stability == IColumn::PermutationSortStability::Stable))
|
|
|
|
limit = res_size;
|
|
|
|
|
2022-03-03 13:02:31 +00:00
|
|
|
if (is_nulls_last)
|
2017-03-12 12:56:59 +00:00
|
|
|
{
|
|
|
|
/// Shift all NULL values to the end.
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-03-12 12:56:59 +00:00
|
|
|
size_t read_idx = 0;
|
|
|
|
size_t write_idx = 0;
|
2022-03-03 13:02:31 +00:00
|
|
|
size_t end_idx = res_size;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-03-12 12:56:59 +00:00
|
|
|
while (read_idx < limit && !isNullAt(res[read_idx]))
|
|
|
|
{
|
|
|
|
++read_idx;
|
|
|
|
++write_idx;
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-02-10 10:20:06 +00:00
|
|
|
++read_idx;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-03-12 12:56:59 +00:00
|
|
|
/// Invariants:
|
|
|
|
/// write_idx < read_idx
|
|
|
|
/// write_idx points to NULL
|
|
|
|
/// read_idx will be incremented to position of next not-NULL
|
|
|
|
/// there are range of NULLs between write_idx and read_idx - 1,
|
|
|
|
/// We are moving elements from end to begin of this range,
|
|
|
|
/// so range will "bubble" towards the end.
|
|
|
|
/// Relative order of NULL elements could be changed,
|
|
|
|
/// but relative order of non-NULLs is preserved.
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-03-12 12:56:59 +00:00
|
|
|
while (read_idx < end_idx && write_idx < limit)
|
|
|
|
{
|
|
|
|
if (!isNullAt(res[read_idx]))
|
|
|
|
{
|
|
|
|
std::swap(res[read_idx], res[write_idx]);
|
|
|
|
++write_idx;
|
|
|
|
}
|
|
|
|
++read_idx;
|
|
|
|
}
|
2022-03-03 13:02:31 +00:00
|
|
|
|
2022-03-11 21:16:25 +00:00
|
|
|
if (unlikely(stability == IColumn::PermutationSortStability::Stable) && write_idx != res_size)
|
2022-03-03 13:02:31 +00:00
|
|
|
{
|
2022-03-11 21:16:25 +00:00
|
|
|
::sort(res.begin() + write_idx, res.begin() + res_size);
|
2022-03-03 13:02:31 +00:00
|
|
|
}
|
2016-08-10 19:12:29 +00:00
|
|
|
}
|
2017-03-12 12:56:59 +00:00
|
|
|
else
|
|
|
|
{
|
2017-06-08 12:15:57 +00:00
|
|
|
/// Shift all NULL values to the beginning.
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-03-12 12:56:59 +00:00
|
|
|
ssize_t read_idx = res.size() - 1;
|
|
|
|
ssize_t write_idx = res.size() - 1;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-03-12 12:56:59 +00:00
|
|
|
while (read_idx >= 0 && !isNullAt(res[read_idx]))
|
|
|
|
{
|
|
|
|
--read_idx;
|
|
|
|
--write_idx;
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-03-12 12:56:59 +00:00
|
|
|
--read_idx;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-03-12 12:56:59 +00:00
|
|
|
while (read_idx >= 0 && write_idx >= 0)
|
2016-08-10 19:12:29 +00:00
|
|
|
{
|
2017-03-12 12:56:59 +00:00
|
|
|
if (!isNullAt(res[read_idx]))
|
|
|
|
{
|
|
|
|
std::swap(res[read_idx], res[write_idx]);
|
|
|
|
--write_idx;
|
|
|
|
}
|
|
|
|
--read_idx;
|
2016-08-10 19:12:29 +00:00
|
|
|
}
|
2022-03-03 13:02:31 +00:00
|
|
|
|
2022-03-11 21:16:25 +00:00
|
|
|
if (unlikely(stability == IColumn::PermutationSortStability::Stable) && write_idx != 0)
|
2022-03-03 13:02:31 +00:00
|
|
|
{
|
2022-03-11 21:16:25 +00:00
|
|
|
::sort(res.begin(), res.begin() + write_idx + 1);
|
2022-03-03 13:02:31 +00:00
|
|
|
}
|
2016-08-10 19:12:29 +00:00
|
|
|
}
|
2016-07-05 16:23:37 +00:00
|
|
|
}
|
|
|
|
|
2022-02-23 17:34:19 +00:00
|
|
|
void ColumnNullable::updatePermutationImpl(IColumn::PermutationSortDirection direction, IColumn::PermutationSortStability stability,
|
|
|
|
size_t limit, int null_direction_hint, Permutation & res, EqualRanges & equal_ranges, const Collator * collator) const
|
2020-05-14 21:00:56 +00:00
|
|
|
{
|
2020-09-04 18:05:06 +00:00
|
|
|
if (equal_ranges.empty())
|
|
|
|
return;
|
|
|
|
|
2020-09-04 16:53:50 +00:00
|
|
|
/// We will sort nested columns into `new_ranges` and call updatePermutation in next columns with `null_ranges`.
|
|
|
|
EqualRanges new_ranges, null_ranges;
|
2020-05-12 00:58:58 +00:00
|
|
|
|
2022-02-23 17:34:19 +00:00
|
|
|
bool reverse = direction == IColumn::PermutationSortDirection::Descending;
|
2020-09-04 14:36:08 +00:00
|
|
|
const auto is_nulls_last = ((null_direction_hint > 0) != reverse);
|
|
|
|
|
|
|
|
if (is_nulls_last)
|
2020-05-14 21:00:56 +00:00
|
|
|
{
|
2020-05-12 00:58:58 +00:00
|
|
|
/// Shift all NULL values to the end.
|
2020-09-04 14:36:08 +00:00
|
|
|
for (const auto & [first, last] : equal_ranges)
|
2020-05-12 00:58:58 +00:00
|
|
|
{
|
2020-10-26 19:12:40 +00:00
|
|
|
/// Current interval is righter than limit.
|
2020-09-08 20:12:55 +00:00
|
|
|
if (limit && first > limit)
|
2020-09-08 15:54:30 +00:00
|
|
|
break;
|
|
|
|
|
2020-09-04 14:36:08 +00:00
|
|
|
/// Consider a half interval [first, last)
|
|
|
|
size_t read_idx = first;
|
|
|
|
size_t write_idx = first;
|
|
|
|
size_t end_idx = last;
|
2020-05-12 00:58:58 +00:00
|
|
|
|
2020-09-09 11:55:20 +00:00
|
|
|
/// We can't check the limit here because the interval is not sorted by nested column.
|
|
|
|
while (read_idx < end_idx && !isNullAt(res[read_idx]))
|
2020-05-12 00:58:58 +00:00
|
|
|
{
|
2020-09-04 14:36:08 +00:00
|
|
|
++read_idx;
|
2020-05-12 00:58:58 +00:00
|
|
|
++write_idx;
|
|
|
|
}
|
2020-09-04 14:36:08 +00:00
|
|
|
|
2020-05-12 00:58:58 +00:00
|
|
|
++read_idx;
|
2020-05-18 11:38:22 +00:00
|
|
|
|
2020-09-04 14:36:08 +00:00
|
|
|
/// Invariants:
|
|
|
|
/// write_idx < read_idx
|
|
|
|
/// write_idx points to NULL
|
|
|
|
/// read_idx will be incremented to position of next not-NULL
|
|
|
|
/// there are range of NULLs between write_idx and read_idx - 1,
|
|
|
|
/// We are moving elements from end to begin of this range,
|
|
|
|
/// so range will "bubble" towards the end.
|
|
|
|
/// Relative order of NULL elements could be changed,
|
|
|
|
/// but relative order of non-NULLs is preserved.
|
|
|
|
|
2020-09-09 11:55:20 +00:00
|
|
|
while (read_idx < end_idx && write_idx < end_idx)
|
2020-09-04 14:36:08 +00:00
|
|
|
{
|
|
|
|
if (!isNullAt(res[read_idx]))
|
|
|
|
{
|
|
|
|
std::swap(res[read_idx], res[write_idx]);
|
|
|
|
++write_idx;
|
|
|
|
}
|
|
|
|
++read_idx;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// We have a range [first, write_idx) of non-NULL values
|
|
|
|
if (first != write_idx)
|
2020-05-12 00:58:58 +00:00
|
|
|
new_ranges.emplace_back(first, write_idx);
|
2020-05-18 11:38:22 +00:00
|
|
|
|
2022-03-11 21:16:25 +00:00
|
|
|
/// We have a range [write_idx, last) of NULL values
|
2020-09-04 14:36:08 +00:00
|
|
|
if (write_idx != last)
|
2020-09-04 16:53:50 +00:00
|
|
|
null_ranges.emplace_back(write_idx, last);
|
2020-05-12 00:58:58 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-04 14:36:08 +00:00
|
|
|
else
|
|
|
|
{
|
2020-09-08 15:54:30 +00:00
|
|
|
/// Shift all NULL values to the beginning.
|
2020-09-04 14:36:08 +00:00
|
|
|
for (const auto & [first, last] : equal_ranges)
|
|
|
|
{
|
2020-09-08 15:54:30 +00:00
|
|
|
/// Current interval is righter than limit.
|
2020-09-08 20:12:55 +00:00
|
|
|
if (limit && first > limit)
|
2020-09-08 15:54:30 +00:00
|
|
|
break;
|
2020-09-04 14:36:08 +00:00
|
|
|
|
|
|
|
ssize_t read_idx = last - 1;
|
|
|
|
ssize_t write_idx = last - 1;
|
|
|
|
ssize_t begin_idx = first;
|
|
|
|
|
|
|
|
while (read_idx >= begin_idx && !isNullAt(res[read_idx]))
|
|
|
|
{
|
|
|
|
--read_idx;
|
|
|
|
--write_idx;
|
|
|
|
}
|
|
|
|
|
|
|
|
--read_idx;
|
|
|
|
|
|
|
|
while (read_idx >= begin_idx && write_idx >= begin_idx)
|
|
|
|
{
|
|
|
|
if (!isNullAt(res[read_idx]))
|
|
|
|
{
|
|
|
|
std::swap(res[read_idx], res[write_idx]);
|
|
|
|
--write_idx;
|
|
|
|
}
|
|
|
|
--read_idx;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// We have a range [write_idx+1, last) of non-NULL values
|
|
|
|
if (write_idx != static_cast<ssize_t>(last))
|
|
|
|
new_ranges.emplace_back(write_idx + 1, last);
|
|
|
|
|
|
|
|
/// We have a range [first, write_idx+1) of NULL values
|
|
|
|
if (static_cast<ssize_t>(first) != write_idx)
|
2020-09-04 16:53:50 +00:00
|
|
|
null_ranges.emplace_back(first, write_idx + 1);
|
2020-09-04 14:36:08 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-18 11:38:22 +00:00
|
|
|
|
2020-10-22 20:23:44 +00:00
|
|
|
if (collator)
|
2022-02-23 17:34:19 +00:00
|
|
|
getNestedColumn().updatePermutationWithCollation(*collator, direction, stability, limit, null_direction_hint, res, new_ranges);
|
2020-10-22 20:23:44 +00:00
|
|
|
else
|
2022-02-23 17:34:19 +00:00
|
|
|
getNestedColumn().updatePermutation(direction, stability, limit, null_direction_hint, res, new_ranges);
|
2020-05-18 11:38:22 +00:00
|
|
|
|
2022-03-12 18:04:08 +00:00
|
|
|
if (unlikely(stability == PermutationSortStability::Stable))
|
|
|
|
{
|
|
|
|
for (auto & null_range : null_ranges)
|
2022-03-11 21:16:25 +00:00
|
|
|
::sort(res.begin() + null_range.first, res.begin() + null_range.second);
|
|
|
|
}
|
|
|
|
|
2023-07-29 03:20:12 +00:00
|
|
|
if (is_nulls_last || null_ranges.empty())
|
|
|
|
{
|
|
|
|
equal_ranges = std::move(new_ranges);
|
|
|
|
std::move(null_ranges.begin(), null_ranges.end(), std::back_inserter(equal_ranges));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
equal_ranges = std::move(null_ranges);
|
|
|
|
std::move(new_ranges.begin(), new_ranges.end(), std::back_inserter(equal_ranges));
|
|
|
|
}
|
2020-05-12 00:58:58 +00:00
|
|
|
}
|
|
|
|
|
2022-02-23 17:34:19 +00:00
|
|
|
void ColumnNullable::getPermutation(IColumn::PermutationSortDirection direction, IColumn::PermutationSortStability stability,
|
|
|
|
size_t limit, int null_direction_hint, Permutation & res) const
|
2020-10-22 20:23:44 +00:00
|
|
|
{
|
2022-02-23 17:34:19 +00:00
|
|
|
getPermutationImpl(direction, stability, limit, null_direction_hint, res);
|
2020-10-22 20:23:44 +00:00
|
|
|
}
|
|
|
|
|
2022-02-23 17:34:19 +00:00
|
|
|
void ColumnNullable::updatePermutation(IColumn::PermutationSortDirection direction, IColumn::PermutationSortStability stability,
|
|
|
|
size_t limit, int null_direction_hint, IColumn::Permutation & res, EqualRanges & equal_ranges) const
|
2020-10-22 20:23:44 +00:00
|
|
|
{
|
2022-02-23 17:34:19 +00:00
|
|
|
updatePermutationImpl(direction, stability, limit, null_direction_hint, res, equal_ranges);
|
2020-10-22 20:23:44 +00:00
|
|
|
}
|
|
|
|
|
2022-02-23 17:34:19 +00:00
|
|
|
void ColumnNullable::getPermutationWithCollation(const Collator & collator, IColumn::PermutationSortDirection direction, IColumn::PermutationSortStability stability,
|
|
|
|
size_t limit, int null_direction_hint, Permutation & res) const
|
2020-10-22 20:23:44 +00:00
|
|
|
{
|
2022-02-23 17:34:19 +00:00
|
|
|
getPermutationImpl(direction, stability, limit, null_direction_hint, res, &collator);
|
2020-10-22 20:23:44 +00:00
|
|
|
}
|
|
|
|
|
2022-02-23 17:34:19 +00:00
|
|
|
void ColumnNullable::updatePermutationWithCollation(const Collator & collator, IColumn::PermutationSortDirection direction, IColumn::PermutationSortStability stability,
|
|
|
|
size_t limit, int null_direction_hint, Permutation & res, EqualRanges & equal_ranges) const
|
2020-10-22 20:23:44 +00:00
|
|
|
{
|
2022-02-23 17:34:19 +00:00
|
|
|
updatePermutationImpl(direction, stability, limit, null_direction_hint, res, equal_ranges, &collator);
|
2020-10-22 20:23:44 +00:00
|
|
|
}
|
|
|
|
|
2017-07-06 13:54:55 +00:00
|
|
|
void ColumnNullable::gather(ColumnGathererStream & gatherer)
|
|
|
|
{
|
|
|
|
gatherer.gather(*this);
|
|
|
|
}
|
|
|
|
|
2016-07-05 16:23:37 +00:00
|
|
|
void ColumnNullable::reserve(size_t n)
|
|
|
|
{
|
2017-12-14 03:56:56 +00:00
|
|
|
getNestedColumn().reserve(n);
|
|
|
|
getNullMapData().reserve(n);
|
2016-07-05 16:23:37 +00:00
|
|
|
}
|
|
|
|
|
2024-01-27 16:40:11 +00:00
|
|
|
void ColumnNullable::shrinkToFit()
|
|
|
|
{
|
|
|
|
getNestedColumn().shrinkToFit();
|
|
|
|
getNullMapData().shrink_to_fit();
|
|
|
|
}
|
|
|
|
|
2022-03-05 12:50:47 +00:00
|
|
|
void ColumnNullable::ensureOwnership()
|
|
|
|
{
|
|
|
|
getNestedColumn().ensureOwnership();
|
|
|
|
}
|
|
|
|
|
2016-07-05 16:23:37 +00:00
|
|
|
size_t ColumnNullable::byteSize() const
|
|
|
|
{
|
2017-12-14 03:56:56 +00:00
|
|
|
return getNestedColumn().byteSize() + getNullMapColumn().byteSize();
|
2016-07-05 16:23:37 +00:00
|
|
|
}
|
|
|
|
|
2021-01-02 22:58:10 +00:00
|
|
|
size_t ColumnNullable::byteSizeAt(size_t n) const
|
|
|
|
{
|
|
|
|
return sizeof(getNullMapData()[0]) + getNestedColumn().byteSizeAt(n);
|
|
|
|
}
|
|
|
|
|
2017-07-13 16:49:09 +00:00
|
|
|
size_t ColumnNullable::allocatedBytes() const
|
2017-01-17 20:54:32 +00:00
|
|
|
{
|
2017-12-14 03:56:56 +00:00
|
|
|
return getNestedColumn().allocatedBytes() + getNullMapColumn().allocatedBytes();
|
2017-01-17 20:54:32 +00:00
|
|
|
}
|
|
|
|
|
2019-03-10 03:16:51 +00:00
|
|
|
void ColumnNullable::protect()
|
|
|
|
{
|
|
|
|
getNestedColumn().protect();
|
|
|
|
getNullMapColumn().protect();
|
|
|
|
}
|
|
|
|
|
2021-02-17 23:09:46 +00:00
|
|
|
ColumnPtr ColumnNullable::compress() const
|
|
|
|
{
|
|
|
|
ColumnPtr nested_compressed = nested_column->compress();
|
|
|
|
ColumnPtr null_map_compressed = null_map->compress();
|
|
|
|
|
|
|
|
size_t byte_size = nested_column->byteSize() + null_map->byteSize();
|
|
|
|
|
|
|
|
return ColumnCompressed::create(size(), byte_size,
|
2023-05-13 00:57:31 +00:00
|
|
|
[my_nested_column = std::move(nested_compressed), my_null_map = std::move(null_map_compressed)]
|
2021-02-17 23:09:46 +00:00
|
|
|
{
|
2023-05-13 00:57:31 +00:00
|
|
|
return ColumnNullable::create(my_nested_column->decompress(), my_null_map->decompress());
|
2021-02-17 23:09:46 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-01-17 20:54:32 +00:00
|
|
|
|
2016-08-16 11:26:17 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
/// The following function implements a slightly more general version
|
2021-01-02 09:47:38 +00:00
|
|
|
/// of getExtremes() than the implementation from Not-Null IColumns.
|
2016-08-16 11:26:17 +00:00
|
|
|
/// It takes into account the possible presence of nullable values.
|
2021-01-02 09:47:38 +00:00
|
|
|
void getExtremesWithNulls(const IColumn & nested_column, const NullMap & null_array, Field & min, Field & max, bool null_last = false)
|
2016-08-16 11:26:17 +00:00
|
|
|
{
|
2021-01-02 09:47:38 +00:00
|
|
|
size_t number_of_nulls = 0;
|
|
|
|
size_t n = null_array.size();
|
|
|
|
NullMap not_null_array(n);
|
|
|
|
for (auto i = 0ul; i < n; ++i)
|
2016-08-16 11:26:17 +00:00
|
|
|
{
|
2021-01-02 09:47:38 +00:00
|
|
|
if (null_array[i])
|
2016-12-30 05:13:14 +00:00
|
|
|
{
|
2021-01-02 09:47:38 +00:00
|
|
|
++number_of_nulls;
|
|
|
|
not_null_array[i] = 0;
|
2016-12-30 05:13:14 +00:00
|
|
|
}
|
2021-01-02 09:47:38 +00:00
|
|
|
else
|
2016-12-30 05:13:14 +00:00
|
|
|
{
|
2021-01-02 09:47:38 +00:00
|
|
|
not_null_array[i] = 1;
|
2016-12-30 05:13:14 +00:00
|
|
|
}
|
2016-08-16 11:26:17 +00:00
|
|
|
}
|
2021-01-02 09:47:38 +00:00
|
|
|
if (number_of_nulls == 0)
|
|
|
|
{
|
|
|
|
nested_column.getExtremes(min, max);
|
|
|
|
}
|
|
|
|
else if (number_of_nulls == n)
|
2016-12-30 05:13:14 +00:00
|
|
|
{
|
2021-08-27 14:09:15 +00:00
|
|
|
min = POSITIVE_INFINITY;
|
|
|
|
max = POSITIVE_INFINITY;
|
2021-01-02 09:47:38 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto filtered_column = nested_column.filter(not_null_array, -1);
|
|
|
|
filtered_column->getExtremes(min, max);
|
|
|
|
if (null_last)
|
2021-08-27 14:09:15 +00:00
|
|
|
max = POSITIVE_INFINITY;
|
2016-12-30 05:13:14 +00:00
|
|
|
}
|
2016-08-16 11:26:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-30 05:13:14 +00:00
|
|
|
|
2016-08-10 19:12:29 +00:00
|
|
|
void ColumnNullable::getExtremes(Field & min, Field & max) const
|
|
|
|
{
|
2021-01-02 09:47:38 +00:00
|
|
|
getExtremesWithNulls(getNestedColumn(), getNullMapData(), min, max);
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2018-08-27 18:20:58 +00:00
|
|
|
|
2021-01-02 09:47:38 +00:00
|
|
|
void ColumnNullable::getExtremesNullLast(Field & min, Field & max) const
|
|
|
|
{
|
|
|
|
getExtremesWithNulls(getNestedColumn(), getNullMapData(), min, max, true);
|
2016-07-05 16:23:37 +00:00
|
|
|
}
|
|
|
|
|
2016-12-30 05:13:14 +00:00
|
|
|
|
2018-03-20 14:17:09 +00:00
|
|
|
ColumnPtr ColumnNullable::replicate(const Offsets & offsets) const
|
2016-07-05 16:23:37 +00:00
|
|
|
{
|
2017-12-14 03:56:56 +00:00
|
|
|
ColumnPtr replicated_data = getNestedColumn().replicate(offsets);
|
|
|
|
ColumnPtr replicated_null_map = getNullMapColumn().replicate(offsets);
|
2017-12-14 01:43:19 +00:00
|
|
|
return ColumnNullable::create(replicated_data, replicated_null_map);
|
2016-07-05 16:23:37 +00:00
|
|
|
}
|
|
|
|
|
2016-12-30 05:13:14 +00:00
|
|
|
|
2017-03-07 20:52:09 +00:00
|
|
|
template <bool negative>
|
2022-03-21 18:08:35 +00:00
|
|
|
void ColumnNullable::applyNullMapImpl(const NullMap & map)
|
2016-07-05 16:23:37 +00:00
|
|
|
{
|
2022-03-21 18:08:35 +00:00
|
|
|
NullMap & arr = getNullMapData();
|
2016-07-05 16:23:37 +00:00
|
|
|
|
2022-03-21 18:08:35 +00:00
|
|
|
if (arr.size() != map.size())
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::LOGICAL_ERROR, "Inconsistent sizes of ColumnNullable objects");
|
2016-07-05 16:23:37 +00:00
|
|
|
|
2022-03-21 18:08:35 +00:00
|
|
|
for (size_t i = 0, size = arr.size(); i < size; ++i)
|
|
|
|
arr[i] |= negative ^ map[i];
|
2017-03-07 20:52:09 +00:00
|
|
|
}
|
|
|
|
|
2022-03-21 18:08:35 +00:00
|
|
|
void ColumnNullable::applyNullMap(const NullMap & map)
|
|
|
|
{
|
|
|
|
applyNullMapImpl<false>(map);
|
|
|
|
}
|
2017-03-07 20:52:09 +00:00
|
|
|
|
2017-03-29 11:33:07 +00:00
|
|
|
void ColumnNullable::applyNullMap(const ColumnUInt8 & map)
|
2017-03-07 20:52:09 +00:00
|
|
|
{
|
2022-03-21 18:08:35 +00:00
|
|
|
applyNullMapImpl<false>(map.getData());
|
2017-03-07 20:52:09 +00:00
|
|
|
}
|
|
|
|
|
2022-03-21 18:08:35 +00:00
|
|
|
void ColumnNullable::applyNegatedNullMap(const NullMap & map)
|
2017-03-07 20:52:09 +00:00
|
|
|
{
|
2017-03-29 11:33:07 +00:00
|
|
|
applyNullMapImpl<true>(map);
|
2016-07-05 16:23:37 +00:00
|
|
|
}
|
|
|
|
|
2022-03-21 18:08:35 +00:00
|
|
|
void ColumnNullable::applyNegatedNullMap(const ColumnUInt8 & map)
|
|
|
|
{
|
|
|
|
applyNullMapImpl<true>(map.getData());
|
|
|
|
}
|
|
|
|
|
2017-03-07 20:42:01 +00:00
|
|
|
|
2017-03-29 11:33:07 +00:00
|
|
|
void ColumnNullable::applyNullMap(const ColumnNullable & other)
|
2017-03-07 20:42:01 +00:00
|
|
|
{
|
2017-12-14 03:56:56 +00:00
|
|
|
applyNullMap(other.getNullMapColumn());
|
2017-03-07 20:42:01 +00:00
|
|
|
}
|
|
|
|
|
2017-04-17 20:19:09 +00:00
|
|
|
void ColumnNullable::checkConsistency() const
|
|
|
|
{
|
2017-12-14 03:56:56 +00:00
|
|
|
if (null_map->size() != getNestedColumn().size())
|
2024-02-26 22:34:50 +00:00
|
|
|
throw Exception(ErrorCodes::LOGICAL_ERROR, "Sizes of nested column and null map of Nullable column are not equal");
|
2017-04-17 20:19:09 +00:00
|
|
|
}
|
|
|
|
|
2024-01-22 13:41:58 +00:00
|
|
|
ColumnPtr ColumnNullable::createWithOffsets(const IColumn::Offsets & offsets, const ColumnConst & column_with_default_value, size_t total_rows, size_t shift) const
|
2021-04-15 02:16:09 +00:00
|
|
|
{
|
2021-09-16 13:57:45 +00:00
|
|
|
ColumnPtr new_values;
|
|
|
|
ColumnPtr new_null_map;
|
|
|
|
|
2024-01-22 13:41:58 +00:00
|
|
|
const ColumnNullable & nullable_column_with_default_value = assert_cast<const ColumnNullable &>(column_with_default_value.getDataColumn());
|
|
|
|
if (nullable_column_with_default_value.isNullAt(0))
|
2021-09-16 13:57:45 +00:00
|
|
|
{
|
|
|
|
/// Value in main column, when null map is 1 is implementation defined. So, take any value.
|
2024-01-22 13:41:58 +00:00
|
|
|
new_values = nested_column->createWithOffsets(offsets, *createColumnConstWithDefaultValue(nested_column), total_rows, shift);
|
|
|
|
new_null_map = null_map->createWithOffsets(offsets, *createColumnConst(null_map, Field(1u)), total_rows, shift);
|
2021-09-16 13:57:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-01-22 13:41:58 +00:00
|
|
|
new_values = nested_column->createWithOffsets(offsets, *ColumnConst::create(nullable_column_with_default_value.getNestedColumnPtr(), 1), total_rows, shift);
|
|
|
|
new_null_map = null_map->createWithOffsets(offsets, *createColumnConst(null_map, Field(0u)), total_rows, shift);
|
2021-09-16 13:57:45 +00:00
|
|
|
}
|
2021-04-15 02:16:09 +00:00
|
|
|
|
|
|
|
return ColumnNullable::create(new_values, new_null_map);
|
2021-04-03 00:04:48 +00:00
|
|
|
}
|
|
|
|
|
2023-02-10 16:40:14 +00:00
|
|
|
ColumnPtr ColumnNullable::getNestedColumnWithDefaultOnNull() const
|
|
|
|
{
|
|
|
|
auto res = nested_column->cloneEmpty();
|
|
|
|
const auto & null_map_data = getNullMapData();
|
|
|
|
size_t start = 0;
|
2023-02-13 12:28:09 +00:00
|
|
|
size_t end = null_map->size();
|
2023-02-10 16:40:14 +00:00
|
|
|
while (start < nested_column->size())
|
|
|
|
{
|
|
|
|
size_t next_null_index = start;
|
2023-02-13 12:28:09 +00:00
|
|
|
while (next_null_index < end && !null_map_data[next_null_index])
|
2023-02-10 16:40:14 +00:00
|
|
|
++next_null_index;
|
|
|
|
|
|
|
|
if (next_null_index != start)
|
|
|
|
res->insertRangeFrom(*nested_column, start, next_null_index - start);
|
|
|
|
|
2023-08-14 11:19:07 +00:00
|
|
|
size_t next_none_null_index = next_null_index;
|
|
|
|
while (next_none_null_index < end && null_map_data[next_none_null_index])
|
2023-08-21 14:22:53 +00:00
|
|
|
++next_none_null_index;
|
2023-02-10 16:40:14 +00:00
|
|
|
|
2023-08-14 11:19:07 +00:00
|
|
|
if (next_null_index != next_none_null_index)
|
|
|
|
res->insertManyDefaults(next_none_null_index - next_null_index);
|
|
|
|
|
|
|
|
start = next_none_null_index;
|
2023-02-10 16:40:14 +00:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2017-12-10 22:44:04 +00:00
|
|
|
ColumnPtr makeNullable(const ColumnPtr & column)
|
|
|
|
{
|
2019-07-01 11:44:19 +00:00
|
|
|
if (isColumnNullable(*column))
|
2017-12-10 22:44:04 +00:00
|
|
|
return column;
|
|
|
|
|
2019-06-27 19:28:52 +00:00
|
|
|
if (isColumnConst(*column))
|
2019-08-21 02:28:04 +00:00
|
|
|
return ColumnConst::create(makeNullable(assert_cast<const ColumnConst &>(*column).getDataColumnPtr()), column->size());
|
2017-12-10 22:44:04 +00:00
|
|
|
|
2017-12-14 01:43:19 +00:00
|
|
|
return ColumnNullable::create(column, ColumnUInt8::create(column->size(), 0));
|
2017-12-10 22:44:04 +00:00
|
|
|
}
|
|
|
|
|
2023-02-08 19:14:28 +00:00
|
|
|
ColumnPtr makeNullableOrLowCardinalityNullable(const ColumnPtr & column)
|
|
|
|
{
|
2023-12-19 16:43:30 +00:00
|
|
|
if (isColumnNullableOrLowCardinalityNullable(*column))
|
2023-02-08 19:14:28 +00:00
|
|
|
return column;
|
|
|
|
|
|
|
|
if (isColumnConst(*column))
|
|
|
|
return ColumnConst::create(makeNullable(assert_cast<const ColumnConst &>(*column).getDataColumnPtr()), column->size());
|
|
|
|
|
|
|
|
if (column->lowCardinality())
|
|
|
|
return assert_cast<const ColumnLowCardinality &>(*column).cloneNullable();
|
|
|
|
|
|
|
|
return ColumnNullable::create(column, ColumnUInt8::create(column->size(), 0));
|
|
|
|
}
|
|
|
|
|
2022-07-07 18:53:20 +00:00
|
|
|
ColumnPtr makeNullableSafe(const ColumnPtr & column)
|
|
|
|
{
|
|
|
|
if (isColumnNullable(*column))
|
|
|
|
return column;
|
|
|
|
|
|
|
|
if (isColumnConst(*column))
|
|
|
|
return ColumnConst::create(makeNullableSafe(assert_cast<const ColumnConst &>(*column).getDataColumnPtr()), column->size());
|
|
|
|
|
|
|
|
if (column->canBeInsideNullable())
|
|
|
|
return makeNullable(column);
|
|
|
|
|
|
|
|
return column;
|
|
|
|
}
|
|
|
|
|
2023-12-19 16:43:30 +00:00
|
|
|
ColumnPtr makeNullableOrLowCardinalityNullableSafe(const ColumnPtr & column)
|
|
|
|
{
|
|
|
|
if (isColumnNullableOrLowCardinalityNullable(*column))
|
|
|
|
return column;
|
|
|
|
|
|
|
|
if (isColumnConst(*column))
|
|
|
|
return ColumnConst::create(makeNullableOrLowCardinalityNullableSafe(assert_cast<const ColumnConst &>(*column).getDataColumnPtr()), column->size());
|
|
|
|
|
|
|
|
if (column->lowCardinality())
|
|
|
|
return assert_cast<const ColumnLowCardinality &>(*column).cloneNullable();
|
|
|
|
|
|
|
|
if (column->canBeInsideNullable())
|
2024-01-09 18:23:34 +00:00
|
|
|
return makeNullable(column);
|
2023-12-19 16:43:30 +00:00
|
|
|
|
|
|
|
return column;
|
|
|
|
}
|
|
|
|
|
2016-07-05 16:23:37 +00:00
|
|
|
}
|