ClickHouse/dbms/DataStreams/CollapsingFinalBlockInputStream.h

212 lines
6.3 KiB
C++
Raw Normal View History

#pragma once
2020-03-20 03:32:47 +00:00
2015-09-29 19:19:54 +00:00
#include <common/logger_useful.h>
#include <DataStreams/IBlockInputStream.h>
#include <Core/SortDescription.h>
#include <Columns/ColumnsNumber.h>
2017-07-13 20:58:19 +00:00
#include <Common/typeid_cast.h>
#include <queue>
2020-03-18 18:26:40 +00:00
#include <cassert>
2020-03-20 03:32:47 +00:00
#include <boost/intrusive_ptr.hpp>
2020-03-18 18:26:40 +00:00
namespace DB
{
2020-02-25 18:10:48 +00:00
namespace ErrorCodes
{
extern const int BAD_TYPE_OF_FIELD;
}
2020-03-20 03:32:47 +00:00
2017-05-13 22:19:04 +00:00
/// Collapses the same rows with the opposite sign roughly like CollapsingSortedBlockInputStream.
/// Outputs the rows in random order (the input streams must still be ordered).
/// Outputs only rows with a positive sign.
class CollapsingFinalBlockInputStream : public IBlockInputStream
{
public:
CollapsingFinalBlockInputStream(
const BlockInputStreams & inputs,
const SortDescription & description_,
const String & sign_column_name_)
: description(description_), sign_column_name(sign_column_name_)
{
children.insert(children.end(), inputs.begin(), inputs.end());
}
~CollapsingFinalBlockInputStream() override;
String getName() const override { return "CollapsingFinal"; }
bool isSortedOutput() const override { return true; }
const SortDescription & getSortDescription() const override { return description; }
Block getHeader() const override { return children.at(0)->getHeader(); }
struct MergingBlock;
using BlockPlainPtrs = std::vector<MergingBlock*>;
struct MergingBlock : boost::noncopyable
{
MergingBlock(const Block & block_,
size_t stream_index_,
const SortDescription & desc,
2019-08-03 11:02:40 +00:00
const String & sign_column_name_,
BlockPlainPtrs * output_blocks_)
: block(block_), stream_index(stream_index_), output_blocks(output_blocks_)
{
sort_columns.resize(desc.size());
for (size_t i = 0; i < desc.size(); ++i)
{
size_t column_number = !desc[i].column_name.empty()
? block.getPositionByName(desc[i].column_name)
: desc[i].column_number;
sort_columns[i] = block.safeGetByPosition(column_number).column.get();
}
2019-08-03 11:02:40 +00:00
const IColumn * sign_icolumn = block.getByName(sign_column_name_).column.get();
sign_column = typeid_cast<const ColumnInt8 *>(sign_icolumn);
if (!sign_column)
throw Exception("Sign column must have type Int8", ErrorCodes::BAD_TYPE_OF_FIELD);
rows = sign_column->size();
2017-05-13 22:19:04 +00:00
/// Filled entirely with zeros. Then `1` are set in the positions of the rows to be left.
filter.resize_fill(rows);
}
Block block;
2017-05-13 22:19:04 +00:00
/// Rows with the same key will be sorted in ascending order of stream_index.
size_t stream_index;
size_t rows;
2017-05-13 22:19:04 +00:00
/// Which rows should be left. Filled when the threads merge.
IColumn::Filter filter;
2017-05-13 22:19:04 +00:00
/// Point to `block`.
ColumnRawPtrs sort_columns;
const ColumnInt8 * sign_column;
2017-05-13 22:19:04 +00:00
/// When it reaches zero, the block can be outputted in response.
int refcount = 0;
2017-05-13 22:19:04 +00:00
/// Where to put the block when it is ready to be outputted in response.
BlockPlainPtrs * output_blocks;
};
2020-03-20 03:32:47 +00:00
private:
Block readImpl() override;
2020-03-20 03:32:47 +00:00
/// When deleting the last block reference, adds a block to `output_blocks`.
using MergingBlockPtr = boost::intrusive_ptr<MergingBlock>;
struct Cursor
{
MergingBlockPtr block;
2019-04-22 16:07:09 +00:00
size_t pos = 0;
Cursor() {}
explicit Cursor(const MergingBlockPtr & block_, size_t pos_ = 0) : block(block_), pos(pos_) {}
bool operator< (const Cursor & rhs) const
{
for (size_t i = 0; i < block->sort_columns.size(); ++i)
{
int res = block->sort_columns[i]->compareAt(pos, rhs.pos, *(rhs.block->sort_columns[i]), 1);
if (res > 0)
return true;
if (res < 0)
return false;
}
return block->stream_index > rhs.block->stream_index;
}
2017-05-13 22:19:04 +00:00
/// Not consistent with operator< : does not consider order.
bool equal(const Cursor & rhs) const
{
if (!block || !rhs.block)
return false;
for (size_t i = 0; i < block->sort_columns.size(); ++i)
{
int res = block->sort_columns[i]->compareAt(pos, rhs.pos, *(rhs.block->sort_columns[i]), 1);
if (res != 0)
return false;
}
return true;
}
Int8 getSign()
{
return block->sign_column->getData()[pos];
}
2017-05-13 22:19:04 +00:00
/// Indicates that this row should be outputted in response.
void addToFilter()
{
block->filter[pos] = 1;
}
bool isLast()
{
return pos + 1 == block->rows;
}
void next()
{
++pos;
}
};
using Queue = std::priority_queue<Cursor>;
const SortDescription description;
String sign_column_name;
Logger * log = &Logger::get("CollapsingFinalBlockInputStream");
bool first = true;
BlockPlainPtrs output_blocks;
Queue queue;
2017-05-13 22:19:04 +00:00
Cursor previous; /// The current primary key.
Cursor last_positive; /// The last positive row for the current primary key.
2017-05-13 22:19:04 +00:00
size_t count_positive = 0; /// The number of positive rows for the current primary key.
size_t count_negative = 0; /// The number of negative rows for the current primary key.
bool last_is_positive = false; /// true if the last row for the current primary key is positive.
2017-05-13 22:19:04 +00:00
size_t count_incorrect_data = 0; /// To prevent too many error messages from writing to the log.
2017-05-13 22:19:04 +00:00
/// Count the number of blocks fetched and outputted.
size_t blocks_fetched = 0;
size_t blocks_output = 0;
void fetchNextBlock(size_t input_index);
void commitCurrent();
void reportBadCounts();
void reportBadSign(Int8 sign);
};
2020-03-20 03:32:47 +00:00
inline void intrusive_ptr_add_ref(CollapsingFinalBlockInputStream::MergingBlock * ptr)
{
++ptr->refcount;
}
inline void intrusive_ptr_release(CollapsingFinalBlockInputStream::MergingBlock * ptr)
{
if (0 == --ptr->refcount)
ptr->output_blocks->push_back(ptr);
}
}