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>
|
2018-06-14 13:03:23 +00:00
|
|
|
|
#include <optional>
|
|
|
|
|
|
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,
|
2017-04-01 07:20:54 +00:00
|
|
|
|
MODIFY_PRIMARY_KEY,
|
2018-10-15 18:47:47 +00:00
|
|
|
|
MODIFY_ORDER_BY,
|
2018-11-14 22:46:39 +00:00
|
|
|
|
UKNOWN_TYPE,
|
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{};
|
2018-11-06 13:26:43 +00:00
|
|
|
|
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;
|
|
|
|
|
|
2017-04-16 15:00:33 +00:00
|
|
|
|
/// For MODIFY_PRIMARY_KEY
|
2017-04-01 07:20:54 +00:00
|
|
|
|
ASTPtr primary_key;
|
|
|
|
|
|
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
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
|
AlterCommand() = default;
|
|
|
|
|
AlterCommand(const Type type, const String & column_name, const DataTypePtr & data_type,
|
2018-03-12 13:47:01 +00:00
|
|
|
|
const ColumnDefaultKind default_kind, const ASTPtr & default_expression,
|
2018-11-14 22:46:39 +00:00
|
|
|
|
const String & after_column = String{}, const String & comment = "") // TODO: разобраться здесь с параметром по умолчанию
|
2018-03-12 13:47:01 +00:00
|
|
|
|
: type{type}, column_name{column_name}, data_type{data_type}, default_kind{default_kind},
|
2018-11-06 13:26:43 +00:00
|
|
|
|
default_expression{default_expression}, comment(comment), after_column{after_column}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
{}
|
2018-06-13 13:49:27 +00:00
|
|
|
|
|
|
|
|
|
static std::optional<AlterCommand> parse(const ASTAlterCommand * command);
|
|
|
|
|
|
2018-11-28 12:06:52 +00:00
|
|
|
|
void apply(ColumnsDescription & columns_description, ASTPtr & order_by_ast, ASTPtr & primary_key_ast) const;
|
2018-11-13 12:08:07 +00:00
|
|
|
|
/// Checks that not only metadata touched by that command
|
|
|
|
|
bool is_mutable() const;
|
2014-07-11 08:10:45 +00:00
|
|
|
|
};
|
|
|
|
|
|
2014-10-21 12:11:20 +00:00
|
|
|
|
class IStorage;
|
|
|
|
|
class Context;
|
|
|
|
|
|
2014-07-11 08:10:45 +00:00
|
|
|
|
class AlterCommands : public std::vector<AlterCommand>
|
|
|
|
|
{
|
|
|
|
|
public:
|
2018-11-28 12:06:52 +00:00
|
|
|
|
void apply(ColumnsDescription & columns_description, ASTPtr & order_by_ast, ASTPtr & primary_key_ast) const;
|
|
|
|
|
|
|
|
|
|
/// For storages that don't support MODIFY_PRIMARY_KEY or MODIFY_ORDER_BY.
|
|
|
|
|
void apply(ColumnsDescription & columns_description) const;
|
2014-10-21 12:11:20 +00:00
|
|
|
|
|
2018-05-15 12:56:14 +00:00
|
|
|
|
void validate(const IStorage & table, const Context & context);
|
2018-11-15 13:12:27 +00:00
|
|
|
|
bool is_mutable() const;
|
2014-07-11 08:10:45 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|