2017-12-20 07:39:52 +00:00
|
|
|
#include <Parsers/ASTAlterQuery.h>
|
|
|
|
#include <Parsers/ASTCheckQuery.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Parsers/ASTCreateQuery.h>
|
2019-12-01 22:01:05 +00:00
|
|
|
#include <Parsers/ASTCreateQuotaQuery.h>
|
|
|
|
#include <Parsers/ASTDropAccessEntityQuery.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Parsers/ASTDropQuery.h>
|
2017-12-20 07:39:52 +00:00
|
|
|
#include <Parsers/ASTInsertQuery.h>
|
|
|
|
#include <Parsers/ASTKillQueryQuery.h>
|
|
|
|
#include <Parsers/ASTOptimizeQuery.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Parsers/ASTRenameQuery.h>
|
2017-12-20 07:39:52 +00:00
|
|
|
#include <Parsers/ASTSelectQuery.h>
|
2018-02-25 06:34:20 +00:00
|
|
|
#include <Parsers/ASTSelectWithUnionQuery.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Parsers/ASTSetQuery.h>
|
2019-12-01 22:01:05 +00:00
|
|
|
#include <Parsers/ASTShowCreateAccessEntityQuery.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Parsers/ASTShowProcesslistQuery.h>
|
2019-12-01 22:01:05 +00:00
|
|
|
#include <Parsers/ASTShowQuotasQuery.h>
|
2017-12-20 07:39:52 +00:00
|
|
|
#include <Parsers/ASTShowTablesQuery.h>
|
|
|
|
#include <Parsers/ASTUseQuery.h>
|
2018-09-21 15:20:23 +00:00
|
|
|
#include <Parsers/ASTExplainQuery.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Parsers/TablePropertiesQueriesASTs.h>
|
2019-05-28 21:17:48 +00:00
|
|
|
#include <Parsers/ASTWatchQuery.h>
|
2015-06-18 02:11:05 +00:00
|
|
|
|
2017-12-20 07:39:52 +00:00
|
|
|
#include <Interpreters/InterpreterAlterQuery.h>
|
|
|
|
#include <Interpreters/InterpreterCheckQuery.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Interpreters/InterpreterCreateQuery.h>
|
2019-12-01 22:01:05 +00:00
|
|
|
#include <Interpreters/InterpreterCreateQuotaQuery.h>
|
2017-12-20 07:39:52 +00:00
|
|
|
#include <Interpreters/InterpreterDescribeQuery.h>
|
2018-09-21 15:20:23 +00:00
|
|
|
#include <Interpreters/InterpreterExplainQuery.h>
|
2019-12-01 22:01:05 +00:00
|
|
|
#include <Interpreters/InterpreterDropAccessEntityQuery.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Interpreters/InterpreterDropQuery.h>
|
2017-12-20 07:39:52 +00:00
|
|
|
#include <Interpreters/InterpreterExistsQuery.h>
|
|
|
|
#include <Interpreters/InterpreterFactory.h>
|
|
|
|
#include <Interpreters/InterpreterInsertQuery.h>
|
|
|
|
#include <Interpreters/InterpreterKillQueryQuery.h>
|
|
|
|
#include <Interpreters/InterpreterOptimizeQuery.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Interpreters/InterpreterRenameQuery.h>
|
2017-12-20 07:39:52 +00:00
|
|
|
#include <Interpreters/InterpreterSelectQuery.h>
|
2018-02-25 06:34:20 +00:00
|
|
|
#include <Interpreters/InterpreterSelectWithUnionQuery.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Interpreters/InterpreterSetQuery.h>
|
2019-12-01 22:01:05 +00:00
|
|
|
#include <Interpreters/InterpreterShowCreateAccessEntityQuery.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Interpreters/InterpreterShowCreateQuery.h>
|
|
|
|
#include <Interpreters/InterpreterShowProcesslistQuery.h>
|
2019-12-01 22:01:05 +00:00
|
|
|
#include <Interpreters/InterpreterShowQuotasQuery.h>
|
2017-12-20 07:39:52 +00:00
|
|
|
#include <Interpreters/InterpreterShowTablesQuery.h>
|
2017-08-31 12:55:19 +00:00
|
|
|
#include <Interpreters/InterpreterSystemQuery.h>
|
2017-12-20 07:39:52 +00:00
|
|
|
#include <Interpreters/InterpreterUseQuery.h>
|
2019-05-28 21:17:48 +00:00
|
|
|
#include <Interpreters/InterpreterWatchQuery.h>
|
2015-06-18 02:11:05 +00:00
|
|
|
|
2017-08-04 15:54:00 +00:00
|
|
|
#include <Parsers/ASTSystemQuery.h>
|
2018-06-08 19:32:35 +00:00
|
|
|
|
2017-12-20 07:39:52 +00:00
|
|
|
#include <Common/typeid_cast.h>
|
2018-06-08 19:32:35 +00:00
|
|
|
#include <Common/ProfileEvents.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace ProfileEvents
|
|
|
|
{
|
|
|
|
extern const Event Query;
|
|
|
|
extern const Event SelectQuery;
|
|
|
|
extern const Event InsertQuery;
|
|
|
|
}
|
2017-08-04 15:54:00 +00:00
|
|
|
|
2017-07-13 20:58:19 +00:00
|
|
|
|
2015-06-18 02:11:05 +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 READONLY;
|
|
|
|
extern const int UNKNOWN_TYPE_OF_QUERY;
|
2018-09-11 18:37:19 +00:00
|
|
|
extern const int QUERY_IS_PROHIBITED;
|
2016-01-11 21:46:36 +00:00
|
|
|
}
|
|
|
|
|
2015-06-18 02:11:05 +00:00
|
|
|
|
2018-09-11 18:37:19 +00:00
|
|
|
static void throwIfNoAccess(Context & context)
|
2015-06-18 02:11:05 +00:00
|
|
|
{
|
2018-03-11 00:15:26 +00:00
|
|
|
if (context.getSettingsRef().readonly)
|
2018-04-20 01:14:04 +00:00
|
|
|
{
|
|
|
|
const auto & client_info = context.getClientInfo();
|
|
|
|
if (client_info.interface == ClientInfo::Interface::HTTP && client_info.http_method == ClientInfo::HTTPMethod::GET)
|
|
|
|
throw Exception("Cannot execute query in readonly mode. "
|
|
|
|
"For queries over HTTP, method GET implies readonly. You should use method POST for modifying queries.", ErrorCodes::READONLY);
|
|
|
|
else
|
|
|
|
throw Exception("Cannot execute query in readonly mode", ErrorCodes::READONLY);
|
|
|
|
}
|
2018-09-11 18:37:19 +00:00
|
|
|
else if (!context.getSettingsRef().allow_ddl)
|
|
|
|
throw Exception("Cannot execute query. DDL queries are prohibited for the user", ErrorCodes::QUERY_IS_PROHIBITED);
|
2015-06-18 02:11:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-05-28 12:22:22 +00:00
|
|
|
std::unique_ptr<IInterpreter> InterpreterFactory::get(ASTPtr & query, Context & context, QueryProcessingStage::Enum stage)
|
2015-06-18 02:11:05 +00:00
|
|
|
{
|
2018-06-08 19:32:35 +00:00
|
|
|
ProfileEvents::increment(ProfileEvents::Query);
|
|
|
|
|
2019-03-11 13:22:51 +00:00
|
|
|
if (query->as<ASTSelectQuery>())
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2018-06-08 19:32:35 +00:00
|
|
|
/// This is internal part of ASTSelectWithUnionQuery.
|
|
|
|
/// Even if there is SELECT without union, it is represented by ASTSelectWithUnionQuery with single ASTSelectQuery as a child.
|
2019-03-15 15:57:18 +00:00
|
|
|
return std::make_unique<InterpreterSelectQuery>(query, context, SelectQueryOptions(stage));
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2019-03-11 13:22:51 +00:00
|
|
|
else if (query->as<ASTSelectWithUnionQuery>())
|
2018-02-25 06:34:20 +00:00
|
|
|
{
|
2018-06-08 19:32:35 +00:00
|
|
|
ProfileEvents::increment(ProfileEvents::SelectQuery);
|
2019-03-15 15:57:18 +00:00
|
|
|
return std::make_unique<InterpreterSelectWithUnionQuery>(query, context, SelectQueryOptions(stage));
|
2018-02-25 06:34:20 +00:00
|
|
|
}
|
2019-03-11 13:22:51 +00:00
|
|
|
else if (query->as<ASTInsertQuery>())
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2018-06-08 19:32:35 +00:00
|
|
|
ProfileEvents::increment(ProfileEvents::InsertQuery);
|
2017-12-20 07:39:52 +00:00
|
|
|
/// readonly is checked inside InterpreterInsertQuery
|
2018-01-12 13:03:19 +00:00
|
|
|
bool allow_materialized = static_cast<bool>(context.getSettingsRef().insert_allow_materialized_columns);
|
|
|
|
return std::make_unique<InterpreterInsertQuery>(query, context, allow_materialized);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2019-03-11 13:22:51 +00:00
|
|
|
else if (query->as<ASTCreateQuery>())
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2018-09-11 18:37:19 +00:00
|
|
|
/// readonly and allow_ddl are checked inside InterpreterCreateQuery
|
2017-04-01 07:20:54 +00:00
|
|
|
return std::make_unique<InterpreterCreateQuery>(query, context);
|
|
|
|
}
|
2019-03-11 13:22:51 +00:00
|
|
|
else if (query->as<ASTDropQuery>())
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2018-09-11 18:37:19 +00:00
|
|
|
/// readonly and allow_ddl are checked inside InterpreterDropQuery
|
2017-04-01 07:20:54 +00:00
|
|
|
return std::make_unique<InterpreterDropQuery>(query, context);
|
|
|
|
}
|
2019-03-11 13:22:51 +00:00
|
|
|
else if (query->as<ASTRenameQuery>())
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2018-09-11 18:37:19 +00:00
|
|
|
throwIfNoAccess(context);
|
2017-04-01 07:20:54 +00:00
|
|
|
return std::make_unique<InterpreterRenameQuery>(query, context);
|
|
|
|
}
|
2019-03-11 13:22:51 +00:00
|
|
|
else if (query->as<ASTShowTablesQuery>())
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
return std::make_unique<InterpreterShowTablesQuery>(query, context);
|
|
|
|
}
|
2019-03-11 13:22:51 +00:00
|
|
|
else if (query->as<ASTUseQuery>())
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
return std::make_unique<InterpreterUseQuery>(query, context);
|
|
|
|
}
|
2019-03-11 13:22:51 +00:00
|
|
|
else if (query->as<ASTSetQuery>())
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-04-02 17:37:49 +00:00
|
|
|
/// readonly is checked inside InterpreterSetQuery
|
2017-04-01 07:20:54 +00:00
|
|
|
return std::make_unique<InterpreterSetQuery>(query, context);
|
|
|
|
}
|
2019-03-11 13:22:51 +00:00
|
|
|
else if (query->as<ASTOptimizeQuery>())
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2018-09-11 18:37:19 +00:00
|
|
|
throwIfNoAccess(context);
|
2017-04-01 07:20:54 +00:00
|
|
|
return std::make_unique<InterpreterOptimizeQuery>(query, context);
|
|
|
|
}
|
2019-10-08 12:34:04 +00:00
|
|
|
else if (query->as<ASTExistsTableQuery>())
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
return std::make_unique<InterpreterExistsQuery>(query, context);
|
|
|
|
}
|
2019-10-11 13:21:52 +00:00
|
|
|
else if (query->as<ASTExistsDictionaryQuery>())
|
|
|
|
{
|
|
|
|
return std::make_unique<InterpreterExistsQuery>(query, context);
|
|
|
|
}
|
2019-03-11 13:22:51 +00:00
|
|
|
else if (query->as<ASTShowCreateTableQuery>())
|
2018-03-12 14:14:56 +00:00
|
|
|
{
|
|
|
|
return std::make_unique<InterpreterShowCreateQuery>(query, context);
|
|
|
|
}
|
2019-03-11 13:22:51 +00:00
|
|
|
else if (query->as<ASTShowCreateDatabaseQuery>())
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
return std::make_unique<InterpreterShowCreateQuery>(query, context);
|
|
|
|
}
|
2019-10-11 13:21:52 +00:00
|
|
|
else if (query->as<ASTShowCreateDictionaryQuery>())
|
|
|
|
{
|
|
|
|
return std::make_unique<InterpreterShowCreateQuery>(query, context);
|
|
|
|
}
|
2019-03-11 13:22:51 +00:00
|
|
|
else if (query->as<ASTDescribeQuery>())
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
return std::make_unique<InterpreterDescribeQuery>(query, context);
|
|
|
|
}
|
2019-03-11 13:22:51 +00:00
|
|
|
else if (query->as<ASTExplainQuery>())
|
2018-09-21 15:20:23 +00:00
|
|
|
{
|
|
|
|
return std::make_unique<InterpreterExplainQuery>(query, context);
|
|
|
|
}
|
2019-03-11 13:22:51 +00:00
|
|
|
else if (query->as<ASTShowProcesslistQuery>())
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
return std::make_unique<InterpreterShowProcesslistQuery>(query, context);
|
|
|
|
}
|
2019-03-11 13:22:51 +00:00
|
|
|
else if (query->as<ASTAlterQuery>())
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2018-09-11 18:37:19 +00:00
|
|
|
throwIfNoAccess(context);
|
2017-04-01 07:20:54 +00:00
|
|
|
return std::make_unique<InterpreterAlterQuery>(query, context);
|
|
|
|
}
|
2019-03-11 13:22:51 +00:00
|
|
|
else if (query->as<ASTCheckQuery>())
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
return std::make_unique<InterpreterCheckQuery>(query, context);
|
|
|
|
}
|
2019-03-11 13:22:51 +00:00
|
|
|
else if (query->as<ASTKillQueryQuery>())
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
return std::make_unique<InterpreterKillQueryQuery>(query, context);
|
|
|
|
}
|
2019-03-11 13:22:51 +00:00
|
|
|
else if (query->as<ASTSystemQuery>())
|
2017-08-04 15:54:00 +00:00
|
|
|
{
|
2018-09-11 18:37:19 +00:00
|
|
|
throwIfNoAccess(context);
|
2017-08-04 15:54:00 +00:00
|
|
|
return std::make_unique<InterpreterSystemQuery>(query, context);
|
|
|
|
}
|
2019-05-28 21:17:48 +00:00
|
|
|
else if (query->as<ASTWatchQuery>())
|
|
|
|
{
|
|
|
|
return std::make_unique<InterpreterWatchQuery>(query, context);
|
|
|
|
}
|
2019-12-01 22:01:05 +00:00
|
|
|
else if (query->as<ASTCreateQuotaQuery>())
|
|
|
|
{
|
|
|
|
return std::make_unique<InterpreterCreateQuotaQuery>(query, context);
|
|
|
|
}
|
|
|
|
else if (query->as<ASTDropAccessEntityQuery>())
|
|
|
|
{
|
|
|
|
return std::make_unique<InterpreterDropAccessEntityQuery>(query, context);
|
|
|
|
}
|
|
|
|
else if (query->as<ASTShowCreateAccessEntityQuery>())
|
|
|
|
{
|
|
|
|
return std::make_unique<InterpreterShowCreateAccessEntityQuery>(query, context);
|
|
|
|
}
|
|
|
|
else if (query->as<ASTShowQuotasQuery>())
|
|
|
|
{
|
|
|
|
return std::make_unique<InterpreterShowQuotasQuery>(query, context);
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
else
|
|
|
|
throw Exception("Unknown type of query: " + query->getID(), ErrorCodes::UNKNOWN_TYPE_OF_QUERY);
|
2015-06-18 02:11:05 +00:00
|
|
|
}
|
|
|
|
}
|