From 5831de291d415718bb542f260fb0e1cd9348696f Mon Sep 17 00:00:00 2001 From: tiancaiamao Date: Fri, 26 Oct 2018 20:04:07 +0800 Subject: [PATCH] *: enable range typed table partition (#8011) --- .gitignore | 1 - ddl/db_change_test.go | 1 - ddl/db_test.go | 10 ---------- ddl/partition.go | 15 ++++++++++++++- executor/admin_test.go | 1 - executor/analyze_test.go | 1 - executor/show_test.go | 1 - executor/write_test.go | 3 --- planner/core/cbo_test.go | 1 - sessionctx/binloginfo/binloginfo_test.go | 1 - sessionctx/variable/session.go | 4 ++-- sessionctx/variable/tidb_vars.go | 6 +++++- sessionctx/variable/varsutil_test.go | 8 ++++---- statistics/ddl_test.go | 1 - statistics/dump_test.go | 1 - statistics/update_test.go | 3 --- table/tables/tables_test.go | 9 ++------- vendor/github.com/pingcap/parser/model/model.go | 1 + 18 files changed, 28 insertions(+), 40 deletions(-) diff --git a/.gitignore b/.gitignore index e01efc8b13a7a..849203151548e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,6 @@ bin /interpreter/interpreter /interpreter/test -/parser/parser.go /tidb-server/tidb-server /tidb-server/debug coverage.out diff --git a/ddl/db_change_test.go b/ddl/db_change_test.go index ce9d7d3c053a2..b05ee4bec93e8 100644 --- a/ddl/db_change_test.go +++ b/ddl/db_change_test.go @@ -567,7 +567,6 @@ func (s *testStateChangeSuite) TestShowIndex(c *C) { callback = &ddl.TestDDLCallback{} d.(ddl.DDLForTest).SetHook(callback) - _, err = s.se.Execute(context.Background(), "set @@tidb_enable_table_partition = 1") c.Assert(err, IsNil) _, err = s.se.Execute(context.Background(), `create table tr( diff --git a/ddl/db_test.go b/ddl/db_test.go index 274eca7290eba..590e2d392ea4b 100644 --- a/ddl/db_test.go +++ b/ddl/db_test.go @@ -556,7 +556,6 @@ func (s *testDBSuite) TestAddIndex(c *C) { func (s *testDBSuite) testAddIndex(c *C, testPartition bool, createTableSQL string) { s.tk = testkit.NewTestKit(c, s.store) s.tk.MustExec("use " + s.schemaName) - s.tk.MustExec("set @@tidb_enable_table_partition = 1") s.tk.MustExec("drop table if exists test_add_index") s.tk.MustExec(createTableSQL) @@ -2624,7 +2623,6 @@ func (s *testDBSuite) TestBackwardCompatibility(c *C) { func (s *testDBSuite) TestAlterTableAddPartition(c *C) { s.tk = testkit.NewTestKit(c, s.store) s.tk.MustExec("use test;") - s.tk.MustExec("set @@session.tidb_enable_table_partition=1") s.tk.MustExec("drop table if exists employees;") s.tk.MustExec(`create table employees ( id int not null, @@ -2725,7 +2723,6 @@ func (s *testDBSuite) TestAlterTableAddPartition(c *C) { func (s *testDBSuite) TestAlterTableDropPartition(c *C) { s.tk = testkit.NewTestKit(c, s.store) s.tk.MustExec("use test") - s.tk.MustExec("set @@session.tidb_enable_table_partition=1") s.tk.MustExec("drop table if exists employees") s.tk.MustExec(`create table employees ( id int not null, @@ -2863,7 +2860,6 @@ func (s *testDBSuite) TestAlterTableDropPartition(c *C) { func (s *testDBSuite) TestAddPartitionTooManyPartitions(c *C) { s.tk = testkit.NewTestKit(c, s.store) s.tk.MustExec("use test") - s.tk.MustExec("set @@session.tidb_enable_table_partition=1") count := ddl.PartitionCountLimit s.tk.MustExec("drop table if exists p1;") sql1 := `create table p1 ( @@ -2946,7 +2942,6 @@ func (s *testDBSuite) TestTruncatePartitionAndDropTable(c *C) { // Test truncate table partition. s.tk.MustExec("drop table if exists t3;") - s.tk.MustExec("set @@session.tidb_enable_table_partition=1;") s.tk.MustExec(`create table t3( id int, name varchar(50), purchased date @@ -2985,7 +2980,6 @@ func (s *testDBSuite) TestTruncatePartitionAndDropTable(c *C) { // Test drop table partition. s.tk.MustExec("drop table if exists t4;") - s.tk.MustExec("set @@session.tidb_enable_table_partition=1;") s.tk.MustExec(`create table t4( id int, name varchar(50), purchased date @@ -3053,7 +3047,6 @@ func (s *testDBSuite) TestTruncatePartitionAndDropTable(c *C) { func (s *testDBSuite) TestPartitionUniqueKeyNeedAllFieldsInPf(c *C) { s.tk = testkit.NewTestKit(c, s.store) s.tk.MustExec("use test;") - s.tk.MustExec("set @@session.tidb_enable_table_partition=1;") s.tk.MustExec("drop table if exists part1;") s.tk.MustExec(`create table part1 ( col1 int not null, @@ -3246,7 +3239,6 @@ func (s *testDBSuite) TestPartitionDropIndex(c *C) { s.tk = testkit.NewTestKit(c, s.store) done := make(chan error, 1) s.tk.MustExec("use " + s.schemaName) - s.tk.MustExec("set @@session.tidb_enable_table_partition=1;") s.tk.MustExec("drop table if exists partition_drop_idx;") s.tk.MustExec(`create table partition_drop_idx ( c1 int, c2 int, c3 int @@ -3326,7 +3318,6 @@ LOOP: func (s *testDBSuite) TestPartitionCancelAddIndex(c *C) { s.tk = testkit.NewTestKit(c, s.store) s.mustExec(c, "use test_db") - s.mustExec(c, "set @@session.tidb_enable_table_partition=1;") s.mustExec(c, "drop table if exists t1;") s.mustExec(c, `create table t1 ( c1 int, c2 int, c3 int @@ -3565,7 +3556,6 @@ LOOP: func (s *testDBSuite) TestPartitionAddIndex(c *C) { tk := testkit.NewTestKit(c, s.store) tk.MustExec("use test") - tk.MustExec("set @@session.tidb_enable_table_partition=1;") tk.MustExec(`create table partition_add_idx ( id int not null, hired date not null diff --git a/ddl/partition.go b/ddl/partition.go index b2fdb760a4c44..18058ed83d02e 100644 --- a/ddl/partition.go +++ b/ddl/partition.go @@ -42,9 +42,22 @@ func buildTablePartitionInfo(ctx sessionctx.Context, d *ddl, s *ast.CreateTableS if s.Partition == nil { return nil, nil } + var enable bool + switch ctx.GetSessionVars().EnableTablePartition { + case "on": + enable = true + case "off": + enable = false + default: + // When tidb_enable_table_partition = 'auto', + // Partition by range expression is enabled by default. + if s.Partition.Tp == model.PartitionTypeRange && s.Partition.ColumnNames == nil { + enable = true + } + } pi := &model.PartitionInfo{ Type: s.Partition.Tp, - Enable: ctx.GetSessionVars().EnableTablePartition, + Enable: enable, } if s.Partition.Expr != nil { buf := new(bytes.Buffer) diff --git a/executor/admin_test.go b/executor/admin_test.go index e80d9f84d0c5a..40220e8aee3cc 100644 --- a/executor/admin_test.go +++ b/executor/admin_test.go @@ -447,7 +447,6 @@ func (s *testSuite) TestAdminCheckTable(c *C) { // Test partitioned table. tk.MustExec(`drop table if exists test`) - tk.MustExec(`set @@tidb_enable_table_partition = 1`) tk.MustExec(`create table test ( a int not null, c int not null, diff --git a/executor/analyze_test.go b/executor/analyze_test.go index c5ba232fb07de..6e362fbdfc12e 100644 --- a/executor/analyze_test.go +++ b/executor/analyze_test.go @@ -28,7 +28,6 @@ import ( func (s *testSuite) TestAnalyzePartition(c *C) { tk := testkit.NewTestKit(c, s.store) - tk.MustExec("set @@session.tidb_enable_table_partition=1") tk.MustExec("use test") tk.MustExec("drop table if exists t") createTable := `CREATE TABLE t (a int, b int, c varchar(10), primary key(a), index idx(b)) diff --git a/executor/show_test.go b/executor/show_test.go index 9c09d7dc81598..5d89eaf9cfce7 100644 --- a/executor/show_test.go +++ b/executor/show_test.go @@ -620,7 +620,6 @@ func (s *testSuite) TestShowTableStatus(c *C) { tk := testkit.NewTestKit(c, s.store) tk.MustExec("use test") - tk.MustExec("set @@session.tidb_enable_table_partition=1;") tk.MustExec(`drop table if exists t;`) tk.MustExec(`create table t(a bigint);`) diff --git a/executor/write_test.go b/executor/write_test.go index 59f6667946615..c2fbf33f4bd9e 100644 --- a/executor/write_test.go +++ b/executor/write_test.go @@ -791,7 +791,6 @@ func (s *testSuite) TestReplace(c *C) { func (s *testSuite) TestPartitionedTableReplace(c *C) { tk := testkit.NewTestKit(c, s.store) tk.MustExec("use test") - tk.MustExec("set @@session.tidb_enable_table_partition=1") testSQL := `drop table if exists replace_test; create table replace_test (id int PRIMARY KEY AUTO_INCREMENT, c1 int, c2 int, c3 int default 1) partition by range (id) ( @@ -1080,7 +1079,6 @@ func (s *testSuite) TestUpdate(c *C) { func (s *testSuite) TestPartitionedTableUpdate(c *C) { tk := testkit.NewTestKit(c, s.store) - tk.MustExec("set @@session.tidb_enable_table_partition=1") tk.MustExec("use test") tk.MustExec("drop table if exists t") tk.MustExec(`create table t (id int not null default 1, name varchar(255)) @@ -1327,7 +1325,6 @@ func (s *testSuite) TestPartitionedTableDelete(c *C) { PARTITION p3 VALUES LESS THAN (21))` tk := testkit.NewTestKit(c, s.store) - tk.MustExec("set @@session.tidb_enable_table_partition=1") tk.MustExec("use test") tk.MustExec("drop table if exists t") tk.MustExec(createTable) diff --git a/planner/core/cbo_test.go b/planner/core/cbo_test.go index e8d01f3353ef7..ffe7d9c1016d2 100644 --- a/planner/core/cbo_test.go +++ b/planner/core/cbo_test.go @@ -441,7 +441,6 @@ func (s *testAnalyzeSuite) TestAnalyze(c *C) { testKit.MustExec("create table t3 (a int, b int)") testKit.MustExec("create index a on t3 (a)") - testKit.MustExec("set @@session.tidb_enable_table_partition=1") testKit.MustExec("create table t4 (a int, b int) partition by range (a) (partition p1 values less than (2), partition p2 values less than (3))") testKit.MustExec("create index a on t4 (a)") testKit.MustExec("create index b on t4 (b)") diff --git a/sessionctx/binloginfo/binloginfo_test.go b/sessionctx/binloginfo/binloginfo_test.go index 73ca53abfd4d7..e6d87fc46b9c6 100644 --- a/sessionctx/binloginfo/binloginfo_test.go +++ b/sessionctx/binloginfo/binloginfo_test.go @@ -401,7 +401,6 @@ func (s *testBinlogSuite) TestPartitionedTable(c *C) { tk := testkit.NewTestKit(c, s.store) tk.MustExec("use test") tk.Se.GetSessionVars().BinlogClient = s.client - tk.MustExec("set @@session.tidb_enable_table_partition=1") tk.MustExec("drop table if exists t") tk.MustExec(`create table t (id int) partition by range (id) ( partition p0 values less than (1), diff --git a/sessionctx/variable/session.go b/sessionctx/variable/session.go index 784bb8d24f049..f08bb7ba40868 100644 --- a/sessionctx/variable/session.go +++ b/sessionctx/variable/session.go @@ -287,7 +287,7 @@ type SessionVars struct { OptimizerSelectivityLevel int // EnableTablePartition enables table partition feature. - EnableTablePartition bool + EnableTablePartition string // EnableCascadesPlanner enables the cascades planner. EnableCascadesPlanner bool @@ -617,7 +617,7 @@ func (s *SessionVars) SetSystemVar(name string, val string) error { case TiDBOptimizerSelectivityLevel: s.OptimizerSelectivityLevel = tidbOptPositiveInt32(val, DefTiDBOptimizerSelectivityLevel) case TiDBEnableTablePartition: - s.EnableTablePartition = TiDBOptOn(val) + s.EnableTablePartition = val case TiDBDDLReorgWorkerCount: SetDDLReorgWorkerCounter(int32(tidbOptPositiveInt32(val, DefTiDBDDLReorgWorkerCount))) case TiDBDDLReorgPriority: diff --git a/sessionctx/variable/tidb_vars.go b/sessionctx/variable/tidb_vars.go index fd2fa6588d7bb..5787d2a99e01f 100644 --- a/sessionctx/variable/tidb_vars.go +++ b/sessionctx/variable/tidb_vars.go @@ -107,7 +107,11 @@ const ( // tidb_optimizer_selectivity_level is used to control the selectivity estimation level. TiDBOptimizerSelectivityLevel = "tidb_optimizer_selectivity_level" - // tidb_enable_table_partition is used to enable table partition feature. + // tidb_enable_table_partition is used to control table partition feature. + // The valid value include auto/on/off: + // auto: enable table partition when that feature is implemented. + // on: always enable table partition. + // off: always disable table partition. TiDBEnableTablePartition = "tidb_enable_table_partition" ) diff --git a/sessionctx/variable/varsutil_test.go b/sessionctx/variable/varsutil_test.go index 147ef678eecd7..c93ac9911973c 100644 --- a/sessionctx/variable/varsutil_test.go +++ b/sessionctx/variable/varsutil_test.go @@ -217,11 +217,11 @@ func (s *testVarsutilSuite) TestVarsutil(c *C) { c.Assert(val, Equals, "3") c.Assert(v.RetryLimit, Equals, int64(3)) - c.Assert(v.EnableTablePartition, IsFalse) - err = SetSessionSystemVar(v, TiDBEnableTablePartition, types.NewStringDatum("1")) + c.Assert(v.EnableTablePartition, Equals, "") + err = SetSessionSystemVar(v, TiDBEnableTablePartition, types.NewStringDatum("on")) c.Assert(err, IsNil) val, err = GetSessionSystemVar(v, TiDBEnableTablePartition) c.Assert(err, IsNil) - c.Assert(val, Equals, "1") - c.Assert(v.EnableTablePartition, IsTrue) + c.Assert(val, Equals, "on") + c.Assert(v.EnableTablePartition, Equals, "on") } diff --git a/statistics/ddl_test.go b/statistics/ddl_test.go index 1d2c9368c0384..06eedfd5935b8 100644 --- a/statistics/ddl_test.go +++ b/statistics/ddl_test.go @@ -175,7 +175,6 @@ func (s *testStatsCacheSuite) TestDDLHistogram(c *C) { func (s *testStatsCacheSuite) TestDDLPartition(c *C) { defer cleanEnv(c, s.store, s.do) testKit := testkit.NewTestKit(c, s.store) - testKit.MustExec("set @@session.tidb_enable_table_partition=1") testKit.MustExec("use test") testKit.MustExec("drop table if exists t") createTable := `CREATE TABLE t (a int, b int, primary key(a), index idx(b)) diff --git a/statistics/dump_test.go b/statistics/dump_test.go index c3824d38b88f3..c165cea1669ec 100644 --- a/statistics/dump_test.go +++ b/statistics/dump_test.go @@ -79,7 +79,6 @@ func (s *testDumpStatsSuite) TestConversion(c *C) { func (s *testDumpStatsSuite) TestDumpPartitions(c *C) { defer cleanEnv(c, s.store, s.do) tk := testkit.NewTestKit(c, s.store) - tk.MustExec("set @@session.tidb_enable_table_partition=1") tk.MustExec("use test") tk.MustExec("drop table if exists t") createTable := `CREATE TABLE t (a int, b int, primary key(a), index idx(b)) diff --git a/statistics/update_test.go b/statistics/update_test.go index 34a8cb19638b6..082865fb6b6b5 100644 --- a/statistics/update_test.go +++ b/statistics/update_test.go @@ -308,7 +308,6 @@ func (s *testStatsUpdateSuite) TestTxnWithFailure(c *C) { func (s *testStatsUpdateSuite) TestUpdatePartition(c *C) { defer cleanEnv(c, s.store, s.do) testKit := testkit.NewTestKit(c, s.store) - testKit.MustExec("set @@session.tidb_enable_table_partition=1") testKit.MustExec("use test") testKit.MustExec("drop table if exists t") createTable := `CREATE TABLE t (a int, b char(5)) PARTITION BY RANGE (a) (PARTITION p0 VALUES LESS THAN (6),PARTITION p1 VALUES LESS THAN (11))` @@ -460,7 +459,6 @@ func (s *testStatsUpdateSuite) TestAutoUpdate(c *C) { func (s *testStatsUpdateSuite) TestAutoUpdatePartition(c *C) { defer cleanEnv(c, s.store, s.do) testKit := testkit.NewTestKit(c, s.store) - testKit.MustExec("set @@session.tidb_enable_table_partition=1") testKit.MustExec("use test") testKit.MustExec("drop table if exists t") testKit.MustExec("create table t (a int) PARTITION BY RANGE (a) (PARTITION p0 VALUES LESS THAN (6))") @@ -828,7 +826,6 @@ func (s *testStatsUpdateSuite) TestQueryFeedbackForPartition(c *C) { defer cleanEnv(c, s.store, s.do) testKit := testkit.NewTestKit(c, s.store) testKit.MustExec("use test") - testKit.MustExec("set @@session.tidb_enable_table_partition=1") testKit.MustExec(`create table t (a bigint(64), b bigint(64), primary key(a), index idx(b)) partition by range (a) ( partition p0 values less than (3), diff --git a/table/tables/tables_test.go b/table/tables/tables_test.go index fd46a6428bdd6..d4a1f640bb3ad 100644 --- a/table/tables/tables_test.go +++ b/table/tables/tables_test.go @@ -339,9 +339,7 @@ PARTITION BY RANGE ( id ) ( PARTITION p3 VALUES LESS THAN (21) )` - _, err := ts.se.Execute(context.Background(), "set @@session.tidb_enable_table_partition=1") - c.Assert(err, IsNil) - _, err = ts.se.Execute(context.Background(), "drop table if exists t1;") + _, err := ts.se.Execute(context.Background(), "drop table if exists t1;") c.Assert(err, IsNil) _, err = ts.se.Execute(context.Background(), createTable1) c.Assert(err, IsNil) @@ -408,9 +406,7 @@ PARTITION BY RANGE ( id ) ( PARTITION p3 VALUES LESS THAN (21) )` - _, err := ts.se.Execute(context.Background(), "set @@session.tidb_enable_table_partition=1") - c.Assert(err, IsNil) - _, err = ts.se.Execute(context.Background(), "Drop table if exists test.t1;") + _, err := ts.se.Execute(context.Background(), "Drop table if exists test.t1;") c.Assert(err, IsNil) _, err = ts.se.Execute(context.Background(), createTable1) c.Assert(err, IsNil) @@ -428,7 +424,6 @@ PARTITION BY RANGE ( id ) ( func (ts *testSuite) TestGeneratePartitionExpr(c *C) { _, err := ts.se.Execute(context.Background(), "use test") c.Assert(err, IsNil) - _, err = ts.se.Execute(context.Background(), "set @@session.tidb_enable_table_partition=1") c.Assert(err, IsNil) _, err = ts.se.Execute(context.Background(), "drop table if exists t1;") c.Assert(err, IsNil) diff --git a/vendor/github.com/pingcap/parser/model/model.go b/vendor/github.com/pingcap/parser/model/model.go index 22891f9d25ef3..6b12051a839a8 100644 --- a/vendor/github.com/pingcap/parser/model/model.go +++ b/vendor/github.com/pingcap/parser/model/model.go @@ -282,6 +282,7 @@ func (t *TableInfo) ColumnIsInIndex(c *ColumnInfo) bool { type PartitionType int // Partition types. +// The type value is persisted in the store, DO NOT change them. const ( PartitionTypeRange PartitionType = 1 PartitionTypeHash PartitionType = 2