2011-08-18 20:33:20 +00:00
|
|
|
#pragma once
|
|
|
|
|
2021-04-10 23:33:54 +00:00
|
|
|
#include <Access/AccessRightsElement.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>
|
2019-01-11 19:12:36 +00:00
|
|
|
#include <Common/ThreadPool.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;
|
2020-12-10 23:56:57 +00:00
|
|
|
class IDatabase;
|
|
|
|
using DatabasePtr = std::shared_ptr<IDatabase>;
|
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-04-10 23:33:54 +00:00
|
|
|
class InterpreterCreateQuery : public IInterpreter, WithContext
|
2011-08-18 20:33:20 +00:00
|
|
|
{
|
|
|
|
public:
|
2021-04-10 23:33:54 +00:00
|
|
|
InterpreterCreateQuery(const ASTPtr & query_ptr_, ContextPtr context_);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
BlockIO execute() override;
|
|
|
|
|
|
|
|
/// List of columns and their types in AST.
|
2017-12-25 21:57:29 +00:00
|
|
|
static ASTPtr formatColumns(const NamesAndTypesList & columns);
|
2018-03-06 20:18:34 +00:00
|
|
|
static ASTPtr formatColumns(const ColumnsDescription & columns);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
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
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
void setForceRestoreData(bool has_force_restore_data_flag_)
|
|
|
|
{
|
|
|
|
has_force_restore_data_flag = has_force_restore_data_flag_;
|
|
|
|
}
|
|
|
|
|
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_;
|
|
|
|
}
|
|
|
|
|
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.
|
2021-04-16 10:15:35 +00:00
|
|
|
static ColumnsDescription getColumnsDescription(const ASTExpressionList & columns, ContextPtr context, bool attach);
|
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
|
|
|
|
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.
|
|
|
|
TableProperties setProperties(ASTCreateQuery & create) const;
|
|
|
|
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.
|
2020-04-06 23:22:44 +00:00
|
|
|
bool doCreateTable(ASTCreateQuery & create, const TableProperties & properties);
|
2020-12-25 16:28:10 +00:00
|
|
|
BlockIO doCreateOrReplaceTable(ASTCreateQuery & create, const InterpreterCreateQuery::TableProperties & properties);
|
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;
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
ASTPtr query_ptr;
|
2016-03-19 01:18:49 +00:00
|
|
|
|
2017-04-01 07:20:54 +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-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
|
|
|
};
|
|
|
|
}
|