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>
|
2019-05-17 14:34:25 +00:00
|
|
|
#include <Storages/IStorage_fwd.h>
|
2019-12-26 18:17:05 +00:00
|
|
|
#include <Storages/StorageInMemoryMetadata.h>
|
2020-01-13 16:39:20 +00:00
|
|
|
#include <Storages/MutationCommands.h>
|
2020-02-19 12:52:27 +00:00
|
|
|
#include <Storages/ColumnsDescription.h>
|
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;
|
2021-08-27 06:30:21 +00:00
|
|
|
class IDatabase;
|
|
|
|
using DatabasePtr = std::shared_ptr<IDatabase>;
|
2018-06-13 13:49:27 +00:00
|
|
|
|
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
|
|
|
|
{
|
2020-02-19 12:52:27 +00:00
|
|
|
/// The AST of the whole command
|
|
|
|
ASTPtr ast;
|
2020-01-13 16:39:20 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
enum Type
|
|
|
|
{
|
2021-05-08 15:20:40 +00:00
|
|
|
UNKNOWN,
|
2017-04-01 07:20:54 +00:00
|
|
|
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,
|
2020-08-27 13:10:10 +00:00
|
|
|
MODIFY_SAMPLE_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,
|
2021-02-10 14:12:49 +00:00
|
|
|
ADD_PROJECTION,
|
|
|
|
DROP_PROJECTION,
|
2023-08-09 20:57:49 +00:00
|
|
|
ADD_STATISTIC,
|
|
|
|
DROP_STATISTIC,
|
2019-04-15 09:30:45 +00:00
|
|
|
MODIFY_TTL,
|
2019-07-24 12:56:39 +00:00
|
|
|
MODIFY_SETTING,
|
2020-12-02 21:18:25 +00:00
|
|
|
RESET_SETTING,
|
2020-01-29 17:44:16 +00:00
|
|
|
MODIFY_QUERY,
|
2020-03-24 17:05:38 +00:00
|
|
|
RENAME_COLUMN,
|
2020-09-20 13:27:33 +00:00
|
|
|
REMOVE_TTL,
|
2021-08-27 06:30:21 +00:00
|
|
|
MODIFY_DATABASE_SETTING,
|
2021-10-14 13:44:28 +00:00
|
|
|
COMMENT_TABLE,
|
|
|
|
REMOVE_SAMPLE_BY,
|
2020-09-20 13:27:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Which property user wants to remove from column
|
|
|
|
enum class RemoveProperty
|
|
|
|
{
|
|
|
|
NO_PROPERTY,
|
|
|
|
/// Default specifiers
|
|
|
|
DEFAULT,
|
|
|
|
MATERIALIZED,
|
|
|
|
ALIAS,
|
|
|
|
|
|
|
|
/// Other properties
|
|
|
|
COMMENT,
|
|
|
|
CODEC,
|
|
|
|
TTL
|
2017-04-01 07:20:54 +00:00
|
|
|
};
|
|
|
|
|
2021-05-08 15:20:40 +00:00
|
|
|
Type type = UNKNOWN;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
String column_name;
|
|
|
|
|
2020-03-17 13:49:50 +00:00
|
|
|
/// For DROP/CLEAR COLUMN/INDEX ... IN PARTITION
|
|
|
|
ASTPtr partition;
|
2017-04-14 12:40:48 +00:00
|
|
|
|
2017-04-16 15:00:33 +00:00
|
|
|
/// For ADD and MODIFY, a new column type.
|
2019-12-27 19:04:30 +00:00
|
|
|
DataTypePtr data_type = nullptr;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2018-03-12 13:47:01 +00:00
|
|
|
ColumnDefaultKind default_kind{};
|
2017-04-01 07:20:54 +00:00
|
|
|
ASTPtr default_expression{};
|
2019-12-26 18:17:05 +00:00
|
|
|
|
2021-09-20 11:06:19 +00:00
|
|
|
/// For COMMENT column or table
|
2019-12-23 16:44:50 +00:00
|
|
|
std::optional<String> comment;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-07-01 14:58:52 +00:00
|
|
|
/// For ADD or MODIFY - after which column to add a new one. If an empty string, add to the end.
|
2017-04-01 07:20:54 +00:00
|
|
|
String after_column;
|
|
|
|
|
2021-07-01 15:59:16 +00:00
|
|
|
/// For ADD_COLUMN, MODIFY_COLUMN, ADD_INDEX - Add to the begin if it is true.
|
2020-07-01 14:58:52 +00:00
|
|
|
bool first = false;
|
|
|
|
|
2020-12-02 21:18:25 +00:00
|
|
|
/// For DROP_COLUMN, MODIFY_COLUMN, COMMENT_COLUMN, RESET_SETTING
|
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
|
2019-12-27 19:04:30 +00:00
|
|
|
ASTPtr order_by = nullptr;
|
2018-10-15 18:47:47 +00:00
|
|
|
|
2020-08-27 13:10:10 +00:00
|
|
|
/// For MODIFY_SAMPLE_BY
|
|
|
|
ASTPtr sample_by = nullptr;
|
|
|
|
|
2019-02-05 14:50:25 +00:00
|
|
|
/// For ADD INDEX
|
2019-12-27 19:04:30 +00:00
|
|
|
ASTPtr index_decl = nullptr;
|
2019-02-05 14:50:25 +00:00
|
|
|
String after_index_name;
|
|
|
|
|
|
|
|
/// For ADD/DROP INDEX
|
|
|
|
String index_name;
|
|
|
|
|
2019-06-02 14:41:12 +00:00
|
|
|
// For ADD CONSTRAINT
|
2019-12-27 19:04:30 +00:00
|
|
|
ASTPtr constraint_decl = nullptr;
|
2019-06-02 14:41:12 +00:00
|
|
|
|
|
|
|
// For ADD/DROP CONSTRAINT
|
|
|
|
String constraint_name;
|
|
|
|
|
2021-02-10 14:12:49 +00:00
|
|
|
/// For ADD PROJECTION
|
|
|
|
ASTPtr projection_decl = nullptr;
|
|
|
|
String after_projection_name;
|
|
|
|
|
|
|
|
/// For ADD/DROP PROJECTION
|
|
|
|
String projection_name;
|
|
|
|
|
2023-08-09 20:57:49 +00:00
|
|
|
ASTPtr statistic_decl = nullptr;
|
2023-09-08 00:27:17 +00:00
|
|
|
std::vector<String> statistic_columns;
|
2023-09-04 13:51:00 +00:00
|
|
|
String statistic_type;
|
2023-08-09 20:57:49 +00:00
|
|
|
|
2019-04-15 09:30:45 +00:00
|
|
|
/// For MODIFY TTL
|
2019-12-27 19:04:30 +00:00
|
|
|
ASTPtr ttl = nullptr;
|
2019-04-15 09:30:45 +00:00
|
|
|
|
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;
|
|
|
|
|
2020-03-17 13:49:50 +00:00
|
|
|
/// Clear columns or index (don't drop from metadata)
|
|
|
|
bool clear = false;
|
|
|
|
|
2018-12-21 14:40:20 +00:00
|
|
|
/// For ADD and MODIFY
|
2020-08-28 17:40:45 +00:00
|
|
|
ASTPtr codec = nullptr;
|
2018-12-21 14:40:20 +00:00
|
|
|
|
2019-07-24 12:56:39 +00:00
|
|
|
/// For MODIFY SETTING
|
|
|
|
SettingsChanges settings_changes;
|
|
|
|
|
2020-12-02 21:18:25 +00:00
|
|
|
/// For RESET SETTING
|
|
|
|
std::set<String> settings_resets;
|
|
|
|
|
2020-01-29 17:44:16 +00:00
|
|
|
/// For MODIFY_QUERY
|
|
|
|
ASTPtr select = nullptr;
|
|
|
|
|
2020-03-24 17:05:38 +00:00
|
|
|
/// Target column name
|
|
|
|
String rename_to;
|
|
|
|
|
2020-09-20 13:27:33 +00:00
|
|
|
/// What to remove from column (or TTL)
|
|
|
|
RemoveProperty to_remove = RemoveProperty::NO_PROPERTY;
|
|
|
|
|
2020-08-28 17:40:45 +00:00
|
|
|
static std::optional<AlterCommand> parse(const ASTAlterCommand * command);
|
2018-06-13 13:49:27 +00:00
|
|
|
|
2021-04-10 23:33:54 +00:00
|
|
|
void apply(StorageInMemoryMetadata & metadata, ContextPtr context) const;
|
2019-05-02 23:56:42 +00:00
|
|
|
|
2020-07-21 15:19:41 +00:00
|
|
|
/// Check that alter command require data modification (mutation) to be
|
|
|
|
/// executed. For example, cast from Date to UInt16 type can be executed
|
|
|
|
/// without any data modifications. But column drop or modify from UInt16 to
|
|
|
|
/// UInt32 require data modification.
|
2020-01-13 16:39:20 +00:00
|
|
|
bool isRequireMutationStage(const StorageInMemoryMetadata & metadata) const;
|
|
|
|
|
2019-12-29 11:25:26 +00:00
|
|
|
/// Checks that only settings changed by alter
|
2019-08-06 12:52:08 +00:00
|
|
|
bool isSettingsAlter() const;
|
2019-12-27 14:36:59 +00:00
|
|
|
|
2019-12-29 11:25:26 +00:00
|
|
|
/// Checks that only comment changed by alter
|
2019-12-27 14:36:59 +00:00
|
|
|
bool isCommentAlter() const;
|
2020-01-13 16:39:20 +00:00
|
|
|
|
2020-05-19 01:53:01 +00:00
|
|
|
/// Checks that any TTL changed by alter
|
|
|
|
bool isTTLAlter(const StorageInMemoryMetadata & metadata) const;
|
|
|
|
|
2020-09-20 13:27:33 +00:00
|
|
|
/// Command removing some property from column or table
|
|
|
|
bool isRemovingProperty() const;
|
|
|
|
|
2023-10-10 17:59:48 +00:00
|
|
|
bool isDropSomething() const;
|
|
|
|
|
2020-02-19 12:52:27 +00:00
|
|
|
/// If possible, convert alter command to mutation command. In other case
|
|
|
|
/// return empty optional. Some storages may execute mutations after
|
|
|
|
/// metadata changes.
|
2021-04-10 23:33:54 +00:00
|
|
|
std::optional<MutationCommand> tryConvertToMutationCommand(StorageInMemoryMetadata & metadata, ContextPtr context) const;
|
2014-07-11 08:10:45 +00:00
|
|
|
};
|
|
|
|
|
2014-10-21 12:11:20 +00:00
|
|
|
class Context;
|
|
|
|
|
2019-12-29 11:25:26 +00:00
|
|
|
/// Vector of AlterCommand with several additional functions
|
2014-07-11 08:10:45 +00:00
|
|
|
class AlterCommands : public std::vector<AlterCommand>
|
|
|
|
{
|
2019-12-26 18:17:05 +00:00
|
|
|
private:
|
|
|
|
bool prepared = false;
|
2020-02-19 12:52:27 +00:00
|
|
|
|
2014-07-11 08:10:45 +00:00
|
|
|
public:
|
2019-12-29 11:25:26 +00:00
|
|
|
/// Validate that commands can be applied to metadata.
|
2020-08-08 00:47:03 +00:00
|
|
|
/// Checks that all columns exist and dependencies between them.
|
2019-12-29 11:25:26 +00:00
|
|
|
/// This check is lightweight and base only on metadata.
|
|
|
|
/// More accurate check have to be performed with storage->checkAlterIsPossible.
|
2022-01-11 01:58:53 +00:00
|
|
|
void validate(const StoragePtr & table, ContextPtr context) const;
|
2019-08-06 12:52:08 +00:00
|
|
|
|
2020-02-19 15:07:02 +00:00
|
|
|
/// Prepare alter commands. Set ignore flag to some of them and set some
|
|
|
|
/// parts to commands from storage's metadata (for example, absent default)
|
2020-02-20 10:04:15 +00:00
|
|
|
void prepare(const StorageInMemoryMetadata & metadata);
|
2019-12-26 18:17:05 +00:00
|
|
|
|
2019-12-29 11:25:26 +00:00
|
|
|
/// Apply all alter command in sequential order to storage metadata.
|
|
|
|
/// Commands have to be prepared before apply.
|
2021-04-10 23:33:54 +00:00
|
|
|
void apply(StorageInMemoryMetadata & metadata, ContextPtr context) const;
|
2019-12-26 18:17:05 +00:00
|
|
|
|
2023-09-06 12:17:46 +00:00
|
|
|
/// At least one command modify settings or comments.
|
|
|
|
bool hasNonReplicatedAlterCommand() const;
|
|
|
|
|
|
|
|
/// All commands modify settings or comments.
|
|
|
|
bool areNonReplicatedAlterCommands() const;
|
2021-07-30 16:34:18 +00:00
|
|
|
|
|
|
|
/// All commands modify settings only.
|
2019-08-06 12:52:08 +00:00
|
|
|
bool isSettingsAlter() const;
|
2019-12-27 14:36:59 +00:00
|
|
|
|
2021-07-30 16:34:18 +00:00
|
|
|
/// All commands modify comments only.
|
2019-12-27 14:36:59 +00:00
|
|
|
bool isCommentAlter() const;
|
2020-01-13 16:39:20 +00:00
|
|
|
|
2020-02-19 12:52:27 +00:00
|
|
|
/// Return mutation commands which some storages may execute as part of
|
2020-05-22 08:17:57 +00:00
|
|
|
/// alter. If alter can be performed as pure metadata update, than result is
|
|
|
|
/// empty. If some TTL changes happened than, depending on materialize_ttl
|
|
|
|
/// additional mutation command (MATERIALIZE_TTL) will be returned.
|
2022-12-22 13:31:42 +00:00
|
|
|
MutationCommands getMutationCommands(StorageInMemoryMetadata metadata, bool materialize_ttl, ContextPtr context, bool with_alters=false) const;
|
2023-01-01 14:11:31 +00:00
|
|
|
|
|
|
|
/// Check if commands have any inverted index
|
2023-01-25 14:49:29 +00:00
|
|
|
static bool hasInvertedIndex(const StorageInMemoryMetadata & metadata);
|
2014-07-11 08:10:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|