提交 6cf9ee70 authored 作者: noelgrandin's avatar noelgrandin

When changing the length of a VARCHAR column, we don't need to copy the table

上级 ddacfd12
......@@ -32,6 +32,7 @@ import org.h2.table.Column;
import org.h2.table.Table;
import org.h2.table.TableView;
import org.h2.util.New;
import org.h2.value.Value;
/**
* This class represents the statements
......@@ -112,6 +113,20 @@ public class AlterTableAlterColumn extends SchemaCommand {
break;
}
case CommandInterface.ALTER_TABLE_ALTER_COLUMN_CHANGE_TYPE: {
// If the change is only increasing the length of a VARCHAR type, then we don't need to copy the table
// because the length is only a constraint, and does not affect the storage structure.
if (oldColumn.getType() == Value.STRING
&& oldColumn.getPrecision() <= newColumn.getPrecision()
&& oldColumn.isNullable() == newColumn.isNullable()
&& oldColumn.isAutoIncrement() == newColumn.isAutoIncrement()
&& oldColumn.isPrimaryKey() == newColumn.isPrimaryKey()
&& oldColumn.getType() == newColumn.getType())
{
oldColumn.setPrecision(newColumn.getPrecision());
db.update(session, table);
}
else
{
oldColumn.setSequence(null);
oldColumn.setDefaultExpression(session, null);
oldColumn.setConvertNullToDefault(false);
......@@ -122,6 +137,7 @@ public class AlterTableAlterColumn extends SchemaCommand {
}
convertAutoIncrementColumn(newColumn);
copyData();
}
break;
}
case CommandInterface.ALTER_TABLE_ADD_COLUMN: {
......
......@@ -62,7 +62,7 @@ public class Column {
public static final int NULLABLE_UNKNOWN = ResultSetMetaData.columnNullableUnknown;
private final int type;
private final long precision;
private long precision;
private final int scale;
private final int displaySize;
private Table table;
......@@ -242,6 +242,10 @@ public class Column {
return precision;
}
public void setPrecision(long p) {
precision = p;
}
public int getDisplaySize() {
return displaySize;
}
......
......@@ -40,6 +40,7 @@ public class TestAlter extends TestBase {
testAlterTableAlterColumn();
testAlterTableDropIdentityColumn();
testAlterTableAddColumnIfNotExists();
testAlterTableAlterColumn2();
conn.close();
deleteDb("alter");
}
......@@ -135,4 +136,11 @@ public class TestAlter extends TestBase {
stat.execute("drop table t");
}
private void testAlterTableAlterColumn2() throws SQLException {
// ensure that increasing a VARCHAR columns length takes effect because we optimise this case
stat.execute("create table t(x varchar(2)) as select 'x'");
stat.execute("alter table t alter column x varchar(20)");
stat.execute("insert into t values('Hello')");
stat.execute("drop table t");
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论