Support PostgreSQL style ALTER MODIFY COLUMN

This commit is contained in:
cmsxbc 2021-11-30 21:07:14 +08:00
parent 1ba5fd5912
commit 366ee3deba
No known key found for this signature in database
GPG Key ID: A58268D081411C9C
6 changed files with 28 additions and 3 deletions

View File

@ -10,7 +10,7 @@ A set of queries that allow changing the table structure.
Syntax:
``` sql
ALTER TABLE [db].name [ON CLUSTER cluster] ADD|DROP|RENAME|CLEAR|COMMENT|MODIFY|MATERIALIZE COLUMN ...
ALTER TABLE [db].name [ON CLUSTER cluster] ADD|DROP|RENAME|CLEAR|COMMENT|{MODIFY|ALTER}|MATERIALIZE COLUMN ...
```
In the query, specify a list of one or more comma-separated actions.
@ -138,6 +138,7 @@ ALTER TABLE visits COMMENT COLUMN browser 'The table shows the browser used for
``` sql
MODIFY COLUMN [IF EXISTS] name [type] [default_expr] [codec] [TTL] [AFTER name_after | FIRST]
ALTER COLUMN [IF EXISTS] name TYPE [type] [default_expr] [codec] [TTL] [AFTER name_after | FIRST]
```
This query changes the `name` column properties:

View File

@ -10,7 +10,7 @@ toc_title: "Манипуляции со столбцами"
Синтаксис:
``` sql
ALTER TABLE [db].name [ON CLUSTER cluster] ADD|DROP|RENAME|CLEAR|COMMENT|MODIFY|MATERIALIZE COLUMN ...
ALTER TABLE [db].name [ON CLUSTER cluster] ADD|DROP|RENAME|CLEAR|COMMENT|{MODIFY|ALTER}|MATERIALIZE COLUMN ...
```
В запросе можно указать сразу несколько действий над одной таблицей через запятую.
@ -138,6 +138,7 @@ ALTER TABLE visits COMMENT COLUMN browser 'Столбец показывает,
``` sql
MODIFY COLUMN [IF EXISTS] name [type] [default_expr] [codec] [TTL] [AFTER name_after | FIRST]
ALTER COLUMN [IF EXISTS] name TYPE [type] [default_expr] [codec] [TTL] [AFTER name_after | FIRST]
```
Запрос изменяет следующие свойства столбца `name`:

View File

@ -26,6 +26,7 @@ bool ParserAlterCommand::parseImpl(Pos & pos, ASTPtr & node, Expected & expected
ParserKeyword s_drop_column("DROP COLUMN");
ParserKeyword s_clear_column("CLEAR COLUMN");
ParserKeyword s_modify_column("MODIFY COLUMN");
ParserKeyword s_alter_column("ALTER COLUMN");
ParserKeyword s_rename_column("RENAME COLUMN");
ParserKeyword s_comment_column("COMMENT COLUMN");
ParserKeyword s_materialize_column("MATERIALIZE COLUMN");
@ -619,11 +620,14 @@ bool ParserAlterCommand::parseImpl(Pos & pos, ASTPtr & node, Expected & expected
return false;
}
}
else if (s_modify_column.ignore(pos, expected))
else if (bool is_modify = s_modify_column.ignore(pos, expected); is_modify || s_alter_column.ignore(pos, expected))
{
if (s_if_exists.ignore(pos, expected))
command->if_exists = true;
if (!is_modify)
parser_modify_col_decl.enableCheckTypeKeyword();
if (!parser_modify_col_decl.parse(pos, command->col_decl, expected))
return false;

View File

@ -96,6 +96,8 @@ public:
{
}
void enableCheckTypeKeyword() { check_type_keyword = true; }
protected:
using ASTDeclarePtr = std::shared_ptr<ASTColumnDeclaration>;
@ -106,6 +108,8 @@ protected:
bool require_type = true;
bool allow_null_modifiers = false;
bool check_keywords_after_name = false;
/// just for ALTER TABLE ALTER COLUMN use
bool check_type_keyword = false;
};
using ParserColumnDeclaration = IParserColumnDeclaration<ParserIdentifier>;
@ -125,6 +129,7 @@ bool IParserColumnDeclaration<NameParser>::parseImpl(Pos & pos, ASTPtr & node, E
ParserKeyword s_codec{"CODEC"};
ParserKeyword s_ttl{"TTL"};
ParserKeyword s_remove{"REMOVE"};
ParserKeyword s_type{"TYPE"};
ParserTernaryOperatorExpression expr_parser;
ParserStringLiteral string_literal_parser;
ParserCodec codec_parser;
@ -171,6 +176,8 @@ bool IParserColumnDeclaration<NameParser>::parseImpl(Pos & pos, ASTPtr & node, E
|| (!s_comment.checkWithoutMoving(pos, expected)
&& !s_codec.checkWithoutMoving(pos, expected))))
{
if (check_type_keyword && !s_type.ignore(pos, expected))
return false;
if (!type_parser.parse(pos, type, expected))
return false;
}

View File

@ -0,0 +1,3 @@
CREATE TABLE default.alter_column_02126\n(\n `a` Int32,\n `x` Int32,\n `y` Int32\n)\nENGINE = MergeTree\nORDER BY a\nSETTINGS index_granularity = 8192
CREATE TABLE default.alter_column_02126\n(\n `a` Int32,\n `x` Float32,\n `y` Int32\n)\nENGINE = MergeTree\nORDER BY a\nSETTINGS index_granularity = 8192
CREATE TABLE default.alter_column_02126\n(\n `a` Int32,\n `x` Float64,\n `y` Float32\n)\nENGINE = MergeTree\nORDER BY a\nSETTINGS index_granularity = 8192

View File

@ -0,0 +1,9 @@
DROP TABLE IF EXISTS alter_column_02126;
CREATE TABLE alter_column_02126 (a Int, x Int, y Int) ENGINE = MergeTree ORDER BY a;
SHOW CREATE TABLE alter_column_02126;
ALTER TABLE alter_column_02126 ALTER COLUMN x TYPE Float32;
SHOW CREATE TABLE alter_column_02126;
ALTER TABLE alter_column_02126 ALTER COLUMN x TYPE Float64, MODIFY COLUMN y Float32;
SHOW CREATE TABLE alter_column_02126;
ALTER TABLE alter_column_02126 MODIFY COLUMN y TYPE Float32; -- { clientError 62 }
ALTER TABLE alter_column_02126 ALTER COLUMN y Float32; -- { clientError 62 }