ClickHouse/dbms/src/DataStreams/DistinctBlockInputStream.cpp

153 lines
3.8 KiB
C++
Raw Normal View History

#include <DB/DataStreams/DistinctBlockInputStream.h>
namespace DB
{
namespace ErrorCodes
{
extern const int SET_SIZE_LIMIT_EXCEEDED;
}
DistinctBlockInputStream::DistinctBlockInputStream(BlockInputStreamPtr input_, const Limits & limits, size_t limit_hint_, Names columns_)
: columns_names(columns_)
, limit_hint(limit_hint_)
, max_rows(limits.max_rows_in_distinct)
, max_bytes(limits.max_bytes_in_distinct)
, overflow_mode(limits.distinct_overflow_mode)
{
children.push_back(input_);
}
String DistinctBlockInputStream::getID() const
{
std::stringstream res;
res << "Distinct(" << children.back()->getID() << ")";
return res.str();
}
Block DistinctBlockInputStream::readImpl()
2016-12-23 08:01:35 +00:00
{
2016-12-22 22:19:57 +00:00
/// Execute until end of stream or until
/// a block with some new records will be gotten.
while (1)
{
2016-12-22 22:19:57 +00:00
/// Stop reading if we already reach the limit.
if (limit_hint && data.getTotalRowCount() >= limit_hint)
return Block();
Block block = children[0]->read();
if (!block)
return Block();
2016-12-22 17:00:23 +00:00
const ConstColumnPlainPtrs column_ptrs(getKeyColumns(block));
if (column_ptrs.empty())
return block;
if (data.empty())
data.init(SetVariants::chooseMethod(column_ptrs, key_sizes));
2016-12-22 17:00:23 +00:00
const size_t old_set_size = data.getTotalRowCount();
const size_t rows = block.rows();
IColumn::Filter filter(rows);
switch (data.type)
{
case SetVariants::Type::EMPTY:
break;
#define M(NAME) \
case SetVariants::Type::NAME: \
buildFilter(*data.NAME, column_ptrs, filter, rows, data); \
break;
APPLY_FOR_SET_VARIANTS(M)
#undef M
}
2016-12-22 22:19:57 +00:00
/// Just go to the next block if there isn't any new record in the current one.
if (data.getTotalRowCount() == old_set_size)
continue;
if (!checkLimits())
{
if (overflow_mode == OverflowMode::THROW)
throw Exception("DISTINCT-Set size limit exceeded."
" Rows: " + toString(data.getTotalRowCount()) +
", limit: " + toString(max_rows) +
". Bytes: " + toString(data.getTotalByteCount()) +
", limit: " + toString(max_bytes) + ".",
ErrorCodes::SET_SIZE_LIMIT_EXCEEDED);
if (overflow_mode == OverflowMode::BREAK)
return Block();
throw Exception("Logical error: unknown overflow mode", ErrorCodes::LOGICAL_ERROR);
}
size_t all_columns = block.columns();
for (size_t i = 0; i < all_columns; ++i)
block.safeGetByPosition(i).column = block.safeGetByPosition(i).column->filter(filter, -1);
return block;
}
}
2016-12-22 17:00:23 +00:00
bool DistinctBlockInputStream::checkLimits() const
{
if (max_rows && data.getTotalRowCount() > max_rows)
return false;
if (max_bytes && data.getTotalByteCount() > max_bytes)
return false;
return true;
}
template <typename Method>
2016-12-22 17:00:23 +00:00
void DistinctBlockInputStream::buildFilter(
Method & method,
const ConstColumnPlainPtrs & columns,
IColumn::Filter & filter,
size_t rows,
SetVariants & variants) const
{
typename Method::State state;
state.init(columns);
for (size_t i = 0; i < rows; ++i)
{
2016-12-22 22:19:57 +00:00
/// Make a key.
2016-12-22 17:00:23 +00:00
typename Method::Key key = state.getKey(columns, columns.size(), i, key_sizes);
typename Method::Data::iterator it = method.data.find(key);
bool inserted;
method.data.emplace(key, it, inserted);
if (inserted)
method.onNewKey(*it, columns.size(), i, variants.string_pool);
2016-12-22 22:19:57 +00:00
/// Emit the record if there is no such key in the current set yet.
/// Skip it otherwise.
filter[i] = inserted;
}
}
2016-12-22 17:00:23 +00:00
ConstColumnPlainPtrs DistinctBlockInputStream::getKeyColumns(const Block & block) const
{
2016-12-22 17:00:23 +00:00
size_t columns = columns_names.empty() ? block.columns() : columns_names.size();
ConstColumnPlainPtrs column_ptrs;
column_ptrs.reserve(columns);
for (size_t i = 0; i < columns; ++i)
{
auto & column = columns_names.empty()
? block.safeGetByPosition(i).column
2016-12-22 17:00:23 +00:00
: block.getByName(columns_names[i]).column;
2016-12-22 22:19:57 +00:00
/// Ignore all constant columns.
2016-12-22 17:00:23 +00:00
if (!column->isConst())
column_ptrs.emplace_back(column.get());
}
return column_ptrs;
}
}