2014-07-11 08:10:45 +00:00
|
|
|
#pragma once
|
2014-10-16 13:37:01 +00:00
|
|
|
|
2018-06-16 02:13:54 +00:00
|
|
|
#include <optional>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Core/NamesAndTypes.h>
|
2018-03-06 20:18:34 +00:00
|
|
|
#include <Storages/ColumnsDescription.h>
|
2019-05-17 14:34:25 +00:00
|
|
|
#include <Storages/IStorage_fwd.h>
|
2019-02-05 14:50:25 +00:00
|
|
|
#include <Storages/IndicesDescription.h>
|
2019-06-02 14:41:12 +00:00
|
|
|
#include <Storages/ConstraintsDescription.h>
|
2014-07-11 08:10:45 +00:00
|
|
|
|
2019-07-24 12:56:39 +00:00
|
|
|
#include <Common/SettingsChanges.h>
|
2014-07-11 08:10:45 +00:00
|
|
|
|
2018-06-16 02:13:54 +00:00
|
|
|
|
2014-07-11 08:10:45 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2018-06-13 13:49:27 +00:00
|
|
|
class ASTAlterCommand;
|
|
|
|
|
2018-11-01 10:35:50 +00:00
|
|
|
/// Operation from the ALTER query (except for manipulation with PART/PARTITION).
|
|
|
|
/// Adding Nested columns is not expanded to add individual columns.
|
2014-07-11 08:10:45 +00:00
|
|
|
struct AlterCommand
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
enum Type
|
|
|
|
{
|
|
|
|
ADD_COLUMN,
|
|
|
|
DROP_COLUMN,
|
|
|
|
MODIFY_COLUMN,
|
2018-10-14 15:30:06 +00:00
|
|
|
COMMENT_COLUMN,
|
2018-10-15 18:47:47 +00:00
|
|
|
MODIFY_ORDER_BY,
|
2019-02-05 14:50:25 +00:00
|
|
|
ADD_INDEX,
|
|
|
|
DROP_INDEX,
|
2019-06-02 14:41:12 +00:00
|
|
|
ADD_CONSTRAINT,
|
|
|
|
DROP_CONSTRAINT,
|
2019-04-15 09:30:45 +00:00
|
|
|
MODIFY_TTL,
|
2018-11-14 22:46:39 +00:00
|
|
|
UKNOWN_TYPE,
|
2019-07-24 12:56:39 +00:00
|
|
|
MODIFY_SETTING,
|
2017-04-01 07:20:54 +00:00
|
|
|
};
|
|
|
|
|
2018-11-14 22:46:39 +00:00
|
|
|
Type type = UKNOWN_TYPE;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
String column_name;
|
|
|
|
|
2017-04-14 12:40:48 +00:00
|
|
|
/// For DROP COLUMN ... FROM PARTITION
|
|
|
|
String partition_name;
|
|
|
|
|
2017-04-16 15:00:33 +00:00
|
|
|
/// For ADD and MODIFY, a new column type.
|
2017-04-01 07:20:54 +00:00
|
|
|
DataTypePtr data_type;
|
|
|
|
|
2018-03-12 13:47:01 +00:00
|
|
|
ColumnDefaultKind default_kind{};
|
2017-04-01 07:20:54 +00:00
|
|
|
ASTPtr default_expression{};
|
2019-12-23 16:44:50 +00:00
|
|
|
std::optional<String> comment;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-04-16 15:00:33 +00:00
|
|
|
/// For ADD - after which column to add a new one. If an empty string, add to the end. To add to the beginning now it is impossible.
|
2017-04-01 07:20:54 +00:00
|
|
|
String after_column;
|
|
|
|
|
2018-12-21 14:53:00 +00:00
|
|
|
/// For DROP_COLUMN, MODIFY_COLUMN, COMMENT_COLUMN
|
2018-12-25 23:06:39 +00:00
|
|
|
bool if_exists = false;
|
2018-12-21 14:53:00 +00:00
|
|
|
|
|
|
|
/// For ADD_COLUMN
|
2018-12-25 23:06:39 +00:00
|
|
|
bool if_not_exists = false;
|
2018-12-21 14:53:00 +00:00
|
|
|
|
2018-10-15 18:47:47 +00:00
|
|
|
/// For MODIFY_ORDER_BY
|
2018-11-06 18:25:36 +00:00
|
|
|
ASTPtr order_by;
|
2018-10-15 18:47:47 +00:00
|
|
|
|
2019-02-05 14:50:25 +00:00
|
|
|
/// For ADD INDEX
|
|
|
|
ASTPtr index_decl;
|
|
|
|
String after_index_name;
|
|
|
|
|
|
|
|
/// For ADD/DROP INDEX
|
|
|
|
String index_name;
|
|
|
|
|
2019-06-02 14:41:12 +00:00
|
|
|
// For ADD CONSTRAINT
|
|
|
|
ASTPtr constraint_decl;
|
|
|
|
|
|
|
|
// For ADD/DROP CONSTRAINT
|
|
|
|
String constraint_name;
|
|
|
|
|
2019-04-15 09:30:45 +00:00
|
|
|
/// For MODIFY TTL
|
|
|
|
ASTPtr ttl;
|
|
|
|
|
2018-12-21 14:53:00 +00:00
|
|
|
/// indicates that this command should not be applied, for example in case of if_exists=true and column doesn't exist.
|
|
|
|
bool ignore = false;
|
|
|
|
|
2018-12-21 14:40:20 +00:00
|
|
|
/// For ADD and MODIFY
|
|
|
|
CompressionCodecPtr codec;
|
|
|
|
|
2019-07-24 12:56:39 +00:00
|
|
|
/// For MODIFY SETTING
|
|
|
|
SettingsChanges settings_changes;
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
AlterCommand() = default;
|
2019-08-03 11:02:40 +00:00
|
|
|
AlterCommand(const Type type_, const String & column_name_, const DataTypePtr & data_type_,
|
|
|
|
const ColumnDefaultKind default_kind_, const ASTPtr & default_expression_,
|
|
|
|
const String & after_column_, const String & comment_,
|
|
|
|
const bool if_exists_, const bool if_not_exists_)
|
|
|
|
: type{type_}, column_name{column_name_}, data_type{data_type_}, default_kind{default_kind_},
|
|
|
|
default_expression{default_expression_}, comment(comment_), after_column{after_column_},
|
|
|
|
if_exists(if_exists_), if_not_exists(if_not_exists_)
|
2017-04-01 07:20:54 +00:00
|
|
|
{}
|
2018-06-13 13:49:27 +00:00
|
|
|
|
|
|
|
static std::optional<AlterCommand> parse(const ASTAlterCommand * command);
|
|
|
|
|
2019-02-05 14:50:25 +00:00
|
|
|
void apply(ColumnsDescription & columns_description, IndicesDescription & indices_description,
|
2019-08-26 10:07:32 +00:00
|
|
|
ConstraintsDescription & constraints_description, ASTPtr & order_by_ast,
|
|
|
|
ASTPtr & primary_key_ast, ASTPtr & ttl_table_ast, SettingsChanges & changes) const;
|
2019-05-02 23:56:42 +00:00
|
|
|
|
2019-12-23 16:44:50 +00:00
|
|
|
/// Checks that alter query changes data. For MergeTree:
|
|
|
|
/// * column files (data and marks)
|
|
|
|
/// * each part meta (columns.txt)
|
|
|
|
/// in each part on disk (it's not lightweight alter).
|
|
|
|
bool isModifyingData() const;
|
2019-08-06 12:52:08 +00:00
|
|
|
|
|
|
|
/// checks that only settings changed by alter
|
|
|
|
bool isSettingsAlter() const;
|
2014-07-11 08:10:45 +00:00
|
|
|
};
|
|
|
|
|
2014-10-21 12:11:20 +00:00
|
|
|
class Context;
|
|
|
|
|
2014-07-11 08:10:45 +00:00
|
|
|
class AlterCommands : public std::vector<AlterCommand>
|
|
|
|
{
|
|
|
|
public:
|
2019-07-29 09:17:37 +00:00
|
|
|
/// Used for primitive table engines, where only columns metadata can be changed
|
2019-07-24 12:56:39 +00:00
|
|
|
void applyForColumnsOnly(ColumnsDescription & columns_description) const;
|
2019-06-02 14:41:12 +00:00
|
|
|
void apply(ColumnsDescription & columns_description, IndicesDescription & indices_description,
|
2019-08-26 10:07:32 +00:00
|
|
|
ConstraintsDescription & constraints_description, ASTPtr & order_by_ast, ASTPtr & primary_key_ast,
|
|
|
|
ASTPtr & ttl_table_ast, SettingsChanges & changes) const;
|
2014-10-21 12:11:20 +00:00
|
|
|
|
2019-08-06 12:52:08 +00:00
|
|
|
/// Apply alter commands only for settings. Exception will be thrown if any other part of table structure will be modified.
|
|
|
|
void applyForSettingsOnly(SettingsChanges & changes) const;
|
|
|
|
|
2018-05-15 12:56:14 +00:00
|
|
|
void validate(const IStorage & table, const Context & context);
|
2019-12-23 16:44:50 +00:00
|
|
|
bool isModifyingData() const;
|
2019-08-06 12:52:08 +00:00
|
|
|
bool isSettingsAlter() const;
|
2014-07-11 08:10:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|