#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace ProfileEvents { extern const Event Query; extern const Event InitialQuery; extern const Event QueriesWithSubqueries; extern const Event SelectQuery; extern const Event InsertQuery; } 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); } InterpreterFactory::InterpreterPtr InterpreterFactory::get(ASTPtr & query, ContextMutablePtr context, const SelectQueryOptions & options) { ProfileEvents::increment(ProfileEvents::Query); 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() || query->as() || query->as() || query->as())) { ProfileEvents::increment(ProfileEvents::QueriesWithSubqueries); } Arguments arguments { .query = query, .context = context, .options = options }; String interpreter_name; if (query->as()) { 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"; } else if (query->as()) { ProfileEvents::increment(ProfileEvents::SelectQuery); if (context->getSettingsRef().allow_experimental_analyzer) interpreter_name = "InterpreterSelectQueryAnalyzer"; else interpreter_name = "InterpreterSelectWithUnionQuery"; } else if (query->as()) { interpreter_name = "InterpreterSelectIntersectExceptQuery"; } else if (query->as()) { ProfileEvents::increment(ProfileEvents::InsertQuery); bool allow_materialized = static_cast(context->getSettingsRef().insert_allow_materialized_columns); arguments.allow_materialized = allow_materialized; interpreter_name = "InterpreterInsertQuery"; } else if (query->as()) { interpreter_name = "InterpreterCreateQuery"; } else if (query->as()) { interpreter_name = "InterpreterDropQuery"; } else if (query->as()) { interpreter_name = "InterpreterUndropQuery"; } else if (query->as()) { interpreter_name = "InterpreterRenameQuery"; } else if (query->as()) { interpreter_name = "InterpreterShowTablesQuery"; } else if (query->as()) { interpreter_name = "InterpreterShowColumnsQuery"; } else if (query->as()) { interpreter_name = "InterpreterShowIndexesQuery"; } else if (query->as()) { interpreter_name = "InterpreterShowSettingQuery"; } else if (query->as()) { interpreter_name = "InterpreterShowEnginesQuery"; } else if (query->as()) { interpreter_name = "InterpreterShowFunctionsQuery"; } else if (query->as()) { interpreter_name = "InterpreterUseQuery"; } else if (query->as()) { /// readonly is checked inside InterpreterSetQuery interpreter_name = "InterpreterSetQuery"; } else if (query->as()) { interpreter_name = "InterpreterSetRoleQuery"; } else if (query->as()) { interpreter_name = "InterpreterOptimizeQuery"; } else if (query->as() || query->as() || query->as() || query->as()) { interpreter_name = "InterpreterExistsQuery"; } else if (query->as() || query->as() || query->as() || query->as()) { interpreter_name = "InterpreterShowCreateQuery"; } else if (query->as()) { interpreter_name = "InterpreterDescribeQuery"; } else if (query->as()) { interpreter_name = "InterpreterDescribeCacheQuery"; } else if (query->as()) { interpreter_name = "InterpreterExplainQuery"; } else if (query->as()) { interpreter_name = "InterpreterShowProcesslistQuery"; } else if (query->as()) { interpreter_name = "InterpreterAlterQuery"; } else if (query->as()) { interpreter_name = "InterpreterAlterNamedCollectionQuery"; } else if (query->as() || query->as()) { interpreter_name = "InterpreterCheckQuery"; } else if (query->as()) { interpreter_name = "InterpreterKillQueryQuery"; } else if (query->as()) { interpreter_name = "InterpreterSystemQuery"; } else if (query->as()) { interpreter_name = "InterpreterWatchQuery"; } else if (query->as()) { interpreter_name = "InterpreterCreateUserQuery"; } else if (query->as()) { interpreter_name = "InterpreterCreateRoleQuery"; } else if (query->as()) { interpreter_name = "InterpreterCreateQuotaQuery"; } else if (query->as()) { interpreter_name = "InterpreterCreateRowPolicyQuery"; } else if (query->as()) { interpreter_name = "InterpreterCreateSettingsProfileQuery"; } else if (query->as()) { interpreter_name = "InterpreterDropAccessEntityQuery"; } else if (query->as()) { interpreter_name = "InterpreterMoveAccessEntityQuery"; } else if (query->as()) { interpreter_name = "InterpreterDropNamedCollectionQuery"; } else if (query->as()) { interpreter_name = "InterpreterGrantQuery"; } else if (query->as()) { interpreter_name = "InterpreterShowCreateAccessEntityQuery"; } else if (query->as()) { interpreter_name = "InterpreterShowGrantsQuery"; } else if (query->as()) { interpreter_name = "InterpreterShowAccessEntitiesQuery"; } else if (query->as()) { interpreter_name= "InterpreterShowAccessQuery"; } else if (query->as()) { interpreter_name = "InterpreterShowPrivilegesQuery"; } else if (query->as()) { interpreter_name = "InterpreterExternalDDLQuery"; } else if (query->as()) { interpreter_name = "InterpreterTransactionControlQuery"; } else if (query->as()) { interpreter_name = "InterpreterCreateFunctionQuery"; } else if (query->as()) { interpreter_name = "InterpreterDropFunctionQuery"; } else if (query->as()) { interpreter_name = "InterpreterCreateIndexQuery"; } else if (query->as()) { interpreter_name = "InterpreterCreateNamedCollectionQuery"; } else if (query->as()) { interpreter_name = "InterpreterDropIndexQuery"; } else if (query->as()) { interpreter_name = "InterpreterBackupQuery"; } else if (query->as()) { 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); } }