From 27db39b4807809b65c39c66f72799571eb89a138 Mon Sep 17 00:00:00 2001 From: tangenta Date: Fri, 5 Feb 2021 20:41:41 +0800 Subject: [PATCH] *: use CLUSTERED and NONCLUSTERED to control primary key type (#22409) --- cmd/ddltest/ddl_test.go | 4 +-- cmd/explaintest/main.go | 5 ++- ddl/db_integration_test.go | 12 +++---- ddl/db_test.go | 12 +++---- ddl/ddl_api.go | 43 +++++++++++++++--------- ddl/failtest/fail_db_test.go | 6 ++-- ddl/serial_test.go | 8 ++--- domain/global_vars_cache.go | 5 +-- executor/admin_test.go | 8 ++--- executor/aggregate_test.go | 2 +- executor/analyze_test.go | 2 +- executor/batch_point_get_test.go | 2 +- executor/clustered_index_test.go | 43 +++++++++++++++++++++++- executor/delete_test.go | 2 +- executor/executor_test.go | 14 ++++---- executor/explainfor_test.go | 2 +- executor/infoschema_reader.go | 6 ++-- executor/infoschema_reader_test.go | 8 ++--- executor/insert_test.go | 16 ++++----- executor/join_test.go | 2 +- executor/parallel_apply_test.go | 4 +-- executor/point_get_test.go | 8 ++--- executor/prepared_test.go | 4 +-- executor/rowid_test.go | 2 +- executor/sample_test.go | 4 +-- executor/seqtest/seq_executor_test.go | 16 ++++----- executor/show.go | 8 +++++ executor/show_test.go | 32 +++++++++--------- executor/update_test.go | 8 ++--- executor/write_test.go | 2 +- expression/integration_test.go | 10 +++--- infoschema/tables_test.go | 2 +- planner/core/cbo_test.go | 2 +- planner/core/integration_test.go | 14 ++++---- planner/core/partition_pruner_test.go | 4 +-- planner/core/point_get_plan_test.go | 6 ++-- server/conn_test.go | 2 +- server/http_handler_test.go | 4 +-- sessionctx/binloginfo/binloginfo_test.go | 14 +++++++- statistics/selectivity_test.go | 4 +-- table/tables/tables_test.go | 4 +-- types/parser_driver/special_cmt_ctrl.go | 4 +++ util/admin/admin_integration_test.go | 2 +- util/ranger/ranger_test.go | 6 ++-- 44 files changed, 223 insertions(+), 145 deletions(-) diff --git a/cmd/ddltest/ddl_test.go b/cmd/ddltest/ddl_test.go index 3d3a6bb1e0a67..11812c6a04de4 100644 --- a/cmd/ddltest/ddl_test.go +++ b/cmd/ddltest/ddl_test.go @@ -532,7 +532,7 @@ func (s *TestDDLSuite) Bootstrap(c *C) { tk.MustExec("create table test_mixed (c1 int, c2 int, primary key(c1))") tk.MustExec("create table test_inc (c1 int, c2 int, primary key(c1))") - tk.MustExec("set @@tidb_enable_clustered_index = 1") + tk.Se.GetSessionVars().EnableClusteredIndex = true tk.MustExec("drop table if exists test_insert_common, test_conflict_insert_common, " + "test_update_common, test_conflict_update_common, test_delete_common, test_conflict_delete_common, " + "test_mixed_common, test_inc_common") @@ -544,7 +544,7 @@ func (s *TestDDLSuite) Bootstrap(c *C) { tk.MustExec("create table test_conflict_delete_common (c1 int, c2 int, primary key(c1, c2))") tk.MustExec("create table test_mixed_common (c1 int, c2 int, primary key(c1, c2))") tk.MustExec("create table test_inc_common (c1 int, c2 int, primary key(c1, c2))") - tk.MustExec("set @@tidb_enable_clustered_index = 0") + tk.Se.GetSessionVars().EnableClusteredIndex = false } func (s *TestDDLSuite) TestSimple(c *C) { diff --git a/cmd/explaintest/main.go b/cmd/explaintest/main.go index d632ac4973141..3fff10a5f8415 100644 --- a/cmd/explaintest/main.go +++ b/cmd/explaintest/main.go @@ -30,6 +30,7 @@ import ( "github.com/pingcap/errors" "github.com/pingcap/log" "github.com/pingcap/parser/ast" + "github.com/pingcap/tidb/domain" "github.com/pingcap/tidb/session" "github.com/pingcap/tidb/sessionctx" "github.com/pingcap/tidb/util/logutil" @@ -656,13 +657,15 @@ func main() { "set @@tidb_window_concurrency=4", "set @@tidb_projection_concurrency=4", "set @@tidb_distsql_scan_concurrency=15", - "set @@tidb_enable_clustered_index=0;", + "set @@global.tidb_enable_clustered_index=0;", } for _, sql := range resets { if _, err = mdb.Exec(sql); err != nil { log.Fatal(fmt.Sprintf("%s failed", sql), zap.Error(err)) } } + // Wait global variables to reload. + time.Sleep(domain.GlobalVariableCacheExpiry) if _, err = mdb.Exec("set sql_mode='STRICT_TRANS_TABLES'"); err != nil { log.Fatal("set sql_mode='STRICT_TRANS_TABLES' failed", zap.Error(err)) diff --git a/ddl/db_integration_test.go b/ddl/db_integration_test.go index 96490aa8c719d..9fc379ad6dd86 100644 --- a/ddl/db_integration_test.go +++ b/ddl/db_integration_test.go @@ -1577,7 +1577,7 @@ func (s *testIntegrationSuite3) TestAlterColumn(c *C) { c.Assert(err, NotNil) result := tk.MustQuery("show create table mc") createSQL := result.Rows()[0][1] - expected := "CREATE TABLE `mc` (\n `a` int(11) NOT NULL,\n `b` int(11) DEFAULT NULL,\n `c` int(11) DEFAULT NULL,\n PRIMARY KEY (`a`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin" + expected := "CREATE TABLE `mc` (\n `a` int(11) NOT NULL,\n `b` int(11) DEFAULT NULL,\n `c` int(11) DEFAULT NULL,\n PRIMARY KEY (`a`) /*T![clustered_index] NONCLUSTERED */\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin" c.Assert(createSQL, Equals, expected) // Change / modify column should preserve index options. @@ -1588,7 +1588,7 @@ func (s *testIntegrationSuite3) TestAlterColumn(c *C) { tk.MustExec("alter table mc modify column c bigint") // Unique should be preserved result = tk.MustQuery("show create table mc") createSQL = result.Rows()[0][1] - expected = "CREATE TABLE `mc` (\n `a` bigint(20) NOT NULL,\n `b` bigint(20) DEFAULT NULL,\n `c` bigint(20) DEFAULT NULL,\n PRIMARY KEY (`a`),\n UNIQUE KEY `c` (`c`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin" + expected = "CREATE TABLE `mc` (\n `a` bigint(20) NOT NULL,\n `b` bigint(20) DEFAULT NULL,\n `c` bigint(20) DEFAULT NULL,\n PRIMARY KEY (`a`) /*T![clustered_index] NONCLUSTERED */,\n UNIQUE KEY `c` (`c`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin" c.Assert(createSQL, Equals, expected) // Dropping or keeping auto_increment is allowed, however adding is not allowed. @@ -1597,7 +1597,7 @@ func (s *testIntegrationSuite3) TestAlterColumn(c *C) { tk.MustExec("alter table mc modify column a bigint auto_increment") // Keeps auto_increment result = tk.MustQuery("show create table mc") createSQL = result.Rows()[0][1] - expected = "CREATE TABLE `mc` (\n `a` bigint(20) NOT NULL AUTO_INCREMENT,\n `b` int(11) DEFAULT NULL,\n PRIMARY KEY (`a`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin" + expected = "CREATE TABLE `mc` (\n `a` bigint(20) NOT NULL AUTO_INCREMENT,\n `b` int(11) DEFAULT NULL,\n PRIMARY KEY (`a`) /*T![clustered_index] NONCLUSTERED */\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin" c.Assert(createSQL, Equals, expected) _, err = tk.Exec("alter table mc modify column a bigint") // Droppping auto_increment is not allow when @@tidb_allow_remove_auto_inc == 'off' c.Assert(err, NotNil) @@ -1605,7 +1605,7 @@ func (s *testIntegrationSuite3) TestAlterColumn(c *C) { tk.MustExec("alter table mc modify column a bigint") // Dropping auto_increment is ok when @@tidb_allow_remove_auto_inc == 'on' result = tk.MustQuery("show create table mc") createSQL = result.Rows()[0][1] - expected = "CREATE TABLE `mc` (\n `a` bigint(20) NOT NULL,\n `b` int(11) DEFAULT NULL,\n PRIMARY KEY (`a`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin" + expected = "CREATE TABLE `mc` (\n `a` bigint(20) NOT NULL,\n `b` int(11) DEFAULT NULL,\n PRIMARY KEY (`a`) /*T![clustered_index] NONCLUSTERED */\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin" c.Assert(createSQL, Equals, expected) _, err = tk.Exec("alter table mc modify column a bigint auto_increment") // Adds auto_increment should throw error @@ -2639,8 +2639,8 @@ func (s *testIntegrationSuite7) TestDuplicateErrorMessage(c *C) { config.UpdateGlobal(func(conf *config.Config) { conf.EnableGlobalIndex = globalIndex }) - for _, clusteredIndex := range []int{0, 1} { - tk.MustExec(fmt.Sprintf("set session tidb_enable_clustered_index=%d;", clusteredIndex)) + for _, clusteredIndex := range []bool{false, true} { + tk.Se.GetSessionVars().EnableClusteredIndex = clusteredIndex for _, t := range tests { tk.MustExec("drop table if exists t;") fields := make([]string, len(t.types)) diff --git a/ddl/db_test.go b/ddl/db_test.go index 470e9447a0db1..1989e6129f934 100644 --- a/ddl/db_test.go +++ b/ddl/db_test.go @@ -180,7 +180,7 @@ func (s *testSerialDBSuite) TestAddIndexWithPK(c *C) { }) testAddIndexWithPK(tk, s, c) - tk.MustExec("set @@tidb_enable_clustered_index = 1;") + tk.Se.GetSessionVars().EnableClusteredIndex = true testAddIndexWithPK(tk, s, c) } @@ -1058,7 +1058,7 @@ func (s *testDBSuite6) TestAddMultiColumnsIndexClusterIndex(c *C) { tk.MustExec("create database test_add_multi_col_index_clustered;") tk.MustExec("use test_add_multi_col_index_clustered;") - tk.MustExec("set @@tidb_enable_clustered_index = 1") + tk.Se.GetSessionVars().EnableClusteredIndex = true tk.MustExec("create table t (a int, b varchar(10), c int, primary key (a, b));") tk.MustExec("insert into t values (1, '1', 1), (2, '2', NULL), (3, '3', 3);") tk.MustExec("create index idx on t (a, c);") @@ -1158,7 +1158,7 @@ func testAddIndex(c *C, store kv.Storage, lease time.Duration, tp testAddIndexTy case testPartition: tk.MustExec("set @@session.tidb_enable_table_partition = '1';") case testClusteredIndex: - tk.MustExec("set @@tidb_enable_clustered_index = 1") + tk.Se.GetSessionVars().EnableClusteredIndex = true } tk.MustExec("drop table if exists test_add_index") tk.MustExec(createTableSQL) @@ -1794,7 +1794,7 @@ func (s *testDBSuite5) TestAlterPrimaryKey(c *C) { " `a` int(11) NOT NULL,\n"+ " `b` int(11) NOT NULL,\n"+ " KEY `idx` (`a`),\n"+ - " PRIMARY KEY (`a`,`b`)\n"+ + " PRIMARY KEY (`a`,`b`) /*T![clustered_index] NONCLUSTERED */\n"+ ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin")) tk.MustExec("alter table test_add_pk2 drop primary key") tk.MustQuery("desc test_add_pk2").Check(testutil.RowsWithSep(",", ""+ @@ -2794,7 +2794,7 @@ func (s *testSerialDBSuite) TestRepairTable(c *C) { // Exec the show create table statement to make sure new tableInfo has been set. result := tk.MustQuery("show create table origin") - c.Assert(result.Rows()[0][1], Equals, "CREATE TABLE `origin` (\n `a` int(11) NOT NULL AUTO_INCREMENT,\n `b` varchar(5) DEFAULT NULL,\n `c` int(11) DEFAULT NULL,\n PRIMARY KEY (`a`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin") + c.Assert(result.Rows()[0][1], Equals, "CREATE TABLE `origin` (\n `a` int(11) NOT NULL AUTO_INCREMENT,\n `b` varchar(5) DEFAULT NULL,\n `c` int(11) DEFAULT NULL,\n PRIMARY KEY (`a`) /*T![clustered_index] NONCLUSTERED */\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin") } @@ -4529,7 +4529,7 @@ func (s *testSerialDBSuite) TestAddIndexForGeneratedColumn(c *C) { }) testAddIndexForGeneratedColumn(tk, s, c) - tk.MustExec("set @@tidb_enable_clustered_index = 1;") + tk.Se.GetSessionVars().EnableClusteredIndex = true testAddIndexForGeneratedColumn(tk, s, c) } diff --git a/ddl/ddl_api.go b/ddl/ddl_api.go index d27cb3213156f..35cfbd0a082a8 100644 --- a/ddl/ddl_api.go +++ b/ddl/ddl_api.go @@ -628,7 +628,8 @@ func columnDefToCol(ctx sessionctx.Context, offset int, colDef *ast.ColumnDef, o case ast.ColumnOptionPrimaryKey: // Check PriKeyFlag first to avoid extra duplicate constraints. if col.Flag&mysql.PriKeyFlag == 0 { - constraint := &ast.Constraint{Tp: ast.ConstraintPrimaryKey, Keys: keys} + constraint := &ast.Constraint{Tp: ast.ConstraintPrimaryKey, Keys: keys, + Option: &ast.IndexOption{PrimaryKeyTp: v.PrimaryKeyTp}} constraints = append(constraints, constraint) col.Flag |= mysql.PriKeyFlag // Add NotNullFlag early so that processColumnFlags() can see it. @@ -1424,23 +1425,35 @@ func buildTableInfo( if err != nil { return nil, err } - if !config.GetGlobalConfig().AlterPrimaryKey { - singleIntPK := isSingleIntPK(constr, lastCol) - clusteredIdx := ctx.GetSessionVars().EnableClusteredIndex - if singleIntPK || clusteredIdx { - // Primary key cannot be invisible. - if constr.Option != nil && constr.Option.Visibility == ast.IndexVisibilityInvisible { - return nil, ErrPKIndexCantBeInvisible - } - } - if singleIntPK { + pkTp := model.PrimaryKeyTypeDefault + if constr.Option != nil { + pkTp = constr.Option.PrimaryKeyTp + } + switch pkTp { + case model.PrimaryKeyTypeNonClustered: + break + case model.PrimaryKeyTypeClustered: + if isSingleIntPK(constr, lastCol) { tbInfo.PKIsHandle = true - // Avoid creating index for PK handle column. - continue - } - if clusteredIdx { + } else { tbInfo.IsCommonHandle = true } + case model.PrimaryKeyTypeDefault: + alterPKConf := config.GetGlobalConfig().AlterPrimaryKey + if isSingleIntPK(constr, lastCol) { + tbInfo.PKIsHandle = !alterPKConf + } else { + tbInfo.IsCommonHandle = !alterPKConf && ctx.GetSessionVars().EnableClusteredIndex + } + } + if tbInfo.PKIsHandle || tbInfo.IsCommonHandle { + // Primary key cannot be invisible. + if constr.Option != nil && constr.Option.Visibility == ast.IndexVisibilityInvisible { + return nil, ErrPKIndexCantBeInvisible + } + } + if tbInfo.PKIsHandle { + continue } } diff --git a/ddl/failtest/fail_db_test.go b/ddl/failtest/fail_db_test.go index 100604baee501..2d08063e1d7a0 100644 --- a/ddl/failtest/fail_db_test.go +++ b/ddl/failtest/fail_db_test.go @@ -351,7 +351,7 @@ func (s *testFailDBSuite) TestAddIndexWorkerNum(c *C) { tk.MustExec("use test_db") tk.MustExec("drop table if exists test_add_index") if s.IsCommonHandle { - tk.MustExec("set @@tidb_enable_clustered_index = 1") + tk.Se.GetSessionVars().EnableClusteredIndex = true tk.MustExec("create table test_add_index (c1 bigint, c2 bigint, c3 bigint, primary key(c1, c3))") } else { tk.MustExec("create table test_add_index (c1 bigint, c2 bigint, c3 bigint, primary key(c1))") @@ -485,7 +485,7 @@ func (s *testFailDBSuite) TestModifyColumn(c *C) { " `bb` mediumint(9) DEFAULT NULL,\n" + " `a` int(11) NOT NULL DEFAULT '1',\n" + " `c` int(11) NOT NULL DEFAULT '0',\n" + - " PRIMARY KEY (`c`),\n" + + " PRIMARY KEY (`c`) /*T![clustered_index] CLUSTERED */,\n" + " KEY `idx` (`bb`),\n" + " KEY `idx1` (`a`),\n" + " KEY `idx2` (`bb`,`c`)\n" + @@ -499,7 +499,7 @@ func (s *testFailDBSuite) TestModifyColumn(c *C) { " `bb` mediumint(9) DEFAULT NULL,\n" + " `c` int(11) NOT NULL DEFAULT '0',\n" + " `aa` mediumint(9) DEFAULT NULL,\n" + - " PRIMARY KEY (`c`),\n" + + " PRIMARY KEY (`c`) /*T![clustered_index] CLUSTERED */,\n" + " KEY `idx` (`bb`),\n" + " KEY `idx1` (`aa`),\n" + " KEY `idx2` (`bb`,`c`)\n" + diff --git a/ddl/serial_test.go b/ddl/serial_test.go index f0b7e2f88f05c..e065af41a372e 100644 --- a/ddl/serial_test.go +++ b/ddl/serial_test.go @@ -113,7 +113,7 @@ func (s *testSerialSuite) TestChangeMaxIndexLength(c *C) { func (s *testSerialSuite) TestPrimaryKey(c *C) { tk := testkit.NewTestKit(c, s.store) tk.MustExec("use test") - tk.MustExec("set @@tidb_enable_clustered_index = 0") + tk.Se.GetSessionVars().EnableClusteredIndex = false tk.MustExec("create table primary_key_test (a int, b varchar(10))") tk.MustExec("create table primary_key_test_1 (a int, b varchar(10), primary key(a))") @@ -174,7 +174,7 @@ func (s *testSerialSuite) TestPrimaryKey(c *C) { conf.AlterPrimaryKey = false }) tk.MustExec("drop table if exists t;") - tk.MustExec("set tidb_enable_clustered_index=1") + tk.Se.GetSessionVars().EnableClusteredIndex = true tk.MustExec("create table t(a int, b varchar(64), primary key(b));") tk.MustExec("insert into t values(1,'a'), (2, 'b');") config.UpdateGlobal(func(conf *config.Config) { @@ -332,7 +332,7 @@ func (s *testSerialSuite) TestMultiRegionGetTableEndCommonHandle(c *C) { tk.MustExec("drop database if exists test_get_endhandle") tk.MustExec("create database test_get_endhandle") tk.MustExec("use test_get_endhandle") - tk.MustExec("set @@tidb_enable_clustered_index = true") + tk.Se.GetSessionVars().EnableClusteredIndex = true tk.MustExec("create table t(a varchar(20), b int, c float, d bigint, primary key (a, b, c))") var builder strings.Builder @@ -376,7 +376,7 @@ func (s *testSerialSuite) TestGetTableEndCommonHandle(c *C) { tk.MustExec("drop database if exists test_get_endhandle") tk.MustExec("create database test_get_endhandle") tk.MustExec("use test_get_endhandle") - tk.MustExec("set @@tidb_enable_clustered_index = true") + tk.Se.GetSessionVars().EnableClusteredIndex = true tk.MustExec("create table t(a varchar(15), b bigint, c int, primary key (a, b))") tk.MustExec("create table t1(a varchar(15), b bigint, c int, primary key (a(2), b))") diff --git a/domain/global_vars_cache.go b/domain/global_vars_cache.go index fb922317793e1..52aa12a5ac955 100644 --- a/domain/global_vars_cache.go +++ b/domain/global_vars_cache.go @@ -39,7 +39,8 @@ type GlobalVariableCache struct { SingleFight singleflight.Group } -const globalVariableCacheExpiry = 2 * time.Second +// GlobalVariableCacheExpiry is the global variable cache TTL. +const GlobalVariableCacheExpiry = 2 * time.Second // Update updates the global variable cache. func (gvc *GlobalVariableCache) Update(rows []chunk.Row, fields []*ast.ResultField) { @@ -56,7 +57,7 @@ func (gvc *GlobalVariableCache) Update(rows []chunk.Row, fields []*ast.ResultFie func (gvc *GlobalVariableCache) Get() (succ bool, rows []chunk.Row, fields []*ast.ResultField) { gvc.RLock() defer gvc.RUnlock() - if time.Since(gvc.lastModify) < globalVariableCacheExpiry { + if time.Since(gvc.lastModify) < GlobalVariableCacheExpiry { succ, rows, fields = !gvc.disable, gvc.rows, gvc.fields return } diff --git a/executor/admin_test.go b/executor/admin_test.go index 2b9bf518ac54f..996dbc303c19d 100644 --- a/executor/admin_test.go +++ b/executor/admin_test.go @@ -194,7 +194,7 @@ func (s *testSuite5) TestClusteredIndexAdminRecoverIndex(c *C) { tk.MustExec("drop database if exists test_cluster_index_admin_recover;") tk.MustExec("create database test_cluster_index_admin_recover;") tk.MustExec("use test_cluster_index_admin_recover;") - tk.MustExec("set tidb_enable_clustered_index=1;") + tk.Se.GetSessionVars().EnableClusteredIndex = true dbName := model.NewCIStr("test_cluster_index_admin_recover") tblName := model.NewCIStr("t") @@ -310,7 +310,7 @@ func (s *testSuite5) TestAdminRecoverIndex1(c *C) { sc := s.ctx.GetSessionVars().StmtCtx tk.MustExec("use test") tk.MustExec("drop table if exists admin_test") - tk.MustExec("set @@tidb_enable_clustered_index=0;") + tk.Se.GetSessionVars().EnableClusteredIndex = false tk.MustExec("create table admin_test (c1 varchar(255), c2 int, c3 int default 1, primary key(c1), unique key(c2))") tk.MustExec("insert admin_test (c1, c2) values ('1', 1), ('2', 2), ('3', 3), ('10', 10), ('20', 20)") @@ -515,7 +515,7 @@ func (s *testSuite5) TestAdminCleanupIndexPKNotHandle(c *C) { tk := testkit.NewTestKit(c, s.store) tk.MustExec("use test") tk.MustExec("drop table if exists admin_test") - tk.MustExec("set @@tidb_enable_clustered_index=0;") + tk.Se.GetSessionVars().EnableClusteredIndex = false tk.MustExec("create table admin_test (c1 int, c2 int, c3 int, primary key (c1, c2))") tk.MustExec("insert admin_test (c1, c2) values (1, 2), (3, 4), (-5, 5)") @@ -627,7 +627,7 @@ func (s *testSuite5) TestClusteredAdminCleanupIndex(c *C) { tk := testkit.NewTestKit(c, s.store) tk.MustExec("use test") tk.MustExec("drop table if exists admin_test") - tk.MustExec("set tidb_enable_clustered_index=1") + tk.Se.GetSessionVars().EnableClusteredIndex = true tk.MustExec("create table admin_test (c1 varchar(255), c2 int, c3 char(10) default 'c3', primary key (c1, c3), unique key(c2), key (c3))") tk.MustExec("insert admin_test (c1, c2) values ('c1_1', 2), ('c1_2', 4), ('c1_3', NULL)") tk.MustExec("insert admin_test (c1, c3) values ('c1_4', 'c3_4'), ('c1_5', 'c3_5'), ('c1_6', default)") diff --git a/executor/aggregate_test.go b/executor/aggregate_test.go index dde2ed9935bde..d03bc3ebe90f6 100644 --- a/executor/aggregate_test.go +++ b/executor/aggregate_test.go @@ -898,7 +898,7 @@ func (s *testSuiteAgg) TestAggEliminator(c *C) { func (s *testSuiteAgg) TestClusterIndexMaxMinEliminator(c *C) { tk := testkit.NewTestKitWithInit(c, s.store) tk.MustExec("drop table if exists t;") - tk.MustExec("set @@tidb_enable_clustered_index=1;") + tk.Se.GetSessionVars().EnableClusteredIndex = true tk.MustExec("create table t (a int, b int, c int, primary key(a, b));") for i := 0; i < 10+1; i++ { tk.MustExec("insert into t values (?, ?, ?)", i, i, i) diff --git a/executor/analyze_test.go b/executor/analyze_test.go index 79060326cc04a..749ffb5444b62 100644 --- a/executor/analyze_test.go +++ b/executor/analyze_test.go @@ -129,7 +129,7 @@ func (s *testSuite1) TestClusterIndexAnalyze(c *C) { tk.MustExec("drop database if exists test_cluster_index_analyze;") tk.MustExec("create database test_cluster_index_analyze;") tk.MustExec("use test_cluster_index_analyze;") - tk.MustExec("set @@tidb_enable_clustered_index=1;") + tk.Se.GetSessionVars().EnableClusteredIndex = true tk.MustExec("create table t (a int, b int, c int, primary key(a, b));") for i := 0; i < 100; i++ { diff --git a/executor/batch_point_get_test.go b/executor/batch_point_get_test.go index b316061532868..c41ac03156243 100644 --- a/executor/batch_point_get_test.go +++ b/executor/batch_point_get_test.go @@ -177,7 +177,7 @@ func (s *testBatchPointGetSuite) TestBatchPointGetLockExistKey(c *C) { errCh <- tk1.ExecToErr("use test") errCh <- tk2.ExecToErr("use test") - errCh <- tk1.ExecToErr("set session tidb_enable_clustered_index = 0") + tk1.Se.GetSessionVars().EnableClusteredIndex = false errCh <- tk1.ExecToErr(fmt.Sprintf("drop table if exists %s", tableName)) errCh <- tk1.ExecToErr(fmt.Sprintf("create table %s(id int, v int, k int, %s key0(id, v))", tableName, key)) diff --git a/executor/clustered_index_test.go b/executor/clustered_index_test.go index e96863522410e..98eeb031a1758 100644 --- a/executor/clustered_index_test.go +++ b/executor/clustered_index_test.go @@ -15,6 +15,7 @@ package executor_test import ( . "github.com/pingcap/check" + "github.com/pingcap/tidb/config" "github.com/pingcap/tidb/errno" "github.com/pingcap/tidb/store/tikv" "github.com/pingcap/tidb/util/collate" @@ -27,7 +28,7 @@ type testClusteredSerialSuite struct{ testClusteredSuiteBase } func (s *testClusteredSuiteBase) newTK(c *C) *testkit.TestKit { tk := testkit.NewTestKitWithInit(c, s.store) - tk.MustExec("set @@tidb_enable_clustered_index = 1") + tk.Se.GetSessionVars().EnableClusteredIndex = true return tk } @@ -292,3 +293,43 @@ func (s *testClusteredSuite) TestClusteredIndexSelectWhereInNull(c *C) { tk.MustExec("create table t (a datetime, b bigint, primary key (a));") tk.MustQuery("select * from t where a in (null);").Check(testkit.Rows( /* empty result */ )) } + +func (s *testClusteredSerialSuite) TestClusteredIndexSyntax(c *C) { + tk := testkit.NewTestKitWithInit(c, s.store) + const showPKType = `select tidb_pk_type from information_schema.tables where table_schema = 'test' and table_name = 't';` + const nonClustered, clustered = `NON-CLUSTERED`, `CLUSTERED` + assertPkType := func(sql string, pkType string) { + tk.MustExec("drop table if exists t;") + tk.MustExec(sql) + tk.MustQuery(showPKType).Check(testkit.Rows(pkType)) + } + + defer config.RestoreFunc() + for _, allowAlterPK := range []bool{true, false} { + config.UpdateGlobal(func(conf *config.Config) { + conf.AlterPrimaryKey = allowAlterPK + }) + // Test single integer column as the primary key. + clusteredDefault := clustered + if allowAlterPK { + clusteredDefault = nonClustered + } + assertPkType("create table t (a int primary key, b int);", clusteredDefault) + assertPkType("create table t (a int, b int, primary key(a) clustered);", clustered) + assertPkType("create table t (a int, b int, primary key(a) /*T![clustered_index] clustered */);", clustered) + assertPkType("create table t (a int, b int, primary key(a) nonclustered);", nonClustered) + assertPkType("create table t (a int, b int, primary key(a) /*T![clustered_index] nonclustered */);", nonClustered) + + // Test for clustered index. + tk.Se.GetSessionVars().EnableClusteredIndex = false + assertPkType("create table t (a int, b varchar(255), primary key(b, a));", nonClustered) + assertPkType("create table t (a int, b varchar(255), primary key(b, a) nonclustered);", nonClustered) + assertPkType("create table t (a int, b varchar(255), primary key(b, a) clustered);", clustered) + tk.Se.GetSessionVars().EnableClusteredIndex = true + assertPkType("create table t (a int, b varchar(255), primary key(b, a));", clusteredDefault) + assertPkType("create table t (a int, b varchar(255), primary key(b, a) nonclustered);", nonClustered) + assertPkType("create table t (a int, b varchar(255), primary key(b, a) /*T![clustered_index] nonclustered */);", nonClustered) + assertPkType("create table t (a int, b varchar(255), primary key(b, a) clustered);", clustered) + assertPkType("create table t (a int, b varchar(255), primary key(b, a) /*T![clustered_index] clustered */);", clustered) + } +} diff --git a/executor/delete_test.go b/executor/delete_test.go index 0f70597c59a1c..278692acdb717 100644 --- a/executor/delete_test.go +++ b/executor/delete_test.go @@ -82,7 +82,7 @@ func (s *testSuite8) TestDeleteLockKey(c *C) { tk1, tk2 := testkit.NewTestKit(c, s.store), testkit.NewTestKit(c, s.store) tk1.MustExec("use test") tk2.MustExec("use test") - tk1.MustExec("set session tidb_enable_clustered_index=0") + tk1.Se.GetSessionVars().EnableClusteredIndex = false tk1.MustExec(t.ddl) tk1.MustExec(t.pre) tk1.MustExec("begin pessimistic") diff --git a/executor/executor_test.go b/executor/executor_test.go index 6b1efbde56e96..08754a3fa8f35 100644 --- a/executor/executor_test.go +++ b/executor/executor_test.go @@ -2292,7 +2292,7 @@ func (s *testSuiteP2) TestClusteredIndexIsPointGet(c *C) { tk.MustExec("create database test_cluster_index_is_point_get;") tk.MustExec("use test_cluster_index_is_point_get;") - tk.MustExec("set tidb_enable_clustered_index=1;") + tk.Se.GetSessionVars().EnableClusteredIndex = true tk.MustExec("drop table if exists t;") tk.MustExec("create table t (a varchar(255), b int, c char(10), primary key (c, a));") ctx := tk.Se.(sessionctx.Context) @@ -3581,7 +3581,7 @@ func (s *testSuite) TestUnsignedPk(c *C) { func (s *testSuite) TestSignedCommonHandle(c *C) { tk := testkit.NewTestKitWithInit(c, s.store) - tk.MustExec("set @@tidb_enable_clustered_index=1") + tk.Se.GetSessionVars().EnableClusteredIndex = true tk.MustExec("drop table if exists t") tk.MustExec("create table t(k1 int, k2 int, primary key(k1, k2))") tk.MustExec("insert into t(k1, k2) value(-100, 1), (-50, 1), (0, 0), (1, 1), (3, 3)") @@ -3749,7 +3749,7 @@ func (s *testSuite) TestCheckTableClusterIndex(c *C) { tk := testkit.NewTestKit(c, s.store) tk.MustExec("use test;") - tk.MustExec("set @@tidb_enable_clustered_index = 1;") + tk.Se.GetSessionVars().EnableClusteredIndex = true tk.MustExec("drop table if exists admin_test;") tk.MustExec("create table admin_test (c1 int, c2 int, c3 int default 1, primary key (c1, c2), index (c1), unique key(c2));") tk.MustExec("insert admin_test (c1, c2) values (1, 1), (2, 2), (3, 3);") @@ -4194,7 +4194,7 @@ func (s *testSuite3) TestRowID(c *C) { tk := testkit.NewTestKit(c, s.store) tk.MustExec(`use test`) tk.MustExec(`drop table if exists t`) - tk.MustExec(`set @@tidb_enable_clustered_index=0;`) + tk.Se.GetSessionVars().EnableClusteredIndex = false tk.MustExec(`create table t(a varchar(10), b varchar(10), c varchar(1), index idx(a, b, c));`) tk.MustExec(`insert into t values('a', 'b', 'c');`) tk.MustExec(`insert into t values('a', 'b', 'c');`) @@ -4742,7 +4742,7 @@ func (s *testSplitTable) TestClusterIndexSplitTableIntegration(c *C) { tk.MustExec("drop database if exists test_cluster_index_index_split_table_integration;") tk.MustExec("create database test_cluster_index_index_split_table_integration;") tk.MustExec("use test_cluster_index_index_split_table_integration;") - tk.MustExec("set @@tidb_enable_clustered_index=1;") + tk.Se.GetSessionVars().EnableClusteredIndex = true tk.MustExec("create table t (a varchar(255), b double, c int, primary key (a, b));") @@ -4797,7 +4797,7 @@ func (s *testSplitTable) TestClusterIndexShowTableRegion(c *C) { tk.MustExec("drop database if exists cluster_index_regions;") tk.MustExec("create database cluster_index_regions;") tk.MustExec("use cluster_index_regions;") - tk.MustExec("set @@tidb_enable_clustered_index=1;") + tk.Se.GetSessionVars().EnableClusteredIndex = true tk.MustExec("create table t (a int, b int, c int, primary key(a, b));") tk.MustExec("insert t values (1, 1, 1), (2, 2, 2);") tk.MustQuery("split table t between (1, 0) and (2, 3) regions 2;").Check(testkit.Rows("1 1")) @@ -4819,8 +4819,8 @@ func (s *testSplitTable) TestClusterIndexShowTableRegion(c *C) { func (s *testSuiteWithData) TestClusterIndexOuterJoinElimination(c *C) { tk := testkit.NewTestKit(c, s.store) - tk.MustExec(`set @@tidb_enable_clustered_index = 1`) tk.MustExec("use test") + tk.Se.GetSessionVars().EnableClusteredIndex = true tk.MustExec("create table t (a int, b int, c int, primary key(a,b))") rows := tk.MustQuery(`explain select t1.a from t t1 left join t t2 on t1.a = t2.a and t1.b = t2.b`).Rows() rowStrs := s.testData.ConvertRowsToStrings(rows) diff --git a/executor/explainfor_test.go b/executor/explainfor_test.go index 4a0eddb9a8b98..7961175acd6b7 100644 --- a/executor/explainfor_test.go +++ b/executor/explainfor_test.go @@ -445,7 +445,7 @@ func (s *testPrepareSerialSuite) TestPointGetUserVarPlanCache(c *C) { tk.MustExec("use test") tk.MustExec("set @@tidb_enable_collect_execution_info=0;") - tk.MustExec("set @@tidb_enable_clustered_index=1;") + tk.Se.GetSessionVars().EnableClusteredIndex = true tk.MustExec("drop table if exists t1") tk.MustExec("CREATE TABLE t1 (a BIGINT, b VARCHAR(40), PRIMARY KEY (a, b))") tk.MustExec("INSERT INTO t1 VALUES (1,'3'),(2,'4')") diff --git a/executor/infoschema_reader.go b/executor/infoschema_reader.go index 7ee62a7561f5b..33712bcab4e65 100644 --- a/executor/infoschema_reader.go +++ b/executor/infoschema_reader.go @@ -487,10 +487,8 @@ func (e *memtableRetriever) setDataFromTables(ctx sessionctx.Context, schemas [] if util.IsSystemView(schema.Name.L) { tableType = "SYSTEM VIEW" } - if table.PKIsHandle { - pkType = "INT CLUSTERED" - } else if table.IsCommonHandle { - pkType = "COMMON CLUSTERED" + if table.PKIsHandle || table.IsCommonHandle { + pkType = "CLUSTERED" } shardingInfo := infoschema.GetShardingInfo(schema, table) record := types.MakeDatums( diff --git a/executor/infoschema_reader_test.go b/executor/infoschema_reader_test.go index 0fc45004870de..b8783b06b58fa 100644 --- a/executor/infoschema_reader_test.go +++ b/executor/infoschema_reader_test.go @@ -866,12 +866,12 @@ func (s *testInfoschemaTableSuite) TestTiFlashSystemTables(c *C) { func (s *testInfoschemaTableSuite) TestTablesPKType(c *C) { tk := testkit.NewTestKitWithInit(c, s.store) tk.MustExec("create table t_int (a int primary key, b int)") - tk.MustQuery("SELECT TIDB_PK_TYPE FROM information_schema.tables where table_schema = 'test' and table_name = 't_int'").Check(testkit.Rows("INT CLUSTERED")) - tk.MustExec("set @@tidb_enable_clustered_index = 0") + tk.MustQuery("SELECT TIDB_PK_TYPE FROM information_schema.tables where table_schema = 'test' and table_name = 't_int'").Check(testkit.Rows("CLUSTERED")) + tk.Se.GetSessionVars().EnableClusteredIndex = false tk.MustExec("create table t_implicit (a varchar(64) primary key, b int)") tk.MustQuery("SELECT TIDB_PK_TYPE FROM information_schema.tables where table_schema = 'test' and table_name = 't_implicit'").Check(testkit.Rows("NON-CLUSTERED")) - tk.MustExec("set @@tidb_enable_clustered_index = 1") + tk.Se.GetSessionVars().EnableClusteredIndex = true tk.MustExec("create table t_common (a varchar(64) primary key, b int)") - tk.MustQuery("SELECT TIDB_PK_TYPE FROM information_schema.tables where table_schema = 'test' and table_name = 't_common'").Check(testkit.Rows("COMMON CLUSTERED")) + tk.MustQuery("SELECT TIDB_PK_TYPE FROM information_schema.tables where table_schema = 'test' and table_name = 't_common'").Check(testkit.Rows("CLUSTERED")) tk.MustQuery("SELECT TIDB_PK_TYPE FROM information_schema.tables where table_schema = 'INFORMATION_SCHEMA' and table_name = 'TABLES'").Check(testkit.Rows("NON-CLUSTERED")) } diff --git a/executor/insert_test.go b/executor/insert_test.go index 73f2914756209..9c81fdfb4faf6 100644 --- a/executor/insert_test.go +++ b/executor/insert_test.go @@ -220,7 +220,7 @@ func (s *testSuite8) TestClusterIndexInsertOnDuplicateKey(c *C) { tk.MustExec("drop database if exists cluster_index_duplicate_entry_error;") tk.MustExec("create database cluster_index_duplicate_entry_error;") tk.MustExec("use cluster_index_duplicate_entry_error;") - tk.MustExec("set @@tidb_enable_clustered_index = 1") + tk.Se.GetSessionVars().EnableClusteredIndex = true tk.MustExec("create table t(a char(20), b int, primary key(a));") tk.MustExec("insert into t values('aa', 1), ('bb', 1);") @@ -237,7 +237,7 @@ func (s *testSuite8) TestClusterIndexInsertOnDuplicateKey(c *C) { func (s *testSuite10) TestPaddingCommonHandle(c *C) { tk := testkit.NewTestKit(c, s.store) tk.MustExec("use test") - tk.MustExec("set @@tidb_enable_clustered_index = 1") + tk.Se.GetSessionVars().EnableClusteredIndex = true tk.MustExec(`create table t1(c1 decimal(6,4), primary key(c1))`) tk.MustExec(`insert into t1 set c1 = 0.1`) tk.MustExec(`insert into t1 set c1 = 0.1 on duplicate key update c1 = 1`) @@ -1305,7 +1305,7 @@ type testSuite10 struct { func (s *testSuite10) TestClusterPrimaryTablePlainInsert(c *C) { tk := testkit.NewTestKit(c, s.store) tk.MustExec(`use test`) - tk.MustExec(`set @@tidb_enable_clustered_index=true`) + tk.Se.GetSessionVars().EnableClusteredIndex = true tk.MustExec(`drop table if exists t1pk`) tk.MustExec(`create table t1pk(id varchar(200) primary key, v int)`) @@ -1347,7 +1347,7 @@ func (s *testSuite10) TestClusterPrimaryTablePlainInsert(c *C) { func (s *testSuite10) TestClusterPrimaryTableInsertIgnore(c *C) { tk := testkit.NewTestKit(c, s.store) tk.MustExec(`use test`) - tk.MustExec(`set @@tidb_enable_clustered_index=true`) + tk.Se.GetSessionVars().EnableClusteredIndex = true tk.MustExec(`drop table if exists it1pk`) tk.MustExec(`create table it1pk(id varchar(200) primary key, v int)`) @@ -1373,7 +1373,7 @@ func (s *testSuite10) TestClusterPrimaryTableInsertIgnore(c *C) { func (s *testSuite10) TestClusterPrimaryTableInsertDuplicate(c *C) { tk := testkit.NewTestKit(c, s.store) tk.MustExec(`use test`) - tk.MustExec(`set @@tidb_enable_clustered_index=true`) + tk.Se.GetSessionVars().EnableClusteredIndex = true tk.MustExec(`drop table if exists dt1pi`) tk.MustExec(`create table dt1pi(id varchar(200) primary key, v int)`) @@ -1405,7 +1405,7 @@ func (s *testSuite10) TestClusterPrimaryTableInsertDuplicate(c *C) { func (s *testSuite10) TestClusterPrimaryKeyForIndexScan(c *C) { tk := testkit.NewTestKit(c, s.store) tk.MustExec(`use test`) - tk.MustExec(`set @@tidb_enable_clustered_index=true`) + tk.Se.GetSessionVars().EnableClusteredIndex = true tk.MustExec("drop table if exists pkt1;") tk.MustExec("CREATE TABLE pkt1 (a varchar(255), b int, index idx(b), primary key(a,b));") @@ -1454,8 +1454,8 @@ func (s *testSerialSuite) TestDuplicateEntryMessage(c *C) { tk := testkit.NewTestKit(c, s.store) tk.MustExec("use test;") - for _, enable := range []int{1, 0} { - tk.MustExec(fmt.Sprintf("set session tidb_enable_clustered_index=%d;", enable)) + for _, enable := range []bool{true, false} { + tk.Se.GetSessionVars().EnableClusteredIndex = enable tk.MustExec("drop table if exists t;") tk.MustExec("create table t(a int, b char(10), unique key(b)) collate utf8mb4_general_ci;") tk.MustExec("insert into t value (34, '12Ak');") diff --git a/executor/join_test.go b/executor/join_test.go index 944c14c853116..ef9f69f288262 100644 --- a/executor/join_test.go +++ b/executor/join_test.go @@ -1374,7 +1374,7 @@ func (s *testSuiteJoinSerial) TestIndexNestedLoopHashJoin(c *C) { tk.MustExec("set @@tidb_init_chunk_size=2") tk.MustExec("set @@tidb_index_join_batch_size=10") tk.MustExec("DROP TABLE IF EXISTS t, s") - tk.MustExec("set @@tidb_enable_clustered_index=0;") + tk.Se.GetSessionVars().EnableClusteredIndex = false tk.MustExec("create table t(pk int primary key, a int)") for i := 0; i < 100; i++ { tk.MustExec(fmt.Sprintf("insert into t values(%d, %d)", i, i)) diff --git a/executor/parallel_apply_test.go b/executor/parallel_apply_test.go index 48c743860d92b..6db533b971fee 100644 --- a/executor/parallel_apply_test.go +++ b/executor/parallel_apply_test.go @@ -421,7 +421,7 @@ func (s *testSerialSuite) TestApplyWithOtherFeatures(c *C) { core.SetPreparedPlanCache(orgEnable) // cluster index - tk.MustExec("set @@tidb_enable_clustered_index = 1") + tk.Se.GetSessionVars().EnableClusteredIndex = true tk.MustExec("drop table if exists t, t2") tk.MustExec("create table t(a int, b int, c int, primary key(a, b))") tk.MustExec("create table t2(a int, b int, c int, primary key(a, c))") @@ -429,7 +429,7 @@ func (s *testSerialSuite) TestApplyWithOtherFeatures(c *C) { tk.MustExec("insert into t2 values (1, 1, 1), (2, 2, 2), (3, 3, 3), (4, 4, 4)") sql = "select * from t where (select min(t2.b) from t2 where t2.a > t.a) > 0" tk.MustQuery(sql).Sort().Check(testkit.Rows("1 1 1", "2 2 2", "3 3 3")) - tk.MustExec("set @@tidb_enable_clustered_index = 0") + tk.Se.GetSessionVars().EnableClusteredIndex = false // partitioning table tk.MustExec("drop table if exists t1, t2") diff --git a/executor/point_get_test.go b/executor/point_get_test.go index b89e4dad27ea1..68dbbc51ab6d1 100644 --- a/executor/point_get_test.go +++ b/executor/point_get_test.go @@ -524,7 +524,7 @@ func (s *testPointGetSuite) TestReturnValues(c *C) { tk := testkit.NewTestKit(c, s.store) tk.MustExec("use test") tk.MustExec("drop table if exists t") - tk.MustExec("set @@tidb_enable_clustered_index=0;") + tk.Se.GetSessionVars().EnableClusteredIndex = false tk.MustExec("create table t (a varchar(64) primary key, b int)") tk.MustExec("insert t values ('a', 1), ('b', 2), ('c', 3)") tk.MustExec("begin pessimistic") @@ -546,8 +546,8 @@ func (s *testPointGetSuite) TestReturnValues(c *C) { func (s *testPointGetSuite) TestClusterIndexPointGet(c *C) { tk := testkit.NewTestKit(c, s.store) - tk.MustExec(`set @@tidb_enable_clustered_index=true`) tk.MustExec("use test") + tk.Se.GetSessionVars().EnableClusteredIndex = true tk.MustExec("drop table if exists pgt") tk.MustExec("create table pgt (a varchar(64), b varchar(64), uk int, v int, primary key(a, b), unique key uuk(uk))") tk.MustExec("insert pgt values ('a', 'a1', 1, 11), ('b', 'b1', 2, 22), ('c', 'c1', 3, 33)") @@ -569,8 +569,8 @@ func (s *testPointGetSuite) TestClusterIndexPointGet(c *C) { func (s *testPointGetSuite) TestClusterIndexCBOPointGet(c *C) { tk := testkit.NewTestKit(c, s.store) - tk.MustExec(`set @@tidb_enable_clustered_index=true`) tk.MustExec("use test") + tk.Se.GetSessionVars().EnableClusteredIndex = true tk.MustExec("drop table if exists t1, t2") tk.MustExec(`create table t1 (a int, b decimal(10,0), c int, primary key(a,b))`) tk.MustExec(`create table t2 (a varchar(20), b int, primary key(a), unique key(b))`) @@ -770,7 +770,7 @@ func (s *testPointGetSuite) TestPointGetLockExistKey(c *C) { errCh <- tk1.ExecToErr("use test") errCh <- tk2.ExecToErr("use test") - errCh <- tk1.ExecToErr("set session tidb_enable_clustered_index = 0") + tk1.Se.GetSessionVars().EnableClusteredIndex = false errCh <- tk1.ExecToErr(fmt.Sprintf("drop table if exists %s", tableName)) errCh <- tk1.ExecToErr(fmt.Sprintf("create table %s(id int, v int, k int, %s key0(id, v))", tableName, key)) diff --git a/executor/prepared_test.go b/executor/prepared_test.go index db9c68072b571..e8f630c8db9d5 100644 --- a/executor/prepared_test.go +++ b/executor/prepared_test.go @@ -200,7 +200,7 @@ func (s *testSerialSuite) TestPlanCacheClusterIndex(c *C) { plannercore.SetPreparedPlanCache(true) tk.MustExec("use test") tk.MustExec("drop table if exists t1") - tk.MustExec("set @@tidb_enable_clustered_index = 1") + tk.Se.GetSessionVars().EnableClusteredIndex = true tk.MustExec("set @@tidb_enable_collect_execution_info=0;") tk.MustExec("create table t1(a varchar(20), b varchar(20), c varchar(20), primary key(a, b))") tk.MustExec("insert into t1 values('1','1','111'),('2','2','222'),('3','3','333')") @@ -289,7 +289,7 @@ func (s *testSerialSuite) TestPlanCacheClusterIndex(c *C) { tk.MustQuery(`execute stmt2 using @v2, @v2, @v3, @v3`).Check(testkit.Rows("b b 2 2 2", "c c 3 3 3")) // For issue 19002 - tk.MustExec(`set @@tidb_enable_clustered_index = 1`) + tk.Se.GetSessionVars().EnableClusteredIndex = true tk.MustExec(`drop table if exists t1`) tk.MustExec(`create table t1(a int, b int, c int, primary key(a, b))`) tk.MustExec(`insert into t1 values(1,1,111),(2,2,222),(3,3,333)`) diff --git a/executor/rowid_test.go b/executor/rowid_test.go index 62a0c82be54b3..4873c637b9cb7 100644 --- a/executor/rowid_test.go +++ b/executor/rowid_test.go @@ -64,7 +64,7 @@ func (s *testSuite1) TestExportRowID(c *C) { func (s *testSuite1) TestNotAllowWriteRowID(c *C) { tk := testkit.NewTestKit(c, s.store) tk.MustExec("use test") - tk.MustExec("set @@tidb_enable_clustered_index=0;") + tk.Se.GetSessionVars().EnableClusteredIndex = false tk.MustExec("create table tt(id binary(10), c int, primary key(id));") tk.MustExec("insert tt values (1, 10);") // select statement diff --git a/executor/sample_test.go b/executor/sample_test.go index 56d29c9689b61..a794df615a10c 100644 --- a/executor/sample_test.go +++ b/executor/sample_test.go @@ -72,7 +72,7 @@ func (s *testTableSampleSuite) initSampleTest(c *C) *testkit.TestKit { func (s *testTableSampleSuite) TestTableSampleBasic(c *C) { tk := s.initSampleTest(c) tk.MustExec("create table t (a int);") - tk.MustExec("set @@tidb_enable_clustered_index=1;") + tk.Se.GetSessionVars().EnableClusteredIndex = true tk.MustQuery("select * from t tablesample regions();").Check(testkit.Rows()) tk.MustExec("insert into t values (0), (1000), (2000);") @@ -122,7 +122,7 @@ func (s *testTableSampleSuite) TestTableSampleMultiRegions(c *C) { func (s *testTableSampleSuite) TestTableSampleSchema(c *C) { tk := s.initSampleTest(c) - tk.MustExec("set @@tidb_enable_clustered_index = 1;") + tk.Se.GetSessionVars().EnableClusteredIndex = true // Clustered index tk.MustExec("create table t (a varchar(255) primary key, b bigint);") tk.MustExec("insert into t values ('b', 100), ('y', 100);") diff --git a/executor/seqtest/seq_executor_test.go b/executor/seqtest/seq_executor_test.go index e409fd7ea2773..fa6cf04806d1f 100644 --- a/executor/seqtest/seq_executor_test.go +++ b/executor/seqtest/seq_executor_test.go @@ -198,7 +198,7 @@ func (s *seqTestSuite) TestShow(c *C) { row := result.Rows()[0] // For issue https://github.com/pingcap/tidb/issues/1061 expectedRow := []interface{}{ - "SHOW_test", "CREATE TABLE `SHOW_test` (\n `id` int(11) NOT NULL AUTO_INCREMENT,\n `c1` int(11) DEFAULT NULL COMMENT 'c1_comment',\n `c2` int(11) DEFAULT NULL,\n `c3` int(11) DEFAULT '1',\n `c4` text DEFAULT NULL,\n `c5` tinyint(1) DEFAULT NULL,\n PRIMARY KEY (`id`),\n KEY `idx_wide_c4` (`c3`,`c4`(10))\n) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=28934 COMMENT='table_comment'"} + "SHOW_test", "CREATE TABLE `SHOW_test` (\n `id` int(11) NOT NULL AUTO_INCREMENT,\n `c1` int(11) DEFAULT NULL COMMENT 'c1_comment',\n `c2` int(11) DEFAULT NULL,\n `c3` int(11) DEFAULT '1',\n `c4` text DEFAULT NULL,\n `c5` tinyint(1) DEFAULT NULL,\n PRIMARY KEY (`id`) /*T![clustered_index] CLUSTERED */,\n KEY `idx_wide_c4` (`c3`,`c4`(10))\n) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=28934 COMMENT='table_comment'"} for i, r := range row { c.Check(r, Equals, expectedRow[i]) } @@ -218,7 +218,7 @@ func (s *seqTestSuite) TestShow(c *C) { c.Check(result.Rows(), HasLen, 1) row = result.Rows()[0] expectedRow = []interface{}{ - "ptest", "CREATE TABLE `ptest` (\n `a` int(11) NOT NULL,\n `b` double NOT NULL DEFAULT '2.0',\n `c` varchar(10) NOT NULL,\n `d` time DEFAULT NULL,\n `e` timestamp NULL DEFAULT NULL,\n `f` timestamp NULL DEFAULT NULL,\n PRIMARY KEY (`a`),\n UNIQUE KEY `d` (`d`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin"} + "ptest", "CREATE TABLE `ptest` (\n `a` int(11) NOT NULL,\n `b` double NOT NULL DEFAULT '2.0',\n `c` varchar(10) NOT NULL,\n `d` time DEFAULT NULL,\n `e` timestamp NULL DEFAULT NULL,\n `f` timestamp NULL DEFAULT NULL,\n PRIMARY KEY (`a`) /*T![clustered_index] CLUSTERED */,\n UNIQUE KEY `d` (`d`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin"} for i, r := range row { c.Check(r, Equals, expectedRow[i]) } @@ -401,7 +401,7 @@ func (s *seqTestSuite) TestShow(c *C) { ""+ "show_auto_increment CREATE TABLE `show_auto_increment` (\n"+ " `id` int(11) NOT NULL AUTO_INCREMENT,\n"+ - " PRIMARY KEY (`id`)\n"+ + " PRIMARY KEY (`id`) /*T![clustered_index] CLUSTERED */\n"+ ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin AUTO_INCREMENT=4", )) // for issue https://github.com/pingcap/tidb/issues/4678 @@ -412,7 +412,7 @@ func (s *seqTestSuite) TestShow(c *C) { ""+ "show_auto_increment CREATE TABLE `show_auto_increment` (\n"+ " `id` int(11) NOT NULL AUTO_INCREMENT,\n"+ - " PRIMARY KEY (`id`)\n"+ + " PRIMARY KEY (`id`) /*T![clustered_index] CLUSTERED */\n"+ ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin AUTO_INCREMENT="+strconv.Itoa(int(autoID)), )) tk.MustExec(`drop table show_auto_increment`) @@ -421,7 +421,7 @@ func (s *seqTestSuite) TestShow(c *C) { ""+ "show_auto_increment CREATE TABLE `show_auto_increment` (\n"+ " `id` int(11) NOT NULL AUTO_INCREMENT,\n"+ - " PRIMARY KEY (`id`)\n"+ + " PRIMARY KEY (`id`) /*T![clustered_index] CLUSTERED */\n"+ ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin", )) tk.MustExec("insert into show_auto_increment values(10)") @@ -430,7 +430,7 @@ func (s *seqTestSuite) TestShow(c *C) { ""+ "show_auto_increment CREATE TABLE `show_auto_increment` (\n"+ " `id` int(11) NOT NULL AUTO_INCREMENT,\n"+ - " PRIMARY KEY (`id`)\n"+ + " PRIMARY KEY (`id`) /*T![clustered_index] CLUSTERED */\n"+ ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin AUTO_INCREMENT="+strconv.Itoa(int(autoID)), )) @@ -594,7 +594,7 @@ func (s *seqTestSuite) TestShow(c *C) { "t CREATE TABLE `t` (\n"+ " `y` year(4) NOT NULL,\n"+ " `x` int(11) DEFAULT NULL,\n"+ - " PRIMARY KEY (`y`)\n"+ + " PRIMARY KEY (`y`) /*T![clustered_index] NONCLUSTERED */\n"+ ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin")) // Test show create table with zerofill flag @@ -604,7 +604,7 @@ func (s *seqTestSuite) TestShow(c *C) { "t CREATE TABLE `t` (\n"+ " `id` int(11) NOT NULL,\n"+ " `val` tinyint(10) unsigned zerofill DEFAULT NULL,\n"+ - " PRIMARY KEY (`id`)\n"+ + " PRIMARY KEY (`id`) /*T![clustered_index] CLUSTERED */\n"+ ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin")) // Test show columns with different types of default value diff --git a/executor/show.go b/executor/show.go index c192c0682243b..bbfff4e33700e 100644 --- a/executor/show.go +++ b/executor/show.go @@ -848,6 +848,7 @@ func ConstructResultOfShowCreateTable(ctx sessionctx.Context, tableInfo *model.T // If PKIsHanle, pk info is not in tb.Indices(). We should handle it here. buf.WriteString(",\n") fmt.Fprintf(buf, " PRIMARY KEY (%s)", stringutil.Escape(pkCol.Name.O, sqlMode)) + buf.WriteString(fmt.Sprintf(" /*T![clustered_index] CLUSTERED */")) } publicIndices := make([]*model.IndexInfo, 0, len(tableInfo.Indices)) @@ -886,6 +887,13 @@ func ConstructResultOfShowCreateTable(ctx sessionctx.Context, tableInfo *model.T if idxInfo.Invisible { fmt.Fprintf(buf, ` /*!80000 INVISIBLE */`) } + if idxInfo.Primary { + if tableInfo.PKIsHandle || tableInfo.IsCommonHandle { + buf.WriteString(fmt.Sprintf(" /*T![clustered_index] CLUSTERED */")) + } else { + buf.WriteString(fmt.Sprintf(" /*T![clustered_index] NONCLUSTERED */")) + } + } if i != len(publicIndices)-1 { buf.WriteString(",\n") } diff --git a/executor/show_test.go b/executor/show_test.go index 6113cfa86fa6c..6cbecb365b152 100644 --- a/executor/show_test.go +++ b/executor/show_test.go @@ -673,7 +673,7 @@ func (s *testSuite5) TestShowCreateTable(c *C) { " `END_TIME` datetime NOT NULL,\n"+ " `USER_TYPE` int(11) DEFAULT NULL,\n"+ " `APP_ID` int(11) DEFAULT NULL,\n"+ - " PRIMARY KEY (`LOG_ID`,`END_TIME`),\n"+ + " PRIMARY KEY (`LOG_ID`,`END_TIME`) /*T![clustered_index] NONCLUSTERED */,\n"+ " KEY `IDX_EndTime` (`END_TIME`),\n"+ " KEY `IDX_RoundId` (`ROUND_ID`),\n"+ " KEY `IDX_UserId_EndTime` (`USER_ID`,`END_TIME`)\n"+ @@ -789,7 +789,7 @@ func (s *testSuite5) TestShowCreateTable(c *C) { "child CREATE TABLE `child` (\n"+ " `id` int(11) NOT NULL AUTO_INCREMENT,\n"+ " `parent_id` int(11) NOT NULL,\n"+ - " PRIMARY KEY (`id`),\n"+ + " PRIMARY KEY (`id`) /*T![clustered_index] CLUSTERED */,\n"+ " KEY `par_ind` (`parent_id`),\n"+ " CONSTRAINT `child_ibfk_1` FOREIGN KEY (`parent_id`) REFERENCES `parent` (`id`)\n"+ ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin", @@ -803,7 +803,7 @@ func (s *testSuite5) TestShowCreateTable(c *C) { "child CREATE TABLE `child` (\n"+ " `id` int(11) NOT NULL AUTO_INCREMENT,\n"+ " `parent_id` int(11) NOT NULL,\n"+ - " PRIMARY KEY (`id`),\n"+ + " PRIMARY KEY (`id`) /*T![clustered_index] CLUSTERED */,\n"+ " KEY `par_ind` (`parent_id`),\n"+ " CONSTRAINT `child_ibfk_1` FOREIGN KEY (`parent_id`) REFERENCES `parent` (`id`) ON DELETE SET NULL ON UPDATE CASCADE\n"+ ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin", @@ -887,7 +887,7 @@ func (s *testAutoRandomSuite) TestShowCreateTableAutoRandom(c *C) { "auto_random_tbl1 CREATE TABLE `auto_random_tbl1` (\n"+ " `a` bigint(20) NOT NULL /*T![auto_rand] AUTO_RANDOM(3) */,\n"+ " `b` varchar(255) DEFAULT NULL,\n"+ - " PRIMARY KEY (`a`)\n"+ + " PRIMARY KEY (`a`) /*T![clustered_index] CLUSTERED */\n"+ ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin", )) @@ -898,7 +898,7 @@ func (s *testAutoRandomSuite) TestShowCreateTableAutoRandom(c *C) { "auto_random_tbl2 CREATE TABLE `auto_random_tbl2` (\n"+ " `a` bigint(20) NOT NULL /*T![auto_rand] AUTO_RANDOM(5) */,\n"+ " `b` char(1) DEFAULT NULL,\n"+ - " PRIMARY KEY (`a`)\n"+ + " PRIMARY KEY (`a`) /*T![clustered_index] CLUSTERED */\n"+ ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin", )) @@ -908,7 +908,7 @@ func (s *testAutoRandomSuite) TestShowCreateTableAutoRandom(c *C) { ""+ "auto_random_tbl3 CREATE TABLE `auto_random_tbl3` (\n"+ " `a` bigint(20) NOT NULL /*T![auto_rand] AUTO_RANDOM(5) */,\n"+ - " PRIMARY KEY (`a`)\n"+ + " PRIMARY KEY (`a`) /*T![clustered_index] CLUSTERED */\n"+ ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin", )) // Test show auto_random table option. @@ -918,7 +918,7 @@ func (s *testAutoRandomSuite) TestShowCreateTableAutoRandom(c *C) { "auto_random_tbl4 CREATE TABLE `auto_random_tbl4` (\n"+ " `a` bigint(20) NOT NULL /*T![auto_rand] AUTO_RANDOM(5) */,\n"+ " `b` varchar(255) DEFAULT NULL,\n"+ - " PRIMARY KEY (`a`)\n"+ + " PRIMARY KEY (`a`) /*T![clustered_index] CLUSTERED */\n"+ ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin /*T![auto_rand_base] AUTO_RANDOM_BASE=100 */", )) // Test implicit auto_random with auto_random table option. @@ -928,7 +928,7 @@ func (s *testAutoRandomSuite) TestShowCreateTableAutoRandom(c *C) { "auto_random_tbl5 CREATE TABLE `auto_random_tbl5` (\n"+ " `a` bigint(20) NOT NULL /*T![auto_rand] AUTO_RANDOM(5) */,\n"+ " `b` char(1) DEFAULT NULL,\n"+ - " PRIMARY KEY (`a`)\n"+ + " PRIMARY KEY (`a`) /*T![clustered_index] CLUSTERED */\n"+ ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin /*T![auto_rand_base] AUTO_RANDOM_BASE=50 */", )) // Test auto_random table option already with special comment. @@ -937,7 +937,7 @@ func (s *testAutoRandomSuite) TestShowCreateTableAutoRandom(c *C) { ""+ "auto_random_tbl6 CREATE TABLE `auto_random_tbl6` (\n"+ " `a` bigint(20) NOT NULL /*T![auto_rand] AUTO_RANDOM(5) */,\n"+ - " PRIMARY KEY (`a`)\n"+ + " PRIMARY KEY (`a`) /*T![clustered_index] CLUSTERED */\n"+ ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin /*T![auto_rand_base] AUTO_RANDOM_BASE=200 */", )) } @@ -953,7 +953,7 @@ func (s *testAutoRandomSuite) TestAutoIdCache(c *C) { ""+ "t CREATE TABLE `t` (\n"+ " `a` int(11) NOT NULL AUTO_INCREMENT,\n"+ - " PRIMARY KEY (`a`)\n"+ + " PRIMARY KEY (`a`) /*T![clustered_index] CLUSTERED */\n"+ ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin /*T![auto_id_cache] AUTO_ID_CACHE=10 */", )) tk.MustExec("drop table if exists t") @@ -963,7 +963,7 @@ func (s *testAutoRandomSuite) TestAutoIdCache(c *C) { "t CREATE TABLE `t` (\n"+ " `a` int(11) NOT NULL AUTO_INCREMENT,\n"+ " `b` int(11) NOT NULL,\n"+ - " PRIMARY KEY (`b`),\n"+ + " PRIMARY KEY (`b`) /*T![clustered_index] CLUSTERED */,\n"+ " UNIQUE KEY `a` (`a`)\n"+ ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin /*T![auto_id_cache] AUTO_ID_CACHE=100 */", )) @@ -973,7 +973,7 @@ func (s *testAutoRandomSuite) TestAutoIdCache(c *C) { ""+ "t CREATE TABLE `t` (\n"+ " `a` int(11) NOT NULL,\n"+ - " PRIMARY KEY (`a`)\n"+ + " PRIMARY KEY (`a`) /*T![clustered_index] CLUSTERED */\n"+ ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin /*T![auto_id_cache] AUTO_ID_CACHE=5 */", )) } @@ -995,7 +995,7 @@ func (s *testAutoRandomSuite) TestAutoRandomBase(c *C) { "t CREATE TABLE `t` (\n"+ " `a` bigint(20) NOT NULL /*T![auto_rand] AUTO_RANDOM(5) */,\n"+ " `b` int(11) NOT NULL AUTO_INCREMENT,\n"+ - " PRIMARY KEY (`a`),\n"+ + " PRIMARY KEY (`a`) /*T![clustered_index] CLUSTERED */,\n"+ " UNIQUE KEY `b` (`b`)\n"+ ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin AUTO_INCREMENT=100 /*T![auto_rand_base] AUTO_RANDOM_BASE=100 */", )) @@ -1006,7 +1006,7 @@ func (s *testAutoRandomSuite) TestAutoRandomBase(c *C) { "t CREATE TABLE `t` (\n"+ " `a` bigint(20) NOT NULL /*T![auto_rand] AUTO_RANDOM(5) */,\n"+ " `b` int(11) NOT NULL AUTO_INCREMENT,\n"+ - " PRIMARY KEY (`a`),\n"+ + " PRIMARY KEY (`a`) /*T![clustered_index] CLUSTERED */,\n"+ " UNIQUE KEY `b` (`b`)\n"+ ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin AUTO_INCREMENT=5100 /*T![auto_rand_base] AUTO_RANDOM_BASE=6001 */", )) @@ -1043,7 +1043,7 @@ func (s *testSuite5) TestShowEscape(c *C) { ""+ "t`abl\"e CREATE TABLE `t``abl\"e` (\n"+ " `c``olum\"n` int(11) NOT NULL,\n"+ - " PRIMARY KEY (`c``olum\"n`)\n"+ + " PRIMARY KEY (`c``olum\"n`) /*T![clustered_index] CLUSTERED */\n"+ ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin", )) @@ -1054,7 +1054,7 @@ func (s *testSuite5) TestShowEscape(c *C) { ""+ "t`abl\"e CREATE TABLE \"t`abl\"\"e\" (\n"+ " \"c`olum\"\"n\" int(11) NOT NULL,\n"+ - " PRIMARY KEY (\"c`olum\"\"n\")\n"+ + " PRIMARY KEY (\"c`olum\"\"n\") /*T![clustered_index] CLUSTERED */\n"+ ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin", )) diff --git a/executor/update_test.go b/executor/update_test.go index 165bd00f3e011..9a6e6ceebbb85 100644 --- a/executor/update_test.go +++ b/executor/update_test.go @@ -336,8 +336,8 @@ type testSuite11 struct { func (s *testSuite11) TestUpdateClusterIndex(c *C) { tk := testkit.NewTestKit(c, s.store) - tk.MustExec(`set @@tidb_enable_clustered_index=true`) tk.MustExec(`use test`) + tk.Se.GetSessionVars().EnableClusteredIndex = true tk.MustExec(`drop table if exists t`) tk.MustExec(`create table t(id varchar(200) primary key, v int)`) @@ -388,8 +388,8 @@ func (s *testSuite11) TestUpdateClusterIndex(c *C) { func (s *testSuite11) TestDeleteClusterIndex(c *C) { tk := testkit.NewTestKit(c, s.store) - tk.MustExec(`set @@tidb_enable_clustered_index=true`) tk.MustExec(`use test`) + tk.Se.GetSessionVars().EnableClusteredIndex = true tk.MustExec(`drop table if exists t`) tk.MustExec(`create table t(id varchar(200) primary key, v int)`) @@ -423,8 +423,8 @@ func (s *testSuite11) TestDeleteClusterIndex(c *C) { func (s *testSuite11) TestReplaceClusterIndex(c *C) { tk := testkit.NewTestKit(c, s.store) - tk.MustExec(`set @@tidb_enable_clustered_index=true`) tk.MustExec(`use test`) + tk.Se.GetSessionVars().EnableClusteredIndex = true tk.MustExec(`drop table if exists rt1pk`) tk.MustExec(`create table rt1pk(id varchar(200) primary key, v int)`) @@ -455,7 +455,7 @@ func (s *testSuite11) TestPessimisticUpdatePKLazyCheck(c *C) { } func (s *testSuite11) testUpdatePKLazyCheck(c *C, tk *testkit.TestKit, clusteredIndex bool) { - tk.MustExec(fmt.Sprintf(`set @@tidb_enable_clustered_index=%v`, clusteredIndex)) + tk.Se.GetSessionVars().EnableClusteredIndex = clusteredIndex tk.MustExec(`drop table if exists upk`) tk.MustExec(`create table upk (a int, b int, c int, primary key (a, b))`) tk.MustExec(`insert upk values (1, 1, 1), (2, 2, 2), (3, 3, 3)`) diff --git a/executor/write_test.go b/executor/write_test.go index ff963fe455914..17a7af729e17b 100644 --- a/executor/write_test.go +++ b/executor/write_test.go @@ -3879,7 +3879,7 @@ func (s *testSerialSuite) TestIssue20840(c *C) { tk := testkit.NewTestKitWithInit(c, s.store) tk.MustExec("use test") tk.MustExec("drop table if exists t1") - tk.MustExec("set tidb_enable_clustered_index = 0") + tk.Se.GetSessionVars().EnableClusteredIndex = false tk.MustExec("create table t1 (i varchar(20) unique key) collate=utf8mb4_general_ci") tk.MustExec("insert into t1 values ('a')") tk.MustExec("replace into t1 values ('A')") diff --git a/expression/integration_test.go b/expression/integration_test.go index dd5bda2dbf901..c45db8537f19d 100644 --- a/expression/integration_test.go +++ b/expression/integration_test.go @@ -5350,7 +5350,7 @@ func (s *testIntegrationSuite) TestIssue16973(c *C) { tk := testkit.NewTestKit(c, s.store) tk.MustExec("use test") tk.MustExec("drop table if exists t1") - tk.MustExec("set @@tidb_enable_clustered_index=0;") + tk.Se.GetSessionVars().EnableClusteredIndex = false tk.MustExec("create table t1(id varchar(36) not null primary key, org_id varchar(36) not null, " + "status tinyint default 1 not null, ns varchar(36) default '' not null);") tk.MustExec("create table t2(id varchar(36) not null primary key, order_id varchar(36) not null, " + @@ -6908,7 +6908,7 @@ func (s *testIntegrationSerialSuite) TestNewCollationCheckClusterIndexTable(c *C tk := testkit.NewTestKit(c, s.store) tk.MustExec("use test") tk.MustExec("drop table if exists t") - tk.MustExec("set tidb_enable_clustered_index=1") + tk.Se.GetSessionVars().EnableClusteredIndex = true tk.MustExec("create table t(name char(255) primary key, b int, c int, index idx(name), unique index uidx(name))") tk.MustExec("insert into t values(\"aaaa\", 1, 1), (\"bbb\", 2, 2), (\"ccc\", 3, 3)") tk.MustExec("admin check table t") @@ -7016,7 +7016,7 @@ func (s *testIntegrationSerialSuite) TestNewCollationWithClusterIndex(c *C) { tk := testkit.NewTestKit(c, s.store) tk.MustExec("use test") tk.MustExec("drop table if exists t") - tk.MustExec("set tidb_enable_clustered_index=1") + tk.Se.GetSessionVars().EnableClusteredIndex = true tk.MustExec("create table t(d double primary key, a int, name varchar(255), index idx(name(2)), index midx(a, name))") tk.MustExec("insert into t values(2.11, 1, \"aa\"), (-1, 0, \"abcd\"), (9.99, 0, \"aaaa\")") tk.MustQuery("select d from t use index(idx) where name=\"aa\"").Check(testkit.Rows("2.11")) @@ -7936,7 +7936,7 @@ func (s *testIntegrationSerialSuite) TestClusteredIndexAndNewCollation(c *C) { tk := testkit.NewTestKit(c, s.store) tk.MustExec("use test") tk.MustExec("drop table if exists t") - tk.MustExec("set @@tidb_enable_clustered_index = 1;") + tk.Se.GetSessionVars().EnableClusteredIndex = true tk.MustExec("CREATE TABLE `t` (" + "`a` char(10) COLLATE utf8mb4_unicode_ci NOT NULL," + "`b` char(20) COLLATE utf8mb4_general_ci NOT NULL," + @@ -8366,8 +8366,8 @@ func (s *testIntegrationSerialSuite) TestIssue20876(c *C) { collate.SetNewCollationEnabledForTest(true) defer collate.SetNewCollationEnabledForTest(false) tk := testkit.NewTestKit(c, s.store) - tk.MustExec("set @@tidb_enable_clustered_index=1;") tk.MustExec("use test") + tk.Se.GetSessionVars().EnableClusteredIndex = true tk.MustExec("drop table if exists t;") tk.MustExec("CREATE TABLE `t` (" + " `a` char(10) COLLATE utf8mb4_unicode_ci NOT NULL," + diff --git a/infoschema/tables_test.go b/infoschema/tables_test.go index c611a1224f9df..716d61f0cc5a0 100644 --- a/infoschema/tables_test.go +++ b/infoschema/tables_test.go @@ -244,7 +244,7 @@ func (s *testTableSuite) TestInfoschemaFieldValue(c *C) { "t CREATE TABLE `t` (\n" + " `c` int(11) NOT NULL AUTO_INCREMENT,\n" + " `d` int(11) DEFAULT NULL,\n" + - " PRIMARY KEY (`c`)\n" + + " PRIMARY KEY (`c`) /*T![clustered_index] CLUSTERED */\n" + ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin AUTO_INCREMENT=30002")) // Test auto_increment for table without auto_increment column diff --git a/planner/core/cbo_test.go b/planner/core/cbo_test.go index c13846c09888a..a6c8b12485ad8 100644 --- a/planner/core/cbo_test.go +++ b/planner/core/cbo_test.go @@ -911,7 +911,7 @@ func (s *testAnalyzeSuite) TestIndexEqualUnknown(c *C) { }() testKit.MustExec("use test") testKit.MustExec("drop table if exists t, t1") - testKit.MustExec("set @@tidb_enable_clustered_index=0") + testKit.Se.GetSessionVars().EnableClusteredIndex = false testKit.MustExec("CREATE TABLE t(a bigint(20) NOT NULL, b bigint(20) NOT NULL, c bigint(20) NOT NULL, PRIMARY KEY (a,c,b), KEY (b))") err = s.loadTableStats("analyzeSuiteTestIndexEqualUnknownT.json", dom) c.Assert(err, IsNil) diff --git a/planner/core/integration_test.go b/planner/core/integration_test.go index dd15c0c8dcdbf..38b91961178b6 100644 --- a/planner/core/integration_test.go +++ b/planner/core/integration_test.go @@ -898,7 +898,7 @@ func (s *testIntegrationSuite) TestMaxMinEliminate(c *C) { tk.MustExec("use test") tk.MustExec("drop table if exists t") tk.MustExec("create table t(a int primary key)") - tk.MustExec("set @@tidb_enable_clustered_index=1;") + tk.Se.GetSessionVars().EnableClusteredIndex = true tk.MustExec("create table cluster_index_t(a int, b int, c int, primary key (a, b));") var input []string @@ -933,7 +933,7 @@ func (s *testIntegrationSuite) TestIndexJoinUniqueCompositeIndex(c *C) { tk.MustExec("use test") tk.MustExec("drop table if exists t1, t2") - tk.MustExec("set @@tidb_enable_clustered_index=0") + tk.Se.GetSessionVars().EnableClusteredIndex = false tk.MustExec("create table t1(a int not null, c int not null)") tk.MustExec("create table t2(a int not null, b int not null, c int not null, primary key(a,b))") tk.MustExec("insert into t1 values(1,1)") @@ -1668,7 +1668,7 @@ func (s *testIntegrationSuite) TestIssue16935(c *C) { func (s *testIntegrationSuite) TestAccessPathOnClusterIndex(c *C) { tk := testkit.NewTestKit(c, s.store) tk.MustExec("use test") - tk.MustExec("set @@tidb_enable_clustered_index = 1") + tk.Se.GetSessionVars().EnableClusteredIndex = true tk.MustExec("drop table if exists t1") tk.MustExec("create table t1 (a int, b varchar(20), c decimal(40,10), d int, primary key(a,b), key(c))") tk.MustExec(`insert into t1 values (1,"111",1.1,11), (2,"222",2.2,12), (3,"333",3.3,13)`) @@ -1697,7 +1697,7 @@ func (s *testIntegrationSuite) TestClusterIndexUniqueDoubleRead(c *C) { tk.MustExec("create database cluster_idx_unique_double_read;") tk.MustExec("use cluster_idx_unique_double_read;") defer tk.MustExec("drop database cluster_idx_unique_double_read;") - tk.MustExec("set @@tidb_enable_clustered_index = 1") + tk.Se.GetSessionVars().EnableClusteredIndex = true tk.MustExec("drop table if exists t") tk.MustExec("create table t (a varchar(64), b varchar(64), uk int, v int, primary key(a, b), unique key uuk(uk));") @@ -1708,7 +1708,7 @@ func (s *testIntegrationSuite) TestClusterIndexUniqueDoubleRead(c *C) { func (s *testIntegrationSuite) TestIndexJoinOnClusteredIndex(c *C) { tk := testkit.NewTestKit(c, s.store) tk.MustExec("use test") - tk.MustExec("set @@tidb_enable_clustered_index = 1") + tk.Se.GetSessionVars().EnableClusteredIndex = true tk.MustExec("drop table if exists t1") tk.MustExec("create table t (a int, b varchar(20), c decimal(40,10), d int, primary key(a,b), key(c))") tk.MustExec(`insert into t values (1,"111",1.1,11), (2,"222",2.2,12), (3,"333",3.3,13)`) @@ -1735,7 +1735,7 @@ func (s *testIntegrationSerialSuite) TestIssue18984(c *C) { tk := testkit.NewTestKit(c, s.store) tk.MustExec("use test") tk.MustExec("drop table if exists t, t2") - tk.MustExec("set tidb_enable_clustered_index=1") + tk.Se.GetSessionVars().EnableClusteredIndex = true tk.MustExec("create table t(a int, b int, c int, primary key(a, b))") tk.MustExec("create table t2(a int, b int, c int, d int, primary key(a,b), index idx(c))") tk.MustExec("insert into t values(1,1,1), (2,2,2), (3,3,3)") @@ -1881,7 +1881,7 @@ func (s *testIntegrationSerialSuite) Test19942(c *C) { tk := testkit.NewTestKit(c, s.store) tk.MustExec("use test") - tk.MustExec("SET @@tidb_enable_clustered_index=1;") + tk.Se.GetSessionVars().EnableClusteredIndex = true tk.MustExec("CREATE TABLE test.`t` (" + " `a` int(11) NOT NULL," + " `b` varchar(10) COLLATE utf8_general_ci NOT NULL," + diff --git a/planner/core/partition_pruner_test.go b/planner/core/partition_pruner_test.go index 064bff95eb91c..e75d958ee1a33 100644 --- a/planner/core/partition_pruner_test.go +++ b/planner/core/partition_pruner_test.go @@ -68,7 +68,7 @@ func (s *testPartitionPruneSuit) TestHashPartitionPruner(c *C) { tk.MustExec("create database test_partition") tk.MustExec("use test_partition") tk.MustExec("drop table if exists t1, t2;") - tk.MustExec("set @@tidb_enable_clustered_index=0;") + tk.Se.GetSessionVars().EnableClusteredIndex = false tk.MustExec("create table t2(id int, a int, b int, primary key(id, a)) partition by hash(id + a) partitions 10;") tk.MustExec("create table t1(id int primary key, a int, b int) partition by hash(id) partitions 10;") tk.MustExec("create table t3(id int, a int, b int, primary key(id, a)) partition by hash(id) partitions 10;") @@ -97,7 +97,7 @@ func (s *testPartitionPruneSuit) TestListPartitionPruner(c *C) { tk.MustExec("drop database if exists test_partition;") tk.MustExec("create database test_partition") tk.MustExec("use test_partition") - tk.MustExec("set @@tidb_enable_clustered_index=0;") + tk.Se.GetSessionVars().EnableClusteredIndex = false tk.MustExec("set @@session.tidb_enable_table_partition = nightly") tk.MustExec("create table t1 (id int, a int, b int ) partition by list ( a ) (partition p0 values in (1,2,3,4,5), partition p1 values in (6,7,8,9,10,null));") tk.MustExec("create table t2 (a int, id int, b int) partition by list (a*3 + b - 2*a - b) (partition p0 values in (1,2,3,4,5), partition p1 values in (6,7,8,9,10,null));") diff --git a/planner/core/point_get_plan_test.go b/planner/core/point_get_plan_test.go index 625189eed8013..db6176d2fae2b 100644 --- a/planner/core/point_get_plan_test.go +++ b/planner/core/point_get_plan_test.go @@ -320,7 +320,7 @@ func (s *testPointGetSuite) TestCBOPointGet(c *C) { tk := testkit.NewTestKit(c, s.store) tk.MustExec("use test") tk.MustExec("drop table if exists t") - tk.MustExec("set @@tidb_enable_clustered_index=0") + tk.Se.GetSessionVars().EnableClusteredIndex = false tk.MustExec("create table t (a varchar(20), b int, c int, d int, primary key(a), unique key(b, c))") tk.MustExec("insert into t values('1',4,4,1), ('2',3,3,2), ('3',2,2,3), ('4',1,1,4)") @@ -393,7 +393,7 @@ func (s *testPointGetSuite) TestBatchPointGetPartition(c *C) { c.Assert(err, IsNil) tk.MustExec("use test") - tk.MustExec("set @@tidb_enable_clustered_index=1;") + tk.Se.GetSessionVars().EnableClusteredIndex = true tk.MustExec("drop table if exists t") tk.MustExec("create table t(a int primary key, b int) PARTITION BY HASH(a) PARTITIONS 4") tk.MustExec("insert into t values (1, 1), (2, 2), (3, 3), (4, 4)") @@ -568,7 +568,7 @@ func (s *testPointGetSuite) TestBatchPointGetWithInvisibleIndex(c *C) { func (s *testPointGetSuite) TestCBOShouldNotUsePointGet(c *C) { tk := testkit.NewTestKit(c, s.store) tk.MustExec("use test") - tk.MustExec("set @@tidb_enable_clustered_index=1;") + tk.Se.GetSessionVars().EnableClusteredIndex = true tk.MustExec("drop tables if exists t1, t2, t3, t4, t5") tk.MustExec("create table t1(id varchar(20) primary key)") tk.MustExec("create table t2(id varchar(20), unique(id))") diff --git a/server/conn_test.go b/server/conn_test.go index 8a54c13ff7dfc..5ed3cf6dfb7f7 100644 --- a/server/conn_test.go +++ b/server/conn_test.go @@ -676,7 +676,7 @@ func (ts *ConnTestSuite) TestPrefetchPointKeys(c *C) { tk := testkit.NewTestKitWithInit(c, ts.store) cc.ctx = &TiDBContext{Session: tk.Se} ctx := context.Background() - tk.MustExec("set @@tidb_enable_clustered_index=0") + tk.Se.GetSessionVars().EnableClusteredIndex = false tk.MustExec("create table prefetch (a int, b int, c int, primary key (a, b))") tk.MustExec("insert prefetch values (1, 1, 1), (2, 2, 2), (3, 3, 3)") tk.MustExec("begin optimistic") diff --git a/server/http_handler_test.go b/server/http_handler_test.go index fd1c3a95a7fac..592dabb6b9ed1 100644 --- a/server/http_handler_test.go +++ b/server/http_handler_test.go @@ -541,10 +541,8 @@ partition by range (a) txn2.Exec("insert into tidb.pt values (666, 'def')") err = txn2.Commit() c.Assert(err, IsNil) - - dbt.mustExec("set @@tidb_enable_clustered_index = 1") dbt.mustExec("drop table if exists t") - dbt.mustExec("create table t (a double, b varchar(20), c int, primary key(a,b))") + dbt.mustExec("create table t (a double, b varchar(20), c int, primary key(a,b) clustered)") dbt.mustExec("insert into t values(1.1,'111',1),(2.2,'222',2)") } diff --git a/sessionctx/binloginfo/binloginfo_test.go b/sessionctx/binloginfo/binloginfo_test.go index 213132c1f01d4..f6d9e4086fca1 100644 --- a/sessionctx/binloginfo/binloginfo_test.go +++ b/sessionctx/binloginfo/binloginfo_test.go @@ -187,7 +187,7 @@ func (s *testBinlogSuite) TestBinlog(c *C) { c.Assert(gotRows, DeepEquals, expected) // Test table primary key is not integer. - tk.MustExec("set @@tidb_enable_clustered_index=0;") + tk.Se.GetSessionVars().EnableClusteredIndex = false tk.MustExec("create table local_binlog2 (name varchar(64) primary key, age int)") tk.MustExec("insert local_binlog2 values ('abc', 16), ('def', 18)") tk.MustExec("delete from local_binlog2 where name = 'def'") @@ -592,6 +592,18 @@ func (s *testBinlogSuite) TestAddSpecialComment(c *C) { "create table t1 (id int) /*T![auto_id_cache] auto_id_cache=5 */ ;", "create table t1 (id int) /*T![auto_id_cache] auto_id_cache=5 */ ;", }, + { + "create table t1 (id int, a varchar(255), primary key (a, b) clustered);", + "create table t1 (id int, a varchar(255), primary key (a, b) /*T![clustered_index] clustered */ );", + }, + { + "create table t1 (id int, a varchar(255), primary key (a, b) nonclustered);", + "create table t1 (id int, a varchar(255), primary key (a, b) /*T![clustered_index] nonclustered */ );", + }, + { + "create table t1 (id int, a varchar(255), primary key (a, b) /*T![clustered_index] nonclustered */);", + "create table t1 (id int, a varchar(255), primary key (a, b) /*T![clustered_index] nonclustered */);", + }, } for _, ca := range testCase { re := binloginfo.AddSpecialComment(ca.input) diff --git a/statistics/selectivity_test.go b/statistics/selectivity_test.go index 39132e5baf7d0..d5aca3cf8a6e0 100644 --- a/statistics/selectivity_test.go +++ b/statistics/selectivity_test.go @@ -512,7 +512,7 @@ func (s *testStatsSuite) TestPrimaryKeySelectivity(c *C) { testKit := testkit.NewTestKit(c, s.store) testKit.MustExec("use test") testKit.MustExec("drop table if exists t") - testKit.MustExec("set @@tidb_enable_clustered_index=0") + testKit.Se.GetSessionVars().EnableClusteredIndex = false testKit.MustExec("create table t(a char(10) primary key, b int)") var input, output [][]string s.testData.GetTestCases(c, &input, &output) @@ -673,7 +673,7 @@ func (s *testStatsSuite) TestUniqCompEqualEst(c *C) { defer cleanEnv(c, s.store, s.do) testKit := testkit.NewTestKit(c, s.store) testKit.MustExec("use test") - testKit.MustExec("set @@tidb_enable_clustered_index=1;") + testKit.Se.GetSessionVars().EnableClusteredIndex = true testKit.MustExec("drop table if exists t") testKit.MustExec("create table t(a int, b int, primary key(a, b))") testKit.MustExec("insert into t values(1,1),(1,2),(1,3),(1,4),(1,5),(1,6),(1,7),(1,8),(1,9),(1,10)") diff --git a/table/tables/tables_test.go b/table/tables/tables_test.go index 5b446b497e8cf..36f13d45e0243 100644 --- a/table/tables/tables_test.go +++ b/table/tables/tables_test.go @@ -470,7 +470,7 @@ func (ts *testSuite) TestHiddenColumn(c *C) { " `a` int(11) NOT NULL,\n" + " `c` int(11) DEFAULT NULL,\n" + " `e` int(11) DEFAULT NULL,\n" + - " PRIMARY KEY (`a`)\n" + + " PRIMARY KEY (`a`) /*T![clustered_index] CLUSTERED */\n" + ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin")) // Test show (extended) columns @@ -572,7 +572,7 @@ func (ts *testSuite) TestHiddenColumn(c *C) { " `a` int(11) NOT NULL,\n" + " `c` int(11) DEFAULT NULL,\n" + " `e` int(11) DEFAULT NULL,\n" + - " PRIMARY KEY (`a`)\n" + + " PRIMARY KEY (`a`) /*T![clustered_index] CLUSTERED */\n" + ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin")) tk.MustQuery("show extended columns from t").Check(testutil.RowsWithSep("|", "a|int(11)|NO|PRI||", diff --git a/types/parser_driver/special_cmt_ctrl.go b/types/parser_driver/special_cmt_ctrl.go index c1b018b685705..5621c29e0acbc 100644 --- a/types/parser_driver/special_cmt_ctrl.go +++ b/types/parser_driver/special_cmt_ctrl.go @@ -34,6 +34,7 @@ func init() { parser.SpecialCommentsController.Register(string(FeatureIDAutoRandom)) parser.SpecialCommentsController.Register(string(FeatureIDAutoIDCache)) parser.SpecialCommentsController.Register(string(FeatureIDAutoRandomBase)) + parser.SpecialCommentsController.Register(string(FeatureClusteredIndex)) } // SpecialCommentVersionPrefix is the prefix of TiDB executable comments. @@ -58,6 +59,8 @@ const ( FeatureIDAutoIDCache featureID = "auto_id_cache" // FeatureIDAutoRandomBase is the `auto_random_base` feature. FeatureIDAutoRandomBase featureID = "auto_rand_base" + // FeatureClusteredIndex is the `clustered_index` feature. + FeatureClusteredIndex featureID = "clustered_index" ) // FeatureIDPatterns is used to record special comments patterns. @@ -65,4 +68,5 @@ var FeatureIDPatterns = map[featureID]*regexp.Regexp{ FeatureIDAutoRandom: regexp.MustCompile(`(?i)AUTO_RANDOM\b\s*(\s*\(\s*\d+\s*\)\s*)?`), FeatureIDAutoIDCache: regexp.MustCompile(`(?i)AUTO_ID_CACHE\s*=?\s*\d+\s*`), FeatureIDAutoRandomBase: regexp.MustCompile(`(?i)AUTO_RANDOM_BASE\s*=?\s*\d+\s*`), + FeatureClusteredIndex: regexp.MustCompile(`(?i)(NON)?CLUSTERED`), } diff --git a/util/admin/admin_integration_test.go b/util/admin/admin_integration_test.go index 989aa3df71a61..0332cc70ca204 100644 --- a/util/admin/admin_integration_test.go +++ b/util/admin/admin_integration_test.go @@ -109,7 +109,7 @@ func (s *testAdminSuite) TestAdminCheckTableClusterIndex(c *C) { tk.MustExec("create database admin_check_table_clustered_index;") tk.MustExec("use admin_check_table_clustered_index;") - tk.MustExec("set @@tidb_enable_clustered_index = 1;") + tk.Se.GetSessionVars().EnableClusteredIndex = true tk.MustExec("create table t (a bigint, b varchar(255), c int, primary key (a, b), index idx_0(a, b), index idx_1(b, c));") tk.MustExec("insert into t values (1, '1', 1);") diff --git a/util/ranger/ranger_test.go b/util/ranger/ranger_test.go index 634d08f35cf88..dd630e8ea1093 100644 --- a/util/ranger/ranger_test.go +++ b/util/ranger/ranger_test.go @@ -1219,7 +1219,7 @@ func (s *testRangerSuite) TestIndexRangeElimininatedProjection(c *C) { testKit := testkit.NewTestKit(c, store) testKit.MustExec("use test") testKit.MustExec("drop table if exists t") - testKit.MustExec("set @@tidb_enable_clustered_index=0") + testKit.Se.GetSessionVars().EnableClusteredIndex = false testKit.MustExec("create table t(a int not null, b int not null, primary key(a,b))") testKit.MustExec("insert into t values(1,2)") testKit.MustExec("analyze table t") @@ -1339,7 +1339,7 @@ func (s *testRangerSuite) TestCompIndexMultiColDNF1(c *C) { c.Assert(err, IsNil) testKit := testkit.NewTestKit(c, store) testKit.MustExec("use test") - testKit.MustExec("set @@tidb_enable_clustered_index=1;") + testKit.Se.GetSessionVars().EnableClusteredIndex = true testKit.MustExec("drop table if exists t") testKit.MustExec("create table t(a int, b int, c int, primary key(a,b));") testKit.MustExec("insert into t values(1,1,1),(2,2,3)") @@ -1373,7 +1373,7 @@ func (s *testRangerSuite) TestCompIndexMultiColDNF2(c *C) { c.Assert(err, IsNil) testKit := testkit.NewTestKit(c, store) testKit.MustExec("use test") - testKit.MustExec("set @@tidb_enable_clustered_index=1;") + testKit.Se.GetSessionVars().EnableClusteredIndex = true testKit.MustExec("drop table if exists t") testKit.MustExec("create table t(a int, b int, c int, primary key(a,b,c));") testKit.MustExec("insert into t values(1,1,1),(2,2,3)")