2012-08-20 05:32:50 +00:00
|
|
|
#pragma once
|
2010-05-13 16:17:10 +00:00
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Columns/IColumn.h>
|
2019-10-07 18:56:03 +00:00
|
|
|
#include <Columns/IColumnImpl.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Columns/ColumnVector.h>
|
2017-06-23 20:22:35 +00:00
|
|
|
#include <Core/Defines.h>
|
2019-03-14 23:10:51 +00:00
|
|
|
#include <Common/typeid_cast.h>
|
2019-08-21 02:28:04 +00:00
|
|
|
#include <Common/assert_cast.h>
|
2010-05-13 16:17:10 +00:00
|
|
|
|
|
|
|
|
2019-03-14 23:10:51 +00:00
|
|
|
namespace DB
|
2016-01-11 21:46:36 +00:00
|
|
|
{
|
|
|
|
|
2017-03-09 00:56:38 +00:00
|
|
|
/** A column of array values.
|
|
|
|
* 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,
|
|
|
|
* and as an array of offsets in it, which allows you to get each element.
|
2010-05-13 16:17:10 +00:00
|
|
|
*/
|
2019-04-19 20:21:17 +00:00
|
|
|
class ColumnArray final : public COWHelper<IColumn, ColumnArray>
|
2010-05-13 16:17:10 +00:00
|
|
|
{
|
2017-12-13 19:14:12 +00:00
|
|
|
private:
|
2019-04-19 20:21:17 +00:00
|
|
|
friend class COWHelper<IColumn, ColumnArray>;
|
2017-12-14 04:25:22 +00:00
|
|
|
|
2017-12-15 19:46:24 +00:00
|
|
|
/** Create an array column with specified values and offsets. */
|
2018-03-20 14:17:09 +00:00
|
|
|
ColumnArray(MutableColumnPtr && nested_column, MutableColumnPtr && offsets_column);
|
2017-12-14 01:43:19 +00:00
|
|
|
|
2017-12-15 19:46:24 +00:00
|
|
|
/** Create an empty column of arrays with the type of values as in the column `nested_column` */
|
2018-03-21 19:39:14 +00:00
|
|
|
explicit ColumnArray(MutableColumnPtr && nested_column);
|
2018-03-20 14:17:09 +00:00
|
|
|
|
2018-03-21 19:39:14 +00:00
|
|
|
ColumnArray(const ColumnArray &) = default;
|
|
|
|
|
|
|
|
public:
|
|
|
|
/** Create immutable column using immutable arguments. This arguments may be shared with other columns.
|
|
|
|
* Use IColumn::mutate in order to make mutable column and mutate shared nested columns.
|
|
|
|
*/
|
2019-04-19 20:21:17 +00:00
|
|
|
using Base = COWHelper<IColumn, ColumnArray>;
|
2018-03-20 14:17:09 +00:00
|
|
|
|
2018-03-21 19:39:14 +00:00
|
|
|
static Ptr create(const ColumnPtr & nested_column, const ColumnPtr & offsets_column)
|
2018-03-20 14:17:09 +00:00
|
|
|
{
|
|
|
|
return ColumnArray::create(nested_column->assumeMutable(), offsets_column->assumeMutable());
|
|
|
|
}
|
|
|
|
|
2018-03-21 19:39:14 +00:00
|
|
|
static Ptr create(const ColumnPtr & nested_column)
|
2018-03-20 14:17:09 +00:00
|
|
|
{
|
|
|
|
return ColumnArray::create(nested_column->assumeMutable());
|
|
|
|
}
|
2017-12-15 19:46:24 +00:00
|
|
|
|
2018-03-21 19:39:14 +00:00
|
|
|
template <typename ... Args, typename = typename std::enable_if<IsMutableColumns<Args ...>::value>::type>
|
|
|
|
static MutablePtr create(Args &&... args) { return Base::create(std::forward<Args>(args)...); }
|
2017-12-13 19:14:12 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/** On the index i there is an offset to the beginning of the i + 1 -th element. */
|
2017-12-15 21:32:25 +00:00
|
|
|
using ColumnOffsets = ColumnVector<Offset>;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
std::string getName() const override;
|
2017-12-07 22:11:51 +00:00
|
|
|
const char * getFamilyName() const override { return "Array"; }
|
2020-06-04 22:02:59 +00:00
|
|
|
TypeIndex getDataType() const override { return TypeIndex::Array; }
|
2017-12-13 19:14:12 +00:00
|
|
|
MutableColumnPtr cloneResized(size_t size) const override;
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t size() const override;
|
|
|
|
Field operator[](size_t n) const override;
|
|
|
|
void get(size_t n, Field & res) const override;
|
|
|
|
StringRef getDataAt(size_t n) const override;
|
|
|
|
void insertData(const char * pos, size_t length) override;
|
|
|
|
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;
|
2017-04-01 07:20:54 +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;
|
2017-04-01 07:20:54 +00:00
|
|
|
void insertRangeFrom(const IColumn & src, size_t start, size_t length) override;
|
|
|
|
void insert(const Field & x) override;
|
|
|
|
void insertFrom(const IColumn & src_, size_t n) override;
|
|
|
|
void insertDefault() override;
|
|
|
|
void popBack(size_t n) override;
|
2018-03-20 14:17:09 +00:00
|
|
|
ColumnPtr filter(const Filter & filt, ssize_t result_size_hint) const override;
|
2019-02-18 19:44:26 +00:00
|
|
|
ColumnPtr permute(const 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;
|
|
|
|
template <typename Type> ColumnPtr indexImpl(const PaddedPODArray<Type> & indexes, size_t limit) const;
|
2017-04-01 07:20:54 +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;
|
2020-10-29 11:24:01 +00:00
|
|
|
int compareAtWithCollation(size_t n, size_t m, const IColumn & rhs_, int nan_direction_hint, const Collator & collator) const override;
|
2021-02-26 04:50:04 +00:00
|
|
|
bool hasEqualValues() const override;
|
2019-02-18 19:44:26 +00:00
|
|
|
void getPermutation(bool reverse, size_t limit, int nan_direction_hint, Permutation & res) const override;
|
2020-05-25 14:33:31 +00:00
|
|
|
void updatePermutation(bool reverse, size_t limit, int nan_direction_hint, Permutation & res, EqualRanges & equal_range) const override;
|
2020-10-29 11:24:01 +00:00
|
|
|
void getPermutationWithCollation(const Collator & collator, bool reverse, size_t limit, int nan_direction_hint, Permutation & res) const override;
|
|
|
|
void updatePermutationWithCollation(const Collator & collator, bool reverse, size_t limit, int nan_direction_hint, Permutation & res, EqualRanges& equal_range) const override;
|
2017-04-01 07:20:54 +00:00
|
|
|
void reserve(size_t n) override;
|
|
|
|
size_t byteSize() const override;
|
2021-01-02 22:58:10 +00:00
|
|
|
size_t byteSizeAt(size_t n) const override;
|
2017-07-13 16:49:09 +00:00
|
|
|
size_t allocatedBytes() const override;
|
2019-03-10 03:16:51 +00:00
|
|
|
void protect() override;
|
2018-03-20 14:17:09 +00:00
|
|
|
ColumnPtr replicate(const Offsets & replicate_offsets) const override;
|
|
|
|
ColumnPtr convertToFullColumnIfConst() const override;
|
2017-04-01 07:20:54 +00:00
|
|
|
void getExtremes(Field & min, Field & max) const override;
|
|
|
|
|
|
|
|
bool hasEqualOffsets(const ColumnArray & other) const;
|
|
|
|
|
|
|
|
/** More efficient methods of manipulation */
|
2019-03-25 01:43:54 +00:00
|
|
|
IColumn & getData() { return *data; }
|
2017-12-14 01:43:19 +00:00
|
|
|
const IColumn & getData() const { return *data; }
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2019-03-25 01:43:54 +00:00
|
|
|
IColumn & getOffsetsColumn() { return *offsets; }
|
2017-12-16 01:21:59 +00:00
|
|
|
const IColumn & getOffsetsColumn() const { return *offsets; }
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-12-15 21:32:25 +00:00
|
|
|
Offsets & ALWAYS_INLINE getOffsets()
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2019-08-21 02:28:04 +00:00
|
|
|
return assert_cast<ColumnOffsets &>(*offsets).getData();
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2017-12-15 21:32:25 +00:00
|
|
|
const Offsets & ALWAYS_INLINE getOffsets() const
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2019-08-21 02:28:04 +00:00
|
|
|
return assert_cast<const ColumnOffsets &>(*offsets).getData();
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2017-12-16 01:21:59 +00:00
|
|
|
const ColumnPtr & getDataPtr() const { return data; }
|
2017-12-16 04:59:32 +00:00
|
|
|
ColumnPtr & getDataPtr() { return data; }
|
2017-12-16 01:21:59 +00:00
|
|
|
|
2017-12-15 02:36:40 +00:00
|
|
|
const ColumnPtr & getOffsetsPtr() const { return offsets; }
|
2017-12-16 01:21:59 +00:00
|
|
|
ColumnPtr & getOffsetsPtr() { return offsets; }
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-12-13 19:14:12 +00:00
|
|
|
MutableColumns scatter(ColumnIndex num_columns, const Selector & selector) const override
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
return scatterImpl<ColumnArray>(num_columns, selector);
|
|
|
|
}
|
2015-10-28 21:21:18 +00:00
|
|
|
|
2017-07-06 13:54:55 +00:00
|
|
|
void gather(ColumnGathererStream & gatherer_stream) override;
|
|
|
|
|
2021-02-17 23:09:46 +00:00
|
|
|
ColumnPtr compress() const override;
|
|
|
|
|
2017-12-07 12:09:55 +00:00
|
|
|
void forEachSubcolumn(ColumnCallback callback) override
|
|
|
|
{
|
|
|
|
callback(offsets);
|
|
|
|
callback(data);
|
|
|
|
}
|
|
|
|
|
2019-03-14 23:10:51 +00:00
|
|
|
bool structureEquals(const IColumn & rhs) const override
|
|
|
|
{
|
|
|
|
if (auto rhs_concrete = typeid_cast<const ColumnArray *>(&rhs))
|
|
|
|
return data->structureEquals(*rhs_concrete->data);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-05-21 00:57:11 +00:00
|
|
|
size_t getNumberOfDefaultRows(size_t step) const override;
|
|
|
|
void getIndicesOfNonDefaultValues(IColumn::Offsets & indices, size_t from, size_t limit) const override;
|
2021-03-09 17:25:23 +00:00
|
|
|
|
2020-10-29 11:24:01 +00:00
|
|
|
bool isCollationSupported() const override { return getData().isCollationSupported(); }
|
|
|
|
|
2016-08-10 19:12:29 +00:00
|
|
|
private:
|
2019-03-25 01:43:54 +00:00
|
|
|
WrappedPtr data;
|
|
|
|
WrappedPtr offsets;
|
2011-08-22 20:24:45 +00:00
|
|
|
|
2018-12-26 05:34:25 +00:00
|
|
|
size_t ALWAYS_INLINE offsetAt(ssize_t i) const { return getOffsets()[i - 1]; }
|
|
|
|
size_t ALWAYS_INLINE sizeAt(ssize_t i) const { return getOffsets()[i] - getOffsets()[i - 1]; }
|
2013-07-27 21:50:13 +00:00
|
|
|
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Multiply values if the nested column is ColumnVector<T>.
|
|
|
|
template <typename T>
|
2018-03-20 14:17:09 +00:00
|
|
|
ColumnPtr replicateNumber(const Offsets & replicate_offsets) const;
|
2013-07-28 00:35:29 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Multiply the values if the nested column is ColumnString. The code is too complicated.
|
2018-03-20 14:17:09 +00:00
|
|
|
ColumnPtr replicateString(const Offsets & replicate_offsets) const;
|
2015-07-06 19:24:51 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/** Non-constant arrays of constant values are quite rare.
|
|
|
|
* Most functions can not work with them, and does not create such columns as a result.
|
2018-09-08 22:04:39 +00:00
|
|
|
* An exception is the function `replicate` (see FunctionsMiscellaneous.h), which has service meaning for the implementation of lambda functions.
|
2017-04-01 07:20:54 +00:00
|
|
|
* Only for its sake is the implementation of the `replicate` method for ColumnArray(ColumnConst).
|
|
|
|
*/
|
2018-03-20 14:17:09 +00:00
|
|
|
ColumnPtr replicateConst(const Offsets & replicate_offsets) const;
|
2015-11-21 03:19:43 +00:00
|
|
|
|
2017-04-18 03:03:39 +00:00
|
|
|
/** The following is done by simply replicating of nested columns.
|
|
|
|
*/
|
2018-03-20 14:17:09 +00:00
|
|
|
ColumnPtr replicateTuple(const Offsets & replicate_offsets) const;
|
|
|
|
ColumnPtr replicateNullable(const Offsets & replicate_offsets) const;
|
|
|
|
ColumnPtr replicateGeneric(const Offsets & replicate_offsets) const;
|
2017-04-18 03:03:39 +00:00
|
|
|
|
2015-11-21 03:19:43 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Specializations for the filter function.
|
|
|
|
template <typename T>
|
2018-03-20 14:17:09 +00:00
|
|
|
ColumnPtr filterNumber(const Filter & filt, ssize_t result_size_hint) const;
|
2015-11-21 03:19:43 +00:00
|
|
|
|
2018-03-20 14:17:09 +00:00
|
|
|
ColumnPtr filterString(const Filter & filt, ssize_t result_size_hint) const;
|
|
|
|
ColumnPtr filterTuple(const Filter & filt, ssize_t result_size_hint) const;
|
|
|
|
ColumnPtr filterNullable(const Filter & filt, ssize_t result_size_hint) const;
|
|
|
|
ColumnPtr filterGeneric(const Filter & filt, ssize_t result_size_hint) const;
|
2020-10-29 11:24:01 +00:00
|
|
|
|
|
|
|
int compareAtImpl(size_t n, size_t m, const IColumn & rhs_, int nan_direction_hint, const Collator * collator=nullptr) const;
|
|
|
|
|
|
|
|
template <typename Comparator>
|
|
|
|
void getPermutationImpl(size_t limit, Permutation & res, Comparator cmp) const;
|
|
|
|
|
|
|
|
template <typename Comparator>
|
|
|
|
void updatePermutationImpl(size_t limit, Permutation & res, EqualRanges & equal_range, Comparator cmp) const;
|
2010-05-13 16:17:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
}
|