2018-08-27 16:16:16 +00:00
|
|
|
#pragma once
|
|
|
|
|
2022-01-27 10:07:53 +00:00
|
|
|
#include <cmath>
|
|
|
|
|
|
|
|
#include <base/sort.h>
|
|
|
|
#include <base/TypeName.h>
|
2019-09-27 13:44:33 +00:00
|
|
|
#include <Core/Field.h>
|
2020-11-14 13:50:04 +00:00
|
|
|
#include <Core/DecimalFunctions.h>
|
2021-09-10 11:49:22 +00:00
|
|
|
#include <Core/TypeId.h>
|
2022-01-27 10:07:53 +00:00
|
|
|
#include <Common/typeid_cast.h>
|
|
|
|
#include <Columns/ColumnVectorHelper.h>
|
|
|
|
#include <Columns/IColumn.h>
|
|
|
|
#include <Columns/IColumnImpl.h>
|
2018-08-27 16:16:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
2021-09-30 20:42:50 +00:00
|
|
|
|
2018-08-27 16:16:16 +00:00
|
|
|
/// A ColumnVector for Decimals
|
2021-09-10 11:49:22 +00:00
|
|
|
template <is_decimal T>
|
2019-04-19 20:21:17 +00:00
|
|
|
class ColumnDecimal final : public COWHelper<ColumnVectorHelper, ColumnDecimal<T>>
|
2018-08-27 16:16:16 +00:00
|
|
|
{
|
|
|
|
private:
|
2018-09-13 14:59:03 +00:00
|
|
|
using Self = ColumnDecimal;
|
2019-04-19 20:21:17 +00:00
|
|
|
friend class COWHelper<ColumnVectorHelper, Self>;
|
2018-08-27 16:16:16 +00:00
|
|
|
|
|
|
|
public:
|
2019-11-20 08:57:19 +00:00
|
|
|
using ValueType = T;
|
2020-08-19 11:52:17 +00:00
|
|
|
using NativeT = typename T::NativeType;
|
2022-01-27 10:07:53 +00:00
|
|
|
using Container = PaddedPODArray<T>;
|
2018-08-27 16:16:16 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
ColumnDecimal(const size_t n, UInt32 scale_)
|
2022-01-27 10:07:53 +00:00
|
|
|
: data(n),
|
2018-08-27 16:16:16 +00:00
|
|
|
scale(scale_)
|
|
|
|
{}
|
|
|
|
|
|
|
|
ColumnDecimal(const ColumnDecimal & src)
|
2022-01-27 10:07:53 +00:00
|
|
|
: data(src.data.begin(), src.data.end()),
|
2018-08-27 16:16:16 +00:00
|
|
|
scale(src.scale)
|
|
|
|
{}
|
|
|
|
|
|
|
|
public:
|
2021-10-03 15:36:08 +00:00
|
|
|
const char * getFamilyName() const override { return TypeName<T>.data(); }
|
2022-01-20 13:51:00 +00:00
|
|
|
TypeIndex getDataType() const override { return TypeToTypeIndex<T>; }
|
2018-08-27 16:16:16 +00:00
|
|
|
|
|
|
|
bool isNumeric() const override { return false; }
|
2018-09-04 18:51:44 +00:00
|
|
|
bool canBeInsideNullable() const override { return true; }
|
2020-11-05 09:38:46 +00:00
|
|
|
bool isFixedAndContiguous() const final { return true; }
|
2018-08-27 16:16:16 +00:00
|
|
|
size_t sizeOfValueIfFixed() const override { return sizeof(T); }
|
|
|
|
|
|
|
|
size_t size() const override { return data.size(); }
|
|
|
|
size_t byteSize() const override { return data.size() * sizeof(data[0]); }
|
2021-01-02 22:58:10 +00:00
|
|
|
size_t byteSizeAt(size_t) const override { return sizeof(data[0]); }
|
2020-11-05 09:47:04 +00:00
|
|
|
size_t allocatedBytes() const override { return data.allocated_bytes(); }
|
|
|
|
void protect() override { data.protect(); }
|
2018-08-27 16:16:16 +00:00
|
|
|
void reserve(size_t n) override { data.reserve(n); }
|
|
|
|
|
|
|
|
void insertFrom(const IColumn & src, size_t n) override { data.push_back(static_cast<const Self &>(src).getData()[n]); }
|
2020-03-09 01:03:43 +00:00
|
|
|
void insertData(const char * src, size_t /*length*/) override;
|
2018-08-27 16:16:16 +00:00
|
|
|
void insertDefault() override { data.push_back(T()); }
|
2020-08-19 11:52:17 +00:00
|
|
|
virtual void insertManyDefaults(size_t length) override
|
|
|
|
{
|
2020-11-05 09:47:04 +00:00
|
|
|
data.resize_fill(data.size() + length);
|
2020-08-19 11:52:17 +00:00
|
|
|
}
|
2021-03-22 20:23:44 +00:00
|
|
|
void insert(const Field & x) override { data.push_back(DB::get<T>(x)); }
|
2018-08-27 16:16:16 +00:00
|
|
|
void insertRangeFrom(const IColumn & src, size_t start, size_t length) override;
|
|
|
|
|
2020-08-19 11:52:17 +00:00
|
|
|
void popBack(size_t n) override
|
|
|
|
{
|
2020-11-05 09:47:04 +00:00
|
|
|
data.resize_assume_reserved(data.size() - n);
|
2020-08-19 11:52:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
StringRef getRawData() const override
|
|
|
|
{
|
2020-11-05 09:47:04 +00:00
|
|
|
return StringRef(reinterpret_cast<const char*>(data.data()), byteSize());
|
2020-08-19 11:52:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
StringRef getDataAt(size_t n) const override
|
|
|
|
{
|
2020-11-05 09:47:04 +00:00
|
|
|
return StringRef(reinterpret_cast<const char *>(&data[n]), sizeof(data[n]));
|
2020-08-19 11:52:17 +00:00
|
|
|
}
|
2018-08-27 16:16:16 +00:00
|
|
|
|
2020-10-26 13:27:58 +00:00
|
|
|
Float64 getFloat64(size_t n) const final { return DecimalUtils::convertTo<Float64>(data[n], scale); }
|
|
|
|
|
2018-08-27 16:16:16 +00:00
|
|
|
StringRef serializeValueIntoArena(size_t n, Arena & arena, char const *& begin) const override;
|
|
|
|
const char * deserializeAndInsertFromArena(const char * pos) override;
|
2021-02-16 21:26:06 +00:00
|
|
|
const char * skipSerializedInArena(const char * pos) const override;
|
2018-08-27 16:16:16 +00:00
|
|
|
void updateHashWithValue(size_t n, SipHash & hash) const override;
|
2020-03-18 16:03:55 +00:00
|
|
|
void updateWeakHash32(WeakHash32 & hash) const override;
|
2020-05-20 22:16:08 +00:00
|
|
|
void updateHashFast(SipHash & hash) const override;
|
2018-08-27 16:16:16 +00:00
|
|
|
int compareAt(size_t n, size_t m, const IColumn & rhs_, int nan_direction_hint) const override;
|
2020-06-02 00:23:41 +00:00
|
|
|
void compareColumn(const IColumn & rhs, size_t rhs_row_num,
|
2020-06-17 11:43:55 +00:00
|
|
|
PaddedPODArray<UInt64> * row_indexes, PaddedPODArray<Int8> & compare_results,
|
2020-06-02 00:23:41 +00:00
|
|
|
int direction, int nan_direction_hint) const override;
|
2021-02-26 04:50:04 +00:00
|
|
|
bool hasEqualValues() const override;
|
2022-02-23 17:34:19 +00:00
|
|
|
void getPermutation(IColumn::PermutationSortDirection direction, IColumn::PermutationSortStability stability,
|
|
|
|
size_t limit, int nan_direction_hint, IColumn::Permutation & res) const override;
|
|
|
|
void updatePermutation(IColumn::PermutationSortDirection direction, IColumn::PermutationSortStability stability,
|
|
|
|
size_t limit, int, IColumn::Permutation & res, EqualRanges& equal_ranges) const override;
|
2018-08-27 16:16:16 +00:00
|
|
|
|
|
|
|
MutableColumnPtr cloneResized(size_t size) const override;
|
|
|
|
|
|
|
|
Field operator[](size_t n) const override { return DecimalField(data[n], scale); }
|
|
|
|
void get(size_t n, Field & res) const override { res = (*this)[n]; }
|
2020-08-19 11:52:17 +00:00
|
|
|
bool getBool(size_t n) const override { return bool(data[n].value); }
|
2022-07-03 22:26:07 +00:00
|
|
|
Int64 getInt(size_t n) const override { return Int64(data[n].value); }
|
2018-10-09 16:50:42 +00:00
|
|
|
UInt64 get64(size_t n) const override;
|
2020-08-19 11:52:17 +00:00
|
|
|
bool isDefaultAt(size_t n) const override { return data[n].value == 0; }
|
2018-08-27 16:16:16 +00:00
|
|
|
|
|
|
|
ColumnPtr filter(const IColumn::Filter & filt, ssize_t result_size_hint) const override;
|
2021-06-07 10:55:55 +00:00
|
|
|
void expand(const IColumn::Filter & mask, bool inverted) override;
|
2021-04-27 12:49:58 +00:00
|
|
|
|
2019-02-18 19:44:26 +00:00
|
|
|
ColumnPtr permute(const IColumn::Permutation & perm, size_t limit) const override;
|
2019-02-18 17:28:53 +00:00
|
|
|
ColumnPtr index(const IColumn & indexes, size_t limit) const override;
|
2018-08-27 16:16:16 +00:00
|
|
|
|
|
|
|
template <typename Type>
|
2019-02-18 17:28:53 +00:00
|
|
|
ColumnPtr indexImpl(const PaddedPODArray<Type> & indexes, size_t limit) const;
|
2018-08-27 16:16:16 +00:00
|
|
|
|
|
|
|
ColumnPtr replicate(const IColumn::Offsets & offsets) const override;
|
|
|
|
void getExtremes(Field & min, Field & max) const override;
|
|
|
|
|
|
|
|
MutableColumns scatter(IColumn::ColumnIndex num_columns, const IColumn::Selector & selector) const override
|
|
|
|
{
|
|
|
|
return this->template scatterImpl<Self>(num_columns, selector);
|
|
|
|
}
|
|
|
|
|
|
|
|
void gather(ColumnGathererStream & gatherer_stream) override;
|
|
|
|
|
2019-03-14 23:10:51 +00:00
|
|
|
bool structureEquals(const IColumn & rhs) const override
|
|
|
|
{
|
|
|
|
if (auto rhs_concrete = typeid_cast<const ColumnDecimal<T> *>(&rhs))
|
|
|
|
return scale == rhs_concrete->scale;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-06-07 13:50:27 +00:00
|
|
|
double getRatioOfDefaultRows(double sample_ratio) const override
|
|
|
|
{
|
|
|
|
return this->template getRatioOfDefaultRowsImpl<Self>(sample_ratio);
|
|
|
|
}
|
2021-02-17 23:09:46 +00:00
|
|
|
|
2021-06-07 15:34:22 +00:00
|
|
|
void getIndicesOfNonDefaultRows(IColumn::Offsets & indices, size_t from, size_t limit) const override
|
|
|
|
{
|
|
|
|
return this->template getIndicesOfNonDefaultRowsImpl<Self>(indices, from, limit);
|
|
|
|
}
|
2021-04-03 00:04:48 +00:00
|
|
|
|
2021-06-07 13:50:27 +00:00
|
|
|
ColumnPtr compress() const override;
|
2018-08-27 16:16:16 +00:00
|
|
|
|
2019-11-11 16:18:37 +00:00
|
|
|
void insertValue(const T value) { data.push_back(value); }
|
2020-11-05 10:11:56 +00:00
|
|
|
Container & getData() { return data; }
|
|
|
|
const Container & getData() const { return data; }
|
2018-08-27 16:16:16 +00:00
|
|
|
const T & getElement(size_t n) const { return data[n]; }
|
|
|
|
T & getElement(size_t n) { return data[n]; }
|
|
|
|
|
2022-01-27 10:07:53 +00:00
|
|
|
UInt32 getScale() const { return scale; }
|
2020-05-08 14:59:58 +00:00
|
|
|
|
2018-08-27 16:16:16 +00:00
|
|
|
protected:
|
2020-11-05 10:11:56 +00:00
|
|
|
Container data;
|
2018-08-27 16:16:16 +00:00
|
|
|
UInt32 scale;
|
|
|
|
};
|
|
|
|
|
2021-09-10 11:49:22 +00:00
|
|
|
template <class> class ColumnVector;
|
|
|
|
template <class T> struct ColumnVectorOrDecimalT { using Col = ColumnVector<T>; };
|
|
|
|
template <is_decimal T> struct ColumnVectorOrDecimalT<T> { using Col = ColumnDecimal<T>; };
|
|
|
|
template <class T> using ColumnVectorOrDecimal = typename ColumnVectorOrDecimalT<T>::Col;
|
|
|
|
|
|
|
|
template <is_decimal T>
|
2018-08-27 16:16:16 +00:00
|
|
|
template <typename Type>
|
2019-02-18 17:28:53 +00:00
|
|
|
ColumnPtr ColumnDecimal<T>::indexImpl(const PaddedPODArray<Type> & indexes, size_t limit) const
|
2018-08-27 16:16:16 +00:00
|
|
|
{
|
2021-09-29 17:51:58 +00:00
|
|
|
assert(limit <= indexes.size());
|
2018-08-27 16:16:16 +00:00
|
|
|
|
|
|
|
auto res = this->create(limit, scale);
|
|
|
|
typename Self::Container & res_data = res->getData();
|
|
|
|
for (size_t i = 0; i < limit; ++i)
|
|
|
|
res_data[i] = data[indexes[i]];
|
|
|
|
|
2019-05-31 08:55:01 +00:00
|
|
|
return res;
|
2018-08-27 16:16:16 +00:00
|
|
|
}
|
|
|
|
|
2021-02-12 09:12:04 +00:00
|
|
|
|
|
|
|
/// Prevent implicit template instantiation of ColumnDecimal for common decimal types
|
|
|
|
|
|
|
|
extern template class ColumnDecimal<Decimal32>;
|
|
|
|
extern template class ColumnDecimal<Decimal64>;
|
|
|
|
extern template class ColumnDecimal<Decimal128>;
|
|
|
|
extern template class ColumnDecimal<Decimal256>;
|
|
|
|
extern template class ColumnDecimal<DateTime64>;
|
|
|
|
|
|
|
|
|
2018-08-27 16:16:16 +00:00
|
|
|
}
|