2018-03-29 14:04:24 +00:00
|
|
|
#pragma once
|
2018-03-14 10:47:13 +00:00
|
|
|
#include <Columns/IColumnUnique.h>
|
2020-06-02 00:23:41 +00:00
|
|
|
#include <Columns/IColumnImpl.h>
|
2018-08-02 15:16:40 +00:00
|
|
|
#include <Columns/ReverseIndex.h>
|
|
|
|
|
2018-03-19 11:45:17 +00:00
|
|
|
#include <Columns/ColumnVector.h>
|
|
|
|
#include <Columns/ColumnNullable.h>
|
2018-03-29 14:04:24 +00:00
|
|
|
#include <Columns/ColumnString.h>
|
2018-08-01 11:26:45 +00:00
|
|
|
#include <Columns/ColumnFixedString.h>
|
2018-08-02 15:16:40 +00:00
|
|
|
|
2018-04-17 11:28:35 +00:00
|
|
|
#include <DataTypes/DataTypeNullable.h>
|
2018-06-28 18:12:29 +00:00
|
|
|
#include <DataTypes/NumberTraits.h>
|
2018-03-14 10:47:13 +00:00
|
|
|
|
2018-08-02 15:16:40 +00:00
|
|
|
#include <Common/typeid_cast.h>
|
2019-08-21 02:28:04 +00:00
|
|
|
#include <Common/assert_cast.h>
|
2018-08-02 15:16:40 +00:00
|
|
|
#include <ext/range.h>
|
2018-03-29 14:04:24 +00:00
|
|
|
|
2018-12-14 17:50:10 +00:00
|
|
|
#include <common/unaligned.h>
|
|
|
|
|
|
|
|
|
2018-03-14 10:47:13 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2018-08-28 19:36:27 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2020-02-25 18:02:41 +00:00
|
|
|
extern const int LOGICAL_ERROR;
|
2018-08-28 19:36:27 +00:00
|
|
|
extern const int ILLEGAL_COLUMN;
|
|
|
|
}
|
2018-08-01 11:26:45 +00:00
|
|
|
|
2018-06-28 18:12:29 +00:00
|
|
|
template <typename ColumnType>
|
2019-04-19 20:21:17 +00:00
|
|
|
class ColumnUnique final : public COWHelper<IColumnUnique, ColumnUnique<ColumnType>>
|
2018-03-14 10:47:13 +00:00
|
|
|
{
|
2019-04-19 20:21:17 +00:00
|
|
|
friend class COWHelper<IColumnUnique, ColumnUnique<ColumnType>>;
|
2018-03-19 11:45:17 +00:00
|
|
|
|
|
|
|
private:
|
2018-06-26 17:07:08 +00:00
|
|
|
explicit ColumnUnique(MutableColumnPtr && holder, bool is_nullable);
|
2018-06-07 18:14:37 +00:00
|
|
|
explicit ColumnUnique(const IDataType & type);
|
2018-08-02 15:16:40 +00:00
|
|
|
ColumnUnique(const ColumnUnique & other);
|
2018-03-19 11:45:17 +00:00
|
|
|
|
2018-03-14 10:47:13 +00:00
|
|
|
public:
|
2018-07-09 18:19:03 +00:00
|
|
|
MutableColumnPtr cloneEmpty() const override;
|
|
|
|
|
|
|
|
const ColumnPtr & getNestedColumn() const override;
|
2018-06-26 17:07:08 +00:00
|
|
|
const ColumnPtr & getNestedNotNullableColumn() const override { return column_holder; }
|
2018-12-03 13:00:01 +00:00
|
|
|
bool nestedColumnIsNullable() const override { return is_nullable; }
|
2018-06-26 17:07:08 +00:00
|
|
|
|
2018-03-19 11:45:17 +00:00
|
|
|
size_t uniqueInsert(const Field & x) override;
|
|
|
|
size_t uniqueInsertFrom(const IColumn & src, size_t n) override;
|
2018-06-07 18:14:37 +00:00
|
|
|
MutableColumnPtr uniqueInsertRangeFrom(const IColumn & src, size_t start, size_t length) override;
|
|
|
|
IColumnUnique::IndexesWithOverflow uniqueInsertRangeWithOverflow(const IColumn & src, size_t start, size_t length,
|
|
|
|
size_t max_dictionary_size) override;
|
2018-03-19 11:45:17 +00:00
|
|
|
size_t uniqueInsertData(const char * pos, size_t length) override;
|
|
|
|
size_t uniqueDeserializeAndInsertFromArena(const char * pos, const char *& new_pos) override;
|
2018-03-14 10:47:13 +00:00
|
|
|
|
2018-09-19 11:59:40 +00:00
|
|
|
size_t getDefaultValueIndex() const override { return 0; }
|
2018-03-19 11:45:17 +00:00
|
|
|
size_t getNullValueIndex() const override;
|
2018-09-19 11:59:40 +00:00
|
|
|
size_t getNestedTypeDefaultValueIndex() const override { return is_nullable ? 1 : 0; }
|
2018-03-19 11:45:17 +00:00
|
|
|
bool canContainNulls() const override { return is_nullable; }
|
|
|
|
|
2018-06-06 11:20:27 +00:00
|
|
|
Field operator[](size_t n) const override { return (*getNestedColumn())[n]; }
|
|
|
|
void get(size_t n, Field & res) const override { getNestedColumn()->get(n, res); }
|
|
|
|
StringRef getDataAt(size_t n) const override { return getNestedColumn()->getDataAt(n); }
|
2018-04-17 10:45:05 +00:00
|
|
|
StringRef getDataAtWithTerminatingZero(size_t n) const override
|
|
|
|
{
|
2018-06-06 11:20:27 +00:00
|
|
|
return getNestedColumn()->getDataAtWithTerminatingZero(n);
|
2018-04-17 10:45:05 +00:00
|
|
|
}
|
2018-06-06 11:20:27 +00:00
|
|
|
UInt64 get64(size_t n) const override { return getNestedColumn()->get64(n); }
|
|
|
|
UInt64 getUInt(size_t n) const override { return getNestedColumn()->getUInt(n); }
|
|
|
|
Int64 getInt(size_t n) const override { return getNestedColumn()->getInt(n); }
|
2019-07-01 13:49:50 +00:00
|
|
|
Float64 getFloat64(size_t n) const override { return getNestedColumn()->getFloat64(n); }
|
2019-12-03 01:55:46 +00:00
|
|
|
Float32 getFloat32(size_t n) const override { return getNestedColumn()->getFloat32(n); }
|
2019-07-01 13:49:50 +00:00
|
|
|
bool getBool(size_t n) const override { return getNestedColumn()->getBool(n); }
|
2018-06-06 13:43:16 +00:00
|
|
|
bool isNullAt(size_t n) const override { return is_nullable && n == getNullValueIndex(); }
|
2018-11-01 13:36:13 +00:00
|
|
|
StringRef serializeValueIntoArena(size_t n, Arena & arena, char const *& begin) const override;
|
2019-01-04 12:10:00 +00:00
|
|
|
void updateHashWithValue(size_t n, SipHash & hash_func) const override
|
2018-04-17 10:45:05 +00:00
|
|
|
{
|
2019-01-04 12:10:00 +00:00
|
|
|
return getNestedColumn()->updateHashWithValue(n, hash_func);
|
2018-04-17 10:45:05 +00:00
|
|
|
}
|
2018-06-06 11:20:27 +00:00
|
|
|
|
2018-11-01 14:08:15 +00:00
|
|
|
int compareAt(size_t n, size_t m, const IColumn & rhs, int nan_direction_hint) const override;
|
2020-05-25 14:33:31 +00:00
|
|
|
void updatePermutation(bool reverse, size_t limit, int nan_direction_hint, IColumn::Permutation & res, EqualRanges & equal_range) const override;
|
2018-06-06 11:20:27 +00:00
|
|
|
|
2018-03-19 11:45:17 +00:00
|
|
|
void getExtremes(Field & min, Field & max) const override { column_holder->getExtremes(min, max); }
|
|
|
|
bool valuesHaveFixedSize() const override { return column_holder->valuesHaveFixedSize(); }
|
|
|
|
bool isFixedAndContiguous() const override { return column_holder->isFixedAndContiguous(); }
|
|
|
|
size_t sizeOfValueIfFixed() const override { return column_holder->sizeOfValueIfFixed(); }
|
|
|
|
bool isNumeric() const override { return column_holder->isNumeric(); }
|
|
|
|
|
|
|
|
size_t byteSize() const override { return column_holder->byteSize(); }
|
2019-03-25 01:43:54 +00:00
|
|
|
void protect() override { column_holder->protect(); }
|
2018-04-17 10:45:05 +00:00
|
|
|
size_t allocatedBytes() const override
|
|
|
|
{
|
2018-06-06 11:20:27 +00:00
|
|
|
return column_holder->allocatedBytes()
|
2018-08-02 15:16:40 +00:00
|
|
|
+ index.allocatedBytes()
|
2018-11-23 08:11:54 +00:00
|
|
|
+ (nested_null_mask ? nested_null_mask->allocatedBytes() : 0);
|
2018-06-06 11:20:27 +00:00
|
|
|
}
|
|
|
|
void forEachSubcolumn(IColumn::ColumnCallback callback) override
|
|
|
|
{
|
2018-06-26 17:07:08 +00:00
|
|
|
callback(column_holder);
|
2018-08-02 15:16:40 +00:00
|
|
|
index.setColumn(getRawColumnPtr());
|
2018-11-23 11:55:06 +00:00
|
|
|
if (is_nullable)
|
|
|
|
nested_column_nullable = ColumnNullable::create(column_holder, nested_null_mask);
|
2018-04-17 10:45:05 +00:00
|
|
|
}
|
2018-03-14 10:47:13 +00:00
|
|
|
|
2019-03-14 23:10:51 +00:00
|
|
|
bool structureEquals(const IColumn & rhs) const override
|
|
|
|
{
|
|
|
|
if (auto rhs_concrete = typeid_cast<const ColumnUnique *>(&rhs))
|
|
|
|
return column_holder->structureEquals(*rhs_concrete->column_holder);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-08-23 13:22:03 +00:00
|
|
|
const UInt64 * tryGetSavedHash() const override { return index.tryGetSavedHash(); }
|
|
|
|
|
2018-09-07 10:08:09 +00:00
|
|
|
UInt128 getHash() const override { return hash.getHash(*getRawColumnPtr()); }
|
|
|
|
|
2018-03-14 10:47:13 +00:00
|
|
|
private:
|
|
|
|
|
2019-03-25 01:43:54 +00:00
|
|
|
IColumn::WrappedPtr column_holder;
|
2018-08-02 15:16:40 +00:00
|
|
|
bool is_nullable;
|
2018-12-12 19:21:48 +00:00
|
|
|
size_t size_of_value_if_fixed = 0;
|
2018-08-02 15:16:40 +00:00
|
|
|
ReverseIndex<UInt64, ColumnType> index;
|
2018-04-17 12:41:16 +00:00
|
|
|
|
2018-06-26 17:07:08 +00:00
|
|
|
/// For DataTypeNullable, stores null map.
|
2019-03-25 01:43:54 +00:00
|
|
|
IColumn::WrappedPtr nested_null_mask;
|
|
|
|
IColumn::WrappedPtr nested_column_nullable;
|
2018-04-17 12:41:16 +00:00
|
|
|
|
2018-09-07 10:08:09 +00:00
|
|
|
class IncrementalHash
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
UInt128 hash;
|
|
|
|
std::atomic<size_t> num_added_rows;
|
|
|
|
|
|
|
|
std::mutex mutex;
|
|
|
|
public:
|
|
|
|
IncrementalHash() : num_added_rows(0) {}
|
|
|
|
|
|
|
|
UInt128 getHash(const ColumnType & column);
|
|
|
|
};
|
|
|
|
|
|
|
|
mutable IncrementalHash hash;
|
|
|
|
|
2018-11-23 10:18:50 +00:00
|
|
|
void createNullMask();
|
2018-11-23 08:11:54 +00:00
|
|
|
void updateNullMask();
|
|
|
|
|
2018-08-02 15:16:40 +00:00
|
|
|
static size_t numSpecialValues(bool is_nullable) { return is_nullable ? 2 : 1; }
|
|
|
|
size_t numSpecialValues() const { return numSpecialValues(is_nullable); }
|
2018-03-19 11:45:17 +00:00
|
|
|
|
2019-08-21 02:28:04 +00:00
|
|
|
ColumnType * getRawColumnPtr() { return assert_cast<ColumnType *>(column_holder.get()); }
|
|
|
|
const ColumnType * getRawColumnPtr() const { return assert_cast<const ColumnType *>(column_holder.get()); }
|
2018-03-14 10:47:13 +00:00
|
|
|
|
2018-06-28 18:12:29 +00:00
|
|
|
template <typename IndexType>
|
|
|
|
MutableColumnPtr uniqueInsertRangeImpl(
|
2018-06-07 18:14:37 +00:00
|
|
|
const IColumn & src,
|
|
|
|
size_t start,
|
|
|
|
size_t length,
|
2018-07-09 18:19:03 +00:00
|
|
|
size_t num_added_rows,
|
2018-06-28 18:12:29 +00:00
|
|
|
typename ColumnVector<IndexType>::MutablePtr && positions_column,
|
2018-08-02 15:16:40 +00:00
|
|
|
ReverseIndex<UInt64, ColumnType> * secondary_index,
|
2018-06-07 18:14:37 +00:00
|
|
|
size_t max_dictionary_size);
|
2018-03-14 10:47:13 +00:00
|
|
|
};
|
|
|
|
|
2018-07-09 18:19:03 +00:00
|
|
|
template <typename ColumnType>
|
|
|
|
MutableColumnPtr ColumnUnique<ColumnType>::cloneEmpty() const
|
|
|
|
{
|
|
|
|
return ColumnUnique<ColumnType>::create(column_holder->cloneResized(numSpecialValues()), is_nullable);
|
|
|
|
}
|
|
|
|
|
2018-06-28 18:12:29 +00:00
|
|
|
template <typename ColumnType>
|
2018-08-02 15:16:40 +00:00
|
|
|
ColumnUnique<ColumnType>::ColumnUnique(const ColumnUnique & other)
|
|
|
|
: column_holder(other.column_holder)
|
|
|
|
, is_nullable(other.is_nullable)
|
2018-12-12 19:21:48 +00:00
|
|
|
, size_of_value_if_fixed (other.size_of_value_if_fixed)
|
2018-08-02 15:16:40 +00:00
|
|
|
, index(numSpecialValues(is_nullable), 0)
|
|
|
|
{
|
|
|
|
index.setColumn(getRawColumnPtr());
|
2018-11-23 10:18:50 +00:00
|
|
|
createNullMask();
|
2018-08-02 15:16:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename ColumnType>
|
|
|
|
ColumnUnique<ColumnType>::ColumnUnique(const IDataType & type)
|
|
|
|
: is_nullable(type.isNullable())
|
|
|
|
, index(numSpecialValues(is_nullable), 0)
|
2018-04-17 12:41:16 +00:00
|
|
|
{
|
2018-06-26 17:07:08 +00:00
|
|
|
const auto & holder_type = is_nullable ? *static_cast<const DataTypeNullable &>(type).getNestedType() : type;
|
|
|
|
column_holder = holder_type.createColumn()->cloneResized(numSpecialValues());
|
2018-08-02 15:16:40 +00:00
|
|
|
index.setColumn(getRawColumnPtr());
|
2018-11-23 10:18:50 +00:00
|
|
|
createNullMask();
|
2018-12-12 19:21:48 +00:00
|
|
|
|
|
|
|
if (column_holder->valuesHaveFixedSize())
|
|
|
|
size_of_value_if_fixed = column_holder->sizeOfValueIfFixed();
|
2018-04-17 12:41:16 +00:00
|
|
|
}
|
|
|
|
|
2018-06-28 18:12:29 +00:00
|
|
|
template <typename ColumnType>
|
2019-08-03 11:02:40 +00:00
|
|
|
ColumnUnique<ColumnType>::ColumnUnique(MutableColumnPtr && holder, bool is_nullable_)
|
2018-08-02 15:16:40 +00:00
|
|
|
: column_holder(std::move(holder))
|
2019-08-03 11:02:40 +00:00
|
|
|
, is_nullable(is_nullable_)
|
|
|
|
, index(numSpecialValues(is_nullable_), 0)
|
2018-03-19 11:45:17 +00:00
|
|
|
{
|
2018-06-26 17:07:08 +00:00
|
|
|
if (column_holder->size() < numSpecialValues())
|
|
|
|
throw Exception("Too small holder column for ColumnUnique.", ErrorCodes::ILLEGAL_COLUMN);
|
2019-07-01 11:44:19 +00:00
|
|
|
if (isColumnNullable(*column_holder))
|
2018-06-26 17:07:08 +00:00
|
|
|
throw Exception("Holder column for ColumnUnique can't be nullable.", ErrorCodes::ILLEGAL_COLUMN);
|
2018-08-02 15:16:40 +00:00
|
|
|
|
|
|
|
index.setColumn(getRawColumnPtr());
|
2018-11-23 10:18:50 +00:00
|
|
|
createNullMask();
|
2018-12-12 19:21:48 +00:00
|
|
|
|
|
|
|
if (column_holder->valuesHaveFixedSize())
|
|
|
|
size_of_value_if_fixed = column_holder->sizeOfValueIfFixed();
|
2018-03-19 11:45:17 +00:00
|
|
|
}
|
|
|
|
|
2018-06-28 18:12:29 +00:00
|
|
|
template <typename ColumnType>
|
2018-11-23 10:18:50 +00:00
|
|
|
void ColumnUnique<ColumnType>::createNullMask()
|
2018-04-17 12:41:16 +00:00
|
|
|
{
|
|
|
|
if (is_nullable)
|
|
|
|
{
|
2018-06-26 17:07:08 +00:00
|
|
|
size_t size = getRawColumnPtr()->size();
|
2018-11-23 08:11:54 +00:00
|
|
|
if (!nested_null_mask)
|
2018-06-26 17:07:08 +00:00
|
|
|
{
|
|
|
|
ColumnUInt8::MutablePtr null_mask = ColumnUInt8::create(size, UInt8(0));
|
|
|
|
null_mask->getData()[getNullValueIndex()] = 1;
|
2018-11-23 08:11:54 +00:00
|
|
|
nested_null_mask = std::move(null_mask);
|
|
|
|
nested_column_nullable = ColumnNullable::create(column_holder, nested_null_mask);
|
2018-06-26 17:07:08 +00:00
|
|
|
}
|
2018-11-23 10:18:50 +00:00
|
|
|
else
|
|
|
|
throw Exception("Null mask for ColumnUnique is already created.", ErrorCodes::LOGICAL_ERROR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename ColumnType>
|
|
|
|
void ColumnUnique<ColumnType>::updateNullMask()
|
|
|
|
{
|
|
|
|
if (is_nullable)
|
|
|
|
{
|
|
|
|
if (!nested_null_mask)
|
|
|
|
throw Exception("Null mask for ColumnUnique is was not created.", ErrorCodes::LOGICAL_ERROR);
|
|
|
|
|
|
|
|
size_t size = getRawColumnPtr()->size();
|
2018-06-26 17:07:08 +00:00
|
|
|
|
2018-11-23 08:11:54 +00:00
|
|
|
if (nested_null_mask->size() != size)
|
2019-08-21 02:28:04 +00:00
|
|
|
assert_cast<ColumnUInt8 &>(*nested_null_mask).getData().resize_fill(size);
|
2018-04-17 12:41:16 +00:00
|
|
|
}
|
2018-11-23 08:11:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename ColumnType>
|
|
|
|
const ColumnPtr & ColumnUnique<ColumnType>::getNestedColumn() const
|
|
|
|
{
|
|
|
|
if (is_nullable)
|
|
|
|
return nested_column_nullable;
|
|
|
|
|
2018-04-17 12:41:16 +00:00
|
|
|
return column_holder;
|
|
|
|
}
|
|
|
|
|
2018-06-28 18:12:29 +00:00
|
|
|
template <typename ColumnType>
|
|
|
|
size_t ColumnUnique<ColumnType>::getNullValueIndex() const
|
2018-03-19 11:45:17 +00:00
|
|
|
{
|
|
|
|
if (!is_nullable)
|
2018-06-26 17:07:08 +00:00
|
|
|
throw Exception("ColumnUnique can't contain null values.", ErrorCodes::LOGICAL_ERROR);
|
2018-03-19 11:45:17 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-06-28 18:12:29 +00:00
|
|
|
template <typename ColumnType>
|
|
|
|
size_t ColumnUnique<ColumnType>::uniqueInsert(const Field & x)
|
2018-03-19 11:45:17 +00:00
|
|
|
{
|
|
|
|
if (x.getType() == Field::Types::Null)
|
|
|
|
return getNullValueIndex();
|
|
|
|
|
2020-05-07 08:58:06 +00:00
|
|
|
if (isNumeric())
|
2019-12-17 10:19:21 +00:00
|
|
|
return uniqueInsertData(&x.reinterpret<char>(), size_of_value_if_fixed);
|
2018-03-19 11:45:17 +00:00
|
|
|
|
2018-12-12 19:21:48 +00:00
|
|
|
auto & val = x.get<String>();
|
|
|
|
return uniqueInsertData(val.data(), val.size());
|
2018-03-19 11:45:17 +00:00
|
|
|
}
|
|
|
|
|
2018-06-28 18:12:29 +00:00
|
|
|
template <typename ColumnType>
|
|
|
|
size_t ColumnUnique<ColumnType>::uniqueInsertFrom(const IColumn & src, size_t n)
|
2018-03-19 11:45:17 +00:00
|
|
|
{
|
2018-05-03 16:28:14 +00:00
|
|
|
if (is_nullable && src.isNullAt(n))
|
|
|
|
return getNullValueIndex();
|
|
|
|
|
2019-06-27 18:50:20 +00:00
|
|
|
if (auto * nullable = checkAndGetColumn<ColumnNullable>(src))
|
2018-07-09 18:19:03 +00:00
|
|
|
return uniqueInsertFrom(nullable->getNestedColumn(), n);
|
|
|
|
|
2018-03-19 11:45:17 +00:00
|
|
|
auto ref = src.getDataAt(n);
|
|
|
|
return uniqueInsertData(ref.data, ref.size);
|
|
|
|
}
|
|
|
|
|
2018-06-28 18:12:29 +00:00
|
|
|
template <typename ColumnType>
|
|
|
|
size_t ColumnUnique<ColumnType>::uniqueInsertData(const char * pos, size_t length)
|
2018-03-19 11:45:17 +00:00
|
|
|
{
|
|
|
|
auto column = getRawColumnPtr();
|
|
|
|
|
2018-09-19 11:59:40 +00:00
|
|
|
if (column->getDataAt(getNestedTypeDefaultValueIndex()) == StringRef(pos, length))
|
|
|
|
return getNestedTypeDefaultValueIndex();
|
2018-03-19 11:45:17 +00:00
|
|
|
|
2018-12-12 19:21:48 +00:00
|
|
|
auto insertion_point = index.insert(StringRef(pos, length));
|
2018-03-19 11:45:17 +00:00
|
|
|
|
2018-11-23 08:11:54 +00:00
|
|
|
updateNullMask();
|
|
|
|
|
2018-08-02 15:16:40 +00:00
|
|
|
return insertion_point;
|
2018-03-19 11:45:17 +00:00
|
|
|
}
|
|
|
|
|
2018-11-01 13:36:13 +00:00
|
|
|
template <typename ColumnType>
|
|
|
|
StringRef ColumnUnique<ColumnType>::serializeValueIntoArena(size_t n, Arena & arena, char const *& begin) const
|
|
|
|
{
|
|
|
|
if (is_nullable)
|
|
|
|
{
|
2019-07-15 16:20:21 +00:00
|
|
|
static constexpr auto s = sizeof(UInt8);
|
2018-11-01 13:36:13 +00:00
|
|
|
|
2019-07-15 16:20:21 +00:00
|
|
|
auto pos = arena.allocContinue(s, begin);
|
|
|
|
UInt8 flag = (n == getNullValueIndex() ? 1 : 0);
|
|
|
|
unalignedStore<UInt8>(pos, flag);
|
2018-11-01 13:36:13 +00:00
|
|
|
|
2019-07-15 16:20:21 +00:00
|
|
|
if (n == getNullValueIndex())
|
|
|
|
return StringRef(pos, s);
|
2018-11-01 13:36:13 +00:00
|
|
|
|
2019-07-15 16:20:21 +00:00
|
|
|
auto nested_ref = column_holder->serializeValueIntoArena(n, arena, begin);
|
2018-11-01 13:36:13 +00:00
|
|
|
|
2019-07-15 16:20:21 +00:00
|
|
|
/// 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);
|
2018-11-01 13:36:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return column_holder->serializeValueIntoArena(n, arena, begin);
|
|
|
|
}
|
|
|
|
|
2018-06-28 18:12:29 +00:00
|
|
|
template <typename ColumnType>
|
|
|
|
size_t ColumnUnique<ColumnType>::uniqueDeserializeAndInsertFromArena(const char * pos, const char *& new_pos)
|
2018-03-19 11:45:17 +00:00
|
|
|
{
|
2018-11-01 13:36:13 +00:00
|
|
|
if (is_nullable)
|
|
|
|
{
|
2020-01-03 15:28:38 +00:00
|
|
|
UInt8 val = unalignedLoad<UInt8>(pos);
|
2018-11-01 13:36:13 +00:00
|
|
|
pos += sizeof(val);
|
|
|
|
|
|
|
|
if (val)
|
|
|
|
{
|
|
|
|
new_pos = pos;
|
|
|
|
return getNullValueIndex();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-14 11:36:08 +00:00
|
|
|
/// Numbers, FixedString
|
2018-12-12 19:21:48 +00:00
|
|
|
if (size_of_value_if_fixed)
|
2018-03-19 11:45:17 +00:00
|
|
|
{
|
2018-12-12 19:21:48 +00:00
|
|
|
new_pos = pos + size_of_value_if_fixed;
|
|
|
|
return uniqueInsertData(pos, size_of_value_if_fixed);
|
2018-03-19 11:45:17 +00:00
|
|
|
}
|
|
|
|
|
2018-12-14 11:36:08 +00:00
|
|
|
/// String
|
2018-12-14 17:50:10 +00:00
|
|
|
const size_t string_size = unalignedLoad<size_t>(pos);
|
2018-12-12 19:21:48 +00:00
|
|
|
pos += sizeof(string_size);
|
|
|
|
new_pos = pos + string_size;
|
2018-03-19 11:45:17 +00:00
|
|
|
|
2018-12-14 11:36:08 +00:00
|
|
|
/// -1 because of terminating zero
|
|
|
|
return uniqueInsertData(pos, string_size - 1);
|
2018-03-19 11:45:17 +00:00
|
|
|
}
|
|
|
|
|
2018-11-01 14:08:15 +00:00
|
|
|
template <typename ColumnType>
|
|
|
|
int ColumnUnique<ColumnType>::compareAt(size_t n, size_t m, const IColumn & rhs, int nan_direction_hint) const
|
|
|
|
{
|
|
|
|
if (is_nullable)
|
|
|
|
{
|
|
|
|
/// See ColumnNullable::compareAt
|
|
|
|
bool lval_is_null = n == getNullValueIndex();
|
|
|
|
bool rval_is_null = m == getNullValueIndex();
|
|
|
|
|
|
|
|
if (unlikely(lval_is_null || rval_is_null))
|
|
|
|
{
|
|
|
|
if (lval_is_null && rval_is_null)
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return lval_is_null ? nan_direction_hint : -nan_direction_hint;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto & column_unique = static_cast<const IColumnUnique &>(rhs);
|
|
|
|
return getNestedColumn()->compareAt(n, m, *column_unique.getNestedColumn(), nan_direction_hint);
|
|
|
|
}
|
|
|
|
|
2020-05-12 00:58:58 +00:00
|
|
|
template <typename ColumnType>
|
2020-05-25 14:33:31 +00:00
|
|
|
void ColumnUnique<ColumnType>::updatePermutation(bool reverse, size_t limit, int nan_direction_hint, IColumn::Permutation & res, EqualRanges & equal_range) const
|
2020-05-14 21:00:56 +00:00
|
|
|
{
|
2020-05-12 00:58:58 +00:00
|
|
|
bool found_null_value_index = false;
|
2020-05-14 21:00:56 +00:00
|
|
|
for (size_t i = 0; i < equal_range.size() && !found_null_value_index; ++i)
|
|
|
|
{
|
2020-05-12 00:58:58 +00:00
|
|
|
auto& [first, last] = equal_range[i];
|
2020-05-14 21:00:56 +00:00
|
|
|
for (auto j = first; j < last; ++j)
|
|
|
|
{
|
|
|
|
if (res[j] == getNullValueIndex())
|
|
|
|
{
|
|
|
|
if ((nan_direction_hint > 0) != reverse)
|
|
|
|
{
|
2020-05-12 01:50:42 +00:00
|
|
|
std::swap(res[j], res[last - 1]);
|
2020-05-12 00:58:58 +00:00
|
|
|
--last;
|
2020-05-14 21:00:56 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-05-12 01:50:42 +00:00
|
|
|
std::swap(res[j], res[first]);
|
2020-05-12 00:58:58 +00:00
|
|
|
++first;
|
|
|
|
}
|
2020-05-14 21:00:56 +00:00
|
|
|
if (last - first <= 1)
|
|
|
|
{
|
2020-05-12 00:58:58 +00:00
|
|
|
equal_range.erase(equal_range.begin() + i);
|
|
|
|
}
|
|
|
|
found_null_value_index = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
getNestedColumn()->updatePermutation(reverse, limit, nan_direction_hint, res, equal_range);
|
|
|
|
}
|
|
|
|
|
2018-07-09 18:19:03 +00:00
|
|
|
template <typename IndexType>
|
|
|
|
static void checkIndexes(const ColumnVector<IndexType> & indexes, size_t max_dictionary_size)
|
|
|
|
{
|
|
|
|
auto & data = indexes.getData();
|
|
|
|
for (size_t i = 0; i < data.size(); ++i)
|
|
|
|
{
|
|
|
|
if (data[i] >= max_dictionary_size)
|
|
|
|
{
|
|
|
|
throw Exception("Found index " + toString(data[i]) + " at position " + toString(i)
|
|
|
|
+ " which is grated or equal than dictionary size " + toString(max_dictionary_size),
|
|
|
|
ErrorCodes::LOGICAL_ERROR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-28 18:12:29 +00:00
|
|
|
template <typename ColumnType>
|
|
|
|
template <typename IndexType>
|
|
|
|
MutableColumnPtr ColumnUnique<ColumnType>::uniqueInsertRangeImpl(
|
2018-06-07 18:14:37 +00:00
|
|
|
const IColumn & src,
|
|
|
|
size_t start,
|
|
|
|
size_t length,
|
2018-07-09 18:19:03 +00:00
|
|
|
size_t num_added_rows,
|
2018-06-28 18:12:29 +00:00
|
|
|
typename ColumnVector<IndexType>::MutablePtr && positions_column,
|
2018-08-02 15:16:40 +00:00
|
|
|
ReverseIndex<UInt64, ColumnType> * secondary_index,
|
2018-06-07 18:14:37 +00:00
|
|
|
size_t max_dictionary_size)
|
2018-03-19 11:45:17 +00:00
|
|
|
{
|
|
|
|
const ColumnType * src_column;
|
|
|
|
const NullMap * null_map = nullptr;
|
2018-06-28 18:12:29 +00:00
|
|
|
auto & positions = positions_column->getData();
|
|
|
|
|
2018-08-02 15:16:40 +00:00
|
|
|
auto update_position = [&](UInt64 & next_position) -> MutableColumnPtr
|
2018-06-28 18:12:29 +00:00
|
|
|
{
|
2018-07-09 18:19:03 +00:00
|
|
|
constexpr auto next_size = NumberTraits::nextSize(sizeof(IndexType));
|
|
|
|
using SuperiorIndexType = typename NumberTraits::Construct<false, false, next_size>::Type;
|
|
|
|
|
2018-06-28 18:12:29 +00:00
|
|
|
++next_position;
|
|
|
|
|
|
|
|
if (next_position > std::numeric_limits<IndexType>::max())
|
|
|
|
{
|
|
|
|
if (sizeof(SuperiorIndexType) == sizeof(IndexType))
|
|
|
|
throw Exception("Can't find superior index type for type " + demangle(typeid(IndexType).name()),
|
|
|
|
ErrorCodes::LOGICAL_ERROR);
|
|
|
|
|
2018-07-09 18:19:03 +00:00
|
|
|
auto expanded_column = ColumnVector<SuperiorIndexType>::create(length);
|
2018-06-28 18:12:29 +00:00
|
|
|
auto & expanded_data = expanded_column->getData();
|
|
|
|
for (size_t i = 0; i < num_added_rows; ++i)
|
|
|
|
expanded_data[i] = positions[i];
|
|
|
|
|
|
|
|
return uniqueInsertRangeImpl<SuperiorIndexType>(
|
|
|
|
src,
|
2018-07-09 18:19:03 +00:00
|
|
|
start,
|
|
|
|
length,
|
|
|
|
num_added_rows,
|
2018-06-28 18:12:29 +00:00
|
|
|
std::move(expanded_column),
|
2018-07-09 18:19:03 +00:00
|
|
|
secondary_index,
|
2018-06-28 18:12:29 +00:00
|
|
|
max_dictionary_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
};
|
2018-03-19 11:45:17 +00:00
|
|
|
|
2019-06-27 18:50:20 +00:00
|
|
|
if (auto * nullable_column = checkAndGetColumn<ColumnNullable>(src))
|
2018-03-19 11:45:17 +00:00
|
|
|
{
|
2018-07-09 18:19:03 +00:00
|
|
|
src_column = typeid_cast<const ColumnType *>(&nullable_column->getNestedColumn());
|
2018-03-19 11:45:17 +00:00
|
|
|
null_map = &nullable_column->getNullMapData();
|
|
|
|
}
|
|
|
|
else
|
2018-07-09 18:19:03 +00:00
|
|
|
src_column = typeid_cast<const ColumnType *>(&src);
|
2018-03-19 11:45:17 +00:00
|
|
|
|
2018-07-09 18:19:03 +00:00
|
|
|
if (src_column == nullptr)
|
|
|
|
throw Exception("Invalid column type for ColumnUnique::insertRangeFrom. Expected " + column_holder->getName() +
|
|
|
|
", got " + src.getName(), ErrorCodes::ILLEGAL_COLUMN);
|
2018-06-07 18:14:37 +00:00
|
|
|
|
2018-03-19 11:45:17 +00:00
|
|
|
auto column = getRawColumnPtr();
|
|
|
|
|
2018-06-28 18:12:29 +00:00
|
|
|
UInt64 next_position = column->size();
|
2018-07-09 18:19:03 +00:00
|
|
|
if (secondary_index)
|
|
|
|
next_position += secondary_index->size();
|
|
|
|
|
2018-12-12 19:21:48 +00:00
|
|
|
auto insert_key = [&](const StringRef & ref, ReverseIndex<UInt64, ColumnType> & cur_index) -> MutableColumnPtr
|
2018-08-02 15:16:40 +00:00
|
|
|
{
|
2018-12-12 19:21:48 +00:00
|
|
|
auto inserted_pos = cur_index.insert(ref);
|
|
|
|
positions[num_added_rows] = inserted_pos;
|
|
|
|
if (inserted_pos == next_position)
|
|
|
|
return update_position(next_position);
|
2018-08-02 15:16:40 +00:00
|
|
|
|
2018-12-12 19:21:48 +00:00
|
|
|
return nullptr;
|
2018-08-02 15:16:40 +00:00
|
|
|
};
|
|
|
|
|
2018-07-09 18:19:03 +00:00
|
|
|
for (; num_added_rows < length; ++num_added_rows)
|
2018-03-19 11:45:17 +00:00
|
|
|
{
|
2018-07-09 18:19:03 +00:00
|
|
|
auto row = start + num_added_rows;
|
2018-03-19 11:45:17 +00:00
|
|
|
|
2018-04-17 12:41:16 +00:00
|
|
|
if (null_map && (*null_map)[row])
|
2018-07-09 18:19:03 +00:00
|
|
|
positions[num_added_rows] = getNullValueIndex();
|
2018-09-19 11:59:40 +00:00
|
|
|
else if (column->compareAt(getNestedTypeDefaultValueIndex(), row, *src_column, 1) == 0)
|
|
|
|
positions[num_added_rows] = getNestedTypeDefaultValueIndex();
|
2018-03-19 11:45:17 +00:00
|
|
|
else
|
|
|
|
{
|
2018-08-02 15:16:40 +00:00
|
|
|
auto ref = src_column->getDataAt(row);
|
2018-12-12 19:21:48 +00:00
|
|
|
MutableColumnPtr res = nullptr;
|
2018-08-02 15:16:40 +00:00
|
|
|
|
2018-12-12 19:21:48 +00:00
|
|
|
if (secondary_index && next_position >= max_dictionary_size)
|
2018-03-19 11:45:17 +00:00
|
|
|
{
|
2018-12-12 19:21:48 +00:00
|
|
|
auto insertion_point = index.getInsertionPoint(ref);
|
|
|
|
if (insertion_point == index.lastInsertionPoint())
|
|
|
|
res = insert_key(ref, *secondary_index);
|
2018-08-02 15:16:40 +00:00
|
|
|
else
|
2018-12-12 19:21:48 +00:00
|
|
|
positions[num_added_rows] = insertion_point;
|
2018-03-19 11:45:17 +00:00
|
|
|
}
|
2018-12-12 19:21:48 +00:00
|
|
|
else
|
|
|
|
res = insert_key(ref, index);
|
|
|
|
|
|
|
|
if (res)
|
|
|
|
return res;
|
2018-03-19 11:45:17 +00:00
|
|
|
}
|
|
|
|
}
|
2018-06-28 18:12:29 +00:00
|
|
|
|
2018-07-20 16:51:44 +00:00
|
|
|
// checkIndexes(*positions_column, column->size() + (overflowed_keys ? overflowed_keys->size() : 0));
|
2018-07-09 18:19:03 +00:00
|
|
|
return std::move(positions_column);
|
2018-06-07 18:14:37 +00:00
|
|
|
}
|
|
|
|
|
2018-06-28 18:12:29 +00:00
|
|
|
template <typename ColumnType>
|
|
|
|
MutableColumnPtr ColumnUnique<ColumnType>::uniqueInsertRangeFrom(const IColumn & src, size_t start, size_t length)
|
2018-06-07 18:14:37 +00:00
|
|
|
{
|
2018-07-09 18:19:03 +00:00
|
|
|
auto callForType = [this, &src, start, length](auto x) -> MutableColumnPtr
|
2018-06-28 18:12:29 +00:00
|
|
|
{
|
2018-07-09 18:19:03 +00:00
|
|
|
size_t size = getRawColumnPtr()->size();
|
|
|
|
|
2018-06-28 18:12:29 +00:00
|
|
|
using IndexType = decltype(x);
|
|
|
|
if (size <= std::numeric_limits<IndexType>::max())
|
|
|
|
{
|
2018-07-09 18:19:03 +00:00
|
|
|
auto positions = ColumnVector<IndexType>::create(length);
|
2018-08-02 15:16:40 +00:00
|
|
|
return this->uniqueInsertRangeImpl<IndexType>(src, start, length, 0, std::move(positions), nullptr, 0);
|
2018-06-28 18:12:29 +00:00
|
|
|
}
|
2018-06-07 18:14:37 +00:00
|
|
|
|
2018-06-28 18:12:29 +00:00
|
|
|
return nullptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
MutableColumnPtr positions_column;
|
|
|
|
if (!positions_column)
|
|
|
|
positions_column = callForType(UInt8());
|
|
|
|
if (!positions_column)
|
|
|
|
positions_column = callForType(UInt16());
|
|
|
|
if (!positions_column)
|
|
|
|
positions_column = callForType(UInt32());
|
|
|
|
if (!positions_column)
|
|
|
|
positions_column = callForType(UInt64());
|
|
|
|
if (!positions_column)
|
|
|
|
throw Exception("Can't find index type for ColumnUnique", ErrorCodes::LOGICAL_ERROR);
|
2018-03-19 11:45:17 +00:00
|
|
|
|
2018-11-23 08:11:54 +00:00
|
|
|
updateNullMask();
|
|
|
|
|
2018-03-19 11:45:17 +00:00
|
|
|
return positions_column;
|
|
|
|
}
|
|
|
|
|
2018-06-28 18:12:29 +00:00
|
|
|
template <typename ColumnType>
|
|
|
|
IColumnUnique::IndexesWithOverflow ColumnUnique<ColumnType>::uniqueInsertRangeWithOverflow(
|
2018-06-07 18:14:37 +00:00
|
|
|
const IColumn & src,
|
|
|
|
size_t start,
|
|
|
|
size_t length,
|
|
|
|
size_t max_dictionary_size)
|
|
|
|
{
|
|
|
|
auto overflowed_keys = column_holder->cloneEmpty();
|
|
|
|
auto overflowed_keys_ptr = typeid_cast<ColumnType *>(overflowed_keys.get());
|
|
|
|
if (!overflowed_keys_ptr)
|
|
|
|
throw Exception("Invalid keys type for ColumnUnique.", ErrorCodes::LOGICAL_ERROR);
|
|
|
|
|
2018-07-09 18:19:03 +00:00
|
|
|
auto callForType = [this, &src, start, length, overflowed_keys_ptr, max_dictionary_size](auto x) -> MutableColumnPtr
|
2018-06-28 18:12:29 +00:00
|
|
|
{
|
2018-07-09 18:19:03 +00:00
|
|
|
size_t size = getRawColumnPtr()->size();
|
|
|
|
|
2018-06-28 18:12:29 +00:00
|
|
|
using IndexType = decltype(x);
|
|
|
|
if (size <= std::numeric_limits<IndexType>::max())
|
|
|
|
{
|
2018-07-09 18:19:03 +00:00
|
|
|
auto positions = ColumnVector<IndexType>::create(length);
|
2018-08-02 15:16:40 +00:00
|
|
|
ReverseIndex<UInt64, ColumnType> secondary_index(0, max_dictionary_size);
|
|
|
|
secondary_index.setColumn(overflowed_keys_ptr);
|
2018-07-09 18:19:03 +00:00
|
|
|
return this->uniqueInsertRangeImpl<IndexType>(src, start, length, 0, std::move(positions),
|
2018-08-02 15:16:40 +00:00
|
|
|
&secondary_index, max_dictionary_size);
|
2018-06-28 18:12:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
MutableColumnPtr positions_column;
|
|
|
|
if (!positions_column)
|
|
|
|
positions_column = callForType(UInt8());
|
|
|
|
if (!positions_column)
|
|
|
|
positions_column = callForType(UInt16());
|
|
|
|
if (!positions_column)
|
|
|
|
positions_column = callForType(UInt32());
|
|
|
|
if (!positions_column)
|
|
|
|
positions_column = callForType(UInt64());
|
|
|
|
if (!positions_column)
|
|
|
|
throw Exception("Can't find index type for ColumnUnique", ErrorCodes::LOGICAL_ERROR);
|
2018-06-07 18:14:37 +00:00
|
|
|
|
2018-11-23 08:11:54 +00:00
|
|
|
updateNullMask();
|
|
|
|
|
2018-06-07 18:14:37 +00:00
|
|
|
IColumnUnique::IndexesWithOverflow indexes_with_overflow;
|
|
|
|
indexes_with_overflow.indexes = std::move(positions_column);
|
|
|
|
indexes_with_overflow.overflowed_keys = std::move(overflowed_keys);
|
|
|
|
return indexes_with_overflow;
|
|
|
|
}
|
|
|
|
|
2018-09-07 10:08:09 +00:00
|
|
|
template <typename ColumnType>
|
|
|
|
UInt128 ColumnUnique<ColumnType>::IncrementalHash::getHash(const ColumnType & column)
|
|
|
|
{
|
|
|
|
size_t column_size = column.size();
|
2018-09-12 13:27:00 +00:00
|
|
|
UInt128 cur_hash;
|
2018-09-07 10:08:09 +00:00
|
|
|
|
|
|
|
if (column_size != num_added_rows.load())
|
|
|
|
{
|
|
|
|
SipHash sip_hash;
|
|
|
|
for (size_t i = 0; i < column_size; ++i)
|
|
|
|
column.updateHashWithValue(i, sip_hash);
|
|
|
|
|
|
|
|
std::lock_guard lock(mutex);
|
2018-09-12 13:27:00 +00:00
|
|
|
sip_hash.get128(hash.low, hash.high);
|
|
|
|
cur_hash = hash;
|
|
|
|
num_added_rows.store(column_size);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::lock_guard lock(mutex);
|
|
|
|
cur_hash = hash;
|
2018-09-07 10:08:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return cur_hash;
|
|
|
|
}
|
|
|
|
|
2018-08-26 00:44:23 +00:00
|
|
|
}
|