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
|
|
|
|
|
{
|
|
|
|
|
|
2016-01-11 21:46:36 +00:00
|
|
|
|
namespace ErrorCodes
|
|
|
|
|
{
|
|
|
|
|
extern const int LOGICAL_ERROR;
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-30 13:52:02 +00:00
|
|
|
|
|
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
|
|
|
|
{
|
2016-08-26 21:25:05 +00:00
|
|
|
|
return make_shared(
|
2014-09-30 03:08:47 +00:00
|
|
|
|
table_name_, database_name_, context_, query_,
|
2014-10-03 15:30:10 +00:00
|
|
|
|
columns_, materialized_columns_, alias_columns_, column_defaults_
|
2016-08-26 21:25:05 +00:00
|
|
|
|
);
|
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
|
|
|
|
|
|
|
|
|
/// Если во внутреннем запросе не указана база данных, получить ее из контекста и записать в запрос.
|
2016-07-22 20:39:28 +00:00
|
|
|
|
select.setDatabaseIfNeeded(database_name);
|
2013-11-13 14:39:48 +00:00
|
|
|
|
|
|
|
|
|
inner_query = select;
|
2013-11-08 17:43:03 +00:00
|
|
|
|
|
2016-06-06 18:19:28 +00:00
|
|
|
|
extractDependentTable(inner_query);
|
|
|
|
|
|
|
|
|
|
if (!select_table_name.empty())
|
|
|
|
|
context.getGlobalContext().addDependency(
|
|
|
|
|
DatabaseAndTableName(select_database_name, select_table_name),
|
|
|
|
|
DatabaseAndTableName(database_name, table_name));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void StorageView::extractDependentTable(const ASTSelectQuery & query)
|
|
|
|
|
{
|
2016-07-22 20:39:28 +00:00
|
|
|
|
auto query_table = query.table();
|
|
|
|
|
|
|
|
|
|
if (!query_table)
|
2016-06-06 18:19:28 +00:00
|
|
|
|
return;
|
|
|
|
|
|
2016-07-22 20:39:28 +00:00
|
|
|
|
if (const ASTIdentifier * ast_id = typeid_cast<const ASTIdentifier *>(query_table.get()))
|
2016-06-06 18:19:28 +00:00
|
|
|
|
{
|
2016-07-22 20:39:28 +00:00
|
|
|
|
auto query_database = query.database();
|
|
|
|
|
|
|
|
|
|
if (!query_database)
|
2016-06-06 18:19:28 +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
|
|
|
|
|
2016-07-22 20:39:28 +00:00
|
|
|
|
select_database_name = typeid_cast<const ASTIdentifier &>(*query_database).name;
|
2016-06-06 18:19:28 +00:00
|
|
|
|
select_table_name = ast_id->name;
|
|
|
|
|
}
|
2016-07-22 20:39:28 +00:00
|
|
|
|
else if (const ASTSelectQuery * ast_select = typeid_cast<const ASTSelectQuery *>(query_table.get()))
|
2016-06-06 18:19:28 +00:00
|
|
|
|
{
|
|
|
|
|
extractDependentTable(*ast_select);
|
|
|
|
|
}
|
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
|
|
|
|
|
2013-11-08 17:43:03 +00:00
|
|
|
|
BlockInputStreams StorageView::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-10-30 13:52:02 +00:00
|
|
|
|
{
|
2015-11-10 20:07:38 +00:00
|
|
|
|
processed_stage = QueryProcessingStage::FetchColumns;
|
|
|
|
|
|
2014-10-07 23:08:56 +00:00
|
|
|
|
ASTPtr inner_query_clone = getInnerQuery();
|
2016-07-22 20:39:28 +00:00
|
|
|
|
/* ASTSelectQuery & inner_select = static_cast<ASTSelectQuery &>(*inner_query_clone);
|
2014-10-07 23:08:56 +00:00
|
|
|
|
const ASTSelectQuery & outer_select = typeid_cast<const ASTSelectQuery &>(*query);
|
|
|
|
|
|
2016-07-22 20:39:28 +00:00
|
|
|
|
/// Пробрасываем внутрь SAMPLE и FINAL, если они есть во внешнем запросе и их нет во внутреннем. TODO
|
2014-10-07 23:08:56 +00:00
|
|
|
|
|
|
|
|
|
if (outer_select.sample_size && !inner_select.sample_size)
|
2015-11-18 21:37:28 +00:00
|
|
|
|
{
|
2014-10-07 23:08:56 +00:00
|
|
|
|
inner_select.sample_size = outer_select.sample_size;
|
|
|
|
|
|
2015-11-18 21:37:28 +00:00
|
|
|
|
if (outer_select.sample_offset && !inner_select.sample_offset)
|
|
|
|
|
inner_select.sample_offset = outer_select.sample_offset;
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-07 23:08:56 +00:00
|
|
|
|
if (outer_select.final && !inner_select.final)
|
2016-07-22 20:39:28 +00:00
|
|
|
|
inner_select.final = outer_select.final;*/
|
2014-10-07 23:08:56 +00:00
|
|
|
|
|
2015-04-21 15:12:08 +00:00
|
|
|
|
return InterpreterSelectQuery(inner_query_clone, context, column_names).executeWithoutUnion();
|
2013-11-08 17:43:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2014-06-12 23:21:38 +00:00
|
|
|
|
void StorageView::drop()
|
|
|
|
|
{
|
2016-06-06 18:19:28 +00:00
|
|
|
|
if (!select_table_name.empty())
|
|
|
|
|
context.getGlobalContext().removeDependency(
|
|
|
|
|
DatabaseAndTableName(select_database_name, select_table_name),
|
|
|
|
|
DatabaseAndTableName(database_name, table_name));
|
2013-10-30 13:52:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|