提交 c34aff38 authored 作者: Thomas Mueller's avatar Thomas Mueller

ALTER TABLE ALTER COLUMN no longer copies the data for widening conversions (for…

ALTER TABLE ALTER COLUMN no longer copies the data for widening conversions (for example if only the precision was increased) unless necessary.
上级 3733388a
...@@ -32,7 +32,6 @@ import org.h2.table.Column; ...@@ -32,7 +32,6 @@ 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
...@@ -113,20 +112,14 @@ public class AlterTableAlterColumn extends SchemaCommand { ...@@ -113,20 +112,14 @@ public class AlterTableAlterColumn extends SchemaCommand {
break; break;
} }
case CommandInterface.ALTER_TABLE_ALTER_COLUMN_CHANGE_TYPE: { 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 // if the change is only increasing the precision, then we don't
// because the length is only a constraint, and does not affect the storage structure. // need to copy the table because the length is only a constraint,
if ((oldColumn.getType() == Value.STRING || oldColumn.getType() == Value.STRING_IGNORECASE) // and does not affect the storage structure.
&& oldColumn.getPrecision() <= newColumn.getPrecision() if (oldColumn.isWideningConversion(newColumn)) {
&& oldColumn.isNullable() == newColumn.isNullable() convertAutoIncrementColumn(newColumn);
&& oldColumn.isAutoIncrement() == newColumn.isAutoIncrement() oldColumn.copy(newColumn);
&& oldColumn.isPrimaryKey() == newColumn.isPrimaryKey()
&& oldColumn.getType() == newColumn.getType())
{
oldColumn.setPrecision(newColumn.getPrecision());
db.update(session, table); db.update(session, table);
} } else {
else
{
oldColumn.setSequence(null); oldColumn.setSequence(null);
oldColumn.setDefaultExpression(session, null); oldColumn.setDefaultExpression(session, null);
oldColumn.setConvertNullToDefault(false); oldColumn.setConvertNullToDefault(false);
......
...@@ -63,8 +63,8 @@ public class Column { ...@@ -63,8 +63,8 @@ public class Column {
private final int type; private final int type;
private long precision; private long precision;
private final int scale; private int scale;
private final int displaySize; private int displaySize;
private Table table; private Table table;
private String name; private String name;
private int columnId; private int columnId;
...@@ -128,19 +128,7 @@ public class Column { ...@@ -128,19 +128,7 @@ public class Column {
public Column getClone() { public Column getClone() {
Column newColumn = new Column(name, type, precision, scale, displaySize); Column newColumn = new Column(name, type, precision, scale, displaySize);
// table is not set newColumn.copy(this);
// columnId is not set
newColumn.nullable = nullable;
newColumn.defaultExpression = defaultExpression;
newColumn.originalSQL = originalSQL;
// autoIncrement, start, increment is not set
newColumn.convertNullToDefault = convertNullToDefault;
newColumn.sequence = sequence;
newColumn.comment = comment;
newColumn.computeTableFilter = computeTableFilter;
newColumn.isComputed = isComputed;
newColumn.selectivity = selectivity;
newColumn.primaryKey = primaryKey;
return newColumn; return newColumn;
} }
...@@ -658,4 +646,69 @@ public class Column { ...@@ -658,4 +646,69 @@ public class Column {
return name; return name;
} }
/**
* Check whether the new column is of the same type and not more restricted
* than this column.
*
* @return true if the new column is compatible
*/
public boolean isWideningConversion(Column newColumn) {
if (type != newColumn.type) {
return false;
}
if (precision > newColumn.precision) {
return false;
}
if (scale != newColumn.scale) {
return false;
}
if (nullable && !newColumn.nullable) {
return false;
}
if (convertNullToDefault != newColumn.convertNullToDefault) {
return false;
}
if (primaryKey != newColumn.primaryKey) {
return false;
}
if (autoIncrement || newColumn.autoIncrement) {
return false;
}
if (checkConstraint != null || newColumn.checkConstraint != null) {
return false;
}
if (convertNullToDefault || newColumn.convertNullToDefault) {
return false;
}
if (defaultExpression != null || newColumn.defaultExpression != null) {
return false;
}
if (isComputed || newColumn.isComputed) {
return false;
}
return true;
}
public void copy(Column newColumn) {
checkConstraint = newColumn.checkConstraint;
checkConstraintSQL = newColumn.checkConstraintSQL;
displaySize = newColumn.displaySize;
name = newColumn.name;
precision = newColumn.precision;
scale = newColumn.scale;
// table is not set
// columnId is not set
nullable = newColumn.nullable;
defaultExpression = newColumn.defaultExpression;
originalSQL = newColumn.originalSQL;
// autoIncrement, start, increment is not set
convertNullToDefault = newColumn.convertNullToDefault;
sequence = newColumn.sequence;
comment = newColumn.comment;
computeTableFilter = newColumn.computeTableFilter;
isComputed = newColumn.isComputed;
selectivity = newColumn.selectivity;
primaryKey = newColumn.primaryKey;
}
} }
...@@ -143,4 +143,5 @@ public class TestAlter extends TestBase { ...@@ -143,4 +143,5 @@ public class TestAlter extends TestBase {
stat.execute("insert into t values('Hello')"); stat.execute("insert into t values('Hello')");
stat.execute("drop table t"); stat.execute("drop table t");
} }
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论