From 46cb6dc679959227135ad3def0762a8a6ac9cf28 Mon Sep 17 00:00:00 2001 From: Hangjie Mo Date: Thu, 4 Aug 2022 15:30:06 +0800 Subject: [PATCH] executor: fix column_default with timestamp and bit type (#36851) close pingcap/tidb#36027 --- executor/infoschema_reader.go | 17 +++++++++++++---- executor/infoschema_reader_test.go | 12 ++++++++++++ executor/show.go | 12 +++++++----- executor/showtest/show_test.go | 7 +++++++ 4 files changed, 39 insertions(+), 9 deletions(-) diff --git a/executor/infoschema_reader.go b/executor/infoschema_reader.go index eef554e77b..c026ed1f1e 100644 --- a/executor/infoschema_reader.go +++ b/executor/infoschema_reader.go @@ -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( diff --git a/executor/infoschema_reader_test.go b/executor/infoschema_reader_test.go index e2eb23122c..b19e8cefbc 100644 --- a/executor/infoschema_reader_test.go +++ b/executor/infoschema_reader_test.go @@ -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 10 0 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) { diff --git a/executor/show.go b/executor/show.go index 938d4f9d62..8f10e78127 100644 --- a/executor/show.go +++ b/executor/show.go @@ -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)) + } } } } diff --git a/executor/showtest/show_test.go b/executor/showtest/show_test.go index 22fa539175..b3bcf6a1c9 100644 --- a/executor/showtest/show_test.go +++ b/executor/showtest/show_test.go @@ -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) {