From 1a83dd90a2390b0b1d78e8f2a476f399ede8c160 Mon Sep 17 00:00:00 2001 From: lihaowei Date: Wed, 2 Jun 2021 20:52:32 +0800 Subject: [PATCH 1/4] virtual columns Signed-off-by: lihaowei --- ddl/db_test.go | 9 +++++++++ ddl/ddl_api.go | 7 +++++++ 2 files changed, 16 insertions(+) diff --git a/ddl/db_test.go b/ddl/db_test.go index 0bfc654941727..a54b8434c8e4d 100644 --- a/ddl/db_test.go +++ b/ddl/db_test.go @@ -3513,6 +3513,15 @@ out: tk.MustExec("drop table tnn") } +func (s *testDBSuite3) TestVirtualColumnDDL(c *C) { + tk := testkit.NewTestKit(c, s.store) + tk.MustExec("use test") + tk.MustExec("drop table if exists test_gv_ddl") + _, err := tk.Exec(`create global temporary table test_gv_ddl(a int, b int as (a+8) virtual, c int as (b + 2) stored) on commit delete rows;`) + c.Assert(err.Error(), Equals, ddl.ErrOptOnTemporaryTable.GenWithStackByArgs("virtual columns").Error()) + tk.MustExec("drop table if exists test_gv_ddl") +} + func (s *testDBSuite3) TestGeneratedColumnDDL(c *C) { tk := testkit.NewTestKit(c, s.store) tk.MustExec("use test") diff --git a/ddl/ddl_api.go b/ddl/ddl_api.go index 640823b23305a..4f611f5a54468 100644 --- a/ddl/ddl_api.go +++ b/ddl/ddl_api.go @@ -1814,6 +1814,13 @@ func (d *ddl) CreateTable(ctx sessionctx.Context, s *ast.CreateTableStmt) (err e if err != nil { return errors.Trace(err) } + if tbInfo.TempTableType != model.TempTableNone { + for _, column := range tbInfo.Columns { + if column.IsGenerated() && !column.GeneratedStored { + return ErrOptOnTemporaryTable.GenWithStackByArgs("virtual columns") + } + } + } if err = checkTableInfoValidWithStmt(ctx, tbInfo, s); err != nil { return err From 434bdd9824622b280d5a7c9af7ff12c0ecd22a30 Mon Sep 17 00:00:00 2001 From: lihaowei Date: Fri, 4 Jun 2021 14:51:06 +0800 Subject: [PATCH 2/4] add tests --- ddl/db_test.go | 20 +++++++++++++++++--- ddl/ddl_api.go | 7 ------- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/ddl/db_test.go b/ddl/db_test.go index a54b8434c8e4d..9b1024294034e 100644 --- a/ddl/db_test.go +++ b/ddl/db_test.go @@ -3517,9 +3517,23 @@ func (s *testDBSuite3) TestVirtualColumnDDL(c *C) { tk := testkit.NewTestKit(c, s.store) tk.MustExec("use test") tk.MustExec("drop table if exists test_gv_ddl") - _, err := tk.Exec(`create global temporary table test_gv_ddl(a int, b int as (a+8) virtual, c int as (b + 2) stored) on commit delete rows;`) - c.Assert(err.Error(), Equals, ddl.ErrOptOnTemporaryTable.GenWithStackByArgs("virtual columns").Error()) - tk.MustExec("drop table if exists test_gv_ddl") + tk.MustExec(`create global temporary table test_gv_ddl(a int, b int as (a+8) virtual, c int as (b + 2) stored) on commit delete rows;`) + defer tk.MustExec("drop table if exists test_gv_ddl") + is := tk.Se.(sessionctx.Context).GetInfoSchema().(infoschema.InfoSchema) + table, err := is.TableByName(model.NewCIStr("test"), model.NewCIStr("test_gv_ddl")) + c.Assert(err, IsNil) + testCases := []struct { + generatedExprString string + generatedStored bool + }{ + {"", false}, + {"`a` + 8", false}, + {"`b` + 2", true}, + } + for i, column := range table.Meta().Columns { + c.Assert(column.GeneratedExprString, Equals, testCases[i].generatedExprString) + c.Assert(column.GeneratedStored, Equals, testCases[i].generatedStored) + } } func (s *testDBSuite3) TestGeneratedColumnDDL(c *C) { diff --git a/ddl/ddl_api.go b/ddl/ddl_api.go index 4f611f5a54468..640823b23305a 100644 --- a/ddl/ddl_api.go +++ b/ddl/ddl_api.go @@ -1814,13 +1814,6 @@ func (d *ddl) CreateTable(ctx sessionctx.Context, s *ast.CreateTableStmt) (err e if err != nil { return errors.Trace(err) } - if tbInfo.TempTableType != model.TempTableNone { - for _, column := range tbInfo.Columns { - if column.IsGenerated() && !column.GeneratedStored { - return ErrOptOnTemporaryTable.GenWithStackByArgs("virtual columns") - } - } - } if err = checkTableInfoValidWithStmt(ctx, tbInfo, s); err != nil { return err From 9d40ce0b144f437a64db9d6f1505bf5910d2f1ec Mon Sep 17 00:00:00 2001 From: lihaowei Date: Fri, 4 Jun 2021 16:30:56 +0800 Subject: [PATCH 3/4] add test --- ddl/db_test.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/ddl/db_test.go b/ddl/db_test.go index 9b1024294034e..ad0da51f4e9a5 100644 --- a/ddl/db_test.go +++ b/ddl/db_test.go @@ -3534,6 +3534,13 @@ func (s *testDBSuite3) TestVirtualColumnDDL(c *C) { c.Assert(column.GeneratedExprString, Equals, testCases[i].generatedExprString) c.Assert(column.GeneratedStored, Equals, testCases[i].generatedStored) } + result := tk.MustQuery(`DESC test_gv_ddl`) + result.Check(testkit.Rows(`a int(11) YES `, `b int(11) YES VIRTUAL GENERATED`, `c int(11) YES STORED GENERATED`)) + tk.MustExec("begin;") + tk.MustExec("insert into test_gv_ddl values (1, default, default)") + tk.MustQuery("select * from test_gv_ddl").Check(testkit.Rows("1 9 11")) + _, err = tk.Exec("commit") + c.Assert(err, IsNil) } func (s *testDBSuite3) TestGeneratedColumnDDL(c *C) { From c0449e3c4cd17c556336fea7749933e307435045 Mon Sep 17 00:00:00 2001 From: lihaowei Date: Fri, 4 Jun 2021 17:31:16 +0800 Subject: [PATCH 4/4] open temp mode --- ddl/db_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/ddl/db_test.go b/ddl/db_test.go index ad0da51f4e9a5..4f1e189a86bba 100644 --- a/ddl/db_test.go +++ b/ddl/db_test.go @@ -3515,6 +3515,7 @@ out: func (s *testDBSuite3) TestVirtualColumnDDL(c *C) { tk := testkit.NewTestKit(c, s.store) + tk.MustExec("set tidb_enable_global_temporary_table=true") tk.MustExec("use test") tk.MustExec("drop table if exists test_gv_ddl") tk.MustExec(`create global temporary table test_gv_ddl(a int, b int as (a+8) virtual, c int as (b + 2) stored) on commit delete rows;`)