2017-04-06 17:21:45 +00:00
|
|
|
#include "MergeTreeBaseBlockInputStream.h"
|
|
|
|
#include <Storages/MergeTree/MergeTreeReader.h>
|
|
|
|
#include <Storages/MergeTree/MergeTreeBlockReadUtils.h>
|
|
|
|
#include <Columns/ColumnConst.h>
|
2017-07-28 14:42:21 +00:00
|
|
|
#include <Columns/ColumnArray.h>
|
2017-07-13 20:58:19 +00:00
|
|
|
#include <Common/typeid_cast.h>
|
2017-06-06 17:18:32 +00:00
|
|
|
#include <ext/range.h>
|
2017-04-06 17:21:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int ILLEGAL_TYPE_OF_COLUMN_FOR_FILTER;
|
2017-08-01 13:04:48 +00:00
|
|
|
extern const int LOGICAL_ERROR;
|
2017-04-06 17:21:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
MergeTreeBaseBlockInputStream::MergeTreeBaseBlockInputStream(
|
|
|
|
MergeTreeData & storage,
|
|
|
|
const ExpressionActionsPtr & prewhere_actions,
|
|
|
|
const String & prewhere_column,
|
|
|
|
size_t max_block_size_rows,
|
|
|
|
size_t preferred_block_size_bytes,
|
2017-06-30 16:28:27 +00:00
|
|
|
size_t preferred_max_column_in_block_size_bytes,
|
2017-04-06 17:21:45 +00:00
|
|
|
size_t min_bytes_to_use_direct_io,
|
|
|
|
size_t max_read_buffer_size,
|
|
|
|
bool use_uncompressed_cache,
|
|
|
|
bool save_marks_in_cache,
|
|
|
|
const Names & virt_column_names)
|
|
|
|
:
|
|
|
|
storage(storage),
|
|
|
|
prewhere_actions(prewhere_actions),
|
|
|
|
prewhere_column(prewhere_column),
|
|
|
|
max_block_size_rows(max_block_size_rows),
|
|
|
|
preferred_block_size_bytes(preferred_block_size_bytes),
|
2017-06-30 16:28:27 +00:00
|
|
|
preferred_max_column_in_block_size_bytes(preferred_max_column_in_block_size_bytes),
|
2017-04-06 17:21:45 +00:00
|
|
|
min_bytes_to_use_direct_io(min_bytes_to_use_direct_io),
|
|
|
|
max_read_buffer_size(max_read_buffer_size),
|
|
|
|
use_uncompressed_cache(use_uncompressed_cache),
|
|
|
|
save_marks_in_cache(save_marks_in_cache),
|
|
|
|
virt_column_names(virt_column_names),
|
|
|
|
max_block_size_marks(max_block_size_rows / storage.index_granularity)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Block MergeTreeBaseBlockInputStream::readImpl()
|
|
|
|
{
|
|
|
|
Block res;
|
|
|
|
|
|
|
|
while (!res && !isCancelled())
|
|
|
|
{
|
|
|
|
if (!task && !getNewTask())
|
|
|
|
break;
|
|
|
|
|
|
|
|
res = readFromPart();
|
|
|
|
|
|
|
|
if (res)
|
|
|
|
injectVirtualColumns(res);
|
|
|
|
|
2017-06-15 17:01:13 +00:00
|
|
|
if (task->isFinished())
|
2017-04-10 17:10:33 +00:00
|
|
|
task.reset();
|
2017-04-10 14:06:44 +00:00
|
|
|
}
|
|
|
|
|
2017-04-06 17:21:45 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Block MergeTreeBaseBlockInputStream::readFromPart()
|
|
|
|
{
|
|
|
|
Block res;
|
|
|
|
|
2017-04-07 11:43:24 +00:00
|
|
|
if (task->size_predictor)
|
|
|
|
task->size_predictor->startBlock();
|
|
|
|
|
2017-07-11 09:32:39 +00:00
|
|
|
const auto max_block_size_rows = this->max_block_size_rows;
|
2017-06-20 10:12:20 +00:00
|
|
|
const auto preferred_block_size_bytes = this->preferred_block_size_bytes;
|
2017-07-29 21:52:35 +00:00
|
|
|
const auto preferred_max_column_in_block_size_bytes = this->preferred_max_column_in_block_size_bytes;
|
2017-06-21 17:19:35 +00:00
|
|
|
const auto index_granularity = storage.index_granularity;
|
2017-06-30 16:28:27 +00:00
|
|
|
const double min_filtration_ratio = 0.00001;
|
2017-06-21 17:19:35 +00:00
|
|
|
|
2017-07-11 09:32:39 +00:00
|
|
|
auto estimateNumRows = [preferred_block_size_bytes, max_block_size_rows,
|
2017-06-30 16:28:27 +00:00
|
|
|
index_granularity, preferred_max_column_in_block_size_bytes, min_filtration_ratio](
|
2017-06-21 17:19:35 +00:00
|
|
|
MergeTreeReadTask & task, MergeTreeRangeReader & reader)
|
|
|
|
{
|
|
|
|
if (!task.size_predictor)
|
2017-07-12 11:40:00 +00:00
|
|
|
return max_block_size_rows;
|
2017-06-30 16:28:27 +00:00
|
|
|
|
2017-07-24 15:06:32 +00:00
|
|
|
/// Calculates number of rows will be read using preferred_block_size_bytes.
|
2017-07-24 13:59:03 +00:00
|
|
|
/// Can't be less than index_granularity.
|
|
|
|
size_t rows_to_read = task.size_predictor->estimateNumRows(preferred_block_size_bytes);
|
|
|
|
if (!rows_to_read)
|
|
|
|
return rows_to_read;
|
|
|
|
rows_to_read = std::max(index_granularity, rows_to_read);
|
2017-07-21 17:45:51 +00:00
|
|
|
|
|
|
|
if (preferred_max_column_in_block_size_bytes)
|
|
|
|
{
|
2017-07-24 13:59:03 +00:00
|
|
|
/// Calculates number of rows will be read using preferred_max_column_in_block_size_bytes.
|
2017-07-21 17:45:51 +00:00
|
|
|
size_t rows_to_read_for_max_size_column
|
|
|
|
= task.size_predictor->estimateNumRowsForMaxSizeColumn(preferred_max_column_in_block_size_bytes);
|
|
|
|
double filtration_ratio = std::max(min_filtration_ratio, 1.0 - task.size_predictor->filtered_rows_ratio);
|
|
|
|
size_t rows_to_read_for_max_size_column_with_filtration
|
|
|
|
= static_cast<size_t>(rows_to_read_for_max_size_column / filtration_ratio);
|
2017-07-21 18:02:02 +00:00
|
|
|
|
|
|
|
/// If preferred_max_column_in_block_size_bytes is used, number of rows to read can be less than index_granularity.
|
2017-07-21 17:45:51 +00:00
|
|
|
rows_to_read = std::min(rows_to_read, rows_to_read_for_max_size_column_with_filtration);
|
|
|
|
}
|
2017-07-11 14:16:00 +00:00
|
|
|
|
|
|
|
size_t unread_rows_in_current_granule = reader.unreadRowsInCurrentGranule();
|
|
|
|
if (unread_rows_in_current_granule >= rows_to_read)
|
|
|
|
return rows_to_read;
|
|
|
|
|
|
|
|
size_t granule_to_read = (rows_to_read + reader.readRowsInCurrentGranule() + index_granularity / 2) / index_granularity;
|
|
|
|
return index_granularity * granule_to_read - reader.readRowsInCurrentGranule();
|
2017-06-21 17:19:35 +00:00
|
|
|
};
|
2017-06-20 10:12:20 +00:00
|
|
|
|
2017-07-11 09:32:39 +00:00
|
|
|
// read rows from reader and clear columns
|
2017-07-24 05:22:24 +00:00
|
|
|
auto skipRows = [&estimateNumRows](
|
2017-06-21 17:19:35 +00:00
|
|
|
Block & block, MergeTreeRangeReader & reader, MergeTreeReadTask & task, size_t rows)
|
|
|
|
{
|
|
|
|
size_t recommended_rows = rows;
|
|
|
|
if (task.size_predictor)
|
|
|
|
recommended_rows = std::max<size_t>(1, estimateNumRows(task, reader));
|
2017-06-20 10:12:20 +00:00
|
|
|
while (rows)
|
|
|
|
{
|
|
|
|
size_t rows_to_skip = std::min(rows, recommended_rows);
|
|
|
|
rows -= rows_to_skip;
|
|
|
|
reader.read(block, rows_to_skip);
|
|
|
|
for (const auto i : ext::range(0, block.columns()))
|
|
|
|
{
|
2017-07-11 09:32:39 +00:00
|
|
|
auto & col = block.getByPosition(i);
|
2017-06-20 10:12:20 +00:00
|
|
|
if (task.column_name_set.count(col.name))
|
2017-07-28 14:42:21 +00:00
|
|
|
{
|
|
|
|
if (ColumnArray * column_array = typeid_cast<ColumnArray *>(col.column.get()))
|
|
|
|
{
|
2017-07-28 19:29:21 +00:00
|
|
|
/// ColumnArray columns in block could have common offset column, which is used while reading.
|
|
|
|
/// This is in case of nested data structures.
|
2017-07-29 21:52:35 +00:00
|
|
|
|
2017-07-28 14:42:21 +00:00
|
|
|
/// Have to call resize(0) instead of cloneEmpty to save structure.
|
2017-07-28 19:29:21 +00:00
|
|
|
/// (To keep offsets possibly shared between different arrays.)
|
2017-07-28 14:42:21 +00:00
|
|
|
column_array->getOffsets().resize(0);
|
|
|
|
/// It's ok until multidimensional arrays are not stored in MergeTree.
|
|
|
|
column_array->getDataPtr() = column_array->getDataPtr()->cloneEmpty();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
col.column = col.column->cloneEmpty();
|
|
|
|
}
|
2017-06-20 10:12:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-04-06 17:21:45 +00:00
|
|
|
if (prewhere_actions)
|
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
|
|
|
/// Let's read the full block of columns needed to calculate the expression in PREWHERE.
|
|
|
|
MarkRanges ranges_to_read;
|
2017-07-07 18:12:25 +00:00
|
|
|
/// Last range may be partl read. The same number of rows we need to read after prewhere
|
|
|
|
size_t rows_was_read_in_last_range = 0;
|
2017-06-21 17:19:35 +00:00
|
|
|
std::experimental::optional<MergeTreeRangeReader> pre_range_reader;
|
2017-04-06 17:21:45 +00:00
|
|
|
|
2017-07-07 18:12:25 +00:00
|
|
|
auto processNextRange = [& ranges_to_read, & rows_was_read_in_last_range, & pre_range_reader](
|
2017-06-21 17:19:35 +00:00
|
|
|
MergeTreeReadTask & task, MergeTreeReader & pre_reader)
|
2017-04-10 17:10:33 +00:00
|
|
|
{
|
2017-06-21 17:19:35 +00:00
|
|
|
auto & range = task.mark_ranges.back();
|
|
|
|
pre_range_reader = pre_reader.readRange(range.begin, range.end);
|
|
|
|
ranges_to_read.push_back(range);
|
2017-07-07 18:12:25 +00:00
|
|
|
rows_was_read_in_last_range = 0;
|
2017-06-21 17:19:35 +00:00
|
|
|
task.mark_ranges.pop_back();
|
|
|
|
};
|
2017-04-10 17:10:33 +00:00
|
|
|
|
2017-06-15 17:01:13 +00:00
|
|
|
if (task->current_range_reader)
|
2017-06-16 20:11:02 +00:00
|
|
|
{
|
2017-07-07 18:12:25 +00:00
|
|
|
/// Havn't finihsed reading at last step. Copy state for prewhere columns
|
2017-06-20 10:12:20 +00:00
|
|
|
pre_range_reader = task->current_range_reader->copyForReader(*pre_reader);
|
2017-06-20 13:46:24 +00:00
|
|
|
if (task->number_of_rows_to_skip)
|
2017-06-20 10:12:20 +00:00
|
|
|
{
|
2017-07-07 18:12:25 +00:00
|
|
|
/// number_of_rows_to_skip already was read for prewhere columns. skip them.
|
2017-06-21 17:19:35 +00:00
|
|
|
pre_range_reader = pre_range_reader->getFutureState(task->number_of_rows_to_skip);
|
2017-06-20 10:12:20 +00:00
|
|
|
pre_range_reader->disableNextSeek();
|
|
|
|
}
|
2017-04-10 17:10:33 +00:00
|
|
|
}
|
2017-06-21 17:19:35 +00:00
|
|
|
else
|
|
|
|
processNextRange(*task, *pre_reader);
|
|
|
|
|
|
|
|
/// FIXME: size prediction model is updated by filtered rows, but it predicts size of unfiltered rows also
|
|
|
|
size_t recommended_rows = estimateNumRows(*task, *pre_range_reader);
|
|
|
|
if (res && recommended_rows < 1)
|
|
|
|
break;
|
2017-09-01 17:21:03 +00:00
|
|
|
size_t space_left = std::max(static_cast<decltype(max_block_size_rows)>(1), std::min(max_block_size_rows, recommended_rows));
|
2017-04-10 17:10:33 +00:00
|
|
|
|
2017-06-20 10:12:20 +00:00
|
|
|
while ((pre_range_reader || !task->mark_ranges.empty()) && space_left && !isCancelled())
|
2017-04-06 17:21:45 +00:00
|
|
|
{
|
2017-06-20 10:12:20 +00:00
|
|
|
if (!pre_range_reader)
|
2017-06-21 17:19:35 +00:00
|
|
|
processNextRange(*task, *pre_reader);
|
2017-04-06 17:21:45 +00:00
|
|
|
|
2017-06-20 10:12:20 +00:00
|
|
|
size_t rows_to_read = std::min(pre_range_reader->unreadRows(), space_left);
|
|
|
|
size_t read_rows = pre_range_reader->read(res, rows_to_read);
|
2017-07-07 18:12:25 +00:00
|
|
|
rows_was_read_in_last_range += read_rows;
|
2017-06-20 10:12:20 +00:00
|
|
|
if (pre_range_reader->isReadingFinished())
|
|
|
|
pre_range_reader = std::experimental::nullopt;
|
2017-04-06 17:21:45 +00:00
|
|
|
|
2017-06-16 20:11:02 +00:00
|
|
|
space_left -= read_rows;
|
2017-04-06 17:21:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// In case of isCancelled.
|
|
|
|
if (!res)
|
2017-08-31 18:49:48 +00:00
|
|
|
{
|
|
|
|
task->current_range_reader = std::experimental::nullopt;
|
2017-08-31 19:14:38 +00:00
|
|
|
return res;
|
2017-08-31 18:49:48 +00:00
|
|
|
}
|
|
|
|
|
2017-04-06 17:21:45 +00:00
|
|
|
progressImpl({ res.rows(), res.bytes() });
|
|
|
|
pre_reader->fillMissingColumns(res, task->ordered_names, task->should_reorder);
|
|
|
|
|
|
|
|
/// Compute the expression in PREWHERE.
|
|
|
|
prewhere_actions->execute(res);
|
|
|
|
|
|
|
|
ColumnPtr column = res.getByName(prewhere_column).column;
|
|
|
|
if (task->remove_prewhere_column)
|
|
|
|
res.erase(prewhere_column);
|
|
|
|
|
|
|
|
const auto pre_bytes = res.bytes();
|
|
|
|
|
|
|
|
ColumnPtr observed_column;
|
|
|
|
if (column->isNullable())
|
|
|
|
{
|
|
|
|
ColumnNullable & nullable_col = static_cast<ColumnNullable &>(*column);
|
|
|
|
observed_column = nullable_col.getNestedColumn();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
observed_column = column;
|
|
|
|
|
|
|
|
/** If the filter is a constant (for example, it says PREWHERE 1),
|
2017-07-11 09:32:39 +00:00
|
|
|
* then either return an empty block, or return the block unchanged.
|
|
|
|
*/
|
2017-07-21 06:35:58 +00:00
|
|
|
if (observed_column->isConst())
|
2017-04-06 17:21:45 +00:00
|
|
|
{
|
2017-07-21 06:35:58 +00:00
|
|
|
if (!static_cast<const ColumnConst &>(*observed_column).getValue<UInt8>())
|
2017-04-06 17:21:45 +00:00
|
|
|
{
|
2017-06-20 10:12:20 +00:00
|
|
|
if (pre_range_reader)
|
2017-06-16 20:11:02 +00:00
|
|
|
{
|
2017-07-07 18:12:25 +00:00
|
|
|
/// Have to read rows from last partly read granula.
|
2017-08-17 19:36:59 +00:00
|
|
|
if (!ranges_to_read.empty())
|
|
|
|
{
|
|
|
|
auto & range = ranges_to_read.back();
|
|
|
|
task->current_range_reader = reader->readRange(range.begin, range.end);
|
|
|
|
}
|
2017-07-07 18:12:25 +00:00
|
|
|
/// But can just skip them.
|
|
|
|
task->number_of_rows_to_skip = rows_was_read_in_last_range;
|
2017-06-16 20:11:02 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
task->current_range_reader = std::experimental::nullopt;
|
|
|
|
|
2017-04-06 17:21:45 +00:00
|
|
|
res.clear();
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2017-06-16 20:11:02 +00:00
|
|
|
if (task->current_range_reader)
|
2017-06-15 17:01:13 +00:00
|
|
|
{
|
2017-06-20 13:46:24 +00:00
|
|
|
if (task->number_of_rows_to_skip)
|
|
|
|
skipRows(res, *task->current_range_reader, *task, task->number_of_rows_to_skip);
|
2017-07-07 18:12:25 +00:00
|
|
|
size_t rows_to_read = ranges_to_read.empty()
|
|
|
|
? rows_was_read_in_last_range : task->current_range_reader->unreadRows();
|
|
|
|
task->current_range_reader->read(res, rows_to_read);
|
2017-06-16 20:11:02 +00:00
|
|
|
}
|
2017-06-15 17:01:13 +00:00
|
|
|
|
2017-07-07 18:12:25 +00:00
|
|
|
for (auto range_idx : ext::range(0, ranges_to_read.size()))
|
2017-06-16 20:11:02 +00:00
|
|
|
{
|
2017-07-07 18:12:25 +00:00
|
|
|
const auto & range = ranges_to_read[range_idx];
|
2017-06-16 20:11:02 +00:00
|
|
|
task->current_range_reader = reader->readRange(range.begin, range.end);
|
2017-07-07 18:12:25 +00:00
|
|
|
size_t rows_to_read = range_idx + 1 == ranges_to_read.size()
|
|
|
|
? rows_was_read_in_last_range : task->current_range_reader->unreadRows();
|
|
|
|
task->current_range_reader->read(res, rows_to_read);
|
2017-06-15 17:01:13 +00:00
|
|
|
}
|
2017-04-06 17:21:45 +00:00
|
|
|
|
2017-06-20 10:12:20 +00:00
|
|
|
if (!pre_range_reader)
|
2017-06-16 20:11:02 +00:00
|
|
|
task->current_range_reader = std::experimental::nullopt;
|
2017-06-20 13:46:24 +00:00
|
|
|
task->number_of_rows_to_skip = 0;
|
2017-04-06 17:21:45 +00:00
|
|
|
|
|
|
|
progressImpl({ 0, res.bytes() - pre_bytes });
|
|
|
|
}
|
|
|
|
else if (const auto column_vec = typeid_cast<const ColumnUInt8 *>(observed_column.get()))
|
|
|
|
{
|
|
|
|
const auto & pre_filter = column_vec->getData();
|
2017-06-21 17:19:35 +00:00
|
|
|
auto & number_of_rows_to_skip = task->number_of_rows_to_skip;
|
2017-06-20 10:12:20 +00:00
|
|
|
if (!task->current_range_reader)
|
2017-06-21 17:19:35 +00:00
|
|
|
number_of_rows_to_skip = 0;
|
2017-04-06 17:21:45 +00:00
|
|
|
IColumn::Filter post_filter(pre_filter.size());
|
|
|
|
|
|
|
|
/// Let's read the rest of the columns in the required segments and compose our own filter for them.
|
|
|
|
size_t pre_filter_pos = 0;
|
|
|
|
size_t post_filter_pos = 0;
|
|
|
|
|
2017-06-15 17:01:13 +00:00
|
|
|
size_t next_range_idx = 0;
|
|
|
|
while (pre_filter_pos < pre_filter.size())
|
2017-04-06 17:21:45 +00:00
|
|
|
{
|
2017-06-15 17:01:13 +00:00
|
|
|
if (!task->current_range_reader)
|
|
|
|
{
|
2017-06-16 20:11:02 +00:00
|
|
|
if (next_range_idx == ranges_to_read.size())
|
2017-08-01 13:04:48 +00:00
|
|
|
throw Exception("Not enough ranges to read after prewhere.", ErrorCodes::LOGICAL_ERROR);
|
2017-06-15 17:01:13 +00:00
|
|
|
const auto & range = ranges_to_read[next_range_idx++];
|
|
|
|
task->current_range_reader = reader->readRange(range.begin, range.end);
|
|
|
|
}
|
2017-10-04 00:22:00 +00:00
|
|
|
MergeTreeRangeReader & range_reader = *task->current_range_reader;
|
2017-06-16 20:11:02 +00:00
|
|
|
size_t current_range_rows_read = 0;
|
2017-04-06 17:21:45 +00:00
|
|
|
auto pre_filter_begin_pos = pre_filter_pos;
|
|
|
|
|
2017-07-07 18:12:25 +00:00
|
|
|
/// Now we need to read the same number of rows as in prewhere.
|
|
|
|
size_t rows_to_read = next_range_idx == ranges_to_read.size()
|
2017-08-01 13:04:48 +00:00
|
|
|
? rows_was_read_in_last_range : (task->current_range_reader->unreadRows() - number_of_rows_to_skip);
|
2017-04-06 17:21:45 +00:00
|
|
|
|
2017-07-07 18:12:25 +00:00
|
|
|
auto readRows = [&]()
|
|
|
|
{
|
|
|
|
if (pre_filter_pos != pre_filter_begin_pos)
|
2017-04-06 17:21:45 +00:00
|
|
|
{
|
2017-07-07 18:12:25 +00:00
|
|
|
/// Fulfilling the promise to read (pre_filter_pos - pre_filter_begin_pos) rows
|
|
|
|
auto rows = pre_filter_pos - pre_filter_begin_pos;
|
|
|
|
memcpy(&post_filter[post_filter_pos], &pre_filter[pre_filter_begin_pos], rows);
|
|
|
|
post_filter_pos += rows;
|
|
|
|
current_range_rows_read += rows;
|
|
|
|
if (number_of_rows_to_skip)
|
|
|
|
{
|
|
|
|
/** Wasn't able to skip 'number_of_rows_to_skip' with false prewhere conditon
|
|
|
|
* Just read them and throw away. */
|
|
|
|
skipRows(res, range_reader, *task, number_of_rows_to_skip);
|
|
|
|
number_of_rows_to_skip = 0;
|
|
|
|
}
|
|
|
|
range_reader.read(res, rows);
|
2017-04-06 17:21:45 +00:00
|
|
|
}
|
2017-07-07 18:12:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/** (pre_filter_pos - pre_filter_begin_pos) here is the number of rows we promies to read, but
|
|
|
|
haven't read yet to merge consecutive nonempy granulas. */
|
|
|
|
while (current_range_rows_read + (pre_filter_pos - pre_filter_begin_pos) < rows_to_read)
|
2017-04-06 17:21:45 +00:00
|
|
|
{
|
2017-06-15 17:01:13 +00:00
|
|
|
auto rows_should_be_copied = pre_filter_pos - pre_filter_begin_pos;
|
2017-06-21 17:19:35 +00:00
|
|
|
auto range_reader_with_skipped_rows = range_reader.getFutureState(number_of_rows_to_skip + rows_should_be_copied);
|
2017-06-20 10:12:20 +00:00
|
|
|
auto unread_rows_in_current_granule = range_reader_with_skipped_rows.unreadRowsInCurrentGranule();
|
|
|
|
|
|
|
|
const size_t limit = std::min(pre_filter.size(), pre_filter_pos + unread_rows_in_current_granule);
|
|
|
|
bool will_read_until_mark = unread_rows_in_current_granule == limit - pre_filter_pos;
|
2017-06-16 20:11:02 +00:00
|
|
|
|
2017-04-06 17:21:45 +00:00
|
|
|
UInt8 nonzero = 0;
|
2017-06-15 17:01:13 +00:00
|
|
|
for (size_t row = pre_filter_pos; row < limit; ++row)
|
|
|
|
nonzero |= pre_filter[row];
|
2017-04-06 17:21:45 +00:00
|
|
|
|
|
|
|
if (!nonzero)
|
|
|
|
{
|
2017-07-07 18:12:25 +00:00
|
|
|
/// Zero! Prewhere condition is false for all (limit - pre_filter_pos) rows.
|
|
|
|
readRows();
|
2017-06-20 10:12:20 +00:00
|
|
|
|
|
|
|
if (will_read_until_mark)
|
2017-04-06 17:21:45 +00:00
|
|
|
{
|
2017-07-07 18:12:25 +00:00
|
|
|
/// Can skip the rest of granule with false prewhere conditon right now.
|
2017-08-01 13:04:48 +00:00
|
|
|
do
|
|
|
|
{
|
|
|
|
size_t rows_was_skipped = range_reader.skipToNextMark();
|
|
|
|
if (number_of_rows_to_skip < rows_was_skipped)
|
|
|
|
{
|
|
|
|
current_range_rows_read += rows_was_skipped - number_of_rows_to_skip;
|
|
|
|
number_of_rows_to_skip = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
number_of_rows_to_skip -= rows_was_skipped;
|
|
|
|
}
|
|
|
|
while (number_of_rows_to_skip);
|
2017-04-06 17:21:45 +00:00
|
|
|
}
|
2017-06-20 10:12:20 +00:00
|
|
|
else
|
|
|
|
{
|
2017-07-07 18:12:25 +00:00
|
|
|
/// Here reading seems to be done. It's still possible to skip rows during next reading.
|
2017-06-21 17:19:35 +00:00
|
|
|
number_of_rows_to_skip += limit - pre_filter_pos;
|
2017-06-20 10:12:20 +00:00
|
|
|
current_range_rows_read += limit - pre_filter_pos;
|
2017-04-06 17:21:45 +00:00
|
|
|
}
|
2017-06-15 17:01:13 +00:00
|
|
|
|
2017-07-07 18:12:25 +00:00
|
|
|
pre_filter_begin_pos = limit;
|
2017-04-06 17:21:45 +00:00
|
|
|
}
|
2017-07-07 18:12:25 +00:00
|
|
|
pre_filter_pos = limit;
|
2017-04-06 17:21:45 +00:00
|
|
|
}
|
2017-06-15 17:01:13 +00:00
|
|
|
|
2017-07-07 18:12:25 +00:00
|
|
|
readRows();
|
|
|
|
|
2017-07-12 11:40:00 +00:00
|
|
|
if (next_range_idx != ranges_to_read.size())
|
2017-06-15 17:01:13 +00:00
|
|
|
task->current_range_reader = std::experimental::nullopt;
|
2017-04-06 17:21:45 +00:00
|
|
|
}
|
|
|
|
|
2017-06-20 10:12:20 +00:00
|
|
|
if (!pre_range_reader)
|
2017-06-16 20:11:02 +00:00
|
|
|
task->current_range_reader = std::experimental::nullopt;
|
|
|
|
|
2017-04-06 17:21:45 +00:00
|
|
|
if (!post_filter_pos)
|
|
|
|
{
|
2017-09-22 15:36:22 +00:00
|
|
|
if (task->size_predictor)
|
|
|
|
task->size_predictor->updateFilteredRowsRation(pre_filter.size(), pre_filter.size());
|
2017-04-06 17:21:45 +00:00
|
|
|
res.clear();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
progressImpl({ 0, res.bytes() - pre_bytes });
|
|
|
|
|
|
|
|
post_filter.resize(post_filter_pos);
|
|
|
|
|
|
|
|
/// Filter the columns related to PREWHERE using pre_filter,
|
|
|
|
/// other columns - using post_filter.
|
|
|
|
size_t rows = 0;
|
|
|
|
for (const auto i : ext::range(0, res.columns()))
|
|
|
|
{
|
|
|
|
auto & col = res.safeGetByPosition(i);
|
|
|
|
if (col.name == prewhere_column && res.columns() > 1)
|
|
|
|
continue;
|
|
|
|
col.column =
|
|
|
|
col.column->filter(task->column_name_set.count(col.name) ? post_filter : pre_filter, -1);
|
|
|
|
rows = col.column->size();
|
|
|
|
}
|
2017-09-22 15:36:22 +00:00
|
|
|
if (task->size_predictor)
|
|
|
|
task->size_predictor->updateFilteredRowsRation(pre_filter.size(), pre_filter.size() - rows);
|
2017-04-06 17:21:45 +00:00
|
|
|
|
|
|
|
/// Replace column with condition value from PREWHERE to a constant.
|
|
|
|
if (!task->remove_prewhere_column)
|
2017-07-21 06:35:58 +00:00
|
|
|
res.getByName(prewhere_column).column = DataTypeUInt8().createConstColumn(rows, UInt64(1));
|
2017-08-01 13:04:48 +00:00
|
|
|
|
2017-04-06 17:21:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
throw Exception{
|
|
|
|
"Illegal type " + column->getName() + " of column for filter. Must be ColumnUInt8 or ColumnConstUInt8.",
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_COLUMN_FOR_FILTER
|
|
|
|
};
|
|
|
|
|
|
|
|
if (res)
|
2017-04-10 17:10:33 +00:00
|
|
|
{
|
|
|
|
if (task->size_predictor)
|
|
|
|
task->size_predictor->update(res);
|
|
|
|
|
2017-04-06 17:21:45 +00:00
|
|
|
reader->fillMissingColumnsAndReorder(res, task->ordered_names);
|
2017-04-10 17:10:33 +00:00
|
|
|
}
|
2017-04-06 17:21:45 +00:00
|
|
|
}
|
2017-06-15 17:01:13 +00:00
|
|
|
while (!task->isFinished() && !res && !isCancelled());
|
2017-04-06 17:21:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-09-01 17:21:03 +00:00
|
|
|
size_t space_left = std::max(static_cast<decltype(max_block_size_rows)>(1), max_block_size_rows);
|
2017-06-15 17:01:13 +00:00
|
|
|
while (!task->isFinished() && space_left && !isCancelled())
|
2017-04-06 17:21:45 +00:00
|
|
|
{
|
2017-06-14 10:50:22 +00:00
|
|
|
if (!task->current_range_reader)
|
2017-04-06 17:21:45 +00:00
|
|
|
{
|
2017-06-14 10:50:22 +00:00
|
|
|
auto & range = task->mark_ranges.back();
|
|
|
|
task->current_range_reader = reader->readRange(range.begin, range.end);
|
|
|
|
task->mark_ranges.pop_back();
|
2017-04-06 17:21:45 +00:00
|
|
|
}
|
|
|
|
|
2017-06-15 17:01:13 +00:00
|
|
|
size_t rows_to_read = space_left;
|
2017-06-21 17:19:35 +00:00
|
|
|
size_t recommended_rows = estimateNumRows(*task, *task->current_range_reader);
|
|
|
|
if (res && recommended_rows < 1)
|
|
|
|
break;
|
2017-09-01 17:21:03 +00:00
|
|
|
rows_to_read = std::min(rows_to_read, std::max(static_cast<decltype(recommended_rows)>(1), recommended_rows));
|
2017-04-06 17:21:45 +00:00
|
|
|
|
2017-07-26 11:46:47 +00:00
|
|
|
size_t rows_was_read = task->current_range_reader->read(res, rows_to_read);
|
2017-06-16 20:11:02 +00:00
|
|
|
if (task->current_range_reader->isReadingFinished())
|
2017-06-14 10:50:22 +00:00
|
|
|
task->current_range_reader = std::experimental::nullopt;
|
2017-04-06 17:21:45 +00:00
|
|
|
|
2017-07-26 11:46:47 +00:00
|
|
|
if (res && task->size_predictor)
|
2017-08-01 13:04:48 +00:00
|
|
|
{
|
2017-04-10 14:06:44 +00:00
|
|
|
task->size_predictor->update(res);
|
2017-08-01 13:04:48 +00:00
|
|
|
}
|
2017-04-06 17:21:45 +00:00
|
|
|
|
2017-07-26 11:46:47 +00:00
|
|
|
space_left -= rows_was_read;
|
2017-04-06 17:21:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// In the case of isCancelled.
|
|
|
|
if (!res)
|
|
|
|
return res;
|
|
|
|
|
|
|
|
progressImpl({ res.rows(), res.bytes() });
|
|
|
|
reader->fillMissingColumns(res, task->ordered_names, task->should_reorder);
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MergeTreeBaseBlockInputStream::injectVirtualColumns(Block & block)
|
|
|
|
{
|
|
|
|
const auto rows = block.rows();
|
|
|
|
|
|
|
|
/// add virtual columns
|
|
|
|
/// Except _sample_factor, which is added from the outside.
|
|
|
|
if (!virt_column_names.empty())
|
|
|
|
{
|
|
|
|
for (const auto & virt_column_name : virt_column_names)
|
|
|
|
{
|
|
|
|
if (virt_column_name == "_part")
|
|
|
|
{
|
|
|
|
block.insert(ColumnWithTypeAndName{
|
2017-07-21 06:35:58 +00:00
|
|
|
DataTypeString().createConstColumn(rows, task->data_part->name)->convertToFullColumnIfConst(),
|
2017-04-06 17:21:45 +00:00
|
|
|
std::make_shared<DataTypeString>(),
|
2017-07-21 06:35:58 +00:00
|
|
|
virt_column_name});
|
2017-04-06 17:21:45 +00:00
|
|
|
}
|
|
|
|
else if (virt_column_name == "_part_index")
|
|
|
|
{
|
|
|
|
block.insert(ColumnWithTypeAndName{
|
2017-09-01 17:21:03 +00:00
|
|
|
DataTypeUInt64().createConstColumn(rows, static_cast<UInt64>(task->part_index_in_query))->convertToFullColumnIfConst(),
|
2017-04-06 17:21:45 +00:00
|
|
|
std::make_shared<DataTypeUInt64>(),
|
2017-07-21 06:35:58 +00:00
|
|
|
virt_column_name});
|
2017-04-06 17:21:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
MergeTreeBaseBlockInputStream::~MergeTreeBaseBlockInputStream() = default;
|
|
|
|
|
|
|
|
}
|