From f24dee95fe195ddee2f95956e9ec6d5841778bba Mon Sep 17 00:00:00 2001 From: Arenatlx <314806019@qq.com> Date: Wed, 2 Jun 2021 11:43:38 +0800 Subject: [PATCH] ddl: fix column type change won't cast the default value to new one (#25025) --- ddl/column_type_change_test.go | 30 ++++++++++++++++++++++++++++++ util/rowDecoder/decoder.go | 2 ++ 2 files changed, 32 insertions(+) diff --git a/ddl/column_type_change_test.go b/ddl/column_type_change_test.go index 32418dd031c29..7febe2209f62c 100644 --- a/ddl/column_type_change_test.go +++ b/ddl/column_type_change_test.go @@ -1883,3 +1883,33 @@ func (s *testColumnTypeChangeSuite) TestChangeIntToBitWillPanicInBackfillIndexes ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin")) tk.MustQuery("select * from t").Check(testkit.Rows("\x13 1 1.00", "\x11 2 2.00")) } + +// Close issue #24971, #24973, #24971 +func (s *testColumnTypeChangeSuite) TestCTCShouldCastTheDefaultValue(c *C) { + tk := testkit.NewTestKit(c, s.store) + tk.MustExec("use test") + // Enable column change variable. + tk.Se.GetSessionVars().EnableChangeColumnType = true + + tk.MustExec("drop table if exists t") + tk.MustExec("create table t(a int)") + tk.MustExec("insert into t values(1)") + tk.MustExec("alter table t add column b bit(51) default 1512687856625472") // virtual fill the column data + tk.MustGetErrCode("alter table t modify column b decimal(30,18)", mysql.ErrDataOutOfRange) // because 1512687856625472 is out of range. + + tk.MustExec("drop table if exists t") + tk.MustExec("create table tbl_1 (col int)") + tk.MustExec("insert into tbl_1 values (9790)") + tk.MustExec("alter table tbl_1 add column col1 blob(6) collate binary not null") + tk.MustQuery("select col1 from tbl_1").Check(testkit.Rows("")) + tk.MustGetErrCode("alter table tbl_1 change column col1 col2 int", mysql.ErrTruncatedWrongValue) + tk.MustQuery("select col1 from tbl_1").Check(testkit.Rows("")) + + tk.MustExec("drop table if exists t") + tk.MustExec("create table tbl(col_214 decimal(30,8))") + tk.MustExec("replace into tbl values (89687.448)") + tk.MustExec("alter table tbl add column col_279 binary(197) collate binary default 'RAWTdm' not null") + tk.MustQuery("select col_279 from tbl").Check(testkit.Rows("RAWTdm\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")) + tk.MustGetErrCode("alter table tbl change column col_279 col_287 int", mysql.ErrTruncatedWrongValue) + tk.MustQuery("select col_279 from tbl").Check(testkit.Rows("RAWTdm\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")) +} diff --git a/util/rowDecoder/decoder.go b/util/rowDecoder/decoder.go index 3fd63c05fae96..35413904fe3d8 100644 --- a/util/rowDecoder/decoder.go +++ b/util/rowDecoder/decoder.go @@ -164,6 +164,8 @@ func (rd *RowDecoder) DecodeTheExistedColumnMap(ctx sessionctx.Context, handle k if err != nil { return nil, err } + // Fill the default value into map. + row[colInfo.ID] = val rd.mutRow.SetValue(colInfo.Offset, val.GetValue()) } // return the existed column map here.