2019-10-10 16:30:30 +00:00
|
|
|
#include <Storages/MergeTree/MergeTreeReaderWide.h>
|
2020-02-25 09:49:45 +00:00
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Columns/ColumnArray.h>
|
2020-02-25 09:49:45 +00:00
|
|
|
#include <DataTypes/DataTypeArray.h>
|
|
|
|
#include <DataTypes/NestedUtils.h>
|
2020-02-25 08:53:14 +00:00
|
|
|
#include <Interpreters/inplaceBlockConversions.h>
|
2020-02-25 09:49:45 +00:00
|
|
|
#include <Storages/MergeTree/IMergeTreeReader.h>
|
2020-02-10 20:27:06 +00:00
|
|
|
#include <Storages/MergeTree/MergeTreeDataPartWide.h>
|
2020-02-25 09:49:45 +00:00
|
|
|
#include <Common/escapeForFileName.h>
|
2017-07-13 20:58:19 +00:00
|
|
|
#include <Common/typeid_cast.h>
|
2016-07-19 10:57:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
using OffsetColumns = std::map<std::string, ColumnPtr>;
|
|
|
|
constexpr auto DATA_FILE_EXTENSION = ".bin";
|
2016-07-19 10:57:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
extern const int LOGICAL_ERROR;
|
|
|
|
extern const int MEMORY_LIMIT_EXCEEDED;
|
2016-11-20 12:43:20 +00:00
|
|
|
}
|
2016-07-21 16:22:24 +00:00
|
|
|
|
2020-02-10 20:27:06 +00:00
|
|
|
MergeTreeReaderWide::MergeTreeReaderWide(
|
2020-03-23 02:12:31 +00:00
|
|
|
DataPartWidePtr data_part_,
|
|
|
|
NamesAndTypesList columns_,
|
2020-02-10 20:27:06 +00:00
|
|
|
UncompressedCache * uncompressed_cache_,
|
|
|
|
MarkCache * mark_cache_,
|
2020-03-23 02:12:31 +00:00
|
|
|
MarkRanges mark_ranges_,
|
|
|
|
MergeTreeReaderSettings settings_,
|
|
|
|
IMergeTreeDataPart::ValueSizeMap avg_value_size_hints_,
|
2020-02-10 20:27:06 +00:00
|
|
|
const ReadBufferFromFileBase::ProfileCallback & profile_callback_,
|
|
|
|
clockid_t clock_type_)
|
2020-02-25 09:49:45 +00:00
|
|
|
: IMergeTreeReader(
|
2020-03-23 02:12:31 +00:00
|
|
|
std::move(data_part_), std::move(columns_), uncompressed_cache_, std::move(mark_cache_),
|
|
|
|
std::move(mark_ranges_), std::move(settings_), std::move(avg_value_size_hints_))
|
2016-07-19 10:57:57 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
try
|
|
|
|
{
|
2020-02-25 09:49:45 +00:00
|
|
|
for (const NameAndTypePair & column_from_part : data_part->getColumns())
|
2020-02-25 08:53:14 +00:00
|
|
|
columns_from_part[column_from_part.name] = column_from_part.type;
|
|
|
|
|
2017-12-25 21:57:29 +00:00
|
|
|
for (const NameAndTypePair & column : columns)
|
2020-02-25 08:53:14 +00:00
|
|
|
{
|
|
|
|
if (columns_from_part.count(column.name))
|
2020-03-30 16:03:08 +00:00
|
|
|
{
|
2020-02-25 08:53:14 +00:00
|
|
|
addStreams(column.name, *columns_from_part[column.name], profile_callback_, clock_type_);
|
2020-03-30 16:03:08 +00:00
|
|
|
}
|
2020-02-25 08:53:14 +00:00
|
|
|
else
|
2020-03-30 16:03:08 +00:00
|
|
|
{
|
2020-04-03 10:40:46 +00:00
|
|
|
if (alter_conversions.isColumnRenamed(column.name))
|
|
|
|
{
|
|
|
|
String old_name = alter_conversions.getColumnOldName(column.name);
|
|
|
|
if (columns_from_part.count(old_name))
|
|
|
|
addStreams(old_name, *columns_from_part[old_name], profile_callback_, clock_type_);
|
|
|
|
}
|
2020-03-30 16:03:08 +00:00
|
|
|
else
|
2020-04-03 10:40:46 +00:00
|
|
|
{
|
2020-03-30 16:03:08 +00:00
|
|
|
addStreams(column.name, *column.type, profile_callback_, clock_type_);
|
2020-04-03 10:40:46 +00:00
|
|
|
}
|
2020-03-30 16:03:08 +00:00
|
|
|
}
|
2020-02-25 08:53:14 +00:00
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
storage.reportBrokenPart(data_part->name);
|
|
|
|
throw;
|
|
|
|
}
|
2016-07-19 10:57:57 +00:00
|
|
|
}
|
|
|
|
|
2019-11-13 01:57:45 +00:00
|
|
|
|
2019-12-19 13:10:57 +00:00
|
|
|
size_t MergeTreeReaderWide::readRows(size_t from_mark, bool continue_reading, size_t max_rows_to_read, Columns & res_columns)
|
2016-07-19 10:57:57 +00:00
|
|
|
{
|
2017-06-16 20:11:02 +00:00
|
|
|
size_t read_rows = 0;
|
2017-04-01 07:20:54 +00:00
|
|
|
try
|
|
|
|
{
|
2019-09-23 19:22:02 +00:00
|
|
|
size_t num_columns = columns.size();
|
|
|
|
|
|
|
|
if (res_columns.size() != num_columns)
|
|
|
|
throw Exception("invalid number of columns passed to MergeTreeReader::readRows. "
|
|
|
|
"Expected " + toString(num_columns) + ", "
|
|
|
|
"got " + toString(res_columns.size()), ErrorCodes::LOGICAL_ERROR);
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Pointers to offset columns that are common to the nested data structure columns.
|
|
|
|
/// If append is true, then the value will be equal to nullptr and will be used only to
|
|
|
|
/// check that the offsets column has been already read.
|
|
|
|
OffsetColumns offset_columns;
|
|
|
|
|
2019-09-23 19:22:02 +00:00
|
|
|
auto name_and_type = columns.begin();
|
|
|
|
for (size_t pos = 0; pos < num_columns; ++pos, ++name_and_type)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2020-03-30 16:03:08 +00:00
|
|
|
String name = name_and_type->name;
|
2020-04-03 10:40:46 +00:00
|
|
|
if (alter_conversions.isColumnRenamed(name))
|
2020-03-30 16:03:08 +00:00
|
|
|
{
|
2020-04-03 10:40:46 +00:00
|
|
|
String original_name = alter_conversions.getColumnOldName(name);
|
2020-03-30 16:03:08 +00:00
|
|
|
if (!columns_from_part.count(name) && columns_from_part.count(original_name))
|
|
|
|
name = original_name;
|
|
|
|
}
|
|
|
|
|
2020-02-25 08:53:14 +00:00
|
|
|
DataTypePtr type;
|
|
|
|
if (columns_from_part.count(name))
|
|
|
|
type = columns_from_part[name];
|
|
|
|
else
|
|
|
|
type = name_and_type->type;
|
2019-09-23 19:22:02 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// The column is already present in the block so we will append the values to the end.
|
2019-09-23 19:22:02 +00:00
|
|
|
bool append = res_columns[pos] != nullptr;
|
2017-12-15 21:11:24 +00:00
|
|
|
if (!append)
|
2020-02-25 08:53:14 +00:00
|
|
|
res_columns[pos] = type->createColumn();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-12-18 05:37:20 +00:00
|
|
|
/// To keep offsets shared. TODO Very dangerous. Get rid of this.
|
2019-09-23 19:22:02 +00:00
|
|
|
MutableColumnPtr column = res_columns[pos]->assumeMutable();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
bool read_offsets = true;
|
|
|
|
|
|
|
|
/// For nested data structures collect pointers to offset columns.
|
2019-09-23 19:22:02 +00:00
|
|
|
if (const auto * type_arr = typeid_cast<const DataTypeArray *>(type.get()))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2019-09-23 19:22:02 +00:00
|
|
|
String table_name = Nested::extractTableName(name);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2019-09-23 19:22:02 +00:00
|
|
|
auto it_inserted = offset_columns.emplace(table_name, nullptr);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-12-17 07:15:13 +00:00
|
|
|
/// offsets have already been read on the previous iteration and we don't need to read it again
|
2017-12-15 21:11:24 +00:00
|
|
|
if (!it_inserted.second)
|
|
|
|
read_offsets = false;
|
2017-12-17 07:15:13 +00:00
|
|
|
|
|
|
|
/// need to create new offsets
|
|
|
|
if (it_inserted.second && !append)
|
|
|
|
it_inserted.first->second = ColumnArray::ColumnOffsets::create();
|
|
|
|
|
|
|
|
/// share offsets in all elements of nested structure
|
|
|
|
if (!append)
|
2018-03-20 14:17:09 +00:00
|
|
|
column = ColumnArray::create(type_arr->getNestedType()->createColumn(),
|
|
|
|
it_inserted.first->second)->assumeMutable();
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2017-12-15 21:11:24 +00:00
|
|
|
size_t column_size_before_reading = column->size();
|
2017-11-21 02:23:41 +00:00
|
|
|
|
2019-09-23 19:22:02 +00:00
|
|
|
readData(name, *type, *column, from_mark, continue_reading, max_rows_to_read, read_offsets);
|
2017-11-21 02:23:41 +00:00
|
|
|
|
|
|
|
/// For elements of Nested, column_size_before_reading may be greater than column size
|
|
|
|
/// if offsets are not empty and were already read, but elements are empty.
|
2019-09-23 19:22:02 +00:00
|
|
|
if (!column->empty())
|
2017-12-15 21:11:24 +00:00
|
|
|
read_rows = std::max(read_rows, column->size() - column_size_before_reading);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
catch (Exception & e)
|
|
|
|
{
|
|
|
|
/// Better diagnostics.
|
2019-09-23 19:22:02 +00:00
|
|
|
e.addMessage("(while reading column " + name + ")");
|
2017-04-01 07:20:54 +00:00
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
2019-09-23 19:22:02 +00:00
|
|
|
if (column->empty())
|
|
|
|
res_columns[pos] = nullptr;
|
2017-12-17 05:21:04 +00:00
|
|
|
else
|
2019-09-23 19:22:02 +00:00
|
|
|
res_columns[pos] = std::move(column);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2019-09-23 19:22:02 +00:00
|
|
|
/// NOTE: positions for all streams must be kept in sync.
|
|
|
|
/// In particular, even if for some streams there are no rows to be read,
|
2017-04-01 07:20:54 +00:00
|
|
|
/// you must ensure that no seeks are skipped and at this point they all point to to_mark.
|
|
|
|
}
|
|
|
|
catch (Exception & e)
|
|
|
|
{
|
|
|
|
if (e.code() != ErrorCodes::MEMORY_LIMIT_EXCEEDED)
|
|
|
|
storage.reportBrokenPart(data_part->name);
|
|
|
|
|
|
|
|
/// Better diagnostics.
|
2020-02-27 16:47:40 +00:00
|
|
|
e.addMessage("(while reading from part " + data_part->getFullPath() + " "
|
2019-09-23 19:22:02 +00:00
|
|
|
"from mark " + toString(from_mark) + " "
|
|
|
|
"with max_rows_to_read = " + toString(max_rows_to_read) + ")");
|
2017-04-01 07:20:54 +00:00
|
|
|
throw;
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
storage.reportBrokenPart(data_part->name);
|
|
|
|
|
|
|
|
throw;
|
|
|
|
}
|
2017-06-16 20:11:02 +00:00
|
|
|
|
|
|
|
return read_rows;
|
2016-07-19 10:57:57 +00:00
|
|
|
}
|
|
|
|
|
2019-10-10 16:30:30 +00:00
|
|
|
void MergeTreeReaderWide::addStreams(const String & name, const IDataType & type,
|
2017-08-07 07:31:16 +00:00
|
|
|
const ReadBufferFromFileBase::ProfileCallback & profile_callback, clockid_t clock_type)
|
2016-07-19 10:57:57 +00:00
|
|
|
{
|
2017-08-07 07:31:16 +00:00
|
|
|
IDataType::StreamCallback callback = [&] (const IDataType::SubstreamPath & substream_path)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-08-07 07:31:16 +00:00
|
|
|
String stream_name = IDataType::getFileNameForStream(name, substream_path);
|
2017-11-20 02:15:15 +00:00
|
|
|
|
2017-08-07 07:31:16 +00:00
|
|
|
if (streams.count(stream_name))
|
|
|
|
return;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2019-06-16 20:13:54 +00:00
|
|
|
bool data_file_exists = data_part->checksums.files.count(stream_name + DATA_FILE_EXTENSION);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-08-07 07:31:16 +00:00
|
|
|
/** If data file is missing then we will not try to open it.
|
|
|
|
* It is necessary since it allows to add new column to structure of the table without creating new files for old parts.
|
|
|
|
*/
|
2017-11-21 02:23:41 +00:00
|
|
|
if (!data_file_exists)
|
2017-04-01 07:20:54 +00:00
|
|
|
return;
|
2019-12-18 16:41:11 +00:00
|
|
|
|
2019-02-05 14:50:25 +00:00
|
|
|
streams.emplace(stream_name, std::make_unique<MergeTreeReaderStream>(
|
2020-02-27 16:47:40 +00:00
|
|
|
data_part->disk, data_part->getFullRelativePath() + stream_name, DATA_FILE_EXTENSION,
|
|
|
|
data_part->getMarksCount(), all_mark_ranges, settings, mark_cache,
|
2019-02-27 20:02:48 +00:00
|
|
|
uncompressed_cache, data_part->getFileSizeOrZero(stream_name + DATA_FILE_EXTENSION),
|
2019-06-19 10:07:56 +00:00
|
|
|
&data_part->index_granularity_info,
|
2018-11-15 14:06:54 +00:00
|
|
|
profile_callback, clock_type));
|
2017-08-07 07:31:16 +00:00
|
|
|
};
|
|
|
|
|
2019-01-04 12:10:00 +00:00
|
|
|
IDataType::SubstreamPath substream_path;
|
|
|
|
type.enumerateStreams(callback, substream_path);
|
2016-07-19 10:57:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-10-10 16:30:30 +00:00
|
|
|
void MergeTreeReaderWide::readData(
|
2017-06-05 20:43:23 +00:00
|
|
|
const String & name, const IDataType & type, IColumn & column,
|
2017-07-11 09:32:39 +00:00
|
|
|
size_t from_mark, bool continue_reading, size_t max_rows_to_read,
|
2017-08-07 07:31:16 +00:00
|
|
|
bool with_offsets)
|
2016-07-19 10:57:57 +00:00
|
|
|
{
|
2018-06-07 18:14:37 +00:00
|
|
|
auto get_stream_getter = [&](bool stream_for_prefix) -> IDataType::InputStreamGetter
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2019-01-04 12:10:00 +00:00
|
|
|
return [&, stream_for_prefix](const IDataType::SubstreamPath & substream_path) -> ReadBuffer *
|
2018-06-07 18:14:37 +00:00
|
|
|
{
|
|
|
|
/// If offsets for arrays have already been read.
|
2019-01-04 12:10:00 +00:00
|
|
|
if (!with_offsets && substream_path.size() == 1 && substream_path[0].type == IDataType::Substream::ArraySizes)
|
2018-06-07 18:14:37 +00:00
|
|
|
return nullptr;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2019-01-04 12:10:00 +00:00
|
|
|
String stream_name = IDataType::getFileNameForStream(name, substream_path);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2018-06-07 18:14:37 +00:00
|
|
|
auto it = streams.find(stream_name);
|
|
|
|
if (it == streams.end())
|
|
|
|
return nullptr;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2019-02-05 14:50:25 +00:00
|
|
|
MergeTreeReaderStream & stream = *it->second;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2018-07-09 18:19:03 +00:00
|
|
|
if (stream_for_prefix)
|
|
|
|
{
|
|
|
|
stream.seekToStart();
|
|
|
|
continue_reading = false;
|
|
|
|
}
|
|
|
|
else if (!continue_reading)
|
2018-06-07 18:14:37 +00:00
|
|
|
stream.seekToMark(from_mark);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2018-06-07 18:14:37 +00:00
|
|
|
return stream.data_buffer;
|
|
|
|
};
|
2017-08-07 07:31:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
double & avg_value_size_hint = avg_value_size_hints[name];
|
2019-12-12 18:55:19 +00:00
|
|
|
IDataType::DeserializeBinaryBulkSettings deserialize_settings;
|
|
|
|
deserialize_settings.avg_value_size_hint = avg_value_size_hint;
|
2018-05-21 16:21:15 +00:00
|
|
|
|
|
|
|
if (deserialize_binary_bulk_state_map.count(name) == 0)
|
2018-06-07 18:14:37 +00:00
|
|
|
{
|
2019-12-12 18:55:19 +00:00
|
|
|
deserialize_settings.getter = get_stream_getter(true);
|
|
|
|
type.deserializeBinaryBulkStatePrefix(deserialize_settings, deserialize_binary_bulk_state_map[name]);
|
2018-06-07 18:14:37 +00:00
|
|
|
}
|
2018-05-21 16:21:15 +00:00
|
|
|
|
2019-12-12 18:55:19 +00:00
|
|
|
deserialize_settings.getter = get_stream_getter(false);
|
|
|
|
deserialize_settings.continuous_reading = continue_reading;
|
2018-05-21 16:21:15 +00:00
|
|
|
auto & deserialize_state = deserialize_binary_bulk_state_map[name];
|
2019-12-12 18:55:19 +00:00
|
|
|
type.deserializeBinaryBulkWithMultipleStreams(column, max_rows_to_read, deserialize_settings, deserialize_state);
|
2017-08-07 07:31:16 +00:00
|
|
|
IDataType::updateAvgValueSizeHint(column, avg_value_size_hint);
|
2016-07-19 10:57:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|