Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ddl, variable: add system variable to control Clustered Index feature #17561

Merged
merged 6 commits into from
Jun 2, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1505,7 +1505,14 @@ func buildTableInfoWithStmt(ctx sessionctx.Context, s *ast.CreateTableStmt, dbCh
if err = handleTableOptions(s.Options, tbInfo); err != nil {
return nil, errors.Trace(err)
}

if ctx.GetSessionVars().EnableClusteredIndex && !tbInfo.PKIsHandle && !config.GetGlobalConfig().AlterPrimaryKey {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better to move to about L1267 if constr.Tp == ast.ConstraintPrimaryKey and deal with the invisible index checking.

for _, idx := range tbInfo.Indices {
if idx.Primary {
tbInfo.IsCommonHandle = true
break
}
}
}
return tbInfo, nil
}

Expand Down
36 changes: 36 additions & 0 deletions ddl/serial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1112,3 +1112,39 @@ 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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add some tests for CreateTableLike

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
}
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