2013-11-15 09:43:50 +00:00
|
|
|
|
#include <DB/Interpreters/InterpreterSelectQuery.h>
|
2013-11-08 17:43:03 +00:00
|
|
|
|
#include <DB/Parsers/ASTIdentifier.h>
|
|
|
|
|
#include <DB/Parsers/ASTCreateQuery.h>
|
|
|
|
|
#include <DB/Parsers/ASTSelectQuery.h>
|
|
|
|
|
|
2013-10-30 13:52:02 +00:00
|
|
|
|
#include <DB/Storages/StorageView.h>
|
2013-11-08 17:43:03 +00:00
|
|
|
|
|
2013-10-30 13:52:02 +00:00
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
2014-09-30 03:08:47 +00:00
|
|
|
|
StoragePtr StorageView::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_)
|
2013-11-08 17:43:03 +00:00
|
|
|
|
{
|
2014-09-30 03:08:47 +00:00
|
|
|
|
return (new StorageView{
|
|
|
|
|
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
|
|
|
|
})->thisPtr();
|
2013-11-08 17:43:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-06-12 23:21:38 +00:00
|
|
|
|
|
2014-09-30 03:08:47 +00:00
|
|
|
|
StorageView::StorageView(
|
|
|
|
|
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_)
|
2014-10-16 13:37:01 +00:00
|
|
|
|
: IStorage{materialized_columns_, alias_columns_, column_defaults_}, table_name(table_name_),
|
2014-09-30 03:08:47 +00:00
|
|
|
|
database_name(database_name_), context(context_), columns(columns_)
|
2013-11-08 17:43:03 +00:00
|
|
|
|
{
|
2014-06-26 00:58:14 +00:00
|
|
|
|
ASTCreateQuery & create = typeid_cast<ASTCreateQuery &>(*query_);
|
|
|
|
|
ASTSelectQuery & select = typeid_cast<ASTSelectQuery &>(*create.select);
|
2013-11-13 14:39:48 +00:00
|
|
|
|
|
|
|
|
|
/// Если во внутреннем запросе не указана база данных, получить ее из контекста и записать в запрос.
|
|
|
|
|
if (!select.database)
|
|
|
|
|
{
|
2013-11-20 16:02:45 +00:00
|
|
|
|
select.database = new ASTIdentifier(StringRange(), database_name_, ASTIdentifier::Database);
|
2013-11-13 14:39:48 +00:00
|
|
|
|
select.children.push_back(select.database);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inner_query = select;
|
2013-11-08 17:43:03 +00:00
|
|
|
|
|
|
|
|
|
if (inner_query.database)
|
2014-06-26 00:58:14 +00:00
|
|
|
|
select_database_name = typeid_cast<const ASTIdentifier &>(*inner_query.database).name;
|
2013-11-08 17:43:03 +00:00
|
|
|
|
else
|
2013-11-13 14:39:48 +00:00
|
|
|
|
throw Exception("Logical error while creating StorageView."
|
|
|
|
|
" Could not retrieve database name from select query.",
|
|
|
|
|
DB::ErrorCodes::LOGICAL_ERROR);
|
2013-11-08 17:43:03 +00:00
|
|
|
|
|
|
|
|
|
if (inner_query.table)
|
2014-06-26 00:58:14 +00:00
|
|
|
|
select_table_name = typeid_cast<const ASTIdentifier &>(*inner_query.table).name;
|
2013-11-08 17:43:03 +00:00
|
|
|
|
else
|
|
|
|
|
throw Exception("Logical error while creating StorageView."
|
|
|
|
|
" Could not retrieve table name from select query.",
|
|
|
|
|
DB::ErrorCodes::LOGICAL_ERROR);
|
|
|
|
|
|
2014-06-12 23:21:38 +00:00
|
|
|
|
context.getGlobalContext().addDependency(
|
|
|
|
|
DatabaseAndTableName(select_database_name, select_table_name),
|
|
|
|
|
DatabaseAndTableName(database_name, table_name));
|
2013-11-08 17:43:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-06-12 23:21:38 +00:00
|
|
|
|
|
2013-11-08 17:43:03 +00:00
|
|
|
|
BlockInputStreams StorageView::read(
|
|
|
|
|
const Names & column_names,
|
|
|
|
|
ASTPtr query,
|
2014-12-12 14:30:35 +00:00
|
|
|
|
const Context & context,
|
2013-11-08 17:43:03 +00:00
|
|
|
|
const Settings & settings,
|
|
|
|
|
QueryProcessingStage::Enum & processed_stage,
|
2014-12-12 14:30:35 +00:00
|
|
|
|
const size_t max_block_size,
|
|
|
|
|
const unsigned threads)
|
2013-10-30 13:52:02 +00:00
|
|
|
|
{
|
2014-10-07 23:08:56 +00:00
|
|
|
|
ASTPtr inner_query_clone = getInnerQuery();
|
|
|
|
|
ASTSelectQuery & inner_select = static_cast<ASTSelectQuery &>(*inner_query_clone);
|
|
|
|
|
const ASTSelectQuery & outer_select = typeid_cast<const ASTSelectQuery &>(*query);
|
|
|
|
|
|
|
|
|
|
/// Пробрасываем внутрь SAMPLE и FINAL, если они есть во внешнем запросе и их нет во внутреннем.
|
|
|
|
|
|
|
|
|
|
if (outer_select.sample_size && !inner_select.sample_size)
|
|
|
|
|
inner_select.sample_size = outer_select.sample_size;
|
|
|
|
|
|
|
|
|
|
if (outer_select.final && !inner_select.final)
|
|
|
|
|
inner_select.final = outer_select.final;
|
|
|
|
|
|
2014-06-12 23:21:38 +00:00
|
|
|
|
return BlockInputStreams(1,
|
2014-10-07 23:08:56 +00:00
|
|
|
|
InterpreterSelectQuery(inner_query_clone, context, column_names).execute());
|
2013-11-08 17:43:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2014-06-12 23:21:38 +00:00
|
|
|
|
void StorageView::drop()
|
|
|
|
|
{
|
|
|
|
|
context.getGlobalContext().removeDependency(
|
|
|
|
|
DatabaseAndTableName(select_database_name, select_table_name),
|
|
|
|
|
DatabaseAndTableName(database_name, table_name));
|
2013-10-30 13:52:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|