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>
|
|
|
|
|
|
|
|
|
|
|
2013-11-08 17:43:03 +00:00
|
|
|
|
namespace DB
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
StoragePtr StorageMaterializedView::create(const String & table_name_, const String & database_name_,
|
2013-11-13 14:39:48 +00:00
|
|
|
|
Context & context_, ASTPtr & query_, NamesAndTypesListPtr columns_, bool attach_)
|
2013-11-08 17:43:03 +00:00
|
|
|
|
{
|
|
|
|
|
return (new StorageMaterializedView(table_name_, database_name_, context_, query_, columns_, attach_))->thisPtr();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
StorageMaterializedView::StorageMaterializedView(const String & table_name_, const String & database_name_,
|
2013-11-13 14:39:48 +00:00
|
|
|
|
Context & context_, ASTPtr & query_, NamesAndTypesListPtr columns_, bool attach_):
|
2013-11-08 17:43:03 +00:00
|
|
|
|
StorageView(table_name_, database_name_, context_, query_, columns_)
|
|
|
|
|
{
|
2014-06-26 00:58:14 +00:00
|
|
|
|
ASTCreateQuery & create = typeid_cast<ASTCreateQuery &>(*query_);
|
2013-11-08 17:43:03 +00:00
|
|
|
|
|
2013-11-13 14:39:48 +00:00
|
|
|
|
/// Если запрос ATTACH, то к этому моменту внутренняя таблица уже должна быть подключена.
|
2013-11-08 17:43:03 +00:00
|
|
|
|
if (attach_)
|
|
|
|
|
{
|
2013-11-13 14:39:48 +00:00
|
|
|
|
if (!context.isTableExist(database_name, getInnerTableName()))
|
|
|
|
|
throw Exception("Inner table is not attached yet."
|
|
|
|
|
" Materialized view: " + database_name + "." + table_name + "."
|
|
|
|
|
" Inner table: " + database_name + "." + getInnerTableName() + ".",
|
|
|
|
|
DB::ErrorCodes::LOGICAL_ERROR);
|
|
|
|
|
data = context.getTable(database_name, getInnerTableName());
|
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;
|
|
|
|
|
manual_create_query->table = getInnerTableName();
|
|
|
|
|
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);
|
|
|
|
|
data = create_interpreter.execute();
|
|
|
|
|
}
|
2013-11-08 17:43:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BlockInputStreams StorageMaterializedView::read(
|
|
|
|
|
const Names & column_names,
|
|
|
|
|
ASTPtr query,
|
|
|
|
|
const Settings & settings,
|
|
|
|
|
QueryProcessingStage::Enum & processed_stage,
|
|
|
|
|
size_t max_block_size,
|
|
|
|
|
unsigned threads)
|
|
|
|
|
{
|
|
|
|
|
return data->read(column_names, query, settings, processed_stage, max_block_size, threads);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BlockOutputStreamPtr StorageMaterializedView::write(ASTPtr query)
|
|
|
|
|
{
|
|
|
|
|
return data->write(query);
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-20 13:28:49 +00:00
|
|
|
|
void StorageMaterializedView::drop()
|
|
|
|
|
{
|
2013-11-13 14:39:48 +00:00
|
|
|
|
context.getGlobalContext().removeDependency(DatabaseAndTableName(select_database_name, select_table_name), DatabaseAndTableName(database_name, table_name));
|
|
|
|
|
|
2014-04-15 14:00:33 +00:00
|
|
|
|
if (context.tryGetTable(database_name, getInnerTableName()))
|
|
|
|
|
{
|
|
|
|
|
/// Состваляем и выполняем запрос drop для внутреннего хранилища.
|
|
|
|
|
ASTDropQuery *drop_query = new ASTDropQuery;
|
|
|
|
|
drop_query->database = database_name;
|
|
|
|
|
drop_query->table = getInnerTableName();
|
|
|
|
|
ASTPtr ast_drop_query = drop_query;
|
|
|
|
|
InterpreterDropQuery drop_interpreter(ast_drop_query, context);
|
|
|
|
|
drop_interpreter.execute();
|
|
|
|
|
}
|
2013-11-08 17:43:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool StorageMaterializedView::optimize() {
|
|
|
|
|
return data->optimize();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|