2017-09-01 12:05:19 +00:00
|
|
|
#include <Common/TypeList.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Interpreters/Aggregator.h>
|
2015-01-08 21:41:35 +00:00
|
|
|
|
2017-07-12 01:16:01 +00:00
|
|
|
#include <AggregateFunctions/AggregateFunctionArgMinMax.h>
|
2017-08-23 13:32:30 +00:00
|
|
|
#include <AggregateFunctions/AggregateFunctionArray.h>
|
|
|
|
#include <AggregateFunctions/AggregateFunctionAvg.h>
|
|
|
|
#include <AggregateFunctions/AggregateFunctionCount.h>
|
|
|
|
#include <AggregateFunctions/AggregateFunctionForEach.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <AggregateFunctions/AggregateFunctionGroupArray.h>
|
2017-08-23 13:32:30 +00:00
|
|
|
#include <AggregateFunctions/AggregateFunctionGroupArrayInsertAt.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <AggregateFunctions/AggregateFunctionGroupUniqArray.h>
|
|
|
|
#include <AggregateFunctions/AggregateFunctionIf.h>
|
|
|
|
#include <AggregateFunctions/AggregateFunctionMerge.h>
|
2017-08-23 13:32:30 +00:00
|
|
|
#include <AggregateFunctions/AggregateFunctionMinMaxAny.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <AggregateFunctions/AggregateFunctionNull.h>
|
2017-08-23 13:32:30 +00:00
|
|
|
#include <AggregateFunctions/AggregateFunctionQuantileDeterministic.h>
|
|
|
|
#include <AggregateFunctions/AggregateFunctionQuantileExact.h>
|
|
|
|
#include <AggregateFunctions/AggregateFunctionQuantileExactWeighted.h>
|
|
|
|
#include <AggregateFunctions/AggregateFunctionQuantile.h>
|
|
|
|
#include <AggregateFunctions/AggregateFunctionQuantileTDigest.h>
|
|
|
|
#include <AggregateFunctions/AggregateFunctionQuantileTiming.h>
|
|
|
|
#include <AggregateFunctions/AggregateFunctionSequenceMatch.h>
|
|
|
|
#include <AggregateFunctions/AggregateFunctionState.h>
|
|
|
|
#include <AggregateFunctions/AggregateFunctionStatistics.h>
|
|
|
|
#include <AggregateFunctions/AggregateFunctionSum.h>
|
|
|
|
#include <AggregateFunctions/AggregateFunctionTopK.h>
|
|
|
|
#include <AggregateFunctions/AggregateFunctionUniq.h>
|
|
|
|
#include <AggregateFunctions/AggregateFunctionUniqUpTo.h>
|
2015-01-08 21:41:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
|
2017-06-02 21:37:28 +00:00
|
|
|
/** An aggregation cycle template that allows you to generate a custom variant for a specific combination of aggregate functions.
|
|
|
|
* It differs from the usual one in that calls to aggregate functions should be inlined, and the update cycle of the aggregate functions should be unfold.
|
2015-01-08 21:41:35 +00:00
|
|
|
*
|
2017-06-02 21:37:28 +00:00
|
|
|
* Since there are too many possible combinations, it is not possible to generate them all in advance.
|
|
|
|
* This template is intended to instantiate it in runtime,
|
|
|
|
* by running the compiler, compiling shared library, and using it with `dlopen`.
|
2015-01-08 21:41:35 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
struct AggregateFunctionsUpdater
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
AggregateFunctionsUpdater(
|
|
|
|
const Aggregator::AggregateFunctionsPlainPtrs & aggregate_functions_,
|
|
|
|
const Sizes & offsets_of_aggregate_states_,
|
|
|
|
Aggregator::AggregateColumns & aggregate_columns_,
|
|
|
|
AggregateDataPtr & value_,
|
|
|
|
size_t row_num_,
|
|
|
|
Arena * arena_)
|
|
|
|
: aggregate_functions(aggregate_functions_),
|
|
|
|
offsets_of_aggregate_states(offsets_of_aggregate_states_),
|
|
|
|
aggregate_columns(aggregate_columns_),
|
|
|
|
value(value_), row_num(row_num_), arena(arena_)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename AggregateFunction, size_t column_num>
|
|
|
|
void operator()() ALWAYS_INLINE;
|
|
|
|
|
|
|
|
const Aggregator::AggregateFunctionsPlainPtrs & aggregate_functions;
|
|
|
|
const Sizes & offsets_of_aggregate_states;
|
|
|
|
Aggregator::AggregateColumns & aggregate_columns;
|
|
|
|
AggregateDataPtr & value;
|
|
|
|
size_t row_num;
|
|
|
|
Arena * arena;
|
2015-01-08 21:41:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename AggregateFunction, size_t column_num>
|
|
|
|
void AggregateFunctionsUpdater::operator()()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
static_cast<AggregateFunction *>(aggregate_functions[column_num])->add(
|
|
|
|
value + offsets_of_aggregate_states[column_num],
|
|
|
|
&aggregate_columns[column_num][0],
|
|
|
|
row_num, arena);
|
2015-01-08 21:41:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct AggregateFunctionsCreator
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
AggregateFunctionsCreator(
|
|
|
|
const Aggregator::AggregateFunctionsPlainPtrs & aggregate_functions_,
|
|
|
|
const Sizes & offsets_of_aggregate_states_,
|
|
|
|
Aggregator::AggregateColumns & aggregate_columns_,
|
|
|
|
AggregateDataPtr & aggregate_data_)
|
|
|
|
: aggregate_functions(aggregate_functions_),
|
|
|
|
offsets_of_aggregate_states(offsets_of_aggregate_states_),
|
|
|
|
aggregate_data(aggregate_data_)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename AggregateFunction, size_t column_num>
|
|
|
|
void operator()() ALWAYS_INLINE;
|
|
|
|
|
|
|
|
const Aggregator::AggregateFunctionsPlainPtrs & aggregate_functions;
|
|
|
|
const Sizes & offsets_of_aggregate_states;
|
|
|
|
AggregateDataPtr & aggregate_data;
|
2015-01-08 21:41:35 +00:00
|
|
|
};
|
|
|
|
|
2015-01-11 02:00:26 +00:00
|
|
|
template <typename AggregateFunction, size_t column_num>
|
|
|
|
void AggregateFunctionsCreator::operator()()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
AggregateFunction * func = static_cast<AggregateFunction *>(aggregate_functions[column_num]);
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2017-06-02 21:37:28 +00:00
|
|
|
/** An exception may occur if there is a shortage of memory.
|
|
|
|
* To ensure that everything is properly destroyed, we "roll back" some of the created states.
|
|
|
|
* The code is not very convenient.
|
|
|
|
*/
|
2017-04-01 07:20:54 +00:00
|
|
|
func->create(aggregate_data + offsets_of_aggregate_states[column_num]);
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
for (size_t rollback_j = 0; rollback_j < column_num; ++rollback_j)
|
|
|
|
func->destroy(aggregate_data + offsets_of_aggregate_states[rollback_j]);
|
|
|
|
|
|
|
|
throw;
|
|
|
|
}
|
2015-01-11 02:00:26 +00:00
|
|
|
}
|
|
|
|
|
2015-01-08 21:41:35 +00:00
|
|
|
|
2015-01-11 00:57:21 +00:00
|
|
|
template <typename Method, typename AggregateFunctionsList>
|
|
|
|
void NO_INLINE Aggregator::executeSpecialized(
|
2017-04-01 07:20:54 +00:00
|
|
|
Method & method,
|
|
|
|
Arena * aggregates_pool,
|
|
|
|
size_t rows,
|
|
|
|
ConstColumnPlainPtrs & key_columns,
|
|
|
|
AggregateColumns & aggregate_columns,
|
|
|
|
const Sizes & key_sizes,
|
|
|
|
StringRefs & keys,
|
|
|
|
bool no_more_keys,
|
|
|
|
AggregateDataPtr overflow_row) const
|
2015-01-08 21:41:35 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
typename Method::State state;
|
|
|
|
state.init(key_columns);
|
|
|
|
|
|
|
|
if (!no_more_keys)
|
|
|
|
executeSpecializedCase<false, Method, AggregateFunctionsList>(
|
|
|
|
method, state, aggregates_pool, rows, key_columns, aggregate_columns, key_sizes, keys, overflow_row);
|
|
|
|
else
|
|
|
|
executeSpecializedCase<true, Method, AggregateFunctionsList>(
|
|
|
|
method, state, aggregates_pool, rows, key_columns, aggregate_columns, key_sizes, keys, overflow_row);
|
2015-01-11 00:57:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wuninitialized"
|
2015-01-08 21:41:35 +00:00
|
|
|
|
2015-01-11 00:57:21 +00:00
|
|
|
template <bool no_more_keys, typename Method, typename AggregateFunctionsList>
|
|
|
|
void NO_INLINE Aggregator::executeSpecializedCase(
|
2017-04-01 07:20:54 +00:00
|
|
|
Method & method,
|
|
|
|
typename Method::State & state,
|
|
|
|
Arena * aggregates_pool,
|
|
|
|
size_t rows,
|
|
|
|
ConstColumnPlainPtrs & key_columns,
|
|
|
|
AggregateColumns & aggregate_columns,
|
|
|
|
const Sizes & key_sizes,
|
|
|
|
StringRefs & keys,
|
|
|
|
AggregateDataPtr overflow_row) const
|
2015-01-11 00:57:21 +00:00
|
|
|
{
|
2017-06-02 21:37:28 +00:00
|
|
|
/// For all rows.
|
2017-04-01 07:20:54 +00:00
|
|
|
typename Method::iterator it;
|
|
|
|
typename Method::Key prev_key;
|
|
|
|
for (size_t i = 0; i < rows; ++i)
|
|
|
|
{
|
2017-06-02 21:37:28 +00:00
|
|
|
bool inserted; /// Inserted a new key, or was this key already?
|
|
|
|
bool overflow = false; /// New key did not fit in the hash table because of no_more_keys.
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-06-02 21:37:28 +00:00
|
|
|
/// Get the key to insert into the hash table.
|
2017-04-01 07:20:54 +00:00
|
|
|
typename Method::Key key = state.getKey(key_columns, params.keys_size, i, key_sizes, keys, *aggregates_pool);
|
|
|
|
|
2017-06-02 21:37:28 +00:00
|
|
|
if (!no_more_keys) /// Insert.
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-06-02 21:37:28 +00:00
|
|
|
/// Optimization for frequently repeating keys.
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!Method::no_consecutive_keys_optimization)
|
|
|
|
{
|
|
|
|
if (i != 0 && key == prev_key)
|
|
|
|
{
|
|
|
|
AggregateDataPtr value = Method::getAggregateData(it->second);
|
|
|
|
|
2017-06-02 21:37:28 +00:00
|
|
|
/// Add values into aggregate functions.
|
2017-04-01 07:20:54 +00:00
|
|
|
AggregateFunctionsList::forEach(AggregateFunctionsUpdater(
|
|
|
|
aggregate_functions, offsets_of_aggregate_states, aggregate_columns, value, i, aggregates_pool));
|
|
|
|
|
|
|
|
method.onExistingKey(key, keys, *aggregates_pool);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
prev_key = key;
|
|
|
|
}
|
|
|
|
|
|
|
|
method.data.emplace(key, it, inserted);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-06-02 21:37:28 +00:00
|
|
|
/// Add only if the key already exists.
|
2017-04-01 07:20:54 +00:00
|
|
|
inserted = false;
|
|
|
|
it = method.data.find(key);
|
|
|
|
if (method.data.end() == it)
|
|
|
|
overflow = true;
|
|
|
|
}
|
|
|
|
|
2017-06-02 21:37:28 +00:00
|
|
|
/// If the key does not fit, and the data does not need to be aggregated in a separate row, then there's nothing to do.
|
2017-04-01 07:20:54 +00:00
|
|
|
if (no_more_keys && overflow && !overflow_row)
|
|
|
|
{
|
|
|
|
method.onExistingKey(key, keys, *aggregates_pool);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-06-02 21:37:28 +00:00
|
|
|
/// If a new key is inserted, initialize the states of the aggregate functions, and possibly some stuff related to the key.
|
2017-04-01 07:20:54 +00:00
|
|
|
if (inserted)
|
|
|
|
{
|
|
|
|
AggregateDataPtr & aggregate_data = Method::getAggregateData(it->second);
|
|
|
|
aggregate_data = nullptr;
|
|
|
|
|
|
|
|
method.onNewKey(*it, params.keys_size, i, keys, *aggregates_pool);
|
|
|
|
|
|
|
|
AggregateDataPtr place = aggregates_pool->alloc(total_size_of_aggregate_states);
|
|
|
|
|
|
|
|
AggregateFunctionsList::forEach(AggregateFunctionsCreator(
|
|
|
|
aggregate_functions, offsets_of_aggregate_states, aggregate_columns, place));
|
|
|
|
|
|
|
|
aggregate_data = place;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
method.onExistingKey(key, keys, *aggregates_pool);
|
|
|
|
|
|
|
|
AggregateDataPtr value = (!no_more_keys || !overflow) ? Method::getAggregateData(it->second) : overflow_row;
|
|
|
|
|
2017-06-02 21:37:28 +00:00
|
|
|
/// Add values into the aggregate functions.
|
2017-04-01 07:20:54 +00:00
|
|
|
AggregateFunctionsList::forEach(AggregateFunctionsUpdater(
|
|
|
|
aggregate_functions, offsets_of_aggregate_states, aggregate_columns, value, i, aggregates_pool));
|
|
|
|
}
|
2015-01-08 21:41:35 +00:00
|
|
|
}
|
|
|
|
|
2015-01-11 00:57:21 +00:00
|
|
|
#pragma GCC diagnostic pop
|
|
|
|
|
2015-01-13 03:03:45 +00:00
|
|
|
template <typename AggregateFunctionsList>
|
|
|
|
void NO_INLINE Aggregator::executeSpecializedWithoutKey(
|
2017-04-01 07:20:54 +00:00
|
|
|
AggregatedDataWithoutKey & res,
|
|
|
|
size_t rows,
|
|
|
|
AggregateColumns & aggregate_columns,
|
|
|
|
Arena * arena) const
|
2015-01-13 03:03:45 +00:00
|
|
|
{
|
2017-06-02 21:37:28 +00:00
|
|
|
/// Optimization in the case of a single aggregate function `count`.
|
2017-04-01 07:20:54 +00:00
|
|
|
AggregateFunctionCount * agg_count = params.aggregates_size == 1
|
|
|
|
? typeid_cast<AggregateFunctionCount *>(aggregate_functions[0])
|
|
|
|
: NULL;
|
|
|
|
|
|
|
|
if (agg_count)
|
|
|
|
agg_count->addDelta(res, rows);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < rows; ++i)
|
|
|
|
{
|
|
|
|
AggregateFunctionsList::forEach(AggregateFunctionsUpdater(
|
|
|
|
aggregate_functions, offsets_of_aggregate_states, aggregate_columns, res, i, arena));
|
|
|
|
}
|
|
|
|
}
|
2015-01-13 03:03:45 +00:00
|
|
|
}
|
|
|
|
|
2015-01-08 21:41:35 +00:00
|
|
|
}
|
2015-04-01 04:14:15 +00:00
|
|
|
|
|
|
|
|
2017-09-17 18:00:27 +00:00
|
|
|
/** The main code is compiled with gcc 7.
|
|
|
|
* But SpecializedAggregator is compiled using clang 6 into the .so file.
|
2017-06-02 21:37:28 +00:00
|
|
|
* This is done because gcc can not get functions inlined,
|
|
|
|
* which were de-virtualized, in a particular case, and the performance is lower.
|
|
|
|
* And also it's easier to distribute clang for deploy to the servers.
|
2015-04-01 04:14:15 +00:00
|
|
|
*
|
2017-06-02 21:37:28 +00:00
|
|
|
* After switching from gcc 4.8 and gnu++1x to gcc 4.9 and gnu++1y (and then to gcc 5),
|
|
|
|
* an error occurred with `dlopen`: undefined symbol: __cxa_pure_virtual
|
2015-04-01 04:14:15 +00:00
|
|
|
*
|
2017-06-02 21:37:28 +00:00
|
|
|
* Most likely, this is due to the changed version of this symbol:
|
|
|
|
* gcc creates a symbol in .so
|
2015-04-01 04:14:15 +00:00
|
|
|
* U __cxa_pure_virtual@@CXXABI_1.3
|
2017-06-02 21:37:28 +00:00
|
|
|
* but clang creates a symbol
|
2015-04-01 04:14:15 +00:00
|
|
|
* U __cxa_pure_virtual
|
|
|
|
*
|
2017-06-02 21:37:28 +00:00
|
|
|
* But it does not matter for us how the __cxa_pure_virtual function will be implemented,
|
|
|
|
* because it is not called during normal program execution,
|
|
|
|
* and if called - then the program is guaranteed buggy.
|
2015-04-01 04:14:15 +00:00
|
|
|
*
|
2017-06-02 21:37:28 +00:00
|
|
|
* Therefore, we can work around the problem this way
|
2015-04-01 04:14:15 +00:00
|
|
|
*/
|
|
|
|
extern "C" void __attribute__((__visibility__("default"), __noreturn__)) __cxa_pure_virtual() { abort(); };
|