executor: fix column_default with timestamp and bit type (#36851)

close pingcap/tidb#36027
This commit is contained in:
Hangjie Mo
2022-08-04 15:30:06 +08:00
committed by GitHub
parent ca86fdd4be
commit 46cb6dc679
4 changed files with 39 additions and 9 deletions

View File

@ -939,10 +939,19 @@ ForColumnsTag:
var columnDefault interface{}
if columnDesc.DefaultValue != nil {
columnDefault = fmt.Sprintf("%v", columnDesc.DefaultValue)
if ft.GetType() == mysql.TypeBit {
defaultStr := fmt.Sprintf("%v", columnDesc.DefaultValue)
defaultValBinaryLiteral := types.BinaryLiteral(defaultStr)
columnDefault = defaultValBinaryLiteral.ToBitLiteralString(true)
switch col.GetDefaultValue() {
case "CURRENT_TIMESTAMP":
default:
if ft.GetType() == mysql.TypeTimestamp && columnDefault != types.ZeroDatetimeStr {
timeValue, err := table.GetColDefaultValue(sctx, col)
if err == nil {
columnDefault = timeValue.GetMysqlTime().String()
}
}
if ft.GetType() == mysql.TypeBit && !col.DefaultIsExpr {
defaultValBinaryLiteral := types.BinaryLiteral(columnDefault.(string))
columnDefault = defaultValBinaryLiteral.ToBitLiteralString(true)
}
}
}
record := types.MakeDatums(

View File

@ -153,6 +153,18 @@ func TestColumnsTables(t *testing.T) {
tk.MustQuery("SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 't'").Check(testkit.Rows(
"def test t bit 1 b'100' YES bit <nil> <nil> 10 0 <nil> <nil> <nil> bit(10) unsigned select,insert,update,references "))
tk.MustExec("drop table if exists t")
tk.MustExec("set time_zone='+08:00'")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t (b timestamp(3) NOT NULL DEFAULT '1970-01-01 08:00:01.000')")
tk.MustQuery("select column_default from information_schema.columns where TABLE_NAME='t' and TABLE_SCHEMA='test';").Check(testkit.Rows("1970-01-01 08:00:01.000"))
tk.MustExec("set time_zone='+04:00'")
tk.MustQuery("select column_default from information_schema.columns where TABLE_NAME='t' and TABLE_SCHEMA='test';").Check(testkit.Rows("1970-01-01 04:00:01.000"))
tk.MustExec("set time_zone=default")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t (a bit DEFAULT (rand()))")
tk.MustQuery("select column_default from information_schema.columns where TABLE_NAME='t' and TABLE_SCHEMA='test';").Check(testkit.Rows("rand()"))
}
func TestEngines(t *testing.T) {

View File

@ -1013,13 +1013,15 @@ func ConstructResultOfShowCreateTable(ctx sessionctx.Context, tableInfo *model.T
defaultValStr = timeValue.GetMysqlTime().String()
}
if col.GetType() == mysql.TypeBit {
defaultValBinaryLiteral := types.BinaryLiteral(defaultValStr)
fmt.Fprintf(buf, " DEFAULT %s", defaultValBinaryLiteral.ToBitLiteralString(true))
} else if col.DefaultIsExpr {
if col.DefaultIsExpr {
fmt.Fprintf(buf, " DEFAULT %s", format.OutputFormat(defaultValStr))
} else {
fmt.Fprintf(buf, " DEFAULT '%s'", format.OutputFormat(defaultValStr))
if col.GetType() == mysql.TypeBit {
defaultValBinaryLiteral := types.BinaryLiteral(defaultValStr)
fmt.Fprintf(buf, " DEFAULT %s", defaultValBinaryLiteral.ToBitLiteralString(true))
} else {
fmt.Fprintf(buf, " DEFAULT '%s'", format.OutputFormat(defaultValStr))
}
}
}
}

View File

@ -465,6 +465,13 @@ func TestShowCreateTable(t *testing.T) {
" `a` set('a','b') CHARACTER SET binary COLLATE binary DEFAULT NULL,\n"+
" `b` enum('a','b') CHARACTER SET ascii COLLATE ascii_bin DEFAULT NULL\n"+
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin"))
tk.MustExec(`drop table if exists t`)
tk.MustExec(`create table t(a bit default (rand()))`)
tk.MustQuery(`show create table t`).Check(testkit.RowsWithSep("|", ""+
"t CREATE TABLE `t` (\n"+
" `a` bit(1) DEFAULT rand()\n"+
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin"))
}
func TestShowCreateTablePlacement(t *testing.T) {