1#pragma once
2
3#include <cmath>
4
5#include <Common/typeid_cast.h>
6#include <Columns/IColumn.h>
7#include <Columns/IColumnImpl.h>
8#include <Columns/ColumnVectorHelper.h>
9#include <Core/Field.h>
10
11
12namespace DB
13{
14
15namespace ErrorCodes
16{
17 extern const int LOGICAL_ERROR;
18}
19
20/// PaddedPODArray extended by Decimal scale
21template <typename T>
22class DecimalPaddedPODArray : public PaddedPODArray<T>
23{
24public:
25 using Base = PaddedPODArray<T>;
26 using Base::operator[];
27
28 DecimalPaddedPODArray(size_t size, UInt32 scale_)
29 : Base(size),
30 scale(scale_)
31 {}
32
33 DecimalPaddedPODArray(const DecimalPaddedPODArray & other)
34 : Base(other.begin(), other.end()),
35 scale(other.scale)
36 {}
37
38 DecimalPaddedPODArray(DecimalPaddedPODArray && other)
39 {
40 this->swap(other);
41 std::swap(scale, other.scale);
42 }
43
44 DecimalPaddedPODArray & operator=(DecimalPaddedPODArray && other)
45 {
46 this->swap(other);
47 std::swap(scale, other.scale);
48 return *this;
49 }
50
51 UInt32 getScale() const { return scale; }
52
53private:
54 UInt32 scale;
55};
56
57
58/// A ColumnVector for Decimals
59template <typename T>
60class ColumnDecimal final : public COWHelper<ColumnVectorHelper, ColumnDecimal<T>>
61{
62 static_assert(IsDecimalNumber<T>);
63
64private:
65 using Self = ColumnDecimal;
66 friend class COWHelper<ColumnVectorHelper, Self>;
67
68public:
69 using ValueType = T;
70 using Container = DecimalPaddedPODArray<T>;
71
72private:
73 ColumnDecimal(const size_t n, UInt32 scale_)
74 : data(n, scale_),
75 scale(scale_)
76 {}
77
78 ColumnDecimal(const ColumnDecimal & src)
79 : data(src.data),
80 scale(src.scale)
81 {}
82
83public:
84 const char * getFamilyName() const override { return TypeName<T>::get(); }
85
86 bool isNumeric() const override { return false; }
87 bool canBeInsideNullable() const override { return true; }
88 bool isFixedAndContiguous() const override { return true; }
89 size_t sizeOfValueIfFixed() const override { return sizeof(T); }
90
91 size_t size() const override { return data.size(); }
92 size_t byteSize() const override { return data.size() * sizeof(data[0]); }
93 size_t allocatedBytes() const override { return data.allocated_bytes(); }
94 void protect() override { data.protect(); }
95 void reserve(size_t n) override { data.reserve(n); }
96
97 void insertFrom(const IColumn & src, size_t n) override { data.push_back(static_cast<const Self &>(src).getData()[n]); }
98 void insertData(const char * pos, size_t /*length*/) override;
99 void insertDefault() override { data.push_back(T()); }
100 virtual void insertManyDefaults(size_t length) override { data.resize_fill(data.size() + length); }
101 void insert(const Field & x) override { data.push_back(DB::get<NearestFieldType<T>>(x)); }
102 void insertRangeFrom(const IColumn & src, size_t start, size_t length) override;
103
104 void popBack(size_t n) override { data.resize_assume_reserved(data.size() - n); }
105
106 StringRef serializeValueIntoArena(size_t n, Arena & arena, char const *& begin) const override;
107 const char * deserializeAndInsertFromArena(const char * pos) override;
108 void updateHashWithValue(size_t n, SipHash & hash) const override;
109 int compareAt(size_t n, size_t m, const IColumn & rhs_, int nan_direction_hint) const override;
110 void getPermutation(bool reverse, size_t limit, int nan_direction_hint, IColumn::Permutation & res) const override;
111
112 MutableColumnPtr cloneResized(size_t size) const override;
113
114 Field operator[](size_t n) const override { return DecimalField(data[n], scale); }
115
116 StringRef getRawData() const override { return StringRef(reinterpret_cast<const char*>(data.data()), data.size()); }
117 StringRef getDataAt(size_t n) const override { return StringRef(reinterpret_cast<const char *>(&data[n]), sizeof(data[n])); }
118 void get(size_t n, Field & res) const override { res = (*this)[n]; }
119 bool getBool(size_t n) const override { return bool(data[n]); }
120 Int64 getInt(size_t n) const override { return Int64(data[n] * scale); }
121 UInt64 get64(size_t n) const override;
122 bool isDefaultAt(size_t n) const override { return data[n] == 0; }
123
124 ColumnPtr filter(const IColumn::Filter & filt, ssize_t result_size_hint) const override;
125 ColumnPtr permute(const IColumn::Permutation & perm, size_t limit) const override;
126 ColumnPtr index(const IColumn & indexes, size_t limit) const override;
127
128 template <typename Type>
129 ColumnPtr indexImpl(const PaddedPODArray<Type> & indexes, size_t limit) const;
130
131 ColumnPtr replicate(const IColumn::Offsets & offsets) const override;
132 void getExtremes(Field & min, Field & max) const override;
133
134 MutableColumns scatter(IColumn::ColumnIndex num_columns, const IColumn::Selector & selector) const override
135 {
136 return this->template scatterImpl<Self>(num_columns, selector);
137 }
138
139 void gather(ColumnGathererStream & gatherer_stream) override;
140
141 bool structureEquals(const IColumn & rhs) const override
142 {
143 if (auto rhs_concrete = typeid_cast<const ColumnDecimal<T> *>(&rhs))
144 return scale == rhs_concrete->scale;
145 return false;
146 }
147
148
149 void insertValue(const T value) { data.push_back(value); }
150 Container & getData() { return data; }
151 const Container & getData() const { return data; }
152 const T & getElement(size_t n) const { return data[n]; }
153 T & getElement(size_t n) { return data[n]; }
154
155protected:
156 Container data;
157 UInt32 scale;
158
159 template <typename U>
160 void permutation(bool reverse, size_t limit, PaddedPODArray<U> & res) const
161 {
162 size_t s = data.size();
163 res.resize(s);
164 for (U i = 0; i < s; ++i)
165 res[i] = i;
166
167 auto sort_end = res.end();
168 if (limit && limit < s)
169 sort_end = res.begin() + limit;
170
171 if (reverse)
172 std::partial_sort(res.begin(), sort_end, res.end(), [this](size_t a, size_t b) { return data[a] > data[b]; });
173 else
174 std::partial_sort(res.begin(), sort_end, res.end(), [this](size_t a, size_t b) { return data[a] < data[b]; });
175 }
176};
177
178template <typename T>
179template <typename Type>
180ColumnPtr ColumnDecimal<T>::indexImpl(const PaddedPODArray<Type> & indexes, size_t limit) const
181{
182 size_t size = indexes.size();
183
184 if (limit == 0)
185 limit = size;
186 else
187 limit = std::min(size, limit);
188
189 auto res = this->create(limit, scale);
190 typename Self::Container & res_data = res->getData();
191 for (size_t i = 0; i < limit; ++i)
192 res_data[i] = data[indexes[i]];
193
194 return res;
195}
196
197}
198