1#pragma once
2
3#include <Interpreters/IInterpreter.h>
4#include <Storages/ColumnsDescription.h>
5#include <Storages/IStorage_fwd.h>
6#include <Storages/IndicesDescription.h>
7#include <Storages/ConstraintsDescription.h>
8#include <Common/ThreadPool.h>
9
10
11namespace DB
12{
13
14class Context;
15class ASTCreateQuery;
16class ASTExpressionList;
17class ASTConstraintDeclaration;
18
19
20/** Allows to create new table or database,
21 * or create an object for existing table or database.
22 */
23class InterpreterCreateQuery : public IInterpreter
24{
25public:
26 InterpreterCreateQuery(const ASTPtr & query_ptr_, Context & context_);
27
28 BlockIO execute() override;
29
30 /// List of columns and their types in AST.
31 static ASTPtr formatColumns(const NamesAndTypesList & columns);
32 static ASTPtr formatColumns(const ColumnsDescription & columns);
33
34 static ASTPtr formatIndices(const IndicesDescription & indices);
35 static ASTPtr formatConstraints(const ConstraintsDescription & constraints);
36
37 void setForceRestoreData(bool has_force_restore_data_flag_)
38 {
39 has_force_restore_data_flag = has_force_restore_data_flag_;
40 }
41
42 void setInternal(bool internal_)
43 {
44 internal = internal_;
45 }
46
47 /// Obtain information about columns, their types, default values and column comments, for case when columns in CREATE query is specified explicitly.
48 static ColumnsDescription getColumnsDescription(const ASTExpressionList & columns, const Context & context);
49 static ConstraintsDescription getConstraintsDescription(const ASTExpressionList * constraints);
50
51private:
52 struct TableProperties
53 {
54 ColumnsDescription columns;
55 IndicesDescription indices;
56 ConstraintsDescription constraints;
57 };
58
59 BlockIO createDatabase(ASTCreateQuery & create);
60 BlockIO createTable(ASTCreateQuery & create);
61 BlockIO createDictionary(ASTCreateQuery & create);
62
63 /// Calculate list of columns, constraints, indices, etc... of table. Rewrite query in canonical way.
64 TableProperties setProperties(ASTCreateQuery & create) const;
65 void validateTableStructure(const ASTCreateQuery & create, const TableProperties & properties) const;
66 void setEngine(ASTCreateQuery & create) const;
67 void checkAccess(const ASTCreateQuery & create);
68
69 /// Create IStorage and add it to database. If table already exists and IF NOT EXISTS specified, do nothing and return false.
70 bool doCreateTable(const ASTCreateQuery & create, const TableProperties & properties);
71 /// Inserts data in created table if it's CREATE ... SELECT
72 BlockIO fillTableIfNeeded(const ASTCreateQuery & create);
73
74 ASTPtr query_ptr;
75 Context & context;
76
77 /// Skip safety threshold when loading tables.
78 bool has_force_restore_data_flag = false;
79 /// Is this an internal query - not from the user.
80 bool internal = false;
81};
82}
83