From b765ad00401c5ccfe043582c4b5038be5c9a2bc6 Mon Sep 17 00:00:00 2001 From: tiancaiamao Date: Wed, 22 Dec 2021 22:26:55 +0800 Subject: [PATCH] executor: display 'show create table' and INFOSCHEMA for cached table correctly --- executor/infoschema_reader.go | 2 ++ executor/show.go | 6 ++++++ executor/show_test.go | 21 +++++++++++++++++++++ 3 files changed, 29 insertions(+) diff --git a/executor/infoschema_reader.go b/executor/infoschema_reader.go index 06d35d24fce1d..b5eee15e32600 100644 --- a/executor/infoschema_reader.go +++ b/executor/infoschema_reader.go @@ -543,6 +543,8 @@ func (e *memtableRetriever) setDataFromTables(ctx context.Context, sctx sessionc if !table.IsView() { if table.GetPartitionInfo() != nil { createOptions = "partitioned" + } else if table.TableCacheStatusType == model.TableCacheStatusEnable { + createOptions = "cached=on" } var autoIncID interface{} hasAutoIncID, _ := infoschema.HasAutoIncrementColumn(table) diff --git a/executor/show.go b/executor/show.go index ea4bdf4f1aee8..066d819cc9b80 100644 --- a/executor/show.go +++ b/executor/show.go @@ -1077,6 +1077,12 @@ func ConstructResultOfShowCreateTable(ctx sessionctx.Context, tableInfo *model.T fmt.Fprintf(buf, " /*T![placement] PLACEMENT POLICY=%s */", stringutil.Escape(tableInfo.PlacementPolicyRef.Name.String(), sqlMode)) } + if tableInfo.TableCacheStatusType == model.TableCacheStatusEnable { + // This is not meant to be understand by other components, so it's not written as /*T![cached] */ + // For all external components, cached table is just a normal table. + fmt.Fprintf(buf, " /* CACHED ON */") + } + // add direct placement info here appendDirectPlacementInfo(tableInfo.DirectPlacementOpts, buf) // add partition info here. diff --git a/executor/show_test.go b/executor/show_test.go index 34db4d54ae962..229d4399dae4d 100644 --- a/executor/show_test.go +++ b/executor/show_test.go @@ -1674,3 +1674,24 @@ func (s *testSuite5) TestShowTemporaryTable(c *C) { ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin AUTO_INCREMENT=2" tk.MustQuery("show create table t7").Check(testkit.Rows("t7 " + expect)) } + +func (s *testSuite5) TestShowCachedTable(c *C) { + tk := testkit.NewTestKit(c, s.store) + tk.MustExec("use test") + tk.MustExec("create table t1 (id int)") + tk.MustExec("alter table t1 cache") + tk.MustQuery("show create table t1").Check( + testkit.Rows("t1 CREATE TABLE `t1` (\n" + + " `id` int(11) DEFAULT NULL\n" + + ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin /* CACHED ON */")) + tk.MustQuery("select create_options from information_schema.tables where table_schema = 'test' and table_name = 't1'").Check( + testkit.Rows("cached=on")) + + tk.MustExec("alter table t1 nocache") + tk.MustQuery("show create table t1").Check( + testkit.Rows("t1 CREATE TABLE `t1` (\n" + + " `id` int(11) DEFAULT NULL\n" + + ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin")) + tk.MustQuery("select create_options from information_schema.tables where table_schema = 'test' and table_name = 't1'").Check( + testkit.Rows("")) +}