ClickHouse/src/Columns/ColumnAggregateFunction.h

229 lines
8.1 KiB
C++
Raw Normal View History

#pragma once
2011-09-19 01:42:16 +00:00
#include <AggregateFunctions/IAggregateFunction.h>
2011-09-19 01:42:16 +00:00
#include <Columns/IColumn.h>
2019-10-07 18:56:03 +00:00
#include <Common/PODArray.h>
2011-09-19 01:42:16 +00:00
#include <Core/Field.h>
#include <IO/ReadBufferFromString.h>
#include <IO/WriteBuffer.h>
#include <IO/WriteHelpers.h>
2019-01-22 21:07:05 +00:00
#include <Functions/FunctionHelpers.h>
2011-09-19 01:42:16 +00:00
namespace DB
{
2020-06-17 11:43:55 +00:00
namespace ErrorCodes
{
extern const int NOT_IMPLEMENTED;
}
class Arena;
using ArenaPtr = std::shared_ptr<Arena>;
using ConstArenaPtr = std::shared_ptr<const Arena>;
using ConstArenas = std::vector<ConstArenaPtr>;
/** Column of states of aggregate functions.
2017-03-09 00:56:38 +00:00
* Presented as an array of pointers to the states of aggregate functions (data).
* The states themselves are stored in one of the pools (arenas).
2014-06-08 22:17:44 +00:00
*
2017-03-09 00:56:38 +00:00
* It can be in two variants:
2014-06-08 22:17:44 +00:00
*
2017-03-09 00:56:38 +00:00
* 1. Own its values - that is, be responsible for destroying them.
* The column consists of the values "assigned to it" after the aggregation is performed (see Aggregator, convertToBlocks function),
* or from values created by itself (see `insert` method).
2017-03-09 00:56:38 +00:00
* In this case, `src` will be `nullptr`, and the column itself will be destroyed (call `IAggregateFunction::destroy`)
* states of aggregate functions in the destructor.
*
* 2. Do not own its values, but use values taken from another ColumnAggregateFunction column.
2017-03-09 00:56:38 +00:00
* For example, this is a column obtained by permutation/filtering or other transformations from another column.
* In this case, `src` will be `shared ptr` to the source column. Destruction of values will be handled by this source column.
2014-06-08 22:17:44 +00:00
*
2017-03-09 00:56:38 +00:00
* This solution is somewhat limited:
* - the variant in which the column contains a part of "it's own" and a part of "another's" values is not supported;
2017-03-09 00:56:38 +00:00
* - the option of having multiple source columns is not supported, which may be necessary for a more optimal merge of the two columns.
2014-06-08 22:17:44 +00:00
*
2017-03-09 00:56:38 +00:00
* These restrictions can be removed if you add an array of flags or even refcount,
* specifying which individual values should be destroyed and which ones should not.
2017-03-09 00:56:38 +00:00
* Clearly, this method would have a substantially non-zero price.
*/
class ColumnAggregateFunction final : public COWHelper<IColumn, ColumnAggregateFunction>
{
public:
using Container = PaddedPODArray<AggregateDataPtr>;
2014-06-08 22:17:44 +00:00
private:
friend class COWHelper<IColumn, ColumnAggregateFunction>;
/// Arenas used by function states that are created elsewhere. We own these
/// arenas in the sense of extending their lifetime, but do not modify them.
/// Even reading these arenas is unsafe, because they may be shared with
/// other data blocks and modified by other threads concurrently.
ConstArenas foreign_arenas;
/// Arena for allocating the internals of function states created by current
/// column (e.g., when inserting new states).
ArenaPtr my_arena;
2014-06-05 23:52:28 +00:00
/// Used for destroying states and for finalization of values.
AggregateFunctionPtr func;
/// Source column. Used (holds source from destruction),
/// if this column has been constructed from another and uses all or part of its values.
ColumnPtr src;
2014-06-05 23:52:28 +00:00
/// Array of pointers to aggregation states, that are placed in arenas.
Container data;
2014-06-05 23:52:28 +00:00
/// Name of the type to distinguish different aggregation states.
String type_string;
ColumnAggregateFunction() = default;
/// Create a new column that has another column as a source.
MutablePtr createView() const;
2014-06-05 23:52:28 +00:00
/// If we have another column as a source (owner of data), copy all data to ourself and reset source.
/// This is needed before inserting new elements, because we must own these elements (to destroy them in destructor),
/// but ownership of different elements cannot be mixed by different columns.
void ensureOwnership();
ColumnAggregateFunction(const AggregateFunctionPtr & func_);
2014-06-06 19:35:41 +00:00
ColumnAggregateFunction(const AggregateFunctionPtr & func_,
const ConstArenas & arenas_);
ColumnAggregateFunction(const ColumnAggregateFunction & src_);
public:
~ColumnAggregateFunction() override;
void set(const AggregateFunctionPtr & func_);
AggregateFunctionPtr getAggregateFunction() { return func; }
AggregateFunctionPtr getAggregateFunction() const { return func; }
2014-02-27 12:49:21 +00:00
/// Take shared ownership of Arena, that holds memory for states of aggregate functions.
void addArena(ConstArenaPtr arena_);
/// Transform column with states of aggregate functions to column with final result values.
/// It expects ColumnAggregateFunction as an argument, this column will be destroyed.
/// This method is made static and receive MutableColumnPtr object to explicitly destroy it.
static MutableColumnPtr convertToValues(MutableColumnPtr column);
2014-02-27 12:49:21 +00:00
std::string getName() const override { return "AggregateFunction(" + func->getName() + ")"; }
const char * getFamilyName() const override { return "AggregateFunction"; }
2020-06-04 22:02:59 +00:00
TypeIndex getDataType() const override { return TypeIndex::AggregateFunction; }
MutableColumnPtr predictValues(const ColumnsWithTypeAndName & arguments, ContextConstPtr context) const;
2019-01-22 21:07:05 +00:00
size_t size() const override
{
return getData().size();
}
2011-09-19 01:42:16 +00:00
MutableColumnPtr cloneEmpty() const override;
2011-09-19 01:42:16 +00:00
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;
2014-05-26 16:11:20 +00:00
2018-08-27 18:16:32 +00:00
void insertFrom(const IColumn & from, size_t n) override;
2014-06-05 23:52:28 +00:00
void insertFrom(ConstAggregateDataPtr place);
/// Merge state at last row with specified state in another column.
void insertMergeFrom(ConstAggregateDataPtr place);
2018-08-27 18:16:32 +00:00
void insertMergeFrom(const IColumn & from, size_t n);
2015-11-10 20:39:11 +00:00
Arena & createOrGetArena();
2014-06-05 23:52:28 +00:00
void insert(const Field & x) override;
2016-04-14 05:03:33 +00:00
void insertDefault() override;
2016-04-14 05:03:33 +00:00
StringRef serializeValueIntoArena(size_t n, Arena & arena, char const *& begin) const override;
2020-03-09 01:03:43 +00:00
const char * deserializeAndInsertFromArena(const char * src_arena) override;
const char * skipSerializedInArena(const char *) const override;
void updateHashWithValue(size_t n, SipHash & hash) const override;
void updateWeakHash32(WeakHash32 & hash) const override;
void updateHashFast(SipHash & hash) const override;
size_t byteSize() const override;
2014-05-28 14:54:42 +00:00
2021-01-02 22:58:10 +00:00
size_t byteSizeAt(size_t n) const override;
size_t allocatedBytes() const override;
void protect() override;
void insertRangeFrom(const IColumn & from, size_t start, size_t length) override;
2013-05-03 05:23:14 +00:00
void popBack(size_t n) override;
2018-03-20 14:17:09 +00:00
ColumnPtr filter(const Filter & filter, ssize_t result_size_hint) const override;
ColumnPtr permute(const Permutation & perm, size_t limit) const override;
2013-05-03 05:23:14 +00:00
ColumnPtr index(const IColumn & indexes, size_t limit) const override;
2018-04-18 21:00:47 +00:00
template <typename Type>
ColumnPtr indexImpl(const PaddedPODArray<Type> & indexes, size_t limit) const;
2018-04-18 21:00:47 +00:00
2018-03-20 14:17:09 +00:00
ColumnPtr replicate(const Offsets & offsets) const override;
MutableColumns scatter(ColumnIndex num_columns, const Selector & selector) const override;
void gather(ColumnGathererStream & gatherer_stream) override;
2017-12-01 19:34:51 +00:00
int compareAt(size_t, size_t, const IColumn &, int) const override
{
return 0;
}
2011-09-26 11:05:38 +00:00
2020-06-17 11:43:55 +00:00
void compareColumn(const IColumn &, size_t, PaddedPODArray<UInt64> *, PaddedPODArray<Int8> &, int, int) const override
2020-06-01 12:10:32 +00:00
{
2020-06-17 11:43:55 +00:00
throw Exception("Method compareColumn is not supported for ColumnAggregateFunction", ErrorCodes::NOT_IMPLEMENTED);
2020-06-01 12:10:32 +00:00
}
2021-02-26 04:50:04 +00:00
bool hasEqualValues() const override
{
throw Exception("Method hasEqualValues is not supported for ColumnAggregateFunction", ErrorCodes::NOT_IMPLEMENTED);
}
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, Permutation & res, EqualRanges & equal_range) const override;
2014-06-05 23:52:28 +00:00
/** More efficient manipulation methods */
Container & getData()
{
return data;
}
2014-06-05 23:52:28 +00:00
const Container & getData() const
{
return data;
}
void getExtremes(Field & min, Field & max) const override;
bool structureEquals(const IColumn &) const override;
2021-01-08 08:10:36 +00:00
2021-01-08 08:23:33 +00:00
MutableColumnPtr cloneResized(size_t size) const override;
2011-09-19 01:42:16 +00:00
};
}