2013-11-08 17:43:03 +00:00
|
|
|
|
#include <DB/Parsers/ASTCreateQuery.h>
|
2013-11-13 14:39:48 +00:00
|
|
|
|
#include <DB/Parsers/ASTDropQuery.h>
|
2013-11-08 17:43:03 +00:00
|
|
|
|
|
|
|
|
|
#include <DB/Interpreters/InterpreterCreateQuery.h>
|
2013-11-13 14:39:48 +00:00
|
|
|
|
#include <DB/Interpreters/InterpreterDropQuery.h>
|
2013-11-08 17:43:03 +00:00
|
|
|
|
|
2013-11-15 09:43:50 +00:00
|
|
|
|
#include <DB/Storages/StorageMaterializedView.h>
|
2014-08-05 10:56:58 +00:00
|
|
|
|
#include <DB/Storages/VirtualColumnFactory.h>
|
2013-11-15 09:43:50 +00:00
|
|
|
|
|
|
|
|
|
|
2013-11-08 17:43:03 +00:00
|
|
|
|
namespace DB
|
|
|
|
|
{
|
|
|
|
|
|
2016-01-11 21:46:36 +00:00
|
|
|
|
namespace ErrorCodes
|
|
|
|
|
{
|
|
|
|
|
extern const int LOGICAL_ERROR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2014-09-30 03:08:47 +00:00
|
|
|
|
StoragePtr StorageMaterializedView::create(
|
|
|
|
|
const String & table_name_,
|
|
|
|
|
const String & database_name_,
|
|
|
|
|
Context & context_,
|
|
|
|
|
ASTPtr & query_,
|
|
|
|
|
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_,
|
|
|
|
|
bool attach_)
|
2013-11-08 17:43:03 +00:00
|
|
|
|
{
|
2014-09-30 03:08:47 +00:00
|
|
|
|
return (new StorageMaterializedView{
|
|
|
|
|
table_name_, database_name_, context_, query_,
|
2014-10-03 15:30:10 +00:00
|
|
|
|
columns_, materialized_columns_, alias_columns_, column_defaults_,
|
2014-09-30 03:08:47 +00:00
|
|
|
|
attach_
|
|
|
|
|
})->thisPtr();
|
2013-11-08 17:43:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-09-30 03:08:47 +00:00
|
|
|
|
StorageMaterializedView::StorageMaterializedView(
|
|
|
|
|
const String & table_name_,
|
|
|
|
|
const String & database_name_,
|
|
|
|
|
Context & context_,
|
|
|
|
|
ASTPtr & query_,
|
|
|
|
|
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_,
|
|
|
|
|
bool attach_)
|
2014-10-03 15:30:10 +00:00
|
|
|
|
: StorageView{table_name_, database_name_, context_, query_, columns_, materialized_columns_, alias_columns_, column_defaults_}
|
2013-11-08 17:43:03 +00:00
|
|
|
|
{
|
2014-06-26 00:58:14 +00:00
|
|
|
|
ASTCreateQuery & create = typeid_cast<ASTCreateQuery &>(*query_);
|
2013-11-08 17:43:03 +00:00
|
|
|
|
|
2015-06-18 02:11:05 +00:00
|
|
|
|
auto inner_table_name = getInnerTableName();
|
|
|
|
|
|
2013-11-13 14:39:48 +00:00
|
|
|
|
/// Если запрос ATTACH, то к этому моменту внутренняя таблица уже должна быть подключена.
|
2013-11-08 17:43:03 +00:00
|
|
|
|
if (attach_)
|
|
|
|
|
{
|
2015-06-22 23:17:49 +00:00
|
|
|
|
data = context.tryGetTable(database_name, inner_table_name);
|
2015-06-18 02:11:05 +00:00
|
|
|
|
if (!data)
|
2013-11-13 14:39:48 +00:00
|
|
|
|
throw Exception("Inner table is not attached yet."
|
|
|
|
|
" Materialized view: " + database_name + "." + table_name + "."
|
2015-06-18 02:11:05 +00:00
|
|
|
|
" Inner table: " + database_name + "." + inner_table_name + ".",
|
2013-11-13 14:39:48 +00:00
|
|
|
|
DB::ErrorCodes::LOGICAL_ERROR);
|
2013-11-08 17:43:03 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2013-11-13 14:39:48 +00:00
|
|
|
|
/// Составим запрос для создания внутреннего хранилища.
|
|
|
|
|
ASTCreateQuery * manual_create_query = new ASTCreateQuery();
|
|
|
|
|
manual_create_query->database = database_name;
|
2015-06-18 02:11:05 +00:00
|
|
|
|
manual_create_query->table = inner_table_name;
|
2013-11-13 14:39:48 +00:00
|
|
|
|
manual_create_query->columns = create.columns;
|
2014-07-14 14:26:11 +00:00
|
|
|
|
manual_create_query->children.push_back(manual_create_query->columns);
|
2013-11-13 14:39:48 +00:00
|
|
|
|
ASTPtr ast_create_query = manual_create_query;
|
|
|
|
|
|
|
|
|
|
/// Если не указан в запросе тип хранилища попробовать извлечь его из запроса Select.
|
|
|
|
|
if (!create.inner_storage)
|
|
|
|
|
{
|
|
|
|
|
/// TODO так же попытаться извлечь params для создания хранилища
|
|
|
|
|
ASTFunction * func = new ASTFunction();
|
|
|
|
|
func->name = context.getTable(select_database_name, select_table_name)->getName();
|
|
|
|
|
manual_create_query->storage = func;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
manual_create_query->storage = create.inner_storage;
|
2013-11-08 17:43:03 +00:00
|
|
|
|
|
2014-07-14 14:26:11 +00:00
|
|
|
|
manual_create_query->children.push_back(manual_create_query->storage);
|
|
|
|
|
|
2013-11-13 14:39:48 +00:00
|
|
|
|
/// Выполним запрос.
|
|
|
|
|
InterpreterCreateQuery create_interpreter(ast_create_query, context);
|
2015-06-18 02:11:05 +00:00
|
|
|
|
create_interpreter.execute();
|
|
|
|
|
|
|
|
|
|
data = context.getTable(database_name, inner_table_name);
|
2013-11-13 14:39:48 +00:00
|
|
|
|
}
|
2013-11-08 17:43:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-08-05 10:56:58 +00:00
|
|
|
|
NameAndTypePair StorageMaterializedView::getColumn(const String & column_name) const
|
|
|
|
|
{
|
|
|
|
|
auto type = VirtualColumnFactory::tryGetType(column_name);
|
|
|
|
|
if (type)
|
|
|
|
|
return NameAndTypePair(column_name, type);
|
|
|
|
|
|
|
|
|
|
return getRealColumn(column_name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool StorageMaterializedView::hasColumn(const String & column_name) const
|
|
|
|
|
{
|
2014-10-03 15:30:10 +00:00
|
|
|
|
return VirtualColumnFactory::hasColumn(column_name) || IStorage::hasColumn(column_name);
|
2014-08-05 10:56:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-11-08 17:43:03 +00:00
|
|
|
|
BlockInputStreams StorageMaterializedView::read(
|
|
|
|
|
const Names & column_names,
|
|
|
|
|
ASTPtr query,
|
2014-12-17 11:53:17 +00:00
|
|
|
|
const Context & context,
|
2013-11-08 17:43:03 +00:00
|
|
|
|
const Settings & settings,
|
|
|
|
|
QueryProcessingStage::Enum & processed_stage,
|
2014-12-17 11:53:17 +00:00
|
|
|
|
const size_t max_block_size,
|
|
|
|
|
const unsigned threads)
|
2013-11-08 17:43:03 +00:00
|
|
|
|
{
|
2014-12-17 11:53:17 +00:00
|
|
|
|
return data->read(column_names, query, context, settings, processed_stage, max_block_size, threads);
|
2013-11-08 17:43:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-09-10 20:43:42 +00:00
|
|
|
|
BlockOutputStreamPtr StorageMaterializedView::write(ASTPtr query, const Settings & settings)
|
2013-11-08 17:43:03 +00:00
|
|
|
|
{
|
2015-09-10 20:43:42 +00:00
|
|
|
|
return data->write(query, settings);
|
2013-11-08 17:43:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-03-20 13:28:49 +00:00
|
|
|
|
void StorageMaterializedView::drop()
|
|
|
|
|
{
|
2015-06-18 02:11:05 +00:00
|
|
|
|
context.getGlobalContext().removeDependency(
|
|
|
|
|
DatabaseAndTableName(select_database_name, select_table_name),
|
|
|
|
|
DatabaseAndTableName(database_name, table_name));
|
|
|
|
|
|
|
|
|
|
auto inner_table_name = getInnerTableName();
|
2013-11-13 14:39:48 +00:00
|
|
|
|
|
2015-06-18 02:11:05 +00:00
|
|
|
|
if (context.tryGetTable(database_name, inner_table_name))
|
2014-04-15 14:00:33 +00:00
|
|
|
|
{
|
|
|
|
|
/// Состваляем и выполняем запрос drop для внутреннего хранилища.
|
|
|
|
|
ASTDropQuery *drop_query = new ASTDropQuery;
|
|
|
|
|
drop_query->database = database_name;
|
2015-06-18 02:11:05 +00:00
|
|
|
|
drop_query->table = inner_table_name;
|
2014-04-15 14:00:33 +00:00
|
|
|
|
ASTPtr ast_drop_query = drop_query;
|
|
|
|
|
InterpreterDropQuery drop_interpreter(ast_drop_query, context);
|
|
|
|
|
drop_interpreter.execute();
|
|
|
|
|
}
|
2013-11-08 17:43:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-04-14 14:58:59 +00:00
|
|
|
|
bool StorageMaterializedView::optimize(const Settings & settings)
|
2015-04-10 17:09:16 +00:00
|
|
|
|
{
|
2015-04-14 13:44:38 +00:00
|
|
|
|
return data->optimize(settings);
|
2013-11-08 17:43:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|