2013-11-26 11:55:11 +00:00
|
|
|
|
#pragma once
|
|
|
|
|
|
2014-03-09 17:36:01 +00:00
|
|
|
|
#include <DB/Storages/MergeTree/MergeTreeData.h>
|
2013-11-26 11:55:11 +00:00
|
|
|
|
#include <DB/DataTypes/IDataType.h>
|
|
|
|
|
#include <DB/DataTypes/DataTypeNested.h>
|
2014-03-13 12:48:07 +00:00
|
|
|
|
#include <DB/DataTypes/DataTypeArray.h>
|
2013-11-26 11:55:11 +00:00
|
|
|
|
#include <DB/Core/NamesAndTypes.h>
|
|
|
|
|
#include <DB/Common/escapeForFileName.h>
|
|
|
|
|
#include <DB/IO/CachedCompressedReadBuffer.h>
|
2014-01-15 14:53:20 +00:00
|
|
|
|
#include <DB/IO/CompressedReadBufferFromFile.h>
|
2014-03-13 12:48:07 +00:00
|
|
|
|
#include <DB/Columns/ColumnArray.h>
|
|
|
|
|
#include <DB/Columns/ColumnNested.h>
|
2014-10-23 13:53:16 +00:00
|
|
|
|
#include <DB/Interpreters/evaluateMissingDefaults.h>
|
2013-11-26 11:55:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
|
{
|
|
|
|
|
|
2014-03-13 12:48:07 +00:00
|
|
|
|
/** Пара засечек, определяющая диапазон строк в куске. Именно, диапазон имеет вид [begin * index_granularity, end * index_granularity).
|
|
|
|
|
*/
|
|
|
|
|
struct MarkRange
|
|
|
|
|
{
|
|
|
|
|
size_t begin;
|
|
|
|
|
size_t end;
|
|
|
|
|
|
|
|
|
|
MarkRange() {}
|
|
|
|
|
MarkRange(size_t begin_, size_t end_) : begin(begin_), end(end_) {}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
typedef std::vector<MarkRange> MarkRanges;
|
|
|
|
|
|
|
|
|
|
|
2013-11-26 11:55:11 +00:00
|
|
|
|
/** Умеет читать данные между парой засечек из одного куска. При чтении последовательных отрезков не делает лишних seek-ов.
|
|
|
|
|
* При чтении почти последовательных отрезков делает seek-и быстро, не выбрасывая содержимое буфера.
|
|
|
|
|
*/
|
|
|
|
|
class MergeTreeReader
|
|
|
|
|
{
|
2014-03-24 16:10:14 +00:00
|
|
|
|
typedef std::map<std::string, ColumnPtr> OffsetColumns;
|
|
|
|
|
|
2013-11-26 11:55:11 +00:00
|
|
|
|
public:
|
2014-12-14 05:27:39 +00:00
|
|
|
|
MergeTreeReader(const String & path_, /// Путь к куску
|
|
|
|
|
const MergeTreeData::DataPartPtr & data_part, const NamesAndTypesList & columns_,
|
|
|
|
|
bool use_uncompressed_cache_, MergeTreeData & storage_, const MarkRanges & all_mark_ranges)
|
|
|
|
|
: path(path_), data_part(data_part), part_name(data_part->name), columns(columns_),
|
|
|
|
|
use_uncompressed_cache(use_uncompressed_cache_), storage(storage_), all_mark_ranges(all_mark_ranges)
|
2013-11-26 11:55:11 +00:00
|
|
|
|
{
|
2014-07-23 09:15:41 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2014-07-23 15:24:45 +00:00
|
|
|
|
if (!Poco::File(path).exists())
|
|
|
|
|
throw Exception("Part " + path + " is missing", ErrorCodes::NOT_FOUND_EXPECTED_DATA_PART);
|
|
|
|
|
|
2014-07-23 09:15:41 +00:00
|
|
|
|
for (const NameAndTypePair & column : columns)
|
|
|
|
|
addStream(column.name, *column.type, all_mark_ranges);
|
|
|
|
|
}
|
|
|
|
|
catch (...)
|
|
|
|
|
{
|
|
|
|
|
storage.reportBrokenPart(part_name);
|
|
|
|
|
throw;
|
|
|
|
|
}
|
2013-11-26 11:55:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Если столбцов нет в блоке, добавляет их, если есть - добавляет прочитанные значения к ним в конец.
|
|
|
|
|
* Не добавляет столбцы, для которых нет файлов. Чтобы их добавить, нужно вызвать fillMissingColumns.
|
2014-07-17 13:41:47 +00:00
|
|
|
|
* В блоке должно быть либо ни одного столбца из columns, либо все, для которых есть файлы. */
|
2013-11-26 11:55:11 +00:00
|
|
|
|
void readRange(size_t from_mark, size_t to_mark, Block & res)
|
|
|
|
|
{
|
2014-07-01 08:50:30 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
size_t max_rows_to_read = (to_mark - from_mark) * storage.index_granularity;
|
2013-11-26 11:55:11 +00:00
|
|
|
|
|
2014-07-01 08:50:30 +00:00
|
|
|
|
/** Для некоторых столбцов файлы с данными могут отсутствовать.
|
|
|
|
|
* Это бывает для старых кусков, после добавления новых столбцов в структуру таблицы.
|
|
|
|
|
*/
|
2014-11-25 20:55:36 +00:00
|
|
|
|
auto has_missing_columns = false;
|
2013-11-26 11:55:11 +00:00
|
|
|
|
|
2014-07-01 08:50:30 +00:00
|
|
|
|
/// Указатели на столбцы смещений, общие для столбцов из вложенных структур данных
|
|
|
|
|
/// Если append, все значения nullptr, и offset_columns используется только для проверки, что столбец смещений уже прочитан.
|
|
|
|
|
OffsetColumns offset_columns;
|
2014-11-25 20:55:34 +00:00
|
|
|
|
const auto read_column = [&] (const NameAndTypePair & it) {
|
2014-07-17 13:41:47 +00:00
|
|
|
|
if (streams.end() == streams.find(it.name))
|
2014-11-25 20:55:36 +00:00
|
|
|
|
{
|
|
|
|
|
has_missing_columns = true;
|
2014-11-25 20:55:34 +00:00
|
|
|
|
return;
|
2014-11-25 20:55:36 +00:00
|
|
|
|
}
|
2013-11-26 11:55:11 +00:00
|
|
|
|
|
2014-07-01 08:50:30 +00:00
|
|
|
|
/// Все столбцы уже есть в блоке. Будем добавлять значения в конец.
|
2014-07-17 13:41:47 +00:00
|
|
|
|
bool append = res.has(it.name);
|
2013-11-26 11:55:11 +00:00
|
|
|
|
|
2014-07-01 08:50:30 +00:00
|
|
|
|
ColumnWithNameAndType column;
|
2014-07-17 13:41:47 +00:00
|
|
|
|
column.name = it.name;
|
|
|
|
|
column.type = it.type;
|
2014-07-01 08:50:30 +00:00
|
|
|
|
if (append)
|
|
|
|
|
column.column = res.getByName(column.name).column;
|
2013-11-26 11:55:11 +00:00
|
|
|
|
|
2014-07-01 08:50:30 +00:00
|
|
|
|
bool read_offsets = true;
|
2013-11-26 11:55:11 +00:00
|
|
|
|
|
2014-07-01 08:50:30 +00:00
|
|
|
|
/// Для вложенных структур запоминаем указатели на столбцы со смещениями
|
|
|
|
|
if (const DataTypeArray * type_arr = typeid_cast<const DataTypeArray *>(&*column.type))
|
|
|
|
|
{
|
|
|
|
|
String name = DataTypeNested::extractNestedTableName(column.name);
|
2013-11-26 11:55:11 +00:00
|
|
|
|
|
2014-07-01 08:50:30 +00:00
|
|
|
|
if (offset_columns.count(name) == 0)
|
2014-11-25 20:55:34 +00:00
|
|
|
|
offset_columns[name] = append ? nullptr : new ColumnArray::ColumnOffsets_t;
|
2014-07-01 08:50:30 +00:00
|
|
|
|
else
|
|
|
|
|
read_offsets = false; /// на предыдущих итерациях смещения уже считали вызовом readData
|
2013-11-26 11:55:11 +00:00
|
|
|
|
|
2014-07-01 08:50:30 +00:00
|
|
|
|
if (!append)
|
|
|
|
|
column.column = new ColumnArray(type_arr->getNestedType()->createColumn(), offset_columns[name]);
|
|
|
|
|
}
|
|
|
|
|
else if (!append)
|
|
|
|
|
column.column = column.type->createColumn();
|
2013-11-26 11:55:11 +00:00
|
|
|
|
|
|
|
|
|
readData(column.name, *column.type, *column.column, from_mark, max_rows_to_read, 0, read_offsets);
|
2014-07-01 08:50:30 +00:00
|
|
|
|
|
|
|
|
|
if (!append && column.column->size())
|
|
|
|
|
res.insert(column);
|
2014-11-25 20:55:34 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
for (const NameAndTypePair & it : columns)
|
|
|
|
|
read_column(it);
|
2013-11-26 11:55:11 +00:00
|
|
|
|
|
2014-11-25 20:55:36 +00:00
|
|
|
|
if (has_missing_columns && !res)
|
2014-11-25 20:55:34 +00:00
|
|
|
|
{
|
|
|
|
|
addMinimumSizeColumn();
|
|
|
|
|
/// minimum size column is necessarily at list's front
|
|
|
|
|
read_column(columns.front());
|
|
|
|
|
}
|
2014-07-01 08:50:30 +00:00
|
|
|
|
}
|
|
|
|
|
catch (const Exception & e)
|
|
|
|
|
{
|
2014-07-23 09:15:41 +00:00
|
|
|
|
if (e.code() != ErrorCodes::ALL_REQUESTED_COLUMNS_ARE_MISSING)
|
|
|
|
|
storage.reportBrokenPart(part_name);
|
|
|
|
|
|
2014-07-01 08:50:30 +00:00
|
|
|
|
/// Более хорошая диагностика.
|
2014-10-21 12:11:20 +00:00
|
|
|
|
throw Exception(e.message() + "\n(while reading from part " + path + " from mark " + toString(from_mark) + " to "
|
2014-07-01 08:50:30 +00:00
|
|
|
|
+ toString(to_mark) + ")", e.code());
|
2013-11-26 11:55:11 +00:00
|
|
|
|
}
|
2014-07-23 09:15:41 +00:00
|
|
|
|
catch (...)
|
|
|
|
|
{
|
|
|
|
|
storage.reportBrokenPart(part_name);
|
|
|
|
|
|
|
|
|
|
throw;
|
|
|
|
|
}
|
2013-11-26 11:55:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-11-25 20:55:34 +00:00
|
|
|
|
void addMinimumSizeColumn()
|
2014-11-25 20:55:23 +00:00
|
|
|
|
{
|
|
|
|
|
const auto get_column_size = [this] (const String & name) {
|
|
|
|
|
const auto & files = data_part->checksums.files;
|
|
|
|
|
|
|
|
|
|
const auto escaped_name = escapeForFileName(name);
|
|
|
|
|
const auto bin_file_name = escaped_name + ".bin";
|
|
|
|
|
const auto mrk_file_name = escaped_name + ".mrk";
|
|
|
|
|
|
|
|
|
|
return files.find(bin_file_name)->second.file_size + files.find(mrk_file_name)->second.file_size;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const auto & storage_columns = storage.getColumnsList();
|
|
|
|
|
const NameAndTypePair * minimum_size_column = nullptr;
|
|
|
|
|
auto minimum_size = std::numeric_limits<size_t>::max();
|
|
|
|
|
|
|
|
|
|
for (const auto & column : storage_columns)
|
|
|
|
|
{
|
|
|
|
|
if (!data_part->hasColumnFiles(column.name))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
const auto size = get_column_size(column.name);
|
|
|
|
|
if (size < minimum_size)
|
|
|
|
|
{
|
|
|
|
|
minimum_size = size;
|
|
|
|
|
minimum_size_column = &column;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!minimum_size_column)
|
|
|
|
|
throw std::logic_error{"could not find a column of minimum size in MergeTree"};
|
|
|
|
|
|
2014-11-25 20:55:34 +00:00
|
|
|
|
addStream(minimum_size_column->name, *minimum_size_column->type, all_mark_ranges);
|
2014-11-25 20:55:23 +00:00
|
|
|
|
columns.emplace(std::begin(columns), *minimum_size_column);
|
2014-11-25 20:55:36 +00:00
|
|
|
|
|
|
|
|
|
added_column = &columns.front();
|
2014-11-25 20:55:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-11-26 11:55:11 +00:00
|
|
|
|
/// Заполняет столбцы, которых нет в блоке, значениями по умолчанию.
|
|
|
|
|
void fillMissingColumns(Block & res)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2014-03-24 16:10:14 +00:00
|
|
|
|
/** Для недостающих столбцов из вложенной структуры нужно создавать не столбец пустых массивов, а столбец массивов
|
|
|
|
|
* правильных длин.
|
|
|
|
|
* TODO: Если для какой-то вложенной структуры были запрошены только отсутствующие столбцы, для них вернутся пустые
|
|
|
|
|
* массивы, даже если в куске есть смещения для этой вложенной структуры. Это можно исправить.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/// Сначала запомним столбцы смещений для всех массивов в блоке.
|
|
|
|
|
OffsetColumns offset_columns;
|
|
|
|
|
for (size_t i = 0; i < res.columns(); ++i)
|
|
|
|
|
{
|
|
|
|
|
const ColumnWithNameAndType & column = res.getByPosition(i);
|
2014-06-26 00:58:14 +00:00
|
|
|
|
if (const ColumnArray * array = typeid_cast<const ColumnArray *>(&*column.column))
|
2014-03-24 16:10:14 +00:00
|
|
|
|
{
|
|
|
|
|
String offsets_name = DataTypeNested::extractNestedTableName(column.name);
|
|
|
|
|
offset_columns[offsets_name] = array->getOffsetsColumn();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-04 15:50:48 +00:00
|
|
|
|
auto should_evaluate_defaults = false;
|
|
|
|
|
auto should_sort = false;
|
|
|
|
|
for (NamesAndTypesList::const_iterator it = columns.begin(); it != columns.end(); ++it)
|
2013-11-26 11:55:11 +00:00
|
|
|
|
{
|
2014-10-17 12:26:53 +00:00
|
|
|
|
/// insert default values only for columns without default expressions
|
2014-12-04 15:50:48 +00:00
|
|
|
|
if (!res.has(it->name))
|
2013-11-26 11:55:11 +00:00
|
|
|
|
{
|
2014-12-04 15:50:48 +00:00
|
|
|
|
should_sort = true;
|
|
|
|
|
if (storage.column_defaults.count(it->name) != 0)
|
|
|
|
|
{
|
|
|
|
|
should_evaluate_defaults = true;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-26 11:55:11 +00:00
|
|
|
|
ColumnWithNameAndType column;
|
2014-07-17 13:41:47 +00:00
|
|
|
|
column.name = it->name;
|
|
|
|
|
column.type = it->type;
|
2013-11-26 11:55:11 +00:00
|
|
|
|
|
2014-03-24 16:10:14 +00:00
|
|
|
|
String offsets_name = DataTypeNested::extractNestedTableName(column.name);
|
|
|
|
|
if (offset_columns.count(offsets_name))
|
|
|
|
|
{
|
|
|
|
|
ColumnPtr offsets_column = offset_columns[offsets_name];
|
2014-06-26 00:58:14 +00:00
|
|
|
|
DataTypePtr nested_type = typeid_cast<DataTypeArray &>(*column.type).getNestedType();
|
2014-03-24 16:10:14 +00:00
|
|
|
|
size_t nested_rows = offsets_column->empty() ? 0
|
2014-06-26 00:58:14 +00:00
|
|
|
|
: typeid_cast<ColumnUInt64 &>(*offsets_column).getData().back();
|
2014-03-24 16:10:14 +00:00
|
|
|
|
|
|
|
|
|
ColumnPtr nested_column = dynamic_cast<IColumnConst &>(*nested_type->createConstColumn(
|
|
|
|
|
nested_rows, nested_type->getDefault())).convertToFullColumn();
|
|
|
|
|
|
|
|
|
|
column.column = new ColumnArray(nested_column, offsets_column);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
/** Нужно превратить константный столбец в полноценный, так как в части блоков (из других кусков),
|
|
|
|
|
* он может быть полноценным (а то интерпретатор может посчитать, что он константный везде).
|
|
|
|
|
*/
|
|
|
|
|
column.column = dynamic_cast<IColumnConst &>(*column.type->createConstColumn(
|
|
|
|
|
res.rows(), column.type->getDefault())).convertToFullColumn();
|
|
|
|
|
}
|
2013-11-26 11:55:11 +00:00
|
|
|
|
|
2014-12-04 15:50:48 +00:00
|
|
|
|
res.insert(column);
|
2013-11-26 11:55:11 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2014-10-16 16:55:23 +00:00
|
|
|
|
|
2014-12-04 15:50:48 +00:00
|
|
|
|
/// evaluate defaulted columns if necessary
|
|
|
|
|
if (should_evaluate_defaults)
|
|
|
|
|
evaluateMissingDefaults(res, columns, storage.column_defaults, storage.context);
|
|
|
|
|
|
|
|
|
|
/// remove added column to ensure same content among all blocks
|
|
|
|
|
if (added_column)
|
|
|
|
|
{
|
|
|
|
|
streams.erase(added_column->name);
|
|
|
|
|
columns.erase(std::begin(columns));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// sort columns to ensure consistent order among all block
|
|
|
|
|
if (should_sort)
|
|
|
|
|
{
|
|
|
|
|
Block sorted_block;
|
|
|
|
|
|
|
|
|
|
for (const auto & name_and_type : columns)
|
|
|
|
|
sorted_block.insert(res.getByName(name_and_type.name));
|
|
|
|
|
|
|
|
|
|
std::swap(res, sorted_block);
|
|
|
|
|
}
|
|
|
|
|
else if (added_column)
|
|
|
|
|
{
|
|
|
|
|
res.erase(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
added_column = nullptr;
|
2013-11-26 11:55:11 +00:00
|
|
|
|
}
|
|
|
|
|
catch (const Exception & e)
|
|
|
|
|
{
|
|
|
|
|
/// Более хорошая диагностика.
|
2014-10-21 12:11:20 +00:00
|
|
|
|
throw Exception(e.message() + '\n' + e.getStackTrace().toString()
|
|
|
|
|
+ "\n(while reading from part " + path + ")", e.code());
|
2013-11-26 11:55:11 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
struct Stream
|
|
|
|
|
{
|
2014-02-11 13:30:42 +00:00
|
|
|
|
MarkCache::MappedPtr marks;
|
2014-01-15 14:53:20 +00:00
|
|
|
|
ReadBuffer * data_buffer;
|
|
|
|
|
Poco::SharedPtr<CachedCompressedReadBuffer> cached_buffer;
|
|
|
|
|
Poco::SharedPtr<CompressedReadBufferFromFile> non_cached_buffer;
|
2013-11-26 11:55:11 +00:00
|
|
|
|
std::string path_prefix;
|
2014-04-23 15:34:47 +00:00
|
|
|
|
size_t max_mark_range;
|
2013-11-26 11:55:11 +00:00
|
|
|
|
|
2014-04-23 15:34:47 +00:00
|
|
|
|
Stream(const String & path_prefix, UncompressedCache * uncompressed_cache, MarkCache * mark_cache, const MarkRanges & all_mark_ranges)
|
2014-02-11 13:30:42 +00:00
|
|
|
|
: path_prefix(path_prefix)
|
2014-01-15 14:53:20 +00:00
|
|
|
|
{
|
2014-02-11 13:30:42 +00:00
|
|
|
|
loadMarks(mark_cache);
|
2014-04-23 15:34:47 +00:00
|
|
|
|
size_t max_mark_range = 0;
|
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < all_mark_ranges.size(); ++i)
|
|
|
|
|
{
|
|
|
|
|
size_t right = all_mark_ranges[i].end;
|
2014-04-25 14:55:27 +00:00
|
|
|
|
|
2014-04-23 15:34:47 +00:00
|
|
|
|
/// Если правая граница лежит внутри блока, то его тоже придется читать.
|
2014-04-25 14:55:27 +00:00
|
|
|
|
if (right < (*marks).size() && (*marks)[right].offset_in_decompressed_block > 0)
|
2014-04-23 15:34:47 +00:00
|
|
|
|
{
|
|
|
|
|
while (right < (*marks).size() && (*marks)[right].offset_in_compressed_file ==
|
|
|
|
|
(*marks)[all_mark_ranges[i].end].offset_in_compressed_file)
|
|
|
|
|
++right;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Если правее засечек нет, просто используем DEFAULT_BUFFER_SIZE
|
2014-04-25 14:55:27 +00:00
|
|
|
|
if (right >= (*marks).size() || (right + 1 == (*marks).size() &&
|
|
|
|
|
(*marks)[right].offset_in_compressed_file == (*marks)[all_mark_ranges[i].end].offset_in_compressed_file))
|
2014-04-23 15:34:47 +00:00
|
|
|
|
{
|
|
|
|
|
max_mark_range = DBMS_DEFAULT_BUFFER_SIZE;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-25 00:18:16 +00:00
|
|
|
|
max_mark_range = std::max(max_mark_range,
|
|
|
|
|
(*marks)[right].offset_in_compressed_file - (*marks)[all_mark_ranges[i].begin].offset_in_compressed_file);
|
2014-04-23 15:34:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t buffer_size = DBMS_DEFAULT_BUFFER_SIZE < max_mark_range ? DBMS_DEFAULT_BUFFER_SIZE : max_mark_range;
|
|
|
|
|
|
2014-01-15 14:53:20 +00:00
|
|
|
|
if (uncompressed_cache)
|
|
|
|
|
{
|
2014-04-23 15:34:47 +00:00
|
|
|
|
cached_buffer = new CachedCompressedReadBuffer(path_prefix + ".bin", uncompressed_cache, buffer_size);
|
2014-01-15 14:53:20 +00:00
|
|
|
|
data_buffer = &*cached_buffer;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2014-04-23 15:34:47 +00:00
|
|
|
|
non_cached_buffer = new CompressedReadBufferFromFile(path_prefix + ".bin", buffer_size);
|
2014-01-15 14:53:20 +00:00
|
|
|
|
data_buffer = &*non_cached_buffer;
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-11-26 11:55:11 +00:00
|
|
|
|
|
2014-02-11 13:30:42 +00:00
|
|
|
|
void loadMarks(MarkCache * cache)
|
2013-11-26 11:55:11 +00:00
|
|
|
|
{
|
2014-02-11 13:30:42 +00:00
|
|
|
|
std::string path = path_prefix + ".mrk";
|
2013-11-26 11:55:11 +00:00
|
|
|
|
|
2014-02-11 13:30:42 +00:00
|
|
|
|
UInt128 key;
|
|
|
|
|
if (cache)
|
2013-11-26 11:55:11 +00:00
|
|
|
|
{
|
2014-02-11 13:30:42 +00:00
|
|
|
|
key = cache->hash(path);
|
|
|
|
|
marks = cache->get(key);
|
|
|
|
|
if (marks)
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
marks.reset(new MarksInCompressedFile);
|
2013-11-26 11:55:11 +00:00
|
|
|
|
|
2014-02-11 13:30:42 +00:00
|
|
|
|
ReadBufferFromFile buffer(path);
|
|
|
|
|
while (!buffer.eof())
|
|
|
|
|
{
|
|
|
|
|
MarkInCompressedFile mark;
|
|
|
|
|
readIntBinary(mark.offset_in_compressed_file, buffer);
|
|
|
|
|
readIntBinary(mark.offset_in_decompressed_block, buffer);
|
|
|
|
|
marks->push_back(mark);
|
2013-11-26 11:55:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-02-11 13:30:42 +00:00
|
|
|
|
if (cache)
|
|
|
|
|
cache->set(key, marks);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void seekToMark(size_t index)
|
|
|
|
|
{
|
|
|
|
|
MarkInCompressedFile mark = (*marks)[index];
|
|
|
|
|
|
2013-11-26 11:55:11 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2014-01-15 14:53:20 +00:00
|
|
|
|
if (cached_buffer)
|
2014-04-23 15:34:47 +00:00
|
|
|
|
{
|
2014-02-11 13:30:42 +00:00
|
|
|
|
cached_buffer->seek(mark.offset_in_compressed_file, mark.offset_in_decompressed_block);
|
2014-04-23 15:34:47 +00:00
|
|
|
|
}
|
2014-01-15 14:53:20 +00:00
|
|
|
|
if (non_cached_buffer)
|
2014-02-11 13:30:42 +00:00
|
|
|
|
non_cached_buffer->seek(mark.offset_in_compressed_file, mark.offset_in_decompressed_block);
|
2013-11-26 11:55:11 +00:00
|
|
|
|
}
|
|
|
|
|
catch (const Exception & e)
|
|
|
|
|
{
|
|
|
|
|
/// Более хорошая диагностика.
|
|
|
|
|
if (e.code() == ErrorCodes::ARGUMENT_OUT_OF_BOUND)
|
2014-02-11 13:30:42 +00:00
|
|
|
|
throw Exception(e.message() + " (while seeking to mark " + Poco::NumberFormatter::format(index)
|
|
|
|
|
+ " of column " + path_prefix + "; offsets are: "
|
|
|
|
|
+ toString(mark.offset_in_compressed_file) + " "
|
|
|
|
|
+ toString(mark.offset_in_decompressed_block) + ")", e.code());
|
2013-11-26 11:55:11 +00:00
|
|
|
|
else
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2014-04-22 22:43:55 +00:00
|
|
|
|
typedef std::map<std::string, std::unique_ptr<Stream> > FileStreams;
|
2013-11-26 11:55:11 +00:00
|
|
|
|
|
|
|
|
|
String path;
|
2014-11-25 20:55:23 +00:00
|
|
|
|
const MergeTreeData::DataPartPtr & data_part;
|
2014-07-23 09:15:41 +00:00
|
|
|
|
String part_name;
|
2013-11-26 11:55:11 +00:00
|
|
|
|
FileStreams streams;
|
2014-07-17 13:41:47 +00:00
|
|
|
|
NamesAndTypesList columns;
|
2013-11-26 11:55:11 +00:00
|
|
|
|
bool use_uncompressed_cache;
|
2014-03-09 17:36:01 +00:00
|
|
|
|
MergeTreeData & storage;
|
2014-11-25 20:55:23 +00:00
|
|
|
|
const MarkRanges & all_mark_ranges;
|
2014-11-25 20:55:36 +00:00
|
|
|
|
const NameAndTypePair * added_column = nullptr;
|
2013-11-26 11:55:11 +00:00
|
|
|
|
|
2014-04-23 15:34:47 +00:00
|
|
|
|
void addStream(const String & name, const IDataType & type, const MarkRanges & all_mark_ranges, size_t level = 0)
|
2013-11-26 11:55:11 +00:00
|
|
|
|
{
|
|
|
|
|
String escaped_column_name = escapeForFileName(name);
|
|
|
|
|
|
|
|
|
|
/** Если файла с данными нет - то не будем пытаться открыть его.
|
|
|
|
|
* Это нужно, чтобы можно было добавлять новые столбцы к структуре таблицы без создания файлов для старых кусков.
|
|
|
|
|
*/
|
|
|
|
|
if (!Poco::File(path + escaped_column_name + ".bin").exists())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
UncompressedCache * uncompressed_cache = use_uncompressed_cache ? storage.context.getUncompressedCache() : NULL;
|
2014-02-11 13:30:42 +00:00
|
|
|
|
MarkCache * mark_cache = storage.context.getMarkCache();
|
2013-11-26 11:55:11 +00:00
|
|
|
|
|
|
|
|
|
/// Для массивов используются отдельные потоки для размеров.
|
2014-06-26 00:58:14 +00:00
|
|
|
|
if (const DataTypeArray * type_arr = typeid_cast<const DataTypeArray *>(&type))
|
2013-11-26 11:55:11 +00:00
|
|
|
|
{
|
|
|
|
|
String size_name = DataTypeNested::extractNestedTableName(name)
|
|
|
|
|
+ ARRAY_SIZES_COLUMN_NAME_SUFFIX + toString(level);
|
|
|
|
|
String escaped_size_name = escapeForFileName(DataTypeNested::extractNestedTableName(name))
|
|
|
|
|
+ ARRAY_SIZES_COLUMN_NAME_SUFFIX + toString(level);
|
|
|
|
|
|
2014-01-15 16:12:48 +00:00
|
|
|
|
if (!streams.count(size_name))
|
2014-04-22 22:43:55 +00:00
|
|
|
|
streams.emplace(size_name, std::unique_ptr<Stream>(new Stream(
|
2014-04-23 15:34:47 +00:00
|
|
|
|
path + escaped_size_name, uncompressed_cache, mark_cache, all_mark_ranges)));
|
2013-11-26 11:55:11 +00:00
|
|
|
|
|
2014-04-23 15:34:47 +00:00
|
|
|
|
addStream(name, *type_arr->getNestedType(), all_mark_ranges, level + 1);
|
2013-11-26 11:55:11 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
2014-04-23 15:34:47 +00:00
|
|
|
|
streams[name].reset(new Stream(path + escaped_column_name, uncompressed_cache, mark_cache, all_mark_ranges));
|
2013-11-26 11:55:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void readData(const String & name, const IDataType & type, IColumn & column, size_t from_mark, size_t max_rows_to_read,
|
|
|
|
|
size_t level = 0, bool read_offsets = true)
|
|
|
|
|
{
|
|
|
|
|
/// Для массивов требуется сначала десериализовать размеры, а потом значения.
|
2014-06-26 00:58:14 +00:00
|
|
|
|
if (const DataTypeArray * type_arr = typeid_cast<const DataTypeArray *>(&type))
|
2013-11-26 11:55:11 +00:00
|
|
|
|
{
|
|
|
|
|
if (read_offsets)
|
|
|
|
|
{
|
|
|
|
|
Stream & stream = *streams[DataTypeNested::extractNestedTableName(name) + ARRAY_SIZES_COLUMN_NAME_SUFFIX + toString(level)];
|
|
|
|
|
stream.seekToMark(from_mark);
|
|
|
|
|
type_arr->deserializeOffsets(
|
|
|
|
|
column,
|
|
|
|
|
*stream.data_buffer,
|
|
|
|
|
max_rows_to_read);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (column.size())
|
2013-12-04 10:14:35 +00:00
|
|
|
|
{
|
2014-06-26 00:58:14 +00:00
|
|
|
|
ColumnArray & array = typeid_cast<ColumnArray &>(column);
|
2013-11-26 11:55:11 +00:00
|
|
|
|
readData(
|
|
|
|
|
name,
|
|
|
|
|
*type_arr->getNestedType(),
|
2013-12-04 10:14:35 +00:00
|
|
|
|
array.getData(),
|
2013-11-26 11:55:11 +00:00
|
|
|
|
from_mark,
|
2013-12-04 10:14:35 +00:00
|
|
|
|
array.getOffsets()[column.size() - 1] - array.getData().size(),
|
2013-11-26 11:55:11 +00:00
|
|
|
|
level + 1);
|
2013-12-04 10:14:35 +00:00
|
|
|
|
}
|
2013-11-26 11:55:11 +00:00
|
|
|
|
}
|
2014-06-26 00:58:14 +00:00
|
|
|
|
else if (const DataTypeNested * type_nested = typeid_cast<const DataTypeNested *>(&type))
|
2013-11-26 11:55:11 +00:00
|
|
|
|
{
|
|
|
|
|
Stream & stream = *streams[name + ARRAY_SIZES_COLUMN_NAME_SUFFIX + toString(level)];
|
|
|
|
|
stream.seekToMark(from_mark);
|
|
|
|
|
type_nested->deserializeOffsets(
|
|
|
|
|
column,
|
|
|
|
|
*stream.data_buffer,
|
|
|
|
|
max_rows_to_read);
|
|
|
|
|
|
|
|
|
|
if (column.size())
|
|
|
|
|
{
|
2014-06-26 00:58:14 +00:00
|
|
|
|
ColumnNested & column_nested = typeid_cast<ColumnNested &>(column);
|
2013-11-26 11:55:11 +00:00
|
|
|
|
|
|
|
|
|
NamesAndTypesList::const_iterator it = type_nested->getNestedTypesList()->begin();
|
|
|
|
|
for (size_t i = 0; i < column_nested.getData().size(); ++i, ++it)
|
|
|
|
|
{
|
|
|
|
|
readData(
|
2014-07-09 11:45:51 +00:00
|
|
|
|
DataTypeNested::concatenateNestedName(name, it->name),
|
|
|
|
|
*it->type,
|
2013-11-26 11:55:11 +00:00
|
|
|
|
*column_nested.getData()[i],
|
|
|
|
|
from_mark,
|
2013-12-04 10:14:35 +00:00
|
|
|
|
column_nested.getOffsets()[column.size() - 1] - column_nested.getData()[i]->size(),
|
2013-11-26 11:55:11 +00:00
|
|
|
|
level + 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Stream & stream = *streams[name];
|
|
|
|
|
stream.seekToMark(from_mark);
|
|
|
|
|
type.deserializeBinary(column, *stream.data_buffer, max_rows_to_read);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|