2017-04-01 09:19:00 +00:00
|
|
|
#include <DataStreams/SummingSortedBlockInputStream.h>
|
2017-10-09 05:56:22 +00:00
|
|
|
#include <DataTypes/DataTypesNumber.h>
|
2017-12-25 18:58:39 +00:00
|
|
|
#include <DataTypes/NestedUtils.h>
|
2017-10-19 04:21:48 +00:00
|
|
|
#include <DataTypes/DataTypeTuple.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <DataTypes/DataTypeArray.h>
|
2018-06-21 15:10:32 +00:00
|
|
|
#include <DataTypes/DataTypeAggregateFunction.h>
|
|
|
|
#include <Columns/ColumnAggregateFunction.h>
|
2017-10-09 05:56:22 +00:00
|
|
|
#include <Columns/ColumnTuple.h>
|
2018-01-15 19:07:47 +00:00
|
|
|
#include <Common/StringUtils/StringUtils.h>
|
2017-11-24 13:55:31 +00:00
|
|
|
#include <Common/FieldVisitors.h>
|
2017-01-14 09:00:19 +00:00
|
|
|
#include <common/logger_useful.h>
|
2017-07-13 20:58:19 +00:00
|
|
|
#include <Common/typeid_cast.h>
|
2013-10-01 18:09:31 +00:00
|
|
|
|
2017-10-09 05:56:22 +00:00
|
|
|
#include <AggregateFunctions/AggregateFunctionFactory.h>
|
|
|
|
#include <Functions/FunctionFactory.h>
|
2017-10-19 04:21:48 +00:00
|
|
|
#include <Functions/FunctionHelpers.h>
|
2017-10-09 05:56:22 +00:00
|
|
|
#include <Interpreters/Context.h>
|
2013-10-01 18:09:31 +00:00
|
|
|
|
2018-04-05 21:36:55 +00:00
|
|
|
|
2013-10-01 18:09:31 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2017-12-15 00:01:59 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int LOGICAL_ERROR;
|
|
|
|
}
|
|
|
|
|
2013-10-01 18:09:31 +00:00
|
|
|
|
2018-04-07 01:46:50 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
bool isInPrimaryKey(const SortDescription & description, const std::string & name, const size_t number)
|
|
|
|
{
|
|
|
|
for (auto & desc : description)
|
|
|
|
if (desc.column_name == name || (desc.column_name.empty() && desc.column_number == number))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SummingSortedBlockInputStream::SummingSortedBlockInputStream(
|
|
|
|
const BlockInputStreams & inputs_,
|
|
|
|
const SortDescription & description_,
|
|
|
|
/// List of columns to be summed. If empty, all numeric columns that are not in the description are taken.
|
|
|
|
const Names & column_names_to_sum,
|
2019-02-19 01:47:27 +00:00
|
|
|
size_t max_block_size_)
|
2018-04-07 01:46:50 +00:00
|
|
|
: MergingSortedBlockInputStream(inputs_, description_, max_block_size_)
|
|
|
|
{
|
|
|
|
current_row.resize(num_columns);
|
|
|
|
|
|
|
|
/// name of nested structure -> the column numbers that refer to it.
|
|
|
|
std::unordered_map<std::string, std::vector<size_t>> discovered_maps;
|
|
|
|
|
|
|
|
/** Fill in the column numbers, which must be summed.
|
|
|
|
* This can only be numeric columns that are not part of the sort key.
|
|
|
|
* If a non-empty column_names_to_sum is specified, then we only take these columns.
|
|
|
|
* Some columns from column_names_to_sum may not be found. This is ignored.
|
|
|
|
*/
|
|
|
|
for (size_t i = 0; i < num_columns; ++i)
|
|
|
|
{
|
|
|
|
const ColumnWithTypeAndName & column = header.safeGetByPosition(i);
|
|
|
|
|
|
|
|
/// Discover nested Maps and find columns for summation
|
|
|
|
if (typeid_cast<const DataTypeArray *>(column.type.get()))
|
|
|
|
{
|
|
|
|
const auto map_name = Nested::extractTableName(column.name);
|
|
|
|
/// if nested table name ends with `Map` it is a possible candidate for special handling
|
|
|
|
if (map_name == column.name || !endsWith(map_name, "Map"))
|
|
|
|
{
|
|
|
|
column_numbers_not_to_aggregate.push_back(i);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
discovered_maps[map_name].emplace_back(i);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-09-07 14:37:26 +00:00
|
|
|
bool is_agg_func = WhichDataType(column.type).isAggregateFunction();
|
2018-07-04 20:46:17 +00:00
|
|
|
if (!column.type->isSummable() && !is_agg_func)
|
2018-04-07 01:46:50 +00:00
|
|
|
{
|
|
|
|
column_numbers_not_to_aggregate.push_back(i);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Are they inside the PK?
|
|
|
|
if (isInPrimaryKey(description, column.name, i))
|
|
|
|
{
|
|
|
|
column_numbers_not_to_aggregate.push_back(i);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (column_names_to_sum.empty()
|
|
|
|
|| column_names_to_sum.end() !=
|
|
|
|
std::find(column_names_to_sum.begin(), column_names_to_sum.end(), column.name))
|
|
|
|
{
|
|
|
|
// Create aggregator to sum this column
|
|
|
|
AggregateDescription desc;
|
2018-07-04 20:46:17 +00:00
|
|
|
desc.is_agg_func_type = is_agg_func;
|
2018-04-07 01:46:50 +00:00
|
|
|
desc.column_numbers = {i};
|
2018-06-21 15:10:32 +00:00
|
|
|
|
2018-07-04 20:46:17 +00:00
|
|
|
if (!is_agg_func)
|
2018-06-21 15:10:32 +00:00
|
|
|
{
|
|
|
|
desc.init("sumWithOverflow", {column.type});
|
|
|
|
}
|
|
|
|
|
2018-04-07 01:46:50 +00:00
|
|
|
columns_to_aggregate.emplace_back(std::move(desc));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Column is not going to be summed, use last value
|
|
|
|
column_numbers_not_to_aggregate.push_back(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// select actual nested Maps from list of candidates
|
|
|
|
for (const auto & map : discovered_maps)
|
|
|
|
{
|
|
|
|
/// map should contain at least two elements (key -> value)
|
|
|
|
if (map.second.size() < 2)
|
|
|
|
{
|
|
|
|
for (auto col : map.second)
|
|
|
|
column_numbers_not_to_aggregate.push_back(col);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// no elements of map could be in primary key
|
|
|
|
auto column_num_it = map.second.begin();
|
|
|
|
for (; column_num_it != map.second.end(); ++column_num_it)
|
|
|
|
if (isInPrimaryKey(description, header.safeGetByPosition(*column_num_it).name, *column_num_it))
|
|
|
|
break;
|
|
|
|
if (column_num_it != map.second.end())
|
|
|
|
{
|
|
|
|
for (auto col : map.second)
|
|
|
|
column_numbers_not_to_aggregate.push_back(col);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
DataTypes argument_types;
|
|
|
|
AggregateDescription desc;
|
|
|
|
MapDescription map_desc;
|
|
|
|
|
|
|
|
column_num_it = map.second.begin();
|
|
|
|
for (; column_num_it != map.second.end(); ++column_num_it)
|
|
|
|
{
|
|
|
|
const ColumnWithTypeAndName & key_col = header.safeGetByPosition(*column_num_it);
|
|
|
|
const String & name = key_col.name;
|
|
|
|
const IDataType & nested_type = *static_cast<const DataTypeArray *>(key_col.type.get())->getNestedType();
|
|
|
|
|
|
|
|
if (column_num_it == map.second.begin()
|
|
|
|
|| endsWith(name, "ID")
|
|
|
|
|| endsWith(name, "Key")
|
|
|
|
|| endsWith(name, "Type"))
|
|
|
|
{
|
|
|
|
if (!nested_type.isValueRepresentedByInteger())
|
|
|
|
break;
|
|
|
|
|
|
|
|
map_desc.key_col_nums.push_back(*column_num_it);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!nested_type.isSummable())
|
|
|
|
break;
|
|
|
|
|
|
|
|
map_desc.val_col_nums.push_back(*column_num_it);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add column to function arguments
|
|
|
|
desc.column_numbers.push_back(*column_num_it);
|
|
|
|
argument_types.push_back(key_col.type);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (column_num_it != map.second.end())
|
|
|
|
{
|
|
|
|
for (auto col : map.second)
|
|
|
|
column_numbers_not_to_aggregate.push_back(col);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (map_desc.key_col_nums.size() == 1)
|
|
|
|
{
|
|
|
|
// Create summation for all value columns in the map
|
2019-01-25 19:36:18 +00:00
|
|
|
desc.init("sumMapWithOverflow", argument_types);
|
2018-04-07 01:46:50 +00:00
|
|
|
columns_to_aggregate.emplace_back(std::move(desc));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Fall back to legacy mergeMaps for composite keys
|
|
|
|
for (auto col : map.second)
|
|
|
|
column_numbers_not_to_aggregate.push_back(col);
|
|
|
|
maps_to_sum.emplace_back(std::move(map_desc));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-08-03 16:27:27 +00:00
|
|
|
void SummingSortedBlockInputStream::insertCurrentRowIfNeeded(MutableColumns & merged_columns)
|
2013-10-01 18:09:31 +00:00
|
|
|
{
|
2017-10-09 05:56:22 +00:00
|
|
|
for (auto & desc : columns_to_aggregate)
|
|
|
|
{
|
|
|
|
// Do not insert if the aggregation state hasn't been created
|
|
|
|
if (desc.created)
|
|
|
|
{
|
2018-07-04 20:46:17 +00:00
|
|
|
if (desc.is_agg_func_type)
|
2017-10-12 21:54:49 +00:00
|
|
|
{
|
2018-06-26 10:28:42 +00:00
|
|
|
current_row_is_zero = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
try
|
2018-06-21 15:10:32 +00:00
|
|
|
{
|
|
|
|
desc.function->insertResultInto(desc.state.data(), *desc.merged_column);
|
2017-11-16 19:03:32 +00:00
|
|
|
|
2018-06-26 10:28:42 +00:00
|
|
|
/// Update zero status of current row
|
|
|
|
if (desc.column_numbers.size() == 1)
|
|
|
|
{
|
|
|
|
// Flag row as non-empty if at least one column number if non-zero
|
2018-10-11 14:33:01 +00:00
|
|
|
current_row_is_zero = current_row_is_zero && desc.merged_column->isDefaultAt(desc.merged_column->size() - 1);
|
2018-06-26 10:28:42 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-01-25 19:36:18 +00:00
|
|
|
/// It is sumMapWithOverflow aggregate function.
|
2018-06-26 10:28:42 +00:00
|
|
|
/// Assume that the row isn't empty in this case (just because it is compatible with previous version)
|
|
|
|
current_row_is_zero = false;
|
|
|
|
}
|
2017-11-16 19:03:32 +00:00
|
|
|
}
|
2018-06-26 10:28:42 +00:00
|
|
|
catch (...)
|
2017-11-16 19:03:32 +00:00
|
|
|
{
|
2018-06-26 10:28:42 +00:00
|
|
|
desc.destroyState();
|
|
|
|
throw;
|
2017-11-16 19:03:32 +00:00
|
|
|
}
|
2017-10-12 21:54:49 +00:00
|
|
|
}
|
2017-11-28 02:14:50 +00:00
|
|
|
desc.destroyState();
|
2017-10-09 05:56:22 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
desc.merged_column->insertDefault();
|
|
|
|
}
|
|
|
|
|
2018-08-03 16:27:27 +00:00
|
|
|
/// If it is "zero" row, then rollback the insertion
|
|
|
|
/// (at this moment we need rollback only cols from columns_to_aggregate)
|
|
|
|
if (current_row_is_zero)
|
2017-11-16 19:03:32 +00:00
|
|
|
{
|
|
|
|
for (auto & desc : columns_to_aggregate)
|
|
|
|
desc.merged_column->popBack(1);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-10-09 05:56:22 +00:00
|
|
|
for (auto i : column_numbers_not_to_aggregate)
|
2017-04-01 07:20:54 +00:00
|
|
|
merged_columns[i]->insert(current_row[i]);
|
2017-11-16 19:03:32 +00:00
|
|
|
|
|
|
|
/// Update per-block and per-group flags
|
|
|
|
++merged_rows;
|
2013-10-01 18:09:31 +00:00
|
|
|
}
|
2013-11-30 18:43:59 +00:00
|
|
|
|
2013-10-01 18:09:31 +00:00
|
|
|
|
|
|
|
Block SummingSortedBlockInputStream::readImpl()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
if (finished)
|
|
|
|
return Block();
|
|
|
|
|
2017-12-15 00:01:59 +00:00
|
|
|
MutableColumns merged_columns;
|
2018-04-07 01:46:50 +00:00
|
|
|
init(merged_columns);
|
2017-12-15 00:01:59 +00:00
|
|
|
|
|
|
|
if (has_collation)
|
|
|
|
throw Exception("Logical error: " + getName() + " does not support collations", ErrorCodes::LOGICAL_ERROR);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
if (merged_columns.empty())
|
2017-12-15 00:01:59 +00:00
|
|
|
return {};
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2018-04-07 01:46:50 +00:00
|
|
|
/// Update aggregation result columns for current block
|
2017-10-19 04:21:48 +00:00
|
|
|
for (auto & desc : columns_to_aggregate)
|
|
|
|
{
|
|
|
|
// Wrap aggregated columns in a tuple to match function signature
|
2018-09-07 14:37:26 +00:00
|
|
|
if (!desc.is_agg_func_type && isTuple(desc.function->getReturnType()))
|
2017-10-19 04:21:48 +00:00
|
|
|
{
|
2017-12-08 00:50:25 +00:00
|
|
|
size_t tuple_size = desc.column_numbers.size();
|
2018-03-20 14:17:09 +00:00
|
|
|
MutableColumns tuple_columns(tuple_size);
|
2017-12-08 00:50:25 +00:00
|
|
|
for (size_t i = 0; i < tuple_size; ++i)
|
2018-05-14 10:48:50 +00:00
|
|
|
tuple_columns[i] = header.safeGetByPosition(desc.column_numbers[i]).column->cloneEmpty();
|
2017-10-19 04:21:48 +00:00
|
|
|
|
2018-03-20 14:17:09 +00:00
|
|
|
desc.merged_column = ColumnTuple::create(std::move(tuple_columns));
|
2017-10-19 04:21:48 +00:00
|
|
|
}
|
|
|
|
else
|
2017-12-15 00:01:59 +00:00
|
|
|
desc.merged_column = header.safeGetByPosition(desc.column_numbers[0]).column->cloneEmpty();
|
2017-10-19 04:21:48 +00:00
|
|
|
}
|
|
|
|
|
2018-08-10 04:02:56 +00:00
|
|
|
merge(merged_columns, queue_without_collation);
|
2017-12-17 05:21:04 +00:00
|
|
|
Block res = header.cloneWithColumns(std::move(merged_columns));
|
|
|
|
|
|
|
|
/// Place aggregation results into block.
|
|
|
|
for (auto & desc : columns_to_aggregate)
|
|
|
|
{
|
2018-09-07 14:37:26 +00:00
|
|
|
if (!desc.is_agg_func_type && isTuple(desc.function->getReturnType()))
|
2017-12-17 05:21:04 +00:00
|
|
|
{
|
|
|
|
/// Unpack tuple into block.
|
|
|
|
size_t tuple_size = desc.column_numbers.size();
|
|
|
|
for (size_t i = 0; i < tuple_size; ++i)
|
|
|
|
res.getByPosition(desc.column_numbers[i]).column = static_cast<const ColumnTuple &>(*desc.merged_column).getColumnPtr(i);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
res.getByPosition(desc.column_numbers[0]).column = std::move(desc.merged_column);
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
2013-10-01 18:09:31 +00:00
|
|
|
}
|
|
|
|
|
2013-11-30 18:43:59 +00:00
|
|
|
|
2017-12-15 00:01:59 +00:00
|
|
|
void SummingSortedBlockInputStream::merge(MutableColumns & merged_columns, std::priority_queue<SortCursor> & queue)
|
2014-11-22 02:22:30 +00:00
|
|
|
{
|
2017-11-16 19:03:32 +00:00
|
|
|
merged_rows = 0;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-12-15 00:01:59 +00:00
|
|
|
/// Take the rows in needed order and put them in `merged_columns` until rows no more than `max_block_size`
|
2017-04-01 07:20:54 +00:00
|
|
|
while (!queue.empty())
|
|
|
|
{
|
2017-12-15 00:01:59 +00:00
|
|
|
SortCursor current = queue.top();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
setPrimaryKeyRef(next_key, current);
|
|
|
|
|
|
|
|
bool key_differs;
|
|
|
|
|
|
|
|
if (current_key.empty()) /// The first key encountered.
|
2018-01-29 17:42:19 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
key_differs = true;
|
2018-07-17 12:39:55 +00:00
|
|
|
current_row_is_zero = true;
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
key_differs = next_key != current_key;
|
|
|
|
|
|
|
|
if (key_differs)
|
|
|
|
{
|
2018-07-17 12:39:55 +00:00
|
|
|
if (!current_key.empty())
|
|
|
|
/// Write the data for the previous group.
|
2018-08-03 16:27:27 +00:00
|
|
|
insertCurrentRowIfNeeded(merged_columns);
|
2018-07-17 12:39:55 +00:00
|
|
|
|
|
|
|
if (merged_rows >= max_block_size)
|
|
|
|
{
|
|
|
|
/// The block is now full and the last row is calculated completely.
|
|
|
|
current_key.reset();
|
|
|
|
return;
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
current_key.swap(next_key);
|
|
|
|
|
|
|
|
setRow(current_row, current);
|
2017-10-09 05:56:22 +00:00
|
|
|
|
|
|
|
/// Reset aggregation states for next row
|
|
|
|
for (auto & desc : columns_to_aggregate)
|
2017-11-28 02:14:50 +00:00
|
|
|
desc.createState();
|
2017-10-17 21:23:14 +00:00
|
|
|
|
|
|
|
// Start aggregations with current row
|
2017-12-01 20:21:35 +00:00
|
|
|
addRow(current);
|
2018-04-06 18:09:20 +00:00
|
|
|
|
|
|
|
if (maps_to_sum.empty())
|
|
|
|
{
|
|
|
|
/// We have only columns_to_aggregate. The status of current row will be determined
|
|
|
|
/// in 'insertCurrentRowIfNeeded' method on the values of aggregate functions.
|
|
|
|
current_row_is_zero = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/// We have complex maps that will be summed with 'mergeMap' method.
|
|
|
|
/// The single row is considered non zero, and the status after merging with other rows
|
|
|
|
/// will be determined in the branch below (when key_differs == false).
|
|
|
|
current_row_is_zero = false;
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-12-01 20:21:35 +00:00
|
|
|
addRow(current);
|
2017-10-17 21:23:14 +00:00
|
|
|
|
2017-10-09 05:56:22 +00:00
|
|
|
// Merge maps only for same rows
|
2018-04-05 21:36:55 +00:00
|
|
|
for (const auto & desc : maps_to_sum)
|
2017-10-12 21:09:42 +00:00
|
|
|
if (mergeMap(desc, current_row, current))
|
2017-10-09 05:56:22 +00:00
|
|
|
current_row_is_zero = false;
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2018-07-17 12:39:55 +00:00
|
|
|
queue.pop();
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!current->isLast())
|
|
|
|
{
|
|
|
|
current->next();
|
|
|
|
queue.push(current);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/// We get the next block from the corresponding source, if there is one.
|
|
|
|
fetchNextBlock(current, queue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// We will write the data for the last group, if it is non-zero.
|
|
|
|
/// If it is zero, and without it the output stream will be empty, we will write it anyway.
|
2018-08-03 16:27:27 +00:00
|
|
|
insertCurrentRowIfNeeded(merged_columns);
|
2017-04-01 07:20:54 +00:00
|
|
|
finished = true;
|
2013-10-01 18:09:31 +00:00
|
|
|
}
|
|
|
|
|
2017-12-15 00:01:59 +00:00
|
|
|
|
|
|
|
bool SummingSortedBlockInputStream::mergeMap(const MapDescription & desc, Row & row, SortCursor & cursor)
|
2016-04-24 06:05:41 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Strongly non-optimal.
|
|
|
|
|
|
|
|
Row & left = row;
|
|
|
|
Row right(left.size());
|
|
|
|
|
|
|
|
for (size_t col_num : desc.key_col_nums)
|
|
|
|
right[col_num] = (*cursor->all_columns[col_num])[cursor->pos].template get<Array>();
|
|
|
|
|
|
|
|
for (size_t col_num : desc.val_col_nums)
|
|
|
|
right[col_num] = (*cursor->all_columns[col_num])[cursor->pos].template get<Array>();
|
|
|
|
|
|
|
|
auto at_ith_column_jth_row = [&](const Row & matrix, size_t i, size_t j) -> const Field &
|
|
|
|
{
|
|
|
|
return matrix[i].get<Array>()[j];
|
|
|
|
};
|
|
|
|
|
|
|
|
auto tuple_of_nth_columns_at_jth_row = [&](const Row & matrix, const ColumnNumbers & col_nums, size_t j) -> Array
|
|
|
|
{
|
|
|
|
size_t size = col_nums.size();
|
|
|
|
Array res(size);
|
|
|
|
for (size_t col_num_index = 0; col_num_index < size; ++col_num_index)
|
|
|
|
res[col_num_index] = at_ith_column_jth_row(matrix, col_nums[col_num_index], j);
|
|
|
|
return res;
|
|
|
|
};
|
|
|
|
|
|
|
|
std::map<Array, Array> merged;
|
|
|
|
|
|
|
|
auto accumulate = [](Array & dst, const Array & src)
|
|
|
|
{
|
|
|
|
bool has_non_zero = false;
|
|
|
|
size_t size = dst.size();
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
if (applyVisitor(FieldVisitorSum(src[i]), dst[i]))
|
|
|
|
has_non_zero = true;
|
|
|
|
return has_non_zero;
|
|
|
|
};
|
|
|
|
|
|
|
|
auto merge = [&](const Row & matrix)
|
|
|
|
{
|
|
|
|
size_t rows = matrix[desc.key_col_nums[0]].get<Array>().size();
|
|
|
|
|
|
|
|
for (size_t j = 0; j < rows; ++j)
|
|
|
|
{
|
|
|
|
Array key = tuple_of_nth_columns_at_jth_row(matrix, desc.key_col_nums, j);
|
|
|
|
Array value = tuple_of_nth_columns_at_jth_row(matrix, desc.val_col_nums, j);
|
|
|
|
|
|
|
|
auto it = merged.find(key);
|
|
|
|
if (merged.end() == it)
|
|
|
|
merged.emplace(std::move(key), std::move(value));
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!accumulate(it->second, value))
|
|
|
|
merged.erase(it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
merge(left);
|
|
|
|
merge(right);
|
|
|
|
|
|
|
|
for (size_t col_num : desc.key_col_nums)
|
|
|
|
row[col_num] = Array(merged.size());
|
|
|
|
for (size_t col_num : desc.val_col_nums)
|
|
|
|
row[col_num] = Array(merged.size());
|
|
|
|
|
|
|
|
size_t row_num = 0;
|
|
|
|
for (const auto & key_value : merged)
|
|
|
|
{
|
|
|
|
for (size_t col_num_index = 0, size = desc.key_col_nums.size(); col_num_index < size; ++col_num_index)
|
|
|
|
row[desc.key_col_nums[col_num_index]].get<Array>()[row_num] = key_value.first[col_num_index];
|
|
|
|
|
|
|
|
for (size_t col_num_index = 0, size = desc.val_col_nums.size(); col_num_index < size; ++col_num_index)
|
|
|
|
row[desc.val_col_nums[col_num_index]].get<Array>()[row_num] = key_value.second[col_num_index];
|
|
|
|
|
|
|
|
++row_num;
|
|
|
|
}
|
|
|
|
|
|
|
|
return row_num != 0;
|
2015-11-16 04:00:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-15 00:01:59 +00:00
|
|
|
void SummingSortedBlockInputStream::addRow(SortCursor & cursor)
|
2015-11-16 04:00:46 +00:00
|
|
|
{
|
2017-10-09 05:56:22 +00:00
|
|
|
for (auto & desc : columns_to_aggregate)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2018-07-17 12:39:55 +00:00
|
|
|
if (!desc.created)
|
|
|
|
throw Exception("Logical error in SummingSortedBlockInputStream, there are no description", ErrorCodes::LOGICAL_ERROR);
|
|
|
|
|
2018-07-04 20:46:17 +00:00
|
|
|
if (desc.is_agg_func_type)
|
2017-10-09 05:56:22 +00:00
|
|
|
{
|
2018-06-26 10:28:42 +00:00
|
|
|
// desc.state is not used for AggregateFunction types
|
2017-11-16 19:03:32 +00:00
|
|
|
auto & col = cursor->all_columns[desc.column_numbers[0]];
|
2018-06-26 10:28:42 +00:00
|
|
|
static_cast<ColumnAggregateFunction &>(*desc.merged_column).insertMergeFrom(*col, cursor->pos);
|
2017-10-09 05:56:22 +00:00
|
|
|
}
|
2017-11-16 19:03:32 +00:00
|
|
|
else
|
|
|
|
{
|
2018-06-26 10:28:42 +00:00
|
|
|
// Specialized case for unary functions
|
|
|
|
if (desc.column_numbers.size() == 1)
|
|
|
|
{
|
|
|
|
auto & col = cursor->all_columns[desc.column_numbers[0]];
|
|
|
|
desc.add_function(desc.function.get(), desc.state.data(), &col, cursor->pos, nullptr);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Gather all source columns into a vector
|
|
|
|
ColumnRawPtrs columns(desc.column_numbers.size());
|
|
|
|
for (size_t i = 0; i < desc.column_numbers.size(); ++i)
|
|
|
|
columns[i] = cursor->all_columns[desc.column_numbers[i]];
|
2015-11-16 04:00:46 +00:00
|
|
|
|
2018-06-26 10:28:42 +00:00
|
|
|
desc.add_function(desc.function.get(), desc.state.data(), columns.data(), cursor->pos, nullptr);
|
|
|
|
}
|
2017-11-16 19:03:32 +00:00
|
|
|
}
|
|
|
|
}
|
2015-11-16 04:00:46 +00:00
|
|
|
}
|
|
|
|
|
2013-10-01 18:09:31 +00:00
|
|
|
}
|