Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ddl: fix alter column drop default (#35072) #35176

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 65 additions & 2 deletions ddl/db_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1653,8 +1653,8 @@ func (s *testIntegrationSuite3) TestAlterColumn(c *C) {
// TODO: After fix issue 2606.
// tk.MustExec( "alter table test_alter_column alter column d set default null")
tk.MustExec("alter table test_alter_column alter column a drop default")
tk.MustExec("insert into test_alter_column set b = 'd', c = 'dd'")
tk.MustQuery("select a from test_alter_column").Check(testkit.Rows("111", "222", "222", "123", "<nil>"))
tk.MustGetErrCode("insert into test_alter_column set b = 'd', c = 'dd'", errno.ErrNoDefaultForField)
tk.MustQuery("select a from test_alter_column").Check(testkit.Rows("111", "222", "222", "123"))

// for failing tests
sql := "alter table db_not_exist.test_alter_column alter column b set default 'c'"
Expand Down Expand Up @@ -1739,7 +1739,70 @@ func (s *testIntegrationSuite3) TestAlterColumn(c *C) {
updateTime3 := rows[0][1].(string)
c.Assert(updateTime3[len(updateTime3)-3:], Not(Equals), "000")
updateTime6 := rows[0][2].(string)
<<<<<<< HEAD
c.Assert(updateTime6[len(updateTime6)-6:], Not(Equals), "000000")
=======
require.NotEqual(t, "000000", updateTime6[len(updateTime6)-6:])

tk.MustExec("set sql_mode=default")
tk.MustExec("drop table if exists t")
tk.MustExec("CREATE TABLE `t` (`a` int auto_increment, b int, key i(a))")
tk.MustExec("alter table t alter column a drop default")
tk.MustGetErrCode("insert into t values ()", errno.ErrNoDefaultForField)
tk.MustExec("insert into t values (1, a + 1)")

tk.MustExec("drop table if exists t")
tk.MustExec("CREATE TABLE `t` (`a` int)")
tk.MustExec("alter table t alter column a drop default")
tk.MustGetErrCode("insert into t values ()", errno.ErrNoDefaultForField)

tk.MustExec("drop table if exists t")
tk.MustExec("CREATE TABLE `t` (`a` enum('a', 'b'))")
tk.MustExec("alter table t alter column a drop default")
tk.MustExec("insert into t values ()")
tk.MustQuery("select * from t").Check(testkit.Rows("<nil>"))

tk.MustExec("drop table if exists t")
tk.MustExec("CREATE TABLE `t` (`a` enum('a', 'b') not null)")
tk.MustExec("alter table t alter column a drop default")
tk.MustExec("insert into t values ()")
tk.MustQuery("select * from t").Check(testkit.Rows("a"))

tk.MustExec("set sql_mode=''")

tk.MustExec("drop table if exists t")
tk.MustExec("CREATE TABLE `t` (`a` int auto_increment, key i(a))")
tk.MustExec("alter table t alter column a drop default")
tk.MustExec("insert into t values ()")
tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1364 Field 'a' doesn't have a default value"))
tk.MustQuery("select * from t").Check(testkit.Rows("1"))

tk.MustExec("drop table if exists t")
tk.MustExec("CREATE TABLE `t` (`a` int)")
tk.MustExec("alter table t alter column a drop default")
tk.MustExec("insert into t values ()")
tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1364 Field 'a' doesn't have a default value"))
tk.MustQuery("select * from t").Check(testkit.Rows("<nil>"))

tk.MustExec("drop table if exists t")
tk.MustExec("CREATE TABLE `t` (`a` int not null)")
tk.MustExec("alter table t alter column a drop default")
tk.MustExec("insert into t values ()")
tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1364 Field 'a' doesn't have a default value"))
tk.MustQuery("select * from t").Check(testkit.Rows("0"))

tk.MustExec("drop table if exists t")
tk.MustExec("CREATE TABLE `t` (`a` enum('a', 'b'))")
tk.MustExec("alter table t alter column a drop default")
tk.MustExec("insert into t values ()")
tk.MustQuery("select * from t").Check(testkit.Rows("<nil>"))

tk.MustExec("drop table if exists t")
tk.MustExec("CREATE TABLE `t` (`a` enum('a', 'b') not null)")
tk.MustExec("alter table t alter column a drop default")
tk.MustExec("insert into t values ()")
tk.MustQuery("select * from t").Check(testkit.Rows("a"))
>>>>>>> cc46266e4... ddl: fix `alter column drop default` (#35072)
}

func (s *testIntegrationSuite) assertWarningExec(tk *testkit.TestKit, c *C, sql string, expectedWarn *terror.Error) {
Expand Down
3 changes: 2 additions & 1 deletion ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -4570,11 +4570,12 @@ func (d *ddl) AlterColumn(ctx sessionctx.Context, ident ast.Ident, spec *ast.Alt
// Clean the NoDefaultValueFlag value.
col.Flag &= ^mysql.NoDefaultValueFlag
if len(specNewColumn.Options) == 0 {
col.DefaultIsExpr = false
err = col.SetDefaultValue(nil)
if err != nil {
return errors.Trace(err)
}
setNoDefaultValueFlag(col, false)
col.AddFlag(mysql.NoDefaultValueFlag)
} else {
if IsAutoRandomColumnID(t.Meta(), col.ID) {
return ErrInvalidAutoRandom.GenWithStackByArgs(autoid.AutoRandomIncompatibleWithDefaultValueErrMsg)
Expand Down
14 changes: 14 additions & 0 deletions executor/insert_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,21 @@ func (e *InsertValues) getColDefaultValue(idx int, col *table.Column) (d types.D
// fillColValue fills the column value if it is not set in the insert statement.
func (e *InsertValues) fillColValue(ctx context.Context, datum types.Datum, idx int, column *table.Column, hasValue bool) (types.Datum,
error) {
<<<<<<< HEAD
if mysql.HasAutoIncrementFlag(column.Flag) {
=======
if mysql.HasAutoIncrementFlag(column.GetFlag()) {
if !hasValue && mysql.HasNoDefaultValueFlag(column.ToInfo().GetFlag()) {
vars := e.ctx.GetSessionVars()
sc := vars.StmtCtx
if !vars.StrictSQLMode {
sc.AppendWarning(table.ErrNoDefaultValue.FastGenByArgs(column.ToInfo().Name))
} else {
return datum, table.ErrNoDefaultValue.FastGenByArgs(column.ToInfo().Name)
}
}

>>>>>>> cc46266e4... ddl: fix `alter column drop default` (#35072)
if e.lazyFillAutoID {
// Handle hasValue info in autoIncrement column previously for lazy handle.
if !hasValue {
Expand Down
25 changes: 24 additions & 1 deletion table/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -616,12 +616,17 @@ func getColDefaultValue(ctx sessionctx.Context, col *model.ColumnInfo, defaultVa
}

func getColDefaultValueFromNil(ctx sessionctx.Context, col *model.ColumnInfo) (types.Datum, error) {
<<<<<<< HEAD
if !mysql.HasNotNullFlag(col.Flag) {
=======
if !mysql.HasNotNullFlag(col.GetFlag()) && !mysql.HasNoDefaultValueFlag(col.GetFlag()) {
>>>>>>> cc46266e4... ddl: fix `alter column drop default` (#35072)
return types.Datum{}, nil
}
if col.Tp == mysql.TypeEnum {
// For enum type, if no default value and not null is set,
// the default value is the first element of the enum list
<<<<<<< HEAD
defEnum, err := types.ParseEnumValue(col.FieldType.Elems, 1)
if err != nil {
return types.Datum{}, err
Expand All @@ -630,13 +635,31 @@ func getColDefaultValueFromNil(ctx sessionctx.Context, col *model.ColumnInfo) (t
}
if mysql.HasAutoIncrementFlag(col.Flag) {
// Auto increment column doesn't has default value and we should not return error.
=======
if mysql.HasNotNullFlag(col.GetFlag()) {
defEnum, err := types.ParseEnumValue(col.FieldType.GetElems(), 1)
if err != nil {
return types.Datum{}, err
}
return types.NewCollateMysqlEnumDatum(defEnum, col.GetCollate()), nil
}
return types.Datum{}, nil
}
if mysql.HasAutoIncrementFlag(col.GetFlag()) && !mysql.HasNoDefaultValueFlag(col.GetFlag()) {
// Auto increment column doesn't have default value and we should not return error.
>>>>>>> cc46266e4... ddl: fix `alter column drop default` (#35072)
return GetZeroValue(col), nil
}
vars := ctx.GetSessionVars()
sc := vars.StmtCtx
if !vars.StrictSQLMode {
sc.AppendWarning(ErrNoDefaultValue.FastGenByArgs(col.Name))
return GetZeroValue(col), nil
if mysql.HasNotNullFlag(col.GetFlag()) {
return GetZeroValue(col), nil
}
if mysql.HasNoDefaultValueFlag(col.GetFlag()) {
return types.Datum{}, nil
}
}
if sc.BadNullAsWarning {
sc.AppendWarning(ErrColumnCantNull.FastGenByArgs(col.Name))
Expand Down