Skip to content

Commit

Permalink
ddl, variable: add system variable to control Clustered Index feature (
Browse files Browse the repository at this point in the history
  • Loading branch information
coocood authored Jun 2, 2020
1 parent 8a164d2 commit 3adb557
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 7 deletions.
27 changes: 21 additions & 6 deletions ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1269,19 +1269,23 @@ func buildTableInfo(
if err != nil {
return nil, err
}
if len(constr.Keys) == 1 && !config.GetGlobalConfig().AlterPrimaryKey {
switch lastCol.Tp {
case mysql.TypeLong, mysql.TypeLonglong,
mysql.TypeTiny, mysql.TypeShort, mysql.TypeInt24:
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 {
tbInfo.PKIsHandle = true
// Avoid creating index for PK handle column.
continue
}
if clusteredIdx {
tbInfo.IsCommonHandle = true
}
}
}

Expand Down Expand Up @@ -1329,6 +1333,18 @@ func buildTableInfo(
return
}

func isSingleIntPK(constr *ast.Constraint, lastCol *model.ColumnInfo) bool {
if len(constr.Keys) != 1 {
return false
}
switch lastCol.Tp {
case mysql.TypeLong, mysql.TypeLonglong,
mysql.TypeTiny, mysql.TypeShort, mysql.TypeInt24:
return true
}
return false
}

// checkTableInfoValidExtra is like checkTableInfoValid, but also assumes the
// table info comes from untrusted source and performs further checks such as
// name length and column count.
Expand Down Expand Up @@ -1505,7 +1521,6 @@ func buildTableInfoWithStmt(ctx sessionctx.Context, s *ast.CreateTableStmt, dbCh
if err = handleTableOptions(s.Options, tbInfo); err != nil {
return nil, errors.Trace(err)
}

return tbInfo, nil
}

Expand Down
53 changes: 53 additions & 0 deletions ddl/serial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1112,3 +1112,56 @@ func (s *testSerialSuite) TestInvisibleIndex(c *C) {
tk.MustQuery("select * from t6").Check(testkit.Rows("1 2"))
tk.MustGetErrCode("alter table t6 drop primary key", errno.ErrPKIndexCantBeInvisible)
}

func (s *testSerialSuite) TestCreateClusteredIndex(c *C) {
tk := testkit.NewTestKitWithInit(c, s.store)
tk.Se.GetSessionVars().EnableClusteredIndex = true
tk.MustExec("CREATE TABLE t1 (a int primary key, b int)")
tk.MustExec("CREATE TABLE t2 (a varchar(255) primary key, b int)")
tk.MustExec("CREATE TABLE t3 (a int, b int, c int, primary key (a, b))")
tk.MustExec("CREATE TABLE t4 (a int, b int, c int)")
ctx := tk.Se.(sessionctx.Context)
is := domain.GetDomain(ctx).InfoSchema()
tbl, err := is.TableByName(model.NewCIStr("test"), model.NewCIStr("t1"))
c.Assert(err, IsNil)
c.Assert(tbl.Meta().PKIsHandle, IsTrue)
c.Assert(tbl.Meta().IsCommonHandle, IsFalse)
tbl, err = is.TableByName(model.NewCIStr("test"), model.NewCIStr("t2"))
c.Assert(err, IsNil)
c.Assert(tbl.Meta().IsCommonHandle, IsTrue)
tbl, err = is.TableByName(model.NewCIStr("test"), model.NewCIStr("t3"))
c.Assert(err, IsNil)
c.Assert(tbl.Meta().IsCommonHandle, IsTrue)
tbl, err = is.TableByName(model.NewCIStr("test"), model.NewCIStr("t4"))
c.Assert(err, IsNil)
c.Assert(tbl.Meta().IsCommonHandle, IsFalse)

config.GetGlobalConfig().AlterPrimaryKey = true
tk.MustExec("CREATE TABLE t5 (a varchar(255) primary key, b int)")
tk.MustExec("CREATE TABLE t6 (a int, b int, c int, primary key (a, b))")
is = domain.GetDomain(ctx).InfoSchema()
tbl, err = is.TableByName(model.NewCIStr("test"), model.NewCIStr("t5"))
c.Assert(err, IsNil)
c.Assert(tbl.Meta().IsCommonHandle, IsFalse)
tbl, err = is.TableByName(model.NewCIStr("test"), model.NewCIStr("t6"))
c.Assert(err, IsNil)
c.Assert(tbl.Meta().IsCommonHandle, IsFalse)
config.GetGlobalConfig().AlterPrimaryKey = false

tk.MustExec("CREATE TABLE t21 like t2")
tk.MustExec("CREATE TABLE t31 like t3")
is = domain.GetDomain(ctx).InfoSchema()
tbl, err = is.TableByName(model.NewCIStr("test"), model.NewCIStr("t21"))
c.Assert(err, IsNil)
c.Assert(tbl.Meta().IsCommonHandle, IsTrue)
tbl, err = is.TableByName(model.NewCIStr("test"), model.NewCIStr("t31"))
c.Assert(err, IsNil)
c.Assert(tbl.Meta().IsCommonHandle, IsTrue)

tk.Se.GetSessionVars().EnableClusteredIndex = false
tk.MustExec("CREATE TABLE t7 (a varchar(255) primary key, b int)")
is = domain.GetDomain(ctx).InfoSchema()
tbl, err = is.TableByName(model.NewCIStr("test"), model.NewCIStr("t7"))
c.Assert(err, IsNil)
c.Assert(tbl.Meta().IsCommonHandle, IsFalse)
}
1 change: 1 addition & 0 deletions session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -2054,6 +2054,7 @@ var builtinGlobalVariable = []string{
variable.TiDBIsolationReadEngines,
variable.TiDBStoreLimit,
variable.TiDBAllowAutoRandExplicitInsert,
variable.TiDBEnableClusteredIndex,
}

var (
Expand Down
6 changes: 6 additions & 0 deletions sessionctx/variable/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,9 @@ type SessionVars struct {

// OptimizerUseInvisibleIndexes indicates whether optimizer can use invisible index
OptimizerUseInvisibleIndexes bool

// EnableClusteredIndex indicates whether to enable clustered index when creating a new table.
EnableClusteredIndex bool
}

// PreparedParams contains the parameters of the current prepared statement when executing it.
Expand Down Expand Up @@ -694,6 +697,7 @@ func NewSessionVars() *SessionVars {
PrevFoundInPlanCache: DefTiDBFoundInPlanCache,
FoundInPlanCache: DefTiDBFoundInPlanCache,
AllowAutoRandExplicitInsert: DefTiDBAllowAutoRandExplicitInsert,
EnableClusteredIndex: DefTiDBEnableClusteredIndex,
}
vars.KVVars = kv.NewVariables(&vars.Killed)
vars.Concurrency = Concurrency{
Expand Down Expand Up @@ -1296,6 +1300,8 @@ func (s *SessionVars) SetSystemVar(name string, val string) error {
config.GetGlobalConfig().EnableCollectExecutionInfo = TiDBOptOn(val)
case TiDBAllowAutoRandExplicitInsert:
s.AllowAutoRandExplicitInsert = TiDBOptOn(val)
case TiDBEnableClusteredIndex:
s.EnableClusteredIndex = TiDBOptOn(val)
}
s.systems[name] = val
return nil
Expand Down
1 change: 1 addition & 0 deletions sessionctx/variable/sysvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,7 @@ var defaultSysVars = []*SysVar{
{ScopeSession, TiDBFoundInPlanCache, BoolToIntStr(DefTiDBFoundInPlanCache)},
{ScopeSession, TiDBEnableCollectExecutionInfo, BoolToIntStr(logutil.DefaultTiDBEnableSlowLog)},
{ScopeSession, TiDBAllowAutoRandExplicitInsert, boolToOnOff(DefTiDBAllowAutoRandExplicitInsert)},
{ScopeGlobal | ScopeSession, TiDBEnableClusteredIndex, BoolToIntStr(DefTiDBEnableClusteredIndex)},
}

// SynonymsSysVariables is synonyms of system variables.
Expand Down
4 changes: 4 additions & 0 deletions sessionctx/variable/tidb_vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,9 @@ const (

// TiDBEnableCollectExecutionInfo indicates that whether execution info is collected.
TiDBEnableCollectExecutionInfo = "tidb_enable_collect_execution_info"

// TiDBEnableClusteredIndex indicates if clustered index feature is enabled.
TiDBEnableClusteredIndex = "tidb_enable_clustered_index"
)

// Default TiDB system variable values.
Expand Down Expand Up @@ -491,6 +494,7 @@ const (
DefTiDBFoundInPlanCache = false
DefTidbEnableCollectExecutionInfo = false
DefTiDBAllowAutoRandExplicitInsert = false
DefTiDBEnableClusteredIndex = false
)

// Process global variables.
Expand Down
2 changes: 1 addition & 1 deletion sessionctx/variable/varsutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ func ValidateSetSystemVar(vars *SessionVars, name string, value string, scope Sc
TiDBCheckMb4ValueInUTF8, TiDBEnableSlowLog, TiDBRecordPlanInSlowLog,
TiDBScatterRegion, TiDBGeneralLog, TiDBConstraintCheckInPlace,
TiDBEnableVectorizedExpression, TiDBFoundInPlanCache, TiDBEnableCollectExecutionInfo,
TiDBAllowAutoRandExplicitInsert:
TiDBAllowAutoRandExplicitInsert, TiDBEnableClusteredIndex:
fallthrough
case GeneralLog, AvoidTemporalUpgrade, BigTables, CheckProxyUsers, LogBin,
CoreFile, EndMakersInJSON, SQLLogBin, OfflineMode, PseudoSlaveMode, LowPriorityUpdates,
Expand Down

0 comments on commit 3adb557

Please sign in to comment.