2011-08-18 20:33:20 +00:00
|
|
|
#pragma once
|
|
|
|
|
2021-06-28 11:42:21 +00:00
|
|
|
#include <Core/NamesAndAliases.h>
|
2022-02-08 12:21:16 +00:00
|
|
|
#include <Core/SettingsEnums.h>
|
2021-10-31 08:51:20 +00:00
|
|
|
#include <Access/Common/AccessRightsElement.h>
|
2024-03-22 23:03:31 +00:00
|
|
|
#include <Databases/LoadingStrictnessLevel.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Interpreters/IInterpreter.h>
|
2018-03-06 20:18:34 +00:00
|
|
|
#include <Storages/ColumnsDescription.h>
|
2021-04-10 23:33:54 +00:00
|
|
|
#include <Storages/ConstraintsDescription.h>
|
2019-05-17 14:34:25 +00:00
|
|
|
#include <Storages/IStorage_fwd.h>
|
2020-05-28 12:37:05 +00:00
|
|
|
#include <Storages/StorageInMemoryMetadata.h>
|
2011-08-18 20:33:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
2019-01-11 16:58:43 +00:00
|
|
|
|
2016-03-19 01:18:49 +00:00
|
|
|
class ASTCreateQuery;
|
2017-09-17 18:49:43 +00:00
|
|
|
class ASTExpressionList;
|
2019-08-24 21:20:20 +00:00
|
|
|
class ASTConstraintDeclaration;
|
2022-02-10 20:35:03 +00:00
|
|
|
class ASTStorage;
|
2020-12-10 23:56:57 +00:00
|
|
|
class IDatabase;
|
2022-09-14 16:03:36 +00:00
|
|
|
class DDLGuard;
|
2020-12-10 23:56:57 +00:00
|
|
|
using DatabasePtr = std::shared_ptr<IDatabase>;
|
2022-09-14 16:03:36 +00:00
|
|
|
using DDLGuardPtr = std::unique_ptr<DDLGuard>;
|
2016-03-19 01:18:49 +00:00
|
|
|
|
2011-08-18 20:33:20 +00:00
|
|
|
|
2016-08-09 21:48:05 +00:00
|
|
|
/** Allows to create new table or database,
|
|
|
|
* or create an object for existing table or database.
|
2011-08-18 20:33:20 +00:00
|
|
|
*/
|
2021-05-31 14:49:02 +00:00
|
|
|
class InterpreterCreateQuery : public IInterpreter, WithMutableContext
|
2011-08-18 20:33:20 +00:00
|
|
|
{
|
|
|
|
public:
|
2021-05-31 14:49:02 +00:00
|
|
|
InterpreterCreateQuery(const ASTPtr & query_ptr_, ContextMutablePtr context_);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2016-03-19 01:18:49 +00:00
|
|
|
BlockIO execute() override;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2016-08-09 21:48:05 +00:00
|
|
|
/// List of columns and their types in AST.
|
2017-12-25 21:57:29 +00:00
|
|
|
static ASTPtr formatColumns(const NamesAndTypesList & columns);
|
2021-06-28 11:42:21 +00:00
|
|
|
static ASTPtr formatColumns(const NamesAndTypesList & columns, const NamesAndAliases & alias_columns);
|
2018-03-06 20:18:34 +00:00
|
|
|
static ASTPtr formatColumns(const ColumnsDescription & columns);
|
2019-02-05 14:50:25 +00:00
|
|
|
static ASTPtr formatIndices(const IndicesDescription & indices);
|
2019-05-17 04:08:03 +00:00
|
|
|
static ASTPtr formatConstraints(const ConstraintsDescription & constraints);
|
2021-02-10 14:12:49 +00:00
|
|
|
static ASTPtr formatProjections(const ProjectionsDescription & projections);
|
2019-02-05 14:50:25 +00:00
|
|
|
|
2016-08-09 21:48:05 +00:00
|
|
|
void setForceRestoreData(bool has_force_restore_data_flag_)
|
|
|
|
{
|
|
|
|
has_force_restore_data_flag = has_force_restore_data_flag_;
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2018-01-18 23:40:32 +00:00
|
|
|
void setInternal(bool internal_)
|
|
|
|
{
|
|
|
|
internal = internal_;
|
|
|
|
}
|
|
|
|
|
2020-08-06 11:56:49 +00:00
|
|
|
void setForceAttach(bool force_attach_)
|
|
|
|
{
|
|
|
|
force_attach = force_attach_;
|
|
|
|
}
|
|
|
|
|
2021-08-26 13:19:52 +00:00
|
|
|
void setLoadDatabaseWithoutTables(bool load_database_without_tables_)
|
|
|
|
{
|
|
|
|
load_database_without_tables = load_database_without_tables_;
|
|
|
|
}
|
|
|
|
|
2023-03-28 21:47:45 +00:00
|
|
|
void setDontNeedDDLGuard()
|
|
|
|
{
|
|
|
|
need_ddl_guard = false;
|
|
|
|
}
|
|
|
|
|
2023-11-03 15:46:07 +00:00
|
|
|
void setIsRestoreFromBackup(bool is_restore_from_backup_)
|
|
|
|
{
|
|
|
|
is_restore_from_backup = is_restore_from_backup_;
|
|
|
|
}
|
|
|
|
|
2020-07-04 21:57:57 +00:00
|
|
|
/// Obtain information about columns, their types, default values and column comments,
|
|
|
|
/// for case when columns in CREATE query is specified explicitly.
|
2024-05-14 18:30:49 +00:00
|
|
|
static ColumnsDescription getColumnsDescription(const ASTExpressionList & columns, ContextPtr context, LoadingStrictnessLevel mode, bool is_restore_from_backup = false);
|
2019-08-24 21:20:20 +00:00
|
|
|
static ConstraintsDescription getConstraintsDescription(const ASTExpressionList * constraints);
|
2016-03-19 01:18:49 +00:00
|
|
|
|
2021-04-10 23:33:54 +00:00
|
|
|
static void prepareOnClusterQuery(ASTCreateQuery & create, ContextPtr context, const String & cluster_name);
|
2020-09-23 18:28:59 +00:00
|
|
|
|
2021-04-10 23:33:54 +00:00
|
|
|
void extendQueryLogElemImpl(QueryLogElement & elem, const ASTPtr & ast, ContextPtr) const override;
|
2020-12-14 03:30:39 +00:00
|
|
|
|
2024-02-28 00:00:17 +00:00
|
|
|
/// Check access right, validate definer statement and replace `CURRENT USER` with actual name.
|
|
|
|
static void processSQLSecurityOption(ContextPtr context_, ASTSQLSecurity & sql_security, bool is_attach = false, bool is_materialized_view = false);
|
|
|
|
|
2016-03-19 01:18:49 +00:00
|
|
|
private:
|
2019-10-23 18:39:07 +00:00
|
|
|
struct TableProperties
|
|
|
|
{
|
|
|
|
ColumnsDescription columns;
|
|
|
|
IndicesDescription indices;
|
|
|
|
ConstraintsDescription constraints;
|
2021-02-10 14:12:49 +00:00
|
|
|
ProjectionsDescription projections;
|
2019-10-23 18:39:07 +00:00
|
|
|
};
|
|
|
|
|
2017-04-25 15:21:03 +00:00
|
|
|
BlockIO createDatabase(ASTCreateQuery & create);
|
2017-04-21 12:39:28 +00:00
|
|
|
BlockIO createTable(ASTCreateQuery & create);
|
2017-04-13 13:42:29 +00:00
|
|
|
|
2019-10-23 18:39:07 +00:00
|
|
|
/// Calculate list of columns, constraints, indices, etc... of table. Rewrite query in canonical way.
|
2024-03-22 23:03:31 +00:00
|
|
|
TableProperties getTablePropertiesAndNormalizeCreateQuery(ASTCreateQuery & create, LoadingStrictnessLevel mode) const;
|
2019-10-23 18:39:07 +00:00
|
|
|
void validateTableStructure(const ASTCreateQuery & create, const TableProperties & properties) const;
|
2017-10-25 19:52:32 +00:00
|
|
|
void setEngine(ASTCreateQuery & create) const;
|
2020-01-24 16:20:36 +00:00
|
|
|
AccessRightsElements getRequiredAccess() const;
|
2017-12-25 21:10:46 +00:00
|
|
|
|
2019-10-23 18:39:07 +00:00
|
|
|
/// Create IStorage and add it to database. If table already exists and IF NOT EXISTS specified, do nothing and return false.
|
2024-03-22 23:03:31 +00:00
|
|
|
bool doCreateTable(ASTCreateQuery & create, const TableProperties & properties, DDLGuardPtr & ddl_guard, LoadingStrictnessLevel mode);
|
|
|
|
BlockIO doCreateOrReplaceTable(ASTCreateQuery & create, const InterpreterCreateQuery::TableProperties & properties, LoadingStrictnessLevel mode);
|
2019-10-23 18:39:07 +00:00
|
|
|
/// Inserts data in created table if it's CREATE ... SELECT
|
2019-12-16 18:29:18 +00:00
|
|
|
BlockIO fillTableIfNeeded(const ASTCreateQuery & create);
|
2019-10-23 18:39:07 +00:00
|
|
|
|
2020-07-13 21:21:01 +00:00
|
|
|
void assertOrSetUUID(ASTCreateQuery & create, const DatabasePtr & database) const;
|
|
|
|
|
2022-03-04 18:23:19 +00:00
|
|
|
/// Update create query with columns description from storage if query doesn't have it.
|
|
|
|
/// It's used to prevent automatic schema inference while table creation on each server startup.
|
|
|
|
void addColumnsDescriptionToCreateQueryIfNecessary(ASTCreateQuery & create, const StoragePtr & storage);
|
|
|
|
|
2022-07-06 16:50:00 +00:00
|
|
|
BlockIO executeQueryOnCluster(ASTCreateQuery & create);
|
|
|
|
|
2011-11-01 15:16:04 +00:00
|
|
|
ASTPtr query_ptr;
|
2016-03-19 01:18:49 +00:00
|
|
|
|
2016-08-09 21:48:05 +00:00
|
|
|
/// Skip safety threshold when loading tables.
|
|
|
|
bool has_force_restore_data_flag = false;
|
2018-01-18 23:40:32 +00:00
|
|
|
/// Is this an internal query - not from the user.
|
|
|
|
bool internal = false;
|
2020-08-06 11:56:49 +00:00
|
|
|
bool force_attach = false;
|
2021-08-26 13:19:52 +00:00
|
|
|
bool load_database_without_tables = false;
|
2023-03-28 21:47:45 +00:00
|
|
|
bool need_ddl_guard = true;
|
2023-11-03 15:46:07 +00:00
|
|
|
bool is_restore_from_backup = false;
|
2021-02-08 19:36:17 +00:00
|
|
|
|
|
|
|
mutable String as_database_saved;
|
|
|
|
mutable String as_table_saved;
|
2011-08-18 20:33:20 +00:00
|
|
|
};
|
|
|
|
}
|