2018-08-27 16:16:16 +00:00
|
|
|
#include <Common/Exception.h>
|
|
|
|
#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>
|
2020-03-18 16:40:23 +00:00
|
|
|
#include <Common/HashTable/Hash.h>
|
2018-08-27 16:16:16 +00:00
|
|
|
|
2018-12-14 17:50:10 +00:00
|
|
|
#include <common/unaligned.h>
|
|
|
|
|
2018-08-27 16:16:16 +00:00
|
|
|
#include <IO/WriteHelpers.h>
|
|
|
|
|
|
|
|
#include <Columns/ColumnsCommon.h>
|
|
|
|
#include <Columns/ColumnDecimal.h>
|
|
|
|
#include <DataStreams/ColumnGathererStream.h>
|
|
|
|
|
2018-12-14 17:50:10 +00:00
|
|
|
|
2018-08-27 16:16:16 +00:00
|
|
|
template <typename T> bool decimalLess(T x, T y, UInt32 x_scale, UInt32 y_scale);
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int PARAMETER_OUT_OF_BOUND;
|
|
|
|
extern const int SIZES_OF_COLUMNS_DOESNT_MATCH;
|
2018-10-09 16:50:42 +00:00
|
|
|
extern const int NOT_IMPLEMENTED;
|
2020-03-19 17:35:08 +00:00
|
|
|
extern const int LOGICAL_ERROR;
|
2018-08-27 16:16:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2018-11-24 01:48:06 +00:00
|
|
|
int ColumnDecimal<T>::compareAt(size_t n, size_t m, const IColumn & rhs_, int) const
|
2018-08-27 16:16:16 +00:00
|
|
|
{
|
2019-08-07 13:53:46 +00:00
|
|
|
auto & other = static_cast<const Self &>(rhs_);
|
2018-08-27 16:16:16 +00:00
|
|
|
const T & a = data[n];
|
2018-08-29 18:47:19 +00:00
|
|
|
const T & b = other.data[m];
|
2018-08-27 16:16:16 +00:00
|
|
|
|
2019-08-07 14:12:57 +00:00
|
|
|
if (scale == other.scale)
|
|
|
|
return a > b ? 1 : (a < b ? -1 : 0);
|
2018-08-27 16:16:16 +00:00
|
|
|
return decimalLess<T>(b, a, other.scale, scale) ? 1 : (decimalLess<T>(a, b, scale, other.scale) ? -1 : 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
StringRef ColumnDecimal<T>::serializeValueIntoArena(size_t n, Arena & arena, char const *& begin) const
|
|
|
|
{
|
2020-04-22 06:22:14 +00:00
|
|
|
auto * pos = arena.allocContinue(sizeof(T), begin);
|
2018-08-27 16:16:16 +00:00
|
|
|
memcpy(pos, &data[n], sizeof(T));
|
|
|
|
return StringRef(pos, sizeof(T));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
const char * ColumnDecimal<T>::deserializeAndInsertFromArena(const char * pos)
|
|
|
|
{
|
2018-12-14 17:50:10 +00:00
|
|
|
data.push_back(unalignedLoad<T>(pos));
|
2018-08-27 16:16:16 +00:00
|
|
|
return pos + sizeof(T);
|
|
|
|
}
|
|
|
|
|
2018-10-09 16:50:42 +00:00
|
|
|
template <typename T>
|
|
|
|
UInt64 ColumnDecimal<T>::get64(size_t n) const
|
|
|
|
{
|
|
|
|
if constexpr (sizeof(T) > sizeof(UInt64))
|
|
|
|
throw Exception(String("Method get64 is not supported for ") + getFamilyName(), ErrorCodes::NOT_IMPLEMENTED);
|
2018-10-11 14:33:01 +00:00
|
|
|
return static_cast<typename T::NativeType>(data[n]);
|
2018-10-09 16:50:42 +00:00
|
|
|
}
|
|
|
|
|
2018-08-27 16:16:16 +00:00
|
|
|
template <typename T>
|
|
|
|
void ColumnDecimal<T>::updateHashWithValue(size_t n, SipHash & hash) const
|
|
|
|
{
|
|
|
|
hash.update(data[n]);
|
|
|
|
}
|
|
|
|
|
2020-03-18 16:03:55 +00:00
|
|
|
template <typename T>
|
|
|
|
void ColumnDecimal<T>::updateWeakHash32(WeakHash32 & hash) const
|
|
|
|
{
|
|
|
|
auto s = data.size();
|
|
|
|
|
|
|
|
if (hash.getData().size() != s)
|
|
|
|
throw Exception("Size of WeakHash32 does not match size of column: column size is " + std::to_string(s) +
|
|
|
|
", hash size is " + std::to_string(hash.getData().size()), ErrorCodes::LOGICAL_ERROR);
|
|
|
|
|
2020-03-25 11:14:11 +00:00
|
|
|
const T * begin = data.data();
|
2020-03-18 16:03:55 +00:00
|
|
|
const T * end = begin + s;
|
2020-03-25 11:14:11 +00:00
|
|
|
UInt32 * hash_data = hash.getData().data();
|
2020-03-18 16:03:55 +00:00
|
|
|
|
|
|
|
while (begin < end)
|
|
|
|
{
|
2020-03-23 19:18:35 +00:00
|
|
|
*hash_data = intHashCRC32(*begin, *hash_data);
|
2020-03-18 16:03:55 +00:00
|
|
|
++begin;
|
|
|
|
++hash_data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-27 16:16:16 +00:00
|
|
|
template <typename T>
|
2019-02-18 19:44:26 +00:00
|
|
|
void ColumnDecimal<T>::getPermutation(bool reverse, size_t limit, int , IColumn::Permutation & res) const
|
2018-08-27 16:16:16 +00:00
|
|
|
{
|
2018-08-29 18:47:19 +00:00
|
|
|
#if 1 /// TODO: perf test
|
|
|
|
if (data.size() <= std::numeric_limits<UInt32>::max())
|
2018-08-27 16:16:16 +00:00
|
|
|
{
|
2018-08-29 18:47:19 +00:00
|
|
|
PaddedPODArray<UInt32> tmp_res;
|
|
|
|
permutation(reverse, limit, tmp_res);
|
|
|
|
|
|
|
|
res.resize(tmp_res.size());
|
|
|
|
for (size_t i = 0; i < tmp_res.size(); ++i)
|
|
|
|
res[i] = tmp_res[i];
|
|
|
|
return;
|
2018-08-27 16:16:16 +00:00
|
|
|
}
|
2018-08-29 18:47:19 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
permutation(reverse, limit, res);
|
|
|
|
}
|
|
|
|
|
2020-05-12 00:58:58 +00:00
|
|
|
template <typename T>
|
|
|
|
void ColumnDecimal<T>::updatePermutation(bool reverse, size_t limit, int, IColumn::Permutation & res, EqualRanges& equal_range) const
|
|
|
|
{
|
2020-05-14 21:00:56 +00:00
|
|
|
if (limit >= data.size() || limit >= equal_range.back().second)
|
|
|
|
{
|
2020-05-12 00:58:58 +00:00
|
|
|
limit = 0;
|
|
|
|
}
|
|
|
|
size_t n = equal_range.size();
|
2020-05-14 21:00:56 +00:00
|
|
|
if (limit)
|
|
|
|
{
|
2020-05-12 00:58:58 +00:00
|
|
|
--n;
|
|
|
|
}
|
|
|
|
EqualRanges new_ranges;
|
2020-05-14 21:00:56 +00:00
|
|
|
for (size_t i = 0; i < n; ++i)
|
|
|
|
{
|
2020-05-12 00:58:58 +00:00
|
|
|
const auto& [first, last] = equal_range[i];
|
|
|
|
if (reverse)
|
|
|
|
std::partial_sort(res.begin() + first, res.begin() + last, res.begin() + last, [this](size_t a, size_t b) { return data[a] > data[b]; });
|
|
|
|
else
|
|
|
|
std::partial_sort(res.begin() + first, res.begin() + last, res.begin() + last, [this](size_t a, size_t b) { return data[a] < data[b]; });
|
|
|
|
auto new_first = first;
|
2020-05-14 21:00:56 +00:00
|
|
|
for (auto j = first + 1; j < last; ++j)
|
|
|
|
{
|
|
|
|
if (data[res[new_first]] != data[res[j]])
|
|
|
|
{
|
|
|
|
if (j - new_first > 1)
|
|
|
|
{
|
2020-05-12 00:58:58 +00:00
|
|
|
new_ranges.emplace_back(new_first, j);
|
|
|
|
}
|
|
|
|
new_first = j;
|
|
|
|
}
|
|
|
|
}
|
2020-05-14 21:00:56 +00:00
|
|
|
if (last - new_first > 1)
|
|
|
|
{
|
2020-05-12 00:58:58 +00:00
|
|
|
new_ranges.emplace_back(new_first, last);
|
|
|
|
}
|
|
|
|
}
|
2020-05-14 21:00:56 +00:00
|
|
|
if (limit)
|
|
|
|
{
|
2020-05-12 00:58:58 +00:00
|
|
|
const auto& [first, last] = equal_range.back();
|
|
|
|
if (reverse)
|
|
|
|
std::partial_sort(res.begin() + first, res.begin() + limit, res.begin() + last, [this](size_t a, size_t b) { return data[a] > data[b]; });
|
|
|
|
else
|
2020-05-16 21:59:05 +00:00
|
|
|
std::partial_sort(res.begin() + first, res.begin() + limit, res.begin() + last, [this](size_t a, size_t b) { return data[a] < data[b]; });
|
2020-05-12 00:58:58 +00:00
|
|
|
auto new_first = first;
|
2020-05-14 21:00:56 +00:00
|
|
|
for (auto j = first + 1; j < limit; ++j)
|
|
|
|
{
|
|
|
|
if (data[res[new_first]] != data[res[j]])
|
|
|
|
{
|
|
|
|
if (j - new_first > 1)
|
|
|
|
{
|
2020-05-12 00:58:58 +00:00
|
|
|
new_ranges.emplace_back(new_first, j);
|
|
|
|
}
|
|
|
|
new_first = j;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
auto new_last = limit;
|
2020-05-14 21:00:56 +00:00
|
|
|
for (auto j = limit; j < last; ++j)
|
|
|
|
{
|
|
|
|
if (data[res[new_first]] == data[res[j]])
|
|
|
|
{
|
2020-05-12 00:58:58 +00:00
|
|
|
std::swap(res[new_last], res[j]);
|
|
|
|
++new_last;
|
|
|
|
}
|
|
|
|
}
|
2020-05-14 21:00:56 +00:00
|
|
|
if (new_last - new_first > 1)
|
|
|
|
{
|
2020-05-12 00:58:58 +00:00
|
|
|
new_ranges.emplace_back(new_first, new_last);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
equal_range = std::move(new_ranges);
|
|
|
|
}
|
|
|
|
|
2018-08-29 18:47:19 +00:00
|
|
|
template <typename T>
|
2019-02-18 19:44:26 +00:00
|
|
|
ColumnPtr ColumnDecimal<T>::permute(const IColumn::Permutation & perm, size_t limit) const
|
2018-08-29 18:47:19 +00:00
|
|
|
{
|
|
|
|
size_t size = limit ? std::min(data.size(), limit) : data.size();
|
|
|
|
if (perm.size() < size)
|
|
|
|
throw Exception("Size of permutation is less than required.", ErrorCodes::SIZES_OF_COLUMNS_DOESNT_MATCH);
|
|
|
|
|
|
|
|
auto res = this->create(size, scale);
|
|
|
|
typename Self::Container & res_data = res->getData();
|
|
|
|
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
res_data[i] = data[perm[i]];
|
|
|
|
|
2019-05-31 08:55:01 +00:00
|
|
|
return res;
|
2018-08-27 16:16:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
MutableColumnPtr ColumnDecimal<T>::cloneResized(size_t size) const
|
|
|
|
{
|
|
|
|
auto res = this->create(0, scale);
|
|
|
|
|
|
|
|
if (size > 0)
|
|
|
|
{
|
|
|
|
auto & new_col = static_cast<Self &>(*res);
|
|
|
|
new_col.data.resize(size);
|
|
|
|
|
|
|
|
size_t count = std::min(this->size(), size);
|
2018-09-02 03:33:48 +00:00
|
|
|
memcpy(new_col.data.data(), data.data(), count * sizeof(data[0]));
|
2018-08-27 16:16:16 +00:00
|
|
|
|
|
|
|
if (size > count)
|
2018-08-29 18:47:19 +00:00
|
|
|
{
|
|
|
|
void * tail = &new_col.data[count];
|
|
|
|
memset(tail, 0, (size - count) * sizeof(T));
|
|
|
|
}
|
2018-08-27 16:16:16 +00:00
|
|
|
}
|
|
|
|
|
2019-05-31 08:55:01 +00:00
|
|
|
return res;
|
2018-08-27 16:16:16 +00:00
|
|
|
}
|
|
|
|
|
2018-10-15 19:14:08 +00:00
|
|
|
template <typename T>
|
2018-10-16 07:31:17 +00:00
|
|
|
void ColumnDecimal<T>::insertData(const char * src, size_t /*length*/)
|
2018-10-15 19:14:08 +00:00
|
|
|
{
|
2018-10-16 07:31:17 +00:00
|
|
|
T tmp;
|
|
|
|
memcpy(&tmp, src, sizeof(T));
|
|
|
|
data.emplace_back(tmp);
|
2018-10-15 19:14:08 +00:00
|
|
|
}
|
|
|
|
|
2018-08-27 16:16:16 +00:00
|
|
|
template <typename T>
|
|
|
|
void ColumnDecimal<T>::insertRangeFrom(const IColumn & src, size_t start, size_t length)
|
|
|
|
{
|
2019-08-21 02:28:04 +00:00
|
|
|
const ColumnDecimal & src_vec = assert_cast<const ColumnDecimal &>(src);
|
2018-08-27 16:16:16 +00:00
|
|
|
|
|
|
|
if (start + length > src_vec.data.size())
|
2018-08-29 18:47:19 +00:00
|
|
|
throw Exception("Parameters start = " + toString(start) + ", length = " + toString(length) +
|
|
|
|
" are out of bound in ColumnDecimal<T>::insertRangeFrom method (data.size() = " + toString(src_vec.data.size()) + ").",
|
2018-08-27 16:16:16 +00:00
|
|
|
ErrorCodes::PARAMETER_OUT_OF_BOUND);
|
|
|
|
|
|
|
|
size_t old_size = data.size();
|
|
|
|
data.resize(old_size + length);
|
2019-03-07 20:04:59 +00:00
|
|
|
memcpy(data.data() + old_size, &src_vec.data[start], length * sizeof(data[0]));
|
2018-08-27 16:16:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
ColumnPtr ColumnDecimal<T>::filter(const IColumn::Filter & filt, ssize_t result_size_hint) const
|
|
|
|
{
|
|
|
|
size_t size = data.size();
|
|
|
|
if (size != filt.size())
|
|
|
|
throw Exception("Size of filter doesn't match size of column.", ErrorCodes::SIZES_OF_COLUMNS_DOESNT_MATCH);
|
|
|
|
|
|
|
|
auto res = this->create(0, scale);
|
|
|
|
Container & res_data = res->getData();
|
|
|
|
|
|
|
|
if (result_size_hint)
|
|
|
|
res_data.reserve(result_size_hint > 0 ? result_size_hint : size);
|
|
|
|
|
2018-09-02 03:00:04 +00:00
|
|
|
const UInt8 * filt_pos = filt.data();
|
2018-08-27 16:16:16 +00:00
|
|
|
const UInt8 * filt_end = filt_pos + size;
|
2018-09-02 03:00:04 +00:00
|
|
|
const T * data_pos = data.data();
|
2018-08-27 16:16:16 +00:00
|
|
|
|
|
|
|
while (filt_pos < filt_end)
|
|
|
|
{
|
|
|
|
if (*filt_pos)
|
|
|
|
res_data.push_back(*data_pos);
|
|
|
|
|
|
|
|
++filt_pos;
|
|
|
|
++data_pos;
|
|
|
|
}
|
|
|
|
|
2019-05-31 08:55:01 +00:00
|
|
|
return res;
|
2018-08-27 16:16:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2019-02-18 17:28:53 +00:00
|
|
|
ColumnPtr ColumnDecimal<T>::index(const IColumn & indexes, size_t limit) const
|
2018-08-27 16:16:16 +00:00
|
|
|
{
|
|
|
|
return selectIndexImpl(*this, indexes, limit);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
ColumnPtr ColumnDecimal<T>::replicate(const IColumn::Offsets & offsets) const
|
|
|
|
{
|
|
|
|
size_t size = data.size();
|
|
|
|
if (size != offsets.size())
|
|
|
|
throw Exception("Size of offsets doesn't match size of column.", ErrorCodes::SIZES_OF_COLUMNS_DOESNT_MATCH);
|
|
|
|
|
|
|
|
auto res = this->create(0, scale);
|
|
|
|
if (0 == size)
|
|
|
|
return res;
|
|
|
|
|
|
|
|
typename Self::Container & res_data = res->getData();
|
|
|
|
res_data.reserve(offsets.back());
|
|
|
|
|
|
|
|
IColumn::Offset prev_offset = 0;
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
{
|
|
|
|
size_t size_to_replicate = offsets[i] - prev_offset;
|
|
|
|
prev_offset = offsets[i];
|
|
|
|
|
|
|
|
for (size_t j = 0; j < size_to_replicate; ++j)
|
|
|
|
res_data.push_back(data[i]);
|
|
|
|
}
|
|
|
|
|
2019-05-31 08:55:01 +00:00
|
|
|
return res;
|
2018-08-27 16:16:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void ColumnDecimal<T>::gather(ColumnGathererStream & gatherer)
|
|
|
|
{
|
|
|
|
gatherer.gather(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void ColumnDecimal<T>::getExtremes(Field & min, Field & max) const
|
|
|
|
{
|
2020-03-08 21:40:00 +00:00
|
|
|
if (data.empty())
|
2018-08-27 16:16:16 +00:00
|
|
|
{
|
2018-11-20 20:09:20 +00:00
|
|
|
min = NearestFieldType<T>(0, scale);
|
|
|
|
max = NearestFieldType<T>(0, scale);
|
2018-08-27 16:16:16 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
T cur_min = data[0];
|
|
|
|
T cur_max = data[0];
|
|
|
|
|
|
|
|
for (const T & x : data)
|
|
|
|
{
|
|
|
|
if (x < cur_min)
|
|
|
|
cur_min = x;
|
|
|
|
else if (x > cur_max)
|
|
|
|
cur_max = x;
|
|
|
|
}
|
|
|
|
|
2018-11-20 20:09:20 +00:00
|
|
|
min = NearestFieldType<T>(cur_min, scale);
|
|
|
|
max = NearestFieldType<T>(cur_max, scale);
|
2018-08-27 16:16:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template class ColumnDecimal<Decimal32>;
|
|
|
|
template class ColumnDecimal<Decimal64>;
|
|
|
|
template class ColumnDecimal<Decimal128>;
|
|
|
|
}
|