2017-04-01 09:19:00 +00:00
|
|
|
#include <Interpreters/InterpreterSelectQuery.h>
|
|
|
|
#include <Parsers/ASTIdentifier.h>
|
|
|
|
#include <Parsers/ASTCreateQuery.h>
|
|
|
|
#include <Parsers/ASTSelectQuery.h>
|
2013-11-08 17:43:03 +00:00
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <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
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
extern const int LOGICAL_ERROR;
|
2016-01-11 21:46:36 +00:00
|
|
|
}
|
|
|
|
|
2013-10-30 13:52:02 +00:00
|
|
|
|
2014-09-30 03:08:47 +00:00
|
|
|
StorageView::StorageView(
|
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_)
|
|
|
|
: IStorage{materialized_columns_, alias_columns_, column_defaults_}, table_name(table_name_),
|
|
|
|
database_name(database_name_), context(context_), columns(columns_)
|
2013-11-08 17:43:03 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
ASTCreateQuery & create = typeid_cast<ASTCreateQuery &>(*query_);
|
|
|
|
ASTSelectQuery & select = typeid_cast<ASTSelectQuery &>(*create.select);
|
2013-11-13 14:39:48 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// If the internal query does not specify a database, retrieve it from the context and write it to the query.
|
|
|
|
select.setDatabaseIfNeeded(database_name);
|
2013-11-13 14:39:48 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
inner_query = select;
|
2013-11-08 17:43:03 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
extractDependentTable(inner_query);
|
2016-06-06 18:19:28 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!select_table_name.empty())
|
|
|
|
context.getGlobalContext().addDependency(
|
|
|
|
DatabaseAndTableName(select_database_name, select_table_name),
|
|
|
|
DatabaseAndTableName(database_name, table_name));
|
2016-06-06 18:19:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void StorageView::extractDependentTable(const ASTSelectQuery & query)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
auto query_table = query.table();
|
|
|
|
|
|
|
|
if (!query_table)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (const ASTIdentifier * ast_id = typeid_cast<const ASTIdentifier *>(query_table.get()))
|
|
|
|
{
|
|
|
|
auto query_database = query.database();
|
|
|
|
|
|
|
|
if (!query_database)
|
|
|
|
throw Exception("Logical error while creating StorageView."
|
|
|
|
" Could not retrieve database name from select query.",
|
|
|
|
DB::ErrorCodes::LOGICAL_ERROR);
|
|
|
|
|
|
|
|
select_database_name = typeid_cast<const ASTIdentifier &>(*query_database).name;
|
|
|
|
select_table_name = ast_id->name;
|
|
|
|
}
|
|
|
|
else if (const ASTSelectQuery * ast_select = typeid_cast<const ASTSelectQuery *>(query_table.get()))
|
|
|
|
{
|
|
|
|
extractDependentTable(*ast_select);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
throw Exception("Logical error while creating StorageView."
|
|
|
|
" Could not retrieve table name from select query.",
|
|
|
|
DB::ErrorCodes::LOGICAL_ERROR);
|
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(
|
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-10-30 13:52:02 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
processed_stage = QueryProcessingStage::FetchColumns;
|
|
|
|
ASTPtr inner_query_clone = getInnerQuery();
|
|
|
|
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()
|
|
|
|
{
|
2017-04-01 07:20:54 +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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|