2017-04-01 09:19:00 +00:00
|
|
|
#include <Parsers/ASTCreateQuery.h>
|
|
|
|
#include <Parsers/ASTDropQuery.h>
|
2013-11-08 17:43:03 +00:00
|
|
|
|
2017-05-23 18:33:48 +00:00
|
|
|
#include <Interpreters/Context.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Interpreters/InterpreterCreateQuery.h>
|
|
|
|
#include <Interpreters/InterpreterDropQuery.h>
|
2013-11-08 17:43:03 +00:00
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Storages/StorageMaterializedView.h>
|
|
|
|
#include <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
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
extern const int LOGICAL_ERROR;
|
2016-01-11 21:46:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-09-30 03:08:47 +00:00
|
|
|
StoragePtr StorageMaterializedView::create(
|
2017-04-01 07:20:54 +00:00
|
|
|
const String & table_name_,
|
|
|
|
const String & database_name_,
|
|
|
|
Context & context_,
|
|
|
|
ASTPtr & query_,
|
|
|
|
NamesAndTypesListPtr columns_,
|
|
|
|
const NamesAndTypesList & materialized_columns_,
|
|
|
|
const NamesAndTypesList & alias_columns_,
|
|
|
|
const ColumnDefaults & column_defaults_,
|
|
|
|
bool attach_)
|
2013-11-08 17:43:03 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return ext::shared_ptr_helper<StorageMaterializedView>::make_shared(
|
|
|
|
table_name_, database_name_, context_, query_, columns_,
|
|
|
|
materialized_columns_, alias_columns_, column_defaults_, attach_
|
|
|
|
);
|
2013-11-08 17:43:03 +00:00
|
|
|
}
|
|
|
|
|
2014-09-30 03:08:47 +00:00
|
|
|
StorageMaterializedView::StorageMaterializedView(
|
2017-04-01 07:20:54 +00:00
|
|
|
const String & table_name_,
|
|
|
|
const String & database_name_,
|
|
|
|
Context & context_,
|
|
|
|
ASTPtr & query_,
|
|
|
|
NamesAndTypesListPtr columns_,
|
|
|
|
const NamesAndTypesList & materialized_columns_,
|
|
|
|
const NamesAndTypesList & alias_columns_,
|
|
|
|
const ColumnDefaults & column_defaults_,
|
|
|
|
bool attach_)
|
|
|
|
: StorageView{table_name_, database_name_, context_, query_, columns_, materialized_columns_, alias_columns_, column_defaults_}
|
2013-11-08 17:43:03 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
ASTCreateQuery & create = typeid_cast<ASTCreateQuery &>(*query_);
|
|
|
|
|
|
|
|
auto inner_table_name = getInnerTableName();
|
|
|
|
|
|
|
|
/// If there is an ATTACH request, then the internal table must already be connected.
|
|
|
|
if (!attach_)
|
|
|
|
{
|
|
|
|
/// We will create a query to create an internal repository.
|
|
|
|
auto manual_create_query = std::make_shared<ASTCreateQuery>();
|
|
|
|
manual_create_query->database = database_name;
|
|
|
|
manual_create_query->table = inner_table_name;
|
|
|
|
manual_create_query->columns = create.columns;
|
|
|
|
manual_create_query->children.push_back(manual_create_query->columns);
|
|
|
|
|
|
|
|
/// If you do not specify a storage type in the query, try retrieving it from SELECT query.
|
|
|
|
if (!create.inner_storage)
|
|
|
|
{
|
|
|
|
/// TODO also try to extract `params` to create a repository
|
|
|
|
auto func = std::make_shared<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;
|
|
|
|
|
|
|
|
manual_create_query->children.push_back(manual_create_query->storage);
|
|
|
|
|
|
|
|
/// Execute the query.
|
|
|
|
try
|
|
|
|
{
|
|
|
|
InterpreterCreateQuery create_interpreter(manual_create_query, context);
|
|
|
|
create_interpreter.execute();
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
/// In case of any error we should remove dependency to the view
|
|
|
|
/// which was added in the constructor of StorageView.
|
|
|
|
if (!select_table_name.empty())
|
|
|
|
context.getGlobalContext().removeDependency(
|
|
|
|
DatabaseAndTableName(select_database_name, select_table_name),
|
|
|
|
DatabaseAndTableName(database_name, table_name));
|
|
|
|
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
2013-11-08 17:43:03 +00:00
|
|
|
}
|
|
|
|
|
2014-08-05 10:56:58 +00:00
|
|
|
NameAndTypePair StorageMaterializedView::getColumn(const String & column_name) const
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
auto type = VirtualColumnFactory::tryGetType(column_name);
|
|
|
|
if (type)
|
|
|
|
return NameAndTypePair(column_name, type);
|
2014-08-05 10:56:58 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return getRealColumn(column_name);
|
2014-08-05 10:56:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool StorageMaterializedView::hasColumn(const String & column_name) const
|
|
|
|
{
|
2017-04-01 07:20:54 +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(
|
2017-04-01 07:20:54 +00:00
|
|
|
const Names & column_names,
|
2017-05-25 01:12:41 +00:00
|
|
|
const ASTPtr & query,
|
2017-04-01 07:20:54 +00:00
|
|
|
const Context & context,
|
|
|
|
QueryProcessingStage::Enum & processed_stage,
|
|
|
|
const size_t max_block_size,
|
2017-06-02 15:54:39 +00:00
|
|
|
const unsigned num_streams)
|
2013-11-08 17:43:03 +00:00
|
|
|
{
|
2017-06-02 15:54:39 +00:00
|
|
|
return getInnerTable()->read(column_names, query, context, processed_stage, max_block_size, num_streams);
|
2013-11-08 17:43:03 +00:00
|
|
|
}
|
|
|
|
|
2017-05-21 22:25:25 +00:00
|
|
|
BlockOutputStreamPtr StorageMaterializedView::write(const ASTPtr & query, const Settings & settings)
|
2013-11-08 17:43:03 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return getInnerTable()->write(query, settings);
|
2013-11-08 17:43:03 +00:00
|
|
|
}
|
|
|
|
|
2014-03-20 13:28:49 +00:00
|
|
|
void StorageMaterializedView::drop()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
context.getGlobalContext().removeDependency(
|
|
|
|
DatabaseAndTableName(select_database_name, select_table_name),
|
|
|
|
DatabaseAndTableName(database_name, table_name));
|
|
|
|
|
|
|
|
auto inner_table_name = getInnerTableName();
|
|
|
|
|
|
|
|
if (context.tryGetTable(database_name, inner_table_name))
|
|
|
|
{
|
|
|
|
/// We create and execute `drop` query for internal repository.
|
|
|
|
auto drop_query = std::make_shared<ASTDropQuery>();
|
|
|
|
drop_query->database = database_name;
|
|
|
|
drop_query->table = inner_table_name;
|
|
|
|
ASTPtr ast_drop_query = drop_query;
|
|
|
|
InterpreterDropQuery drop_interpreter(ast_drop_query, context);
|
|
|
|
drop_interpreter.execute();
|
|
|
|
}
|
2013-11-08 17:43:03 +00:00
|
|
|
}
|
|
|
|
|
2017-04-13 13:13:08 +00:00
|
|
|
bool StorageMaterializedView::optimize(const String & partition, bool final, bool deduplicate, const Settings & settings)
|
2015-04-10 17:09:16 +00:00
|
|
|
{
|
2017-04-13 13:13:08 +00:00
|
|
|
return getInnerTable()->optimize(partition, final, deduplicate, settings);
|
2013-11-08 17:43:03 +00:00
|
|
|
}
|
|
|
|
|
2017-03-11 00:27:59 +00:00
|
|
|
StoragePtr StorageMaterializedView::getInnerTable() const
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return context.getTable(database_name, getInnerTableName());
|
2017-03-11 00:27:59 +00:00
|
|
|
}
|
|
|
|
|
2013-11-08 17:43:03 +00:00
|
|
|
|
|
|
|
}
|