提交 b0b3c31b authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

ALTER TABLE ALTER COLUMN SET DATA TYPE is a standard syntax

上级 56b472bd
......@@ -359,6 +359,7 @@ ALTER TABLE [ IF EXISTS ] tableName ALTER COLUMN columnName
| { SET ON UPDATE expression }
| { SET NOT NULL }
| { DROP NOT NULL } | { SET NULL }
| { SET DATA TYPE dataType }
| { SET { VISIBLE | INVISIBLE } }
| { DROP ON UPDATE } }
","
......@@ -386,6 +387,8 @@ SET NOT NULL sets a column to not allow NULL. Rows may not contains NULL in this
DROP NOT NULL and SET NULL set a column to allow NULL. The row may not be part of a primary key.
SET DATA TYPE changes the data type of a column.
SET INVISIBLE makes the column hidden, i.e. it will not appear in SELECT * results.
SET VISIBLE has the reverse effect.
......
......@@ -7277,14 +7277,11 @@ public class Parser {
return command;
} else if (readIf("TYPE")) {
// PostgreSQL compatibility
return parseAlterTableAlterColumnType(schema, tableName,
columnName, ifTableExists);
return parseAlterTableAlterColumnDataType(schema, tableName, columnName, ifTableExists);
} else if (readIf("SET")) {
if (readIf("DATA")) {
// Derby compatibility
read("TYPE");
return parseAlterTableAlterColumnType(schema, tableName, columnName,
ifTableExists);
return parseAlterTableAlterColumnDataType(schema, tableName, columnName, ifTableExists);
}
AlterTableAlterColumn command = new AlterTableAlterColumn(
session, schema);
......@@ -7341,8 +7338,7 @@ public class Parser {
command.setSelectivity(readExpression());
return command;
} else {
return parseAlterTableAlterColumnType(schema, tableName,
columnName, ifTableExists);
return parseAlterTableAlterColumnType(schema, tableName, columnName, ifTableExists);
}
}
throw getSyntaxError();
......@@ -7374,8 +7370,48 @@ public class Parser {
Column oldColumn = columnIfTableExists(schema, tableName, columnName, ifTableExists);
Column newColumn = parseColumnForTable(columnName,
oldColumn == null ? true : oldColumn.isNullable(), true);
AlterTableAlterColumn command = new AlterTableAlterColumn(session,
schema);
if (readIf(CHECK)) {
Expression expr = readExpression();
newColumn.addCheckConstraint(session, expr);
}
AlterTableAlterColumn command = new AlterTableAlterColumn(session, schema);
command.setTableName(tableName);
command.setIfTableExists(ifTableExists);
command.setType(CommandInterface.ALTER_TABLE_ALTER_COLUMN_CHANGE_TYPE);
command.setOldColumn(oldColumn);
command.setNewColumn(newColumn);
return command;
}
private AlterTableAlterColumn parseAlterTableAlterColumnDataType(Schema schema,
String tableName, String columnName, boolean ifTableExists) {
Column oldColumn = columnIfTableExists(schema, tableName, columnName, ifTableExists);
Column newColumn = parseColumnWithType(columnName, true);
if (oldColumn != null) {
if (!oldColumn.isNullable()) {
newColumn.setNullable(false);
}
if (!oldColumn.getVisible()) {
newColumn.setVisible(false);
}
Expression e = oldColumn.getDefaultExpression();
if (e != null) {
newColumn.setDefaultExpression(session, e);
}
e = oldColumn.getOnUpdateExpression();
if (e != null) {
newColumn.setOnUpdateExpression(session, e);
}
e = oldColumn.getCheckConstraint(session, columnName);
if (e != null) {
newColumn.addCheckConstraint(session, e);
}
String c = oldColumn.getComment();
if (c != null) {
newColumn.setComment(c);
}
}
AlterTableAlterColumn command = new AlterTableAlterColumn(session, schema);
command.setTableName(tableName);
command.setIfTableExists(ifTableExists);
command.setType(CommandInterface.ALTER_TABLE_ALTER_COLUMN_CHANGE_TYPE);
......
......@@ -47,5 +47,24 @@ ALTER TABLE TEST ALTER COLUMN T SET NULL;
SELECT COLUMN_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME = 'T';
>> INT
-- SET DATA TYPE
ALTER TABLE TEST ALTER COLUMN T SET DATA TYPE BIGINT;
> ok
SELECT COLUMN_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME = 'T';
>> BIGINT
ALTER TABLE TEST ALTER COLUMN T INT INVISIBLE DEFAULT 1 ON UPDATE 2 NOT NULL COMMENT 'C' CHECK T < 100;
> ok
SELECT COLUMN_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME = 'T';
>> INT INVISIBLE DEFAULT 1 ON UPDATE 2 NOT NULL COMMENT 'C' CHECK (T < 100)
ALTER TABLE TEST ALTER COLUMN T SET DATA TYPE BIGINT;
> ok
SELECT COLUMN_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME = 'T';
>> BIGINT INVISIBLE DEFAULT 1 ON UPDATE 2 NOT NULL COMMENT 'C' CHECK (T < 100)
DROP TABLE TEST;
> ok
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论