2014-12-30 18:04:53 +00:00
|
|
|
|
#include <DB/DataStreams/AddingConstColumnBlockInputStream.h>
|
2015-10-01 03:30:50 +00:00
|
|
|
|
#include <DB/DataStreams/narrowBlockInputStreams.h>
|
2012-05-30 05:53:09 +00:00
|
|
|
|
#include <DB/Storages/StorageMerge.h>
|
2014-01-17 15:19:20 +00:00
|
|
|
|
#include <DB/Common/VirtualColumnUtils.h>
|
2014-07-11 08:12:03 +00:00
|
|
|
|
#include <DB/Interpreters/InterpreterAlterQuery.h>
|
2015-05-04 17:52:19 +00:00
|
|
|
|
#include <DB/Interpreters/ExpressionActions.h>
|
2014-07-31 09:11:49 +00:00
|
|
|
|
#include <DB/Storages/VirtualColumnFactory.h>
|
2014-12-30 18:04:53 +00:00
|
|
|
|
#include <DB/Parsers/ASTSelectQuery.h>
|
|
|
|
|
#include <DB/DataTypes/DataTypeString.h>
|
|
|
|
|
#include <DB/Columns/ColumnString.h>
|
2016-03-19 01:18:49 +00:00
|
|
|
|
#include <DB/Databases/IDatabase.h>
|
2014-12-30 18:04:53 +00:00
|
|
|
|
|
2012-05-30 05:53:09 +00:00
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
|
{
|
|
|
|
|
|
2016-01-11 21:46:36 +00:00
|
|
|
|
namespace ErrorCodes
|
|
|
|
|
{
|
|
|
|
|
extern const int ILLEGAL_PREWHERE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2012-05-30 05:53:09 +00:00
|
|
|
|
StorageMerge::StorageMerge(
|
|
|
|
|
const std::string & name_,
|
|
|
|
|
NamesAndTypesListPtr columns_,
|
|
|
|
|
const String & source_database_,
|
|
|
|
|
const String & table_name_regexp_,
|
2013-05-05 18:02:05 +00:00
|
|
|
|
const Context & context_)
|
2014-09-30 03:08:47 +00:00
|
|
|
|
: name(name_), columns(columns_), source_database(source_database_),
|
|
|
|
|
table_name_regexp(table_name_regexp_), context(context_)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
StorageMerge::StorageMerge(
|
|
|
|
|
const std::string & name_,
|
|
|
|
|
NamesAndTypesListPtr columns_,
|
2014-10-03 15:30:10 +00:00
|
|
|
|
const NamesAndTypesList & materialized_columns_,
|
2014-09-30 03:08:47 +00:00
|
|
|
|
const NamesAndTypesList & alias_columns_,
|
|
|
|
|
const ColumnDefaults & column_defaults_,
|
|
|
|
|
const String & source_database_,
|
|
|
|
|
const String & table_name_regexp_,
|
|
|
|
|
const Context & context_)
|
2014-10-03 15:30:10 +00:00
|
|
|
|
: IStorage{materialized_columns_, alias_columns_, column_defaults_},
|
2014-09-30 03:08:47 +00:00
|
|
|
|
name(name_), columns(columns_), source_database(source_database_),
|
|
|
|
|
table_name_regexp(table_name_regexp_), context(context_)
|
2012-05-30 05:53:09 +00:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-06 11:26:35 +00:00
|
|
|
|
StoragePtr StorageMerge::create(
|
|
|
|
|
const std::string & name_,
|
|
|
|
|
NamesAndTypesListPtr columns_,
|
|
|
|
|
const String & source_database_,
|
|
|
|
|
const String & table_name_regexp_,
|
2013-05-05 18:02:05 +00:00
|
|
|
|
const Context & context_)
|
2013-02-06 11:26:35 +00:00
|
|
|
|
{
|
2016-08-26 21:25:05 +00:00
|
|
|
|
return make_shared(
|
2014-09-30 03:08:47 +00:00
|
|
|
|
name_, columns_,
|
|
|
|
|
source_database_, table_name_regexp_, context_
|
2016-08-26 21:25:05 +00:00
|
|
|
|
);
|
2014-09-30 03:08:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
StoragePtr StorageMerge::create(
|
|
|
|
|
const std::string & name_,
|
|
|
|
|
NamesAndTypesListPtr columns_,
|
2014-10-03 15:30:10 +00:00
|
|
|
|
const NamesAndTypesList & materialized_columns_,
|
2014-09-30 03:08:47 +00:00
|
|
|
|
const NamesAndTypesList & alias_columns_,
|
|
|
|
|
const ColumnDefaults & column_defaults_,
|
|
|
|
|
const String & source_database_,
|
|
|
|
|
const String & table_name_regexp_,
|
|
|
|
|
const Context & context_)
|
|
|
|
|
{
|
2016-08-26 21:25:05 +00:00
|
|
|
|
return make_shared(
|
2014-10-03 15:30:10 +00:00
|
|
|
|
name_, columns_, materialized_columns_, alias_columns_, column_defaults_,
|
2014-09-30 03:08:47 +00:00
|
|
|
|
source_database_, table_name_regexp_, context_
|
2016-08-26 21:25:05 +00:00
|
|
|
|
);
|
2013-02-06 11:26:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-07-31 09:11:49 +00:00
|
|
|
|
NameAndTypePair StorageMerge::getColumn(const String & column_name) const
|
2014-01-16 14:52:13 +00:00
|
|
|
|
{
|
2014-07-31 09:11:49 +00:00
|
|
|
|
auto type = VirtualColumnFactory::tryGetType(column_name);
|
|
|
|
|
if (type)
|
|
|
|
|
return NameAndTypePair(column_name, type);
|
|
|
|
|
|
2014-09-30 03:08:47 +00:00
|
|
|
|
return IStorage::getColumn(column_name);
|
2014-01-16 14:52:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-07-31 09:11:49 +00:00
|
|
|
|
bool StorageMerge::hasColumn(const String & column_name) const
|
2014-01-16 14:52:13 +00:00
|
|
|
|
{
|
2014-09-30 03:08:47 +00:00
|
|
|
|
return VirtualColumnFactory::hasColumn(column_name) || IStorage::hasColumn(column_name);
|
2014-01-16 14:52:13 +00:00
|
|
|
|
}
|
2012-05-30 05:53:09 +00:00
|
|
|
|
|
|
|
|
|
BlockInputStreams StorageMerge::read(
|
|
|
|
|
const Names & column_names,
|
|
|
|
|
ASTPtr query,
|
2014-12-17 11:53:17 +00:00
|
|
|
|
const Context & context,
|
2013-02-01 19:02:04 +00:00
|
|
|
|
const Settings & settings,
|
2012-05-30 05:53:09 +00:00
|
|
|
|
QueryProcessingStage::Enum & processed_stage,
|
2014-12-17 11:53:17 +00:00
|
|
|
|
const size_t max_block_size,
|
|
|
|
|
const unsigned threads)
|
2012-05-30 05:53:09 +00:00
|
|
|
|
{
|
|
|
|
|
BlockInputStreams res;
|
|
|
|
|
|
2014-01-16 14:52:13 +00:00
|
|
|
|
Names virt_column_names, real_column_names;
|
2014-01-22 14:24:05 +00:00
|
|
|
|
for (const auto & it : column_names)
|
2014-07-31 09:11:49 +00:00
|
|
|
|
if (it != "_table")
|
2014-01-16 14:52:13 +00:00
|
|
|
|
real_column_names.push_back(it);
|
|
|
|
|
else
|
|
|
|
|
virt_column_names.push_back(it);
|
|
|
|
|
|
2014-03-20 10:59:45 +00:00
|
|
|
|
StorageVector selected_tables;
|
2012-05-30 05:53:09 +00:00
|
|
|
|
|
|
|
|
|
/// Среди всех стадий, до которых обрабатывается запрос в таблицах-источниках, выберем минимальную.
|
|
|
|
|
processed_stage = QueryProcessingStage::Complete;
|
|
|
|
|
QueryProcessingStage::Enum tmp_processed_stage = QueryProcessingStage::Complete;
|
|
|
|
|
|
2014-04-13 09:46:36 +00:00
|
|
|
|
/** Сначала составим список выбранных таблиц, чтобы узнать его размер.
|
|
|
|
|
* Это нужно, чтобы правильно передать в каждую таблицу рекомендацию по количеству потоков.
|
|
|
|
|
*/
|
|
|
|
|
getSelectedTables(selected_tables);
|
2012-05-30 05:53:09 +00:00
|
|
|
|
|
2014-04-12 16:11:06 +00:00
|
|
|
|
/// Если в запросе используется PREWHERE, надо убедиться, что все таблицы это поддерживают.
|
2014-06-26 00:58:14 +00:00
|
|
|
|
if (typeid_cast<const ASTSelectQuery &>(*query).prewhere_expression)
|
2014-04-12 16:11:06 +00:00
|
|
|
|
for (const auto & table : selected_tables)
|
|
|
|
|
if (!table->supportsPrewhere())
|
|
|
|
|
throw Exception("Storage " + table->getName() + " doesn't support PREWHERE.", ErrorCodes::ILLEGAL_PREWHERE);
|
|
|
|
|
|
2014-03-20 10:59:45 +00:00
|
|
|
|
TableLocks table_locks;
|
|
|
|
|
|
|
|
|
|
/// Нельзя, чтобы эти таблицы кто-нибудь удалил, пока мы их читаем.
|
2014-04-12 16:11:06 +00:00
|
|
|
|
for (auto & table : selected_tables)
|
2014-03-20 10:59:45 +00:00
|
|
|
|
table_locks.push_back(table->lockStructure(false));
|
|
|
|
|
|
2014-02-11 18:38:21 +00:00
|
|
|
|
Block virtual_columns_block = getBlockWithVirtualColumns(selected_tables);
|
2014-02-26 10:59:56 +00:00
|
|
|
|
|
|
|
|
|
/// Если запрошен хотя бы один виртуальный столбец, пробуем индексировать
|
|
|
|
|
if (!virt_column_names.empty())
|
2014-10-07 18:42:35 +00:00
|
|
|
|
VirtualColumnUtils::filterBlockWithQuery(query, virtual_columns_block, context);
|
2014-02-26 10:59:56 +00:00
|
|
|
|
|
2014-07-31 09:11:49 +00:00
|
|
|
|
std::multiset<String> values = VirtualColumnUtils::extractSingleValueFromBlock<String>(virtual_columns_block, "_table");
|
2014-02-11 18:38:21 +00:00
|
|
|
|
|
2015-06-03 03:56:40 +00:00
|
|
|
|
/** На всякий случай отключаем оптимизацию "перенос в PREWHERE",
|
|
|
|
|
* так как нет уверенности, что она работает, когда одна из таблиц MergeTree, а другая - нет.
|
|
|
|
|
*/
|
|
|
|
|
Settings modified_settings = settings;
|
|
|
|
|
modified_settings.optimize_move_to_prewhere = false;
|
|
|
|
|
|
2014-04-12 16:11:06 +00:00
|
|
|
|
for (size_t i = 0, size = selected_tables.size(); i < size; ++i)
|
2012-05-30 05:53:09 +00:00
|
|
|
|
{
|
2014-03-20 10:59:45 +00:00
|
|
|
|
StoragePtr table = selected_tables[i];
|
2014-04-12 16:11:06 +00:00
|
|
|
|
auto & table_lock = table_locks[i];
|
2014-03-20 10:59:45 +00:00
|
|
|
|
|
2014-07-29 14:05:15 +00:00
|
|
|
|
if (values.find(table->getTableName()) == values.end())
|
2014-02-11 18:38:21 +00:00
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
/// Если в запросе только виртуальные столбцы, надо запросить хотя бы один любой другой.
|
|
|
|
|
if (real_column_names.size() == 0)
|
2014-03-20 10:59:45 +00:00
|
|
|
|
real_column_names.push_back(ExpressionActions::getSmallestColumn(table->getColumnsList()));
|
2014-02-11 18:38:21 +00:00
|
|
|
|
|
2014-02-26 10:59:56 +00:00
|
|
|
|
/// Подменяем виртуальный столбец на его значение
|
|
|
|
|
ASTPtr modified_query_ast = query->clone();
|
2014-07-31 09:11:49 +00:00
|
|
|
|
VirtualColumnUtils::rewriteEntityInAst(modified_query_ast, "_table", table->getTableName());
|
2014-02-26 10:59:56 +00:00
|
|
|
|
|
2014-03-20 10:59:45 +00:00
|
|
|
|
BlockInputStreams source_streams = table->read(
|
2014-01-16 14:52:13 +00:00
|
|
|
|
real_column_names,
|
2014-02-26 10:59:56 +00:00
|
|
|
|
modified_query_ast,
|
2014-12-17 11:53:17 +00:00
|
|
|
|
context,
|
2015-06-03 03:56:40 +00:00
|
|
|
|
modified_settings,
|
2012-05-30 05:53:09 +00:00
|
|
|
|
tmp_processed_stage,
|
|
|
|
|
max_block_size,
|
2014-04-12 16:11:06 +00:00
|
|
|
|
size > threads ? 1 : (threads / size));
|
2012-05-30 05:53:09 +00:00
|
|
|
|
|
2014-03-20 10:59:45 +00:00
|
|
|
|
for (auto & stream : source_streams)
|
|
|
|
|
stream->addTableLock(table_lock);
|
|
|
|
|
|
2014-01-16 14:52:13 +00:00
|
|
|
|
for (auto & virtual_column : virt_column_names)
|
|
|
|
|
{
|
2014-07-31 09:11:49 +00:00
|
|
|
|
if (virtual_column == "_table")
|
2014-01-16 14:52:13 +00:00
|
|
|
|
{
|
|
|
|
|
for (auto & stream : source_streams)
|
2016-05-28 12:22:22 +00:00
|
|
|
|
stream = std::make_shared<AddingConstColumnBlockInputStream<String>>(
|
|
|
|
|
stream, std::make_shared<DataTypeString>(), table->getTableName(), "_table");
|
2014-01-16 14:52:13 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-20 10:59:45 +00:00
|
|
|
|
res.insert(res.end(), source_streams.begin(), source_streams.end());
|
2012-05-30 05:53:09 +00:00
|
|
|
|
|
|
|
|
|
if (tmp_processed_stage < processed_stage)
|
|
|
|
|
processed_stage = tmp_processed_stage;
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-01 03:30:50 +00:00
|
|
|
|
return narrowBlockInputStreams(res, threads);
|
2012-05-30 05:53:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-02-11 18:38:21 +00:00
|
|
|
|
/// Построить блок состоящий только из возможных значений виртуальных столбцов
|
|
|
|
|
Block StorageMerge::getBlockWithVirtualColumns(const std::vector<StoragePtr> & selected_tables) const
|
|
|
|
|
{
|
|
|
|
|
Block res;
|
2016-05-28 07:48:40 +00:00
|
|
|
|
ColumnWithTypeAndName _table(std::make_shared<ColumnString>(), std::make_shared<DataTypeString>(), "_table");
|
2014-02-11 18:38:21 +00:00
|
|
|
|
|
2014-03-20 10:59:45 +00:00
|
|
|
|
for (StorageVector::const_iterator it = selected_tables.begin(); it != selected_tables.end(); ++it)
|
2014-02-11 18:38:21 +00:00
|
|
|
|
_table.column->insert((*it)->getTableName());
|
|
|
|
|
|
|
|
|
|
res.insert(_table);
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-13 09:46:36 +00:00
|
|
|
|
void StorageMerge::getSelectedTables(StorageVector & selected_tables) const
|
2013-09-23 12:01:19 +00:00
|
|
|
|
{
|
2016-03-19 01:18:49 +00:00
|
|
|
|
auto database = context.getDatabase(source_database);
|
|
|
|
|
auto iterator = database->getIterator();
|
|
|
|
|
|
|
|
|
|
while (iterator->isValid())
|
|
|
|
|
{
|
|
|
|
|
if (table_name_regexp.match(iterator->name()))
|
|
|
|
|
{
|
|
|
|
|
auto & table = iterator->table();
|
|
|
|
|
if (table.get() != this)
|
|
|
|
|
selected_tables.emplace_back(table);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
iterator->next();
|
|
|
|
|
}
|
2012-05-30 05:53:09 +00:00
|
|
|
|
}
|
2013-09-23 12:01:19 +00:00
|
|
|
|
|
|
|
|
|
|
2016-01-28 01:00:27 +00:00
|
|
|
|
void StorageMerge::alter(const AlterCommands & params, const String & database_name, const String & table_name, const Context & context)
|
2013-09-23 12:01:19 +00:00
|
|
|
|
{
|
2016-05-05 18:28:46 +00:00
|
|
|
|
for (const auto & param : params)
|
|
|
|
|
if (param.type == AlterCommand::MODIFY_PRIMARY_KEY)
|
|
|
|
|
throw Exception("Storage engine " + getName() + " doesn't support primary key.", ErrorCodes::NOT_IMPLEMENTED);
|
|
|
|
|
|
2014-07-11 08:12:03 +00:00
|
|
|
|
auto lock = lockStructureForAlter();
|
2014-10-16 13:37:01 +00:00
|
|
|
|
params.apply(*columns, materialized_columns, alias_columns, column_defaults);
|
2016-05-13 21:08:19 +00:00
|
|
|
|
|
|
|
|
|
context.getDatabase(database_name)->alterTable(
|
|
|
|
|
context, table_name,
|
|
|
|
|
*columns, materialized_columns, alias_columns, column_defaults, {});
|
2013-09-23 12:01:19 +00:00
|
|
|
|
}
|
2014-07-11 08:12:03 +00:00
|
|
|
|
|
2013-09-23 12:01:19 +00:00
|
|
|
|
}
|