ClickHouse/src/Interpreters/InterpreterFactory.cpp

353 lines
12 KiB
C++
Raw Normal View History

#include <Parsers/ASTAlterQuery.h>
2021-08-18 22:19:14 +00:00
#include <Parsers/ASTBackupQuery.h>
#include <Parsers/ASTCheckQuery.h>
#include <Parsers/ASTCreateQuery.h>
2021-08-23 14:31:58 +00:00
#include <Parsers/ASTCreateFunctionQuery.h>
#include <Parsers/ASTCreateIndexQuery.h>
#include <Parsers/ASTDeleteQuery.h>
2021-05-14 22:43:04 +00:00
#include <Parsers/ASTDropFunctionQuery.h>
#include <Parsers/ASTDropIndexQuery.h>
#include <Parsers/ASTDropQuery.h>
2023-03-21 12:14:26 +00:00
#include <Parsers/ASTUndropQuery.h>
2021-04-16 20:18:39 +00:00
#include <Parsers/ASTExplainQuery.h>
#include <Parsers/ASTInsertQuery.h>
2021-08-12 11:42:51 +00:00
#include <Parsers/ASTSelectIntersectExceptQuery.h>
#include <Parsers/ASTKillQueryQuery.h>
#include <Parsers/ASTOptimizeQuery.h>
#include <Parsers/ASTRenameQuery.h>
#include <Parsers/ASTSelectQuery.h>
2018-02-25 06:34:20 +00:00
#include <Parsers/ASTSelectWithUnionQuery.h>
#include <Parsers/ASTSetQuery.h>
2023-01-31 15:35:10 +00:00
#include <Parsers/ASTShowEngineQuery.h>
#include <Parsers/ASTShowFunctionsQuery.h>
#include <Parsers/ASTShowProcesslistQuery.h>
#include <Parsers/ASTShowTablesQuery.h>
#include <Parsers/ASTShowColumnsQuery.h>
2023-04-25 18:04:19 +00:00
#include <Parsers/ASTShowIndexesQuery.h>
2023-10-24 14:50:33 +00:00
#include <Parsers/ASTShowSettingQuery.h>
#include <Parsers/ASTUseQuery.h>
#include <Parsers/ASTWatchQuery.h>
#include <Parsers/ASTCreateNamedCollectionQuery.h>
#include <Parsers/ASTDropNamedCollectionQuery.h>
#include <Parsers/ASTAlterNamedCollectionQuery.h>
2021-03-31 17:55:04 +00:00
#include <Parsers/ASTTransactionControl.h>
2021-04-16 20:18:39 +00:00
#include <Parsers/TablePropertiesQueriesASTs.h>
2015-06-18 02:11:05 +00:00
#include <Parsers/Access/ASTCreateQuotaQuery.h>
#include <Parsers/Access/ASTCreateRoleQuery.h>
#include <Parsers/Access/ASTCreateRowPolicyQuery.h>
#include <Parsers/Access/ASTCreateSettingsProfileQuery.h>
#include <Parsers/Access/ASTCreateUserQuery.h>
#include <Parsers/Access/ASTDropAccessEntityQuery.h>
#include <Parsers/Access/ASTGrantQuery.h>
#include <Parsers/Access/ASTMoveAccessEntityQuery.h>
#include <Parsers/Access/ASTSetRoleQuery.h>
#include <Parsers/Access/ASTShowAccessEntitiesQuery.h>
#include <Parsers/Access/ASTShowAccessQuery.h>
#include <Parsers/Access/ASTShowCreateAccessEntityQuery.h>
#include <Parsers/Access/ASTShowGrantsQuery.h>
#include <Parsers/Access/ASTShowPrivilegesQuery.h>
2022-06-21 13:42:36 +00:00
#include <Parsers/ASTDescribeCacheQuery.h>
#include <Interpreters/InterpreterFactory.h>
#include <Interpreters/InterpreterSelectQuery.h>
2022-07-14 11:20:16 +00:00
#include <Interpreters/InterpreterSelectQueryAnalyzer.h>
2018-02-25 06:34:20 +00:00
#include <Interpreters/InterpreterSelectWithUnionQuery.h>
#include <Interpreters/InterpreterWatchQuery.h>
#include <Interpreters/OpenTelemetrySpanLog.h>
2015-06-18 02:11:05 +00:00
#include <Parsers/ASTSystemQuery.h>
2020-07-06 13:57:45 +00:00
#include <Parsers/ASTExternalDDLQuery.h>
#include <Common/ProfileEvents.h>
#include <Common/typeid_cast.h>
namespace ProfileEvents
{
extern const Event Query;
2023-12-27 04:37:06 +00:00
extern const Event InitialQuery;
extern const Event QueriesWithSubqueries;
extern const Event SelectQuery;
extern const Event InsertQuery;
}
2017-07-13 20:58:19 +00:00
2015-06-18 02:11:05 +00:00
namespace DB
{
namespace ErrorCodes
{
extern const int UNKNOWN_TYPE_OF_QUERY;
extern const int LOGICAL_ERROR;
}
InterpreterFactory & InterpreterFactory::instance()
{
static InterpreterFactory interpreter_fact;
return interpreter_fact;
}
void InterpreterFactory::registerInterpreter(const std::string & name, CreatorFn creator_fn)
{
if (!interpreters.emplace(name, std::move(creator_fn)).second)
throw Exception(ErrorCodes::LOGICAL_ERROR, "InterpreterFactory: the interpreter name '{}' is not unique", name);
}
2015-06-18 02:11:05 +00:00
InterpreterFactory::InterpreterPtr InterpreterFactory::get(ASTPtr & query, ContextMutablePtr context, const SelectQueryOptions & options)
2015-06-18 02:11:05 +00:00
{
ProfileEvents::increment(ProfileEvents::Query);
2023-12-27 04:37:06 +00:00
if (context->getClientInfo().query_kind == ClientInfo::QueryKind::INITIAL_QUERY)
ProfileEvents::increment(ProfileEvents::InitialQuery);
/// SELECT and INSERT query will handle QueriesWithSubqueries on their own.
if (!(query->as<ASTSelectQuery>() ||
query->as<ASTSelectWithUnionQuery>() ||
query->as<ASTSelectIntersectExceptQuery>() ||
query->as<ASTInsertQuery>()))
{
ProfileEvents::increment(ProfileEvents::QueriesWithSubqueries);
}
Arguments arguments {
.query = query,
.context = context,
.options = options
};
String interpreter_name;
2019-03-11 13:22:51 +00:00
if (query->as<ASTSelectQuery>())
{
if (context->getSettingsRef().allow_experimental_analyzer)
interpreter_name = "InterpreterSelectQueryAnalyzer";
/// This is internal part of ASTSelectWithUnionQuery.
/// Even if there is SELECT without union, it is represented by ASTSelectWithUnionQuery with single ASTSelectQuery as a child.
else
interpreter_name = "InterpreterSelectQuery";
}
2019-03-11 13:22:51 +00:00
else if (query->as<ASTSelectWithUnionQuery>())
2018-02-25 06:34:20 +00:00
{
ProfileEvents::increment(ProfileEvents::SelectQuery);
2022-07-14 11:20:16 +00:00
if (context->getSettingsRef().allow_experimental_analyzer)
interpreter_name = "InterpreterSelectQueryAnalyzer";
else
interpreter_name = "InterpreterSelectWithUnionQuery";
2018-02-25 06:34:20 +00:00
}
2021-08-12 11:42:51 +00:00
else if (query->as<ASTSelectIntersectExceptQuery>())
2021-04-16 20:18:39 +00:00
{
interpreter_name = "InterpreterSelectIntersectExceptQuery";
2021-04-16 20:18:39 +00:00
}
2019-03-11 13:22:51 +00:00
else if (query->as<ASTInsertQuery>())
{
ProfileEvents::increment(ProfileEvents::InsertQuery);
bool allow_materialized = static_cast<bool>(context->getSettingsRef().insert_allow_materialized_columns);
arguments.allow_materialized = allow_materialized;
interpreter_name = "InterpreterInsertQuery";
}
2019-03-11 13:22:51 +00:00
else if (query->as<ASTCreateQuery>())
{
interpreter_name = "InterpreterCreateQuery";
}
2019-03-11 13:22:51 +00:00
else if (query->as<ASTDropQuery>())
{
interpreter_name = "InterpreterDropQuery";
}
2023-03-21 12:14:26 +00:00
else if (query->as<ASTUndropQuery>())
{
interpreter_name = "InterpreterUndropQuery";
2023-03-21 12:14:26 +00:00
}
2019-03-11 13:22:51 +00:00
else if (query->as<ASTRenameQuery>())
{
interpreter_name = "InterpreterRenameQuery";
}
2019-03-11 13:22:51 +00:00
else if (query->as<ASTShowTablesQuery>())
{
interpreter_name = "InterpreterShowTablesQuery";
}
else if (query->as<ASTShowColumnsQuery>())
{
interpreter_name = "InterpreterShowColumnsQuery";
}
2023-04-25 18:04:19 +00:00
else if (query->as<ASTShowIndexesQuery>())
{
interpreter_name = "InterpreterShowIndexesQuery";
2023-04-25 18:04:19 +00:00
}
2023-10-24 14:50:33 +00:00
else if (query->as<ASTShowSettingQuery>())
{
interpreter_name = "InterpreterShowSettingQuery";
2023-10-24 14:50:33 +00:00
}
2023-01-31 15:35:10 +00:00
else if (query->as<ASTShowEnginesQuery>())
{
interpreter_name = "InterpreterShowEnginesQuery";
2023-01-31 15:35:10 +00:00
}
else if (query->as<ASTShowFunctionsQuery>())
{
interpreter_name = "InterpreterShowFunctionsQuery";
}
2019-03-11 13:22:51 +00:00
else if (query->as<ASTUseQuery>())
{
interpreter_name = "InterpreterUseQuery";
}
2019-03-11 13:22:51 +00:00
else if (query->as<ASTSetQuery>())
{
2017-04-02 17:37:49 +00:00
/// readonly is checked inside InterpreterSetQuery
interpreter_name = "InterpreterSetQuery";
}
else if (query->as<ASTSetRoleQuery>())
{
interpreter_name = "InterpreterSetRoleQuery";
}
2019-03-11 13:22:51 +00:00
else if (query->as<ASTOptimizeQuery>())
{
interpreter_name = "InterpreterOptimizeQuery";
}
else if (query->as<ASTExistsDatabaseQuery>() || query->as<ASTExistsTableQuery>() || query->as<ASTExistsViewQuery>() || query->as<ASTExistsDictionaryQuery>())
2020-12-14 14:37:25 +00:00
{
interpreter_name = "InterpreterExistsQuery";
2020-12-14 14:37:25 +00:00
}
else if (query->as<ASTShowCreateTableQuery>() || query->as<ASTShowCreateViewQuery>() || query->as<ASTShowCreateDatabaseQuery>() || query->as<ASTShowCreateDictionaryQuery>())
{
interpreter_name = "InterpreterShowCreateQuery";
}
2019-03-11 13:22:51 +00:00
else if (query->as<ASTDescribeQuery>())
{
interpreter_name = "InterpreterDescribeQuery";
}
2022-06-21 13:42:36 +00:00
else if (query->as<ASTDescribeCacheQuery>())
{
interpreter_name = "InterpreterDescribeCacheQuery";
2022-06-21 13:42:36 +00:00
}
2019-03-11 13:22:51 +00:00
else if (query->as<ASTExplainQuery>())
2018-09-21 15:20:23 +00:00
{
interpreter_name = "InterpreterExplainQuery";
2018-09-21 15:20:23 +00:00
}
2019-03-11 13:22:51 +00:00
else if (query->as<ASTShowProcesslistQuery>())
{
interpreter_name = "InterpreterShowProcesslistQuery";
}
2019-03-11 13:22:51 +00:00
else if (query->as<ASTAlterQuery>())
{
interpreter_name = "InterpreterAlterQuery";
}
else if (query->as<ASTAlterNamedCollectionQuery>())
{
interpreter_name = "InterpreterAlterNamedCollectionQuery";
}
2023-10-24 12:50:24 +00:00
else if (query->as<ASTCheckTableQuery>() || query->as<ASTCheckAllTablesQuery>())
{
interpreter_name = "InterpreterCheckQuery";
2023-10-24 12:50:24 +00:00
}
else if (query->as<ASTKillQueryQuery>())
{
interpreter_name = "InterpreterKillQueryQuery";
}
2019-03-11 13:22:51 +00:00
else if (query->as<ASTSystemQuery>())
{
interpreter_name = "InterpreterSystemQuery";
}
else if (query->as<ASTWatchQuery>())
{
interpreter_name = "InterpreterWatchQuery";
}
else if (query->as<ASTCreateUserQuery>())
{
interpreter_name = "InterpreterCreateUserQuery";
}
else if (query->as<ASTCreateRoleQuery>())
{
interpreter_name = "InterpreterCreateRoleQuery";
}
2019-12-01 22:01:05 +00:00
else if (query->as<ASTCreateQuotaQuery>())
{
interpreter_name = "InterpreterCreateQuotaQuery";
2019-12-01 22:01:05 +00:00
}
2019-11-29 17:22:56 +00:00
else if (query->as<ASTCreateRowPolicyQuery>())
{
interpreter_name = "InterpreterCreateRowPolicyQuery";
2019-11-29 17:22:56 +00:00
}
else if (query->as<ASTCreateSettingsProfileQuery>())
{
interpreter_name = "InterpreterCreateSettingsProfileQuery";
}
2019-12-01 22:01:05 +00:00
else if (query->as<ASTDropAccessEntityQuery>())
{
interpreter_name = "InterpreterDropAccessEntityQuery";
2019-12-01 22:01:05 +00:00
}
else if (query->as<ASTMoveAccessEntityQuery>())
{
interpreter_name = "InterpreterMoveAccessEntityQuery";
}
else if (query->as<ASTDropNamedCollectionQuery>())
{
interpreter_name = "InterpreterDropNamedCollectionQuery";
}
else if (query->as<ASTGrantQuery>())
{
interpreter_name = "InterpreterGrantQuery";
}
2019-12-01 22:01:05 +00:00
else if (query->as<ASTShowCreateAccessEntityQuery>())
{
interpreter_name = "InterpreterShowCreateAccessEntityQuery";
2019-12-01 22:01:05 +00:00
}
else if (query->as<ASTShowGrantsQuery>())
{
interpreter_name = "InterpreterShowGrantsQuery";
}
else if (query->as<ASTShowAccessEntitiesQuery>())
2019-11-29 17:22:56 +00:00
{
interpreter_name = "InterpreterShowAccessEntitiesQuery";
2019-11-29 17:22:56 +00:00
}
2020-06-10 23:08:37 +00:00
else if (query->as<ASTShowAccessQuery>())
{
interpreter_name= "InterpreterShowAccessQuery";
2020-06-10 23:08:37 +00:00
}
2020-05-12 21:46:14 +00:00
else if (query->as<ASTShowPrivilegesQuery>())
{
interpreter_name = "InterpreterShowPrivilegesQuery";
2020-05-12 21:46:14 +00:00
}
2020-07-06 13:57:45 +00:00
else if (query->as<ASTExternalDDLQuery>())
{
interpreter_name = "InterpreterExternalDDLQuery";
2020-07-06 13:57:45 +00:00
}
2021-03-31 17:55:04 +00:00
else if (query->as<ASTTransactionControl>())
{
interpreter_name = "InterpreterTransactionControlQuery";
2021-03-31 17:55:04 +00:00
}
else if (query->as<ASTCreateFunctionQuery>())
{
interpreter_name = "InterpreterCreateFunctionQuery";
}
2021-05-14 22:43:04 +00:00
else if (query->as<ASTDropFunctionQuery>())
{
interpreter_name = "InterpreterDropFunctionQuery";
2021-05-14 22:43:04 +00:00
}
else if (query->as<ASTCreateIndexQuery>())
{
interpreter_name = "InterpreterCreateIndexQuery";
}
else if (query->as<ASTCreateNamedCollectionQuery>())
{
interpreter_name = "InterpreterCreateNamedCollectionQuery";
}
else if (query->as<ASTDropIndexQuery>())
{
interpreter_name = "InterpreterDropIndexQuery";
}
2021-08-18 22:19:14 +00:00
else if (query->as<ASTBackupQuery>())
{
interpreter_name = "InterpreterBackupQuery";
2021-08-18 22:19:14 +00:00
}
else if (query->as<ASTDeleteQuery>())
{
interpreter_name = "InterpreterDeleteQuery";
}
if (!interpreters.contains(interpreter_name))
throw Exception(ErrorCodes::UNKNOWN_TYPE_OF_QUERY, "Unknown type of query: {}", query->getID());
// creator_fn creates and returns a InterpreterPtr with the supplied arguments
auto creator_fn = interpreters.at(interpreter_name);
return creator_fn(arguments);
2015-06-18 02:11:05 +00:00
}
}