1#pragma once
2
3#include <Columns/IColumn.h>
4#include <Columns/IColumnImpl.h>
5#include <Columns/ColumnVector.h>
6#include <Core/Defines.h>
7#include <Common/typeid_cast.h>
8#include <Common/assert_cast.h>
9
10
11namespace DB
12{
13
14/** A column of array values.
15 * In memory, it is represented as one column of a nested type, whose size is equal to the sum of the sizes of all arrays,
16 * and as an array of offsets in it, which allows you to get each element.
17 */
18class ColumnArray final : public COWHelper<IColumn, ColumnArray>
19{
20private:
21 friend class COWHelper<IColumn, ColumnArray>;
22
23 /** Create an array column with specified values and offsets. */
24 ColumnArray(MutableColumnPtr && nested_column, MutableColumnPtr && offsets_column);
25
26 /** Create an empty column of arrays with the type of values as in the column `nested_column` */
27 explicit ColumnArray(MutableColumnPtr && nested_column);
28
29 ColumnArray(const ColumnArray &) = default;
30
31public:
32 /** Create immutable column using immutable arguments. This arguments may be shared with other columns.
33 * Use IColumn::mutate in order to make mutable column and mutate shared nested columns.
34 */
35 using Base = COWHelper<IColumn, ColumnArray>;
36
37 static Ptr create(const ColumnPtr & nested_column, const ColumnPtr & offsets_column)
38 {
39 return ColumnArray::create(nested_column->assumeMutable(), offsets_column->assumeMutable());
40 }
41
42 static Ptr create(const ColumnPtr & nested_column)
43 {
44 return ColumnArray::create(nested_column->assumeMutable());
45 }
46
47 template <typename ... Args, typename = typename std::enable_if<IsMutableColumns<Args ...>::value>::type>
48 static MutablePtr create(Args &&... args) { return Base::create(std::forward<Args>(args)...); }
49
50 /** On the index i there is an offset to the beginning of the i + 1 -th element. */
51 using ColumnOffsets = ColumnVector<Offset>;
52
53 std::string getName() const override;
54 const char * getFamilyName() const override { return "Array"; }
55 MutableColumnPtr cloneResized(size_t size) const override;
56 size_t size() const override;
57 Field operator[](size_t n) const override;
58 void get(size_t n, Field & res) const override;
59 StringRef getDataAt(size_t n) const override;
60 void insertData(const char * pos, size_t length) override;
61 StringRef serializeValueIntoArena(size_t n, Arena & arena, char const *& begin) const override;
62 const char * deserializeAndInsertFromArena(const char * pos) override;
63 void updateHashWithValue(size_t n, SipHash & hash) const override;
64 void insertRangeFrom(const IColumn & src, size_t start, size_t length) override;
65 void insert(const Field & x) override;
66 void insertFrom(const IColumn & src_, size_t n) override;
67 void insertDefault() override;
68 void popBack(size_t n) override;
69 ColumnPtr filter(const Filter & filt, ssize_t result_size_hint) const override;
70 ColumnPtr permute(const Permutation & perm, size_t limit) const override;
71 ColumnPtr index(const IColumn & indexes, size_t limit) const override;
72 template <typename Type> ColumnPtr indexImpl(const PaddedPODArray<Type> & indexes, size_t limit) const;
73 int compareAt(size_t n, size_t m, const IColumn & rhs_, int nan_direction_hint) const override;
74 void getPermutation(bool reverse, size_t limit, int nan_direction_hint, Permutation & res) const override;
75 void reserve(size_t n) override;
76 size_t byteSize() const override;
77 size_t allocatedBytes() const override;
78 void protect() override;
79 ColumnPtr replicate(const Offsets & replicate_offsets) const override;
80 ColumnPtr convertToFullColumnIfConst() const override;
81 void getExtremes(Field & min, Field & max) const override;
82
83 bool hasEqualOffsets(const ColumnArray & other) const;
84
85 /** More efficient methods of manipulation */
86 IColumn & getData() { return *data; }
87 const IColumn & getData() const { return *data; }
88
89 IColumn & getOffsetsColumn() { return *offsets; }
90 const IColumn & getOffsetsColumn() const { return *offsets; }
91
92 Offsets & ALWAYS_INLINE getOffsets()
93 {
94 return assert_cast<ColumnOffsets &>(*offsets).getData();
95 }
96
97 const Offsets & ALWAYS_INLINE getOffsets() const
98 {
99 return assert_cast<const ColumnOffsets &>(*offsets).getData();
100 }
101
102 const ColumnPtr & getDataPtr() const { return data; }
103 ColumnPtr & getDataPtr() { return data; }
104
105 const ColumnPtr & getOffsetsPtr() const { return offsets; }
106 ColumnPtr & getOffsetsPtr() { return offsets; }
107
108 MutableColumns scatter(ColumnIndex num_columns, const Selector & selector) const override
109 {
110 return scatterImpl<ColumnArray>(num_columns, selector);
111 }
112
113 void gather(ColumnGathererStream & gatherer_stream) override;
114
115 void forEachSubcolumn(ColumnCallback callback) override
116 {
117 callback(offsets);
118 callback(data);
119 }
120
121 bool structureEquals(const IColumn & rhs) const override
122 {
123 if (auto rhs_concrete = typeid_cast<const ColumnArray *>(&rhs))
124 return data->structureEquals(*rhs_concrete->data);
125 return false;
126 }
127
128private:
129 WrappedPtr data;
130 WrappedPtr offsets;
131
132 size_t ALWAYS_INLINE offsetAt(ssize_t i) const { return getOffsets()[i - 1]; }
133 size_t ALWAYS_INLINE sizeAt(ssize_t i) const { return getOffsets()[i] - getOffsets()[i - 1]; }
134
135
136 /// Multiply values if the nested column is ColumnVector<T>.
137 template <typename T>
138 ColumnPtr replicateNumber(const Offsets & replicate_offsets) const;
139
140 /// Multiply the values if the nested column is ColumnString. The code is too complicated.
141 ColumnPtr replicateString(const Offsets & replicate_offsets) const;
142
143 /** Non-constant arrays of constant values are quite rare.
144 * Most functions can not work with them, and does not create such columns as a result.
145 * An exception is the function `replicate` (see FunctionsMiscellaneous.h), which has service meaning for the implementation of lambda functions.
146 * Only for its sake is the implementation of the `replicate` method for ColumnArray(ColumnConst).
147 */
148 ColumnPtr replicateConst(const Offsets & replicate_offsets) const;
149
150 /** The following is done by simply replicating of nested columns.
151 */
152 ColumnPtr replicateTuple(const Offsets & replicate_offsets) const;
153 ColumnPtr replicateNullable(const Offsets & replicate_offsets) const;
154 ColumnPtr replicateGeneric(const Offsets & replicate_offsets) const;
155
156
157 /// Specializations for the filter function.
158 template <typename T>
159 ColumnPtr filterNumber(const Filter & filt, ssize_t result_size_hint) const;
160
161 ColumnPtr filterString(const Filter & filt, ssize_t result_size_hint) const;
162 ColumnPtr filterTuple(const Filter & filt, ssize_t result_size_hint) const;
163 ColumnPtr filterNullable(const Filter & filt, ssize_t result_size_hint) const;
164 ColumnPtr filterGeneric(const Filter & filt, ssize_t result_size_hint) const;
165};
166
167
168}
169