2013-08-07 13:07:42 +00:00
|
|
|
#pragma once
|
|
|
|
|
2020-12-04 02:15:44 +00:00
|
|
|
#include <Parsers/ASTExpressionList.h>
|
2017-04-21 12:39:28 +00:00
|
|
|
#include <Parsers/ASTQueryWithOnCluster.h>
|
2020-12-04 02:15:44 +00:00
|
|
|
#include <Parsers/ASTQueryWithTableAndOutput.h>
|
2019-10-09 13:02:05 +00:00
|
|
|
#include <Parsers/ASTTTLElement.h>
|
2020-12-04 02:15:44 +00:00
|
|
|
#include <Parsers/IAST.h>
|
2015-08-05 21:38:31 +00:00
|
|
|
|
2013-08-07 13:07:42 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2017-04-21 12:39:28 +00:00
|
|
|
/** ALTER query:
|
2013-08-07 13:07:42 +00:00
|
|
|
* ALTER TABLE [db.]name_type
|
2017-04-01 07:20:54 +00:00
|
|
|
* ADD COLUMN col_name type [AFTER col_after],
|
2017-04-21 12:39:28 +00:00
|
|
|
* DROP COLUMN col_drop [FROM PARTITION partition],
|
|
|
|
* MODIFY COLUMN col_name type,
|
|
|
|
* DROP PARTITION partition,
|
2018-10-14 15:30:06 +00:00
|
|
|
* COMMENT_COLUMN col_name 'comment',
|
2019-05-28 21:17:48 +00:00
|
|
|
* ALTER LIVE VIEW [db.]name_type
|
|
|
|
* REFRESH
|
2013-08-07 13:07:42 +00:00
|
|
|
*/
|
|
|
|
|
2018-06-09 15:53:14 +00:00
|
|
|
class ASTAlterCommand : public IAST
|
2013-08-07 13:07:42 +00:00
|
|
|
{
|
|
|
|
public:
|
2018-06-09 15:53:14 +00:00
|
|
|
enum Type
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
ADD_COLUMN,
|
|
|
|
DROP_COLUMN,
|
|
|
|
MODIFY_COLUMN,
|
2018-10-14 15:30:06 +00:00
|
|
|
COMMENT_COLUMN,
|
2020-03-24 17:05:38 +00:00
|
|
|
RENAME_COLUMN,
|
2021-07-31 18:17:06 +00:00
|
|
|
MATERIALIZE_COLUMN,
|
|
|
|
|
2018-10-15 18:47:47 +00:00
|
|
|
MODIFY_ORDER_BY,
|
2020-08-27 13:10:10 +00:00
|
|
|
MODIFY_SAMPLE_BY,
|
2019-04-15 09:30:45 +00:00
|
|
|
MODIFY_TTL,
|
2020-01-22 13:24:20 +00:00
|
|
|
MATERIALIZE_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-09-20 13:27:33 +00:00
|
|
|
REMOVE_TTL,
|
2021-10-14 13:44:28 +00:00
|
|
|
REMOVE_SAMPLE_BY,
|
2016-05-05 18:28:46 +00:00
|
|
|
|
2019-02-05 14:50:25 +00:00
|
|
|
ADD_INDEX,
|
|
|
|
DROP_INDEX,
|
2019-04-09 15:36:33 +00:00
|
|
|
MATERIALIZE_INDEX,
|
2019-02-05 14:50:25 +00:00
|
|
|
|
2019-06-02 14:41:12 +00:00
|
|
|
ADD_CONSTRAINT,
|
|
|
|
DROP_CONSTRAINT,
|
2019-02-05 14:50:25 +00:00
|
|
|
|
2021-02-10 14:12:49 +00:00
|
|
|
ADD_PROJECTION,
|
|
|
|
DROP_PROJECTION,
|
|
|
|
MATERIALIZE_PROJECTION,
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
DROP_PARTITION,
|
2019-07-22 11:23:11 +00:00
|
|
|
DROP_DETACHED_PARTITION,
|
2017-04-01 07:20:54 +00:00
|
|
|
ATTACH_PARTITION,
|
2019-07-26 08:42:17 +00:00
|
|
|
MOVE_PARTITION,
|
2018-05-21 13:49:54 +00:00
|
|
|
REPLACE_PARTITION,
|
2017-04-01 07:20:54 +00:00
|
|
|
FETCH_PARTITION,
|
|
|
|
FREEZE_PARTITION,
|
2018-10-30 15:04:13 +00:00
|
|
|
FREEZE_ALL,
|
2021-03-02 20:28:42 +00:00
|
|
|
UNFREEZE_PARTITION,
|
|
|
|
UNFREEZE_ALL,
|
2016-05-05 18:28:46 +00:00
|
|
|
|
2018-02-02 16:02:43 +00:00
|
|
|
DELETE,
|
2018-08-07 13:58:11 +00:00
|
|
|
UPDATE,
|
2018-02-02 16:02:43 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
NO_TYPE,
|
2019-05-28 21:17:48 +00:00
|
|
|
|
|
|
|
LIVE_VIEW_REFRESH,
|
2021-08-27 06:30:21 +00:00
|
|
|
|
|
|
|
MODIFY_DATABASE_SETTING,
|
2021-09-20 11:06:19 +00:00
|
|
|
|
|
|
|
MODIFY_COMMENT,
|
2017-04-01 07:20:54 +00:00
|
|
|
};
|
2013-08-07 13:07:42 +00:00
|
|
|
|
2018-06-09 15:53:14 +00:00
|
|
|
Type type = NO_TYPE;
|
2016-01-28 01:00:27 +00:00
|
|
|
|
2018-06-09 15:53:14 +00:00
|
|
|
/** The ADD COLUMN query stores the name and type of the column to add
|
|
|
|
* This field is not used in the DROP query
|
|
|
|
* In MODIFY query, the column name and the new type are stored here
|
|
|
|
*/
|
|
|
|
ASTPtr col_decl;
|
2013-08-07 13:07:42 +00:00
|
|
|
|
2020-07-01 14:58:52 +00:00
|
|
|
/** The ADD COLUMN and MODIFY COLUMN query here optionally stores the name of the column following AFTER
|
2018-06-09 15:53:14 +00:00
|
|
|
* The DROP query stores the column name for deletion here
|
2020-03-24 17:05:38 +00:00
|
|
|
* Also used for RENAME COLUMN.
|
2018-06-09 15:53:14 +00:00
|
|
|
*/
|
|
|
|
ASTPtr column;
|
2014-07-10 10:16:50 +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
|
|
|
|
2020-08-27 13:10:10 +00:00
|
|
|
/** For MODIFY SAMPLE BY
|
|
|
|
*/
|
|
|
|
ASTPtr sample_by;
|
|
|
|
|
2019-02-05 14:50:25 +00:00
|
|
|
/** The ADD INDEX query stores the IndexDeclaration there.
|
|
|
|
*/
|
|
|
|
ASTPtr index_decl;
|
|
|
|
|
|
|
|
/** The ADD INDEX query stores the name of the index following AFTER.
|
|
|
|
* The DROP INDEX query stores the name for deletion.
|
2019-05-05 17:01:54 +00:00
|
|
|
* The MATERIALIZE INDEX query stores the name of the index to materialize.
|
2019-05-09 14:25:18 +00:00
|
|
|
* The CLEAR INDEX query stores the name of the index to clear.
|
2019-02-05 14:50:25 +00:00
|
|
|
*/
|
2019-07-24 12:56:39 +00:00
|
|
|
ASTPtr index;
|
2019-02-05 14:50:25 +00:00
|
|
|
|
2019-06-02 14:41:12 +00:00
|
|
|
/** The ADD CONSTRAINT query stores the ConstraintDeclaration there.
|
|
|
|
*/
|
|
|
|
ASTPtr constraint_decl;
|
|
|
|
|
|
|
|
/** The DROP CONSTRAINT query stores the name for deletion.
|
|
|
|
*/
|
|
|
|
ASTPtr constraint;
|
2019-02-05 14:50:25 +00:00
|
|
|
|
2021-02-10 14:12:49 +00:00
|
|
|
/** The ADD PROJECTION query stores the ProjectionDeclaration there.
|
|
|
|
*/
|
|
|
|
ASTPtr projection_decl;
|
|
|
|
|
|
|
|
/** The ADD PROJECTION query stores the name of the projection following AFTER.
|
|
|
|
* The DROP PROJECTION query stores the name for deletion.
|
|
|
|
* The MATERIALIZE PROJECTION query stores the name of the projection to materialize.
|
|
|
|
* The CLEAR PROJECTION query stores the name of the projection to clear.
|
|
|
|
*/
|
|
|
|
ASTPtr projection;
|
|
|
|
|
2020-11-10 10:23:46 +00:00
|
|
|
/** Used in DROP PARTITION, ATTACH PARTITION FROM, UPDATE, DELETE queries.
|
2018-06-09 15:53:14 +00:00
|
|
|
* The value or ID of the partition is stored here.
|
|
|
|
*/
|
|
|
|
ASTPtr partition;
|
2016-05-05 18:28:46 +00:00
|
|
|
|
2018-08-07 13:58:11 +00:00
|
|
|
/// For DELETE/UPDATE WHERE: the predicate that filters the rows to delete/update.
|
2018-06-09 15:53:14 +00:00
|
|
|
ASTPtr predicate;
|
2017-09-06 20:34:26 +00:00
|
|
|
|
2018-08-07 13:58:11 +00:00
|
|
|
/// A list of expressions of the form `column = expr` for the UPDATE command.
|
|
|
|
ASTPtr update_assignments;
|
|
|
|
|
2018-10-14 15:30:06 +00:00
|
|
|
/// A column comment
|
|
|
|
ASTPtr comment;
|
|
|
|
|
2019-04-15 09:30:45 +00:00
|
|
|
/// For MODIFY TTL query
|
|
|
|
ASTPtr ttl;
|
|
|
|
|
2019-07-24 12:56:39 +00:00
|
|
|
/// FOR MODIFY_SETTING
|
|
|
|
ASTPtr settings_changes;
|
|
|
|
|
2020-12-02 21:18:25 +00:00
|
|
|
/// FOR RESET_SETTING
|
|
|
|
ASTPtr settings_resets;
|
|
|
|
|
2020-01-29 17:44:16 +00:00
|
|
|
/// For MODIFY_QUERY
|
|
|
|
ASTPtr select;
|
|
|
|
|
2019-05-28 21:17:48 +00:00
|
|
|
/** In ALTER CHANNEL, ADD, DROP, SUSPEND, RESUME, REFRESH, MODIFY queries, the list of live views is stored here
|
|
|
|
*/
|
|
|
|
ASTPtr values;
|
|
|
|
|
2018-06-09 15:53:14 +00:00
|
|
|
bool detach = false; /// true for DETACH PARTITION
|
2018-02-02 16:02:43 +00:00
|
|
|
|
2019-08-29 16:17:47 +00:00
|
|
|
bool part = false; /// true for ATTACH PART, DROP DETACHED PART and MOVE
|
2014-08-06 09:24:30 +00:00
|
|
|
|
2018-06-09 15:53:14 +00:00
|
|
|
bool clear_column = false; /// for CLEAR COLUMN (do not drop column from metadata)
|
2017-06-22 11:01:30 +00:00
|
|
|
|
2019-05-09 14:25:18 +00:00
|
|
|
bool clear_index = false; /// for CLEAR INDEX (do not drop index from metadata)
|
2018-12-21 14:53:00 +00:00
|
|
|
|
2021-02-10 14:12:49 +00:00
|
|
|
bool clear_projection = false; /// for CLEAR PROJECTION (do not drop projection from metadata)
|
|
|
|
|
2019-05-09 14:25:18 +00:00
|
|
|
bool if_not_exists = false; /// option for ADD_COLUMN
|
|
|
|
|
|
|
|
bool if_exists = false; /// option for DROP_COLUMN, MODIFY_COLUMN, COMMENT_COLUMN
|
2018-12-21 14:53:00 +00:00
|
|
|
|
2020-07-01 14:58:52 +00:00
|
|
|
bool first = false; /// option for ADD_COLUMN, MODIFY_COLUMN
|
|
|
|
|
2020-05-22 13:29:33 +00:00
|
|
|
DataDestinationType move_destination_type; /// option for MOVE PART/PARTITION
|
2019-07-18 15:19:03 +00:00
|
|
|
|
2019-10-31 10:40:11 +00:00
|
|
|
String move_destination_name; /// option for MOVE PART/PARTITION
|
2019-07-18 15:19:03 +00:00
|
|
|
|
2018-06-09 15:53:14 +00:00
|
|
|
/** For FETCH PARTITION - the path in ZK to the shard, from which to download the partition.
|
|
|
|
*/
|
|
|
|
String from;
|
2016-03-25 11:48:45 +00:00
|
|
|
|
2021-02-24 14:26:46 +00:00
|
|
|
/**
|
|
|
|
* For FREEZE PARTITION - place local backup to directory with specified name.
|
|
|
|
* For UNFREEZE - delete local backup at directory with specified name.
|
2018-06-09 15:53:14 +00:00
|
|
|
*/
|
|
|
|
String with_name;
|
2014-10-09 20:28:33 +00:00
|
|
|
|
2018-06-09 15:53:14 +00:00
|
|
|
/// REPLACE(ATTACH) PARTITION partition FROM db.table
|
|
|
|
String from_database;
|
|
|
|
String from_table;
|
|
|
|
/// To distinguish REPLACE and ATTACH PARTITION partition FROM db.table
|
|
|
|
bool replace = true;
|
2019-09-17 11:59:09 +00:00
|
|
|
/// MOVE PARTITION partition TO TABLE db.table
|
2019-07-26 10:26:23 +00:00
|
|
|
String to_database;
|
|
|
|
String to_table;
|
2019-07-29 03:14:53 +00:00
|
|
|
|
2020-03-24 17:05:38 +00:00
|
|
|
/// Target column name
|
|
|
|
ASTPtr rename_to;
|
|
|
|
|
2020-09-20 13:27:33 +00:00
|
|
|
/// Which property user want to remove
|
|
|
|
String remove_property;
|
|
|
|
|
2022-01-04 08:51:40 +00:00
|
|
|
String getID(char delim) const override;
|
2018-05-21 13:49:54 +00:00
|
|
|
|
2018-06-09 15:53:14 +00:00
|
|
|
ASTPtr clone() const override;
|
|
|
|
|
2022-01-04 08:51:40 +00:00
|
|
|
static const char * typeToString(Type type);
|
|
|
|
|
2018-06-09 15:53:14 +00:00
|
|
|
protected:
|
|
|
|
void formatImpl(const FormatSettings & settings, FormatState & state, FormatStateStacked frame) const override;
|
|
|
|
};
|
|
|
|
|
2018-10-18 15:03:14 +00:00
|
|
|
class ASTAlterQuery : public ASTQueryWithTableAndOutput, public ASTQueryWithOnCluster
|
2018-06-09 15:53:14 +00:00
|
|
|
{
|
|
|
|
public:
|
2021-08-20 17:59:57 +00:00
|
|
|
enum class AlterObjectType
|
|
|
|
{
|
|
|
|
TABLE,
|
|
|
|
DATABASE,
|
2021-08-27 06:30:21 +00:00
|
|
|
LIVE_VIEW,
|
|
|
|
UNKNOWN,
|
2021-08-20 17:59:57 +00:00
|
|
|
};
|
|
|
|
|
2021-08-27 06:30:21 +00:00
|
|
|
AlterObjectType alter_object = AlterObjectType::UNKNOWN;
|
2019-05-28 21:17:48 +00:00
|
|
|
|
2020-12-04 02:15:44 +00:00
|
|
|
ASTExpressionList * command_list = nullptr;
|
2013-08-07 13:07:42 +00:00
|
|
|
|
2021-01-06 12:05:19 +00:00
|
|
|
bool isSettingsAlter() const;
|
|
|
|
|
2021-01-26 17:51:25 +00:00
|
|
|
bool isFreezeAlter() const;
|
|
|
|
|
2021-09-20 17:08:59 +00:00
|
|
|
bool isAttachAlter() const;
|
|
|
|
|
|
|
|
bool isFetchAlter() const;
|
|
|
|
|
2021-09-26 16:47:24 +00:00
|
|
|
bool isDropPartitionAlter() const;
|
|
|
|
|
2018-12-07 12:34:40 +00:00
|
|
|
String getID(char) const override;
|
2013-08-07 13:07:42 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
ASTPtr clone() const override;
|
2015-08-05 21:38:31 +00:00
|
|
|
|
2022-04-22 11:31:39 +00:00
|
|
|
ASTPtr getRewrittenASTWithoutOnCluster(const WithoutOnClusterASTRewriteParams & params) const override
|
2018-10-24 15:31:07 +00:00
|
|
|
{
|
2022-04-22 11:31:39 +00:00
|
|
|
return removeOnCluster<ASTAlterQuery>(clone(), params.default_database);
|
2018-10-24 15:31:07 +00:00
|
|
|
}
|
2017-04-21 12:39:28 +00:00
|
|
|
|
2021-12-29 12:32:49 +00:00
|
|
|
virtual QueryKind getQueryKind() const override { return QueryKind::Alter; }
|
2021-08-04 14:09:23 +00:00
|
|
|
|
2015-08-05 21:38:31 +00:00
|
|
|
protected:
|
2017-08-03 17:00:41 +00:00
|
|
|
void formatQueryImpl(const FormatSettings & settings, FormatState & state, FormatStateStacked frame) const override;
|
2021-01-26 17:51:25 +00:00
|
|
|
|
|
|
|
bool isOneCommandTypeOnly(const ASTAlterCommand::Type & type) const;
|
2013-08-07 13:07:42 +00:00
|
|
|
};
|
2015-08-05 21:38:31 +00:00
|
|
|
|
2013-08-07 13:07:42 +00:00
|
|
|
}
|