2014-02-27 12:49:21 +00:00
|
|
|
#include <DB/DataStreams/TotalsHavingBlockInputStream.h>
|
2016-01-13 00:32:59 +00:00
|
|
|
#include <DB/Interpreters/ExpressionActions.h>
|
|
|
|
#include <DB/Interpreters/AggregateDescription.h>
|
2014-02-27 12:49:21 +00:00
|
|
|
#include <DB/Columns/ColumnAggregateFunction.h>
|
|
|
|
#include <DB/Columns/ColumnsNumber.h>
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2016-01-11 21:46:36 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int ILLEGAL_COLUMN;
|
|
|
|
extern const int ILLEGAL_TYPE_OF_COLUMN_FOR_FILTER;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-13 00:32:59 +00:00
|
|
|
TotalsHavingBlockInputStream::TotalsHavingBlockInputStream(
|
|
|
|
BlockInputStreamPtr input_,
|
|
|
|
bool overflow_row_, ExpressionActionsPtr expression_,
|
|
|
|
const std::string & filter_column_, TotalsMode totals_mode_, double auto_include_threshold_)
|
|
|
|
: overflow_row(overflow_row_),
|
|
|
|
expression(expression_), filter_column_name(filter_column_), totals_mode(totals_mode_),
|
|
|
|
auto_include_threshold(auto_include_threshold_)
|
|
|
|
{
|
|
|
|
children.push_back(input_);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
String TotalsHavingBlockInputStream::getID() const
|
|
|
|
{
|
|
|
|
std::stringstream res;
|
|
|
|
res << "TotalsHavingBlockInputStream(" << children.back()->getID()
|
|
|
|
<< "," << filter_column_name << ")";
|
|
|
|
return res.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-27 12:49:21 +00:00
|
|
|
static void finalize(Block & block)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < block.columns(); ++i)
|
|
|
|
{
|
2017-01-02 20:12:12 +00:00
|
|
|
ColumnWithTypeAndName & current = block.safeGetByPosition(i);
|
2014-06-26 00:58:14 +00:00
|
|
|
ColumnAggregateFunction * unfinalized_column = typeid_cast<ColumnAggregateFunction *>(&*current.column);
|
2014-02-27 12:49:21 +00:00
|
|
|
if (unfinalized_column)
|
|
|
|
{
|
|
|
|
current.type = unfinalized_column->getAggregateFunction()->getReturnType();
|
|
|
|
current.column = unfinalized_column->convertToValues();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-21 16:09:04 +00:00
|
|
|
|
|
|
|
const Block & TotalsHavingBlockInputStream::getTotals()
|
|
|
|
{
|
|
|
|
if (!totals)
|
|
|
|
{
|
2017-03-26 01:28:07 +00:00
|
|
|
/** If totals_mode == AFTER_HAVING_AUTO, you need to decide whether to add aggregates to TOTALS for strings,
|
2017-03-25 20:12:56 +00:00
|
|
|
* not passed max_rows_to_group_by.
|
2014-04-21 16:09:04 +00:00
|
|
|
*/
|
2015-01-02 05:28:21 +00:00
|
|
|
if (overflow_aggregates)
|
|
|
|
{
|
|
|
|
if (totals_mode == TotalsMode::BEFORE_HAVING
|
|
|
|
|| totals_mode == TotalsMode::AFTER_HAVING_INCLUSIVE
|
|
|
|
|| (totals_mode == TotalsMode::AFTER_HAVING_AUTO
|
|
|
|
&& static_cast<double>(passed_keys) / total_keys >= auto_include_threshold))
|
|
|
|
addToTotals(current_totals, overflow_aggregates, nullptr);
|
|
|
|
}
|
2014-04-21 16:09:04 +00:00
|
|
|
|
|
|
|
finalize(current_totals);
|
|
|
|
totals = current_totals;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (totals && expression)
|
|
|
|
expression->execute(totals);
|
|
|
|
|
|
|
|
return totals;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-27 12:49:21 +00:00
|
|
|
Block TotalsHavingBlockInputStream::readImpl()
|
|
|
|
{
|
|
|
|
Block finalized;
|
|
|
|
Block block;
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
block = children[0]->read();
|
|
|
|
|
2017-03-26 01:20:25 +00:00
|
|
|
/// Block with values not included in `max_rows_to_group_by`. We'll postpone it.
|
2015-01-03 03:18:49 +00:00
|
|
|
if (overflow_row && block && block.info.is_overflows)
|
2015-01-02 05:28:21 +00:00
|
|
|
{
|
|
|
|
overflow_aggregates = block;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-02-27 12:49:21 +00:00
|
|
|
if (!block)
|
|
|
|
return finalized;
|
|
|
|
|
|
|
|
finalized = block;
|
|
|
|
finalize(finalized);
|
|
|
|
|
2015-01-02 05:28:21 +00:00
|
|
|
total_keys += finalized.rows();
|
2014-02-27 12:49:21 +00:00
|
|
|
|
2015-01-02 05:28:21 +00:00
|
|
|
if (filter_column_name.empty())
|
2014-02-27 12:49:21 +00:00
|
|
|
{
|
|
|
|
addToTotals(current_totals, block, nullptr);
|
|
|
|
}
|
2015-01-02 05:28:21 +00:00
|
|
|
else
|
2014-02-27 12:49:21 +00:00
|
|
|
{
|
2017-03-25 20:12:56 +00:00
|
|
|
/// Compute the expression in HAVING.
|
2014-02-27 12:49:21 +00:00
|
|
|
expression->execute(finalized);
|
|
|
|
|
|
|
|
size_t filter_column_pos = finalized.getPositionByName(filter_column_name);
|
2017-01-02 20:12:12 +00:00
|
|
|
ColumnPtr filter_column_ptr = finalized.safeGetByPosition(filter_column_pos).column;
|
2014-02-27 12:49:21 +00:00
|
|
|
|
2014-06-26 00:58:14 +00:00
|
|
|
ColumnConstUInt8 * column_const = typeid_cast<ColumnConstUInt8 *>(&*filter_column_ptr);
|
2014-02-27 12:49:21 +00:00
|
|
|
if (column_const)
|
|
|
|
filter_column_ptr = column_const->convertToFullColumn();
|
|
|
|
|
2014-06-26 00:58:14 +00:00
|
|
|
ColumnUInt8 * filter_column = typeid_cast<ColumnUInt8 *>(&*filter_column_ptr);
|
2014-02-27 12:49:21 +00:00
|
|
|
if (!filter_column)
|
|
|
|
throw Exception("Filter column must have type UInt8, found " +
|
2017-01-02 20:12:12 +00:00
|
|
|
finalized.safeGetByPosition(filter_column_pos).type->getName(),
|
2014-02-27 12:49:21 +00:00
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_COLUMN_FOR_FILTER);
|
|
|
|
|
|
|
|
IColumn::Filter & filter = filter_column->getData();
|
|
|
|
|
2017-03-26 01:20:25 +00:00
|
|
|
/// Add values to `totals` (if it was not already done).
|
2015-01-02 05:28:21 +00:00
|
|
|
if (totals_mode == TotalsMode::BEFORE_HAVING)
|
|
|
|
addToTotals(current_totals, block, nullptr);
|
|
|
|
else
|
|
|
|
addToTotals(current_totals, block, &filter);
|
2014-02-27 12:49:21 +00:00
|
|
|
|
2017-03-25 20:12:56 +00:00
|
|
|
/// Filter the block by expression in HAVING.
|
2014-02-27 12:49:21 +00:00
|
|
|
size_t columns = finalized.columns();
|
|
|
|
|
|
|
|
for (size_t i = 0; i < columns; ++i)
|
|
|
|
{
|
2017-01-02 20:12:12 +00:00
|
|
|
ColumnWithTypeAndName & current_column = finalized.safeGetByPosition(i);
|
2015-12-05 07:01:18 +00:00
|
|
|
current_column.column = current_column.column->filter(filter, -1);
|
2014-02-27 12:49:21 +00:00
|
|
|
if (current_column.column->empty())
|
|
|
|
{
|
|
|
|
finalized.clear();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!finalized)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
passed_keys += finalized.rows();
|
|
|
|
return finalized;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-02 05:28:21 +00:00
|
|
|
void TotalsHavingBlockInputStream::addToTotals(Block & totals, Block & block, const IColumn::Filter * filter)
|
2014-02-27 12:49:21 +00:00
|
|
|
{
|
|
|
|
bool init = !totals;
|
|
|
|
|
|
|
|
ArenaPtr arena;
|
|
|
|
if (init)
|
2016-05-28 10:15:36 +00:00
|
|
|
arena = std::make_shared<Arena>();
|
2014-02-27 12:49:21 +00:00
|
|
|
|
|
|
|
for (size_t i = 0; i < block.columns(); ++i)
|
|
|
|
{
|
2017-01-02 20:12:12 +00:00
|
|
|
const ColumnWithTypeAndName & current = block.safeGetByPosition(i);
|
2014-06-26 00:58:14 +00:00
|
|
|
const ColumnAggregateFunction * column = typeid_cast<const ColumnAggregateFunction *>(&*current.column);
|
2014-02-27 12:49:21 +00:00
|
|
|
|
|
|
|
if (!column)
|
|
|
|
{
|
|
|
|
if (init)
|
|
|
|
{
|
|
|
|
ColumnPtr new_column = current.type->createColumn();
|
2016-11-18 08:29:45 +00:00
|
|
|
new_column->insert(current.type->getDefault());
|
2015-07-17 01:27:35 +00:00
|
|
|
totals.insert(ColumnWithTypeAndName(new_column, current.type, current.name));
|
2014-02-27 12:49:21 +00:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
IAggregateFunction * function;
|
|
|
|
AggregateDataPtr data;
|
|
|
|
|
|
|
|
if (init)
|
|
|
|
{
|
2016-05-28 16:19:27 +00:00
|
|
|
function = column->getAggregateFunction().get();
|
2016-05-28 05:31:36 +00:00
|
|
|
auto target = std::make_shared<ColumnAggregateFunction>(column->getAggregateFunction(), Arenas(1, arena));
|
2015-07-17 01:27:35 +00:00
|
|
|
totals.insert(ColumnWithTypeAndName(target, current.type, current.name));
|
2014-02-27 12:49:21 +00:00
|
|
|
|
|
|
|
data = arena->alloc(function->sizeOfData());
|
|
|
|
function->create(data);
|
|
|
|
target->getData().push_back(data);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-01-02 20:12:12 +00:00
|
|
|
auto target = typeid_cast<ColumnAggregateFunction *>(totals.safeGetByPosition(i).column.get());
|
2014-02-27 12:49:21 +00:00
|
|
|
if (!target)
|
2017-01-02 20:12:12 +00:00
|
|
|
throw Exception("Unexpected type of column: " + totals.safeGetByPosition(i).column->getName(),
|
2014-02-27 12:49:21 +00:00
|
|
|
ErrorCodes::ILLEGAL_COLUMN);
|
2016-05-28 16:19:27 +00:00
|
|
|
function = target->getAggregateFunction().get();
|
2014-02-27 12:49:21 +00:00
|
|
|
data = target->getData()[0];
|
|
|
|
}
|
|
|
|
|
2014-04-21 16:09:04 +00:00
|
|
|
const ColumnAggregateFunction::Container_t & vec = column->getData();
|
2015-01-02 05:28:21 +00:00
|
|
|
size_t size = vec.size();
|
2014-02-27 12:49:21 +00:00
|
|
|
|
|
|
|
if (filter)
|
|
|
|
{
|
|
|
|
for (size_t j = 0; j < size; ++j)
|
|
|
|
if ((*filter)[j])
|
2016-09-23 23:33:17 +00:00
|
|
|
function->merge(data, vec[j], arena.get());
|
2014-02-27 12:49:21 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (size_t j = 0; j < size; ++j)
|
2016-09-23 23:33:17 +00:00
|
|
|
function->merge(data, vec[j], arena.get());
|
2014-02-27 12:49:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|