提交 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; ...@@ -32,6 +32,7 @@ import org.h2.table.Column;
import org.h2.table.Table; import org.h2.table.Table;
import org.h2.table.TableView; import org.h2.table.TableView;
import org.h2.util.New; import org.h2.util.New;
import org.h2.value.Value;
/** /**
* This class represents the statements * This class represents the statements
...@@ -112,16 +113,31 @@ public class AlterTableAlterColumn extends SchemaCommand { ...@@ -112,16 +113,31 @@ public class AlterTableAlterColumn extends SchemaCommand {
break; break;
} }
case CommandInterface.ALTER_TABLE_ALTER_COLUMN_CHANGE_TYPE: { case CommandInterface.ALTER_TABLE_ALTER_COLUMN_CHANGE_TYPE: {
oldColumn.setSequence(null); // If the change is only increasing the length of a VARCHAR type, then we don't need to copy the table
oldColumn.setDefaultExpression(session, null); // because the length is only a constraint, and does not affect the storage structure.
oldColumn.setConvertNullToDefault(false); if (oldColumn.getType() == Value.STRING
if (oldColumn.isNullable() && !newColumn.isNullable()) { && oldColumn.getPrecision() <= newColumn.getPrecision()
checkNoNullValues(); && oldColumn.isNullable() == newColumn.isNullable()
} else if (!oldColumn.isNullable() && newColumn.isNullable()) { && oldColumn.isAutoIncrement() == newColumn.isAutoIncrement()
checkNullable(); && 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);
if (oldColumn.isNullable() && !newColumn.isNullable()) {
checkNoNullValues();
} else if (!oldColumn.isNullable() && newColumn.isNullable()) {
checkNullable();
}
convertAutoIncrementColumn(newColumn);
copyData();
} }
convertAutoIncrementColumn(newColumn);
copyData();
break; break;
} }
case CommandInterface.ALTER_TABLE_ADD_COLUMN: { case CommandInterface.ALTER_TABLE_ADD_COLUMN: {
......
...@@ -62,7 +62,7 @@ public class Column { ...@@ -62,7 +62,7 @@ public class Column {
public static final int NULLABLE_UNKNOWN = ResultSetMetaData.columnNullableUnknown; public static final int NULLABLE_UNKNOWN = ResultSetMetaData.columnNullableUnknown;
private final int type; private final int type;
private final long precision; private long precision;
private final int scale; private final int scale;
private final int displaySize; private final int displaySize;
private Table table; private Table table;
...@@ -242,6 +242,10 @@ public class Column { ...@@ -242,6 +242,10 @@ public class Column {
return precision; return precision;
} }
public void setPrecision(long p) {
precision = p;
}
public int getDisplaySize() { public int getDisplaySize() {
return displaySize; return displaySize;
} }
......
...@@ -40,6 +40,7 @@ public class TestAlter extends TestBase { ...@@ -40,6 +40,7 @@ public class TestAlter extends TestBase {
testAlterTableAlterColumn(); testAlterTableAlterColumn();
testAlterTableDropIdentityColumn(); testAlterTableDropIdentityColumn();
testAlterTableAddColumnIfNotExists(); testAlterTableAddColumnIfNotExists();
testAlterTableAlterColumn2();
conn.close(); conn.close();
deleteDb("alter"); deleteDb("alter");
} }
...@@ -135,4 +136,11 @@ public class TestAlter extends TestBase { ...@@ -135,4 +136,11 @@ public class TestAlter extends TestBase {
stat.execute("drop table t"); 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 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论