2017-09-01 12:05:19 +00:00
|
|
|
#include <Common/TypeList.h>
|
2017-09-17 20:22:39 +00:00
|
|
|
#include <Common/typeid_cast.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Interpreters/Aggregator.h>
|
2017-09-18 18:52:12 +00:00
|
|
|
#include <AggregateFunctions/AggregateFunctionCount.h>
|
2015-01-08 21:41:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
|
2017-09-17 18:17:04 +00:00
|
|
|
/** An aggregation loop 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 loop of the aggregate functions should be unrolled.
|
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],
|
2018-09-02 03:33:48 +00:00
|
|
|
aggregate_columns[column_num].data(),
|
2017-04-01 07:20:54 +00:00
|
|
|
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_,
|
|
|
|
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,
|
2017-12-13 01:27:53 +00:00
|
|
|
ColumnRawPtrs & key_columns,
|
2017-04-01 07:20:54 +00:00
|
|
|
AggregateColumns & aggregate_columns,
|
|
|
|
StringRefs & keys,
|
|
|
|
bool no_more_keys,
|
|
|
|
AggregateDataPtr overflow_row) const
|
2015-01-08 21:41:35 +00:00
|
|
|
{
|
2019-02-01 08:23:38 +00:00
|
|
|
typename Method::State state(key_columns, key_sizes, aggregation_state_cache);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
if (!no_more_keys)
|
|
|
|
executeSpecializedCase<false, Method, AggregateFunctionsList>(
|
2018-08-27 18:05:28 +00:00
|
|
|
method, state, aggregates_pool, rows, key_columns, aggregate_columns, keys, overflow_row);
|
2017-04-01 07:20:54 +00:00
|
|
|
else
|
|
|
|
executeSpecializedCase<true, Method, AggregateFunctionsList>(
|
2018-08-27 18:05:28 +00:00
|
|
|
method, state, aggregates_pool, rows, key_columns, aggregate_columns, 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,
|
2019-02-01 08:23:38 +00:00
|
|
|
ColumnRawPtrs & /*key_columns*/,
|
2017-04-01 07:20:54 +00:00
|
|
|
AggregateColumns & aggregate_columns,
|
2019-02-01 08:23:38 +00:00
|
|
|
StringRefs & /*keys*/,
|
2017-04-01 07:20:54 +00:00
|
|
|
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
|
|
|
for (size_t i = 0; i < rows; ++i)
|
|
|
|
{
|
2019-02-01 08:23:38 +00:00
|
|
|
AggregateDataPtr aggregate_data = nullptr;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-06-02 21:37:28 +00:00
|
|
|
if (!no_more_keys) /// Insert.
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2019-02-01 08:23:38 +00:00
|
|
|
auto emplace_result = state.emplaceKey(method.data, i, *aggregates_pool);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2019-02-01 08:23:38 +00:00
|
|
|
/// If a new key is inserted, initialize the states of the aggregate functions, and possibly something related to the key.
|
|
|
|
if (emplace_result.isInserted())
|
2018-12-20 13:28:20 +00:00
|
|
|
{
|
2019-02-01 08:23:38 +00:00
|
|
|
/// exception-safety - if you can not allocate memory or create states, then destructors will not be called.
|
|
|
|
emplace_result.setMapped(nullptr);
|
|
|
|
|
|
|
|
aggregate_data = aggregates_pool->alignedAlloc(total_size_of_aggregate_states, align_aggregate_states);
|
|
|
|
AggregateFunctionsList::forEach(AggregateFunctionsCreator(
|
|
|
|
aggregate_functions, offsets_of_aggregate_states, aggregate_data));
|
|
|
|
|
|
|
|
emplace_result.setMapped(aggregate_data);
|
2018-12-20 13:28:20 +00:00
|
|
|
}
|
2019-02-01 08:23:38 +00:00
|
|
|
else
|
|
|
|
aggregate_data = emplace_result.getMapped();
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-06-02 21:37:28 +00:00
|
|
|
/// Add only if the key already exists.
|
2019-02-01 08:23:38 +00:00
|
|
|
auto find_result = state.findKey(method.data, i, *aggregates_pool);
|
|
|
|
if (find_result.isFound())
|
|
|
|
aggregate_data = find_result.getMapped();
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
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.
|
2018-12-20 13:28:20 +00:00
|
|
|
if (!aggregate_data && !overflow_row)
|
2017-04-01 07:20:54 +00:00
|
|
|
continue;
|
|
|
|
|
2019-02-01 08:23:38 +00:00
|
|
|
auto value = aggregate_data ? aggregate_data : overflow_row;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
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])
|
2017-11-09 13:44:08 +00:00
|
|
|
: nullptr;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
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
|
|
|
*/
|
2018-06-03 20:39:06 +00:00
|
|
|
extern "C" void __attribute__((__visibility__("default"), __noreturn__)) __cxa_pure_virtual() { abort(); }
|