Skip to content

Commit

Permalink
Merge branch 'release-6.1' into cherry-pick-41036-to-release-6.1
Browse files Browse the repository at this point in the history
  • Loading branch information
guo-shaoge authored Mar 29, 2023
2 parents f351e98 + 9a875bd commit 24d4db6
Show file tree
Hide file tree
Showing 39 changed files with 789 additions and 171 deletions.
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -892,7 +892,7 @@ var defaultConf = Config{
},
Experimental: Experimental{},
EnableCollectExecutionInfo: true,
EnableTelemetry: true,
EnableTelemetry: false,
Labels: make(map[string]string),
EnableGlobalIndex: false,
Security: Security{
Expand Down
2 changes: 1 addition & 1 deletion config/config.toml.example
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ skip-register-to-dashboard = false
# When enabled, usage data (for example, instance versions) will be reported to PingCAP periodically for user experience analytics.
# If this config is set to `false` on all TiDB servers, telemetry will be always disabled regardless of the value of the global variable `tidb_enable_telemetry`.
# See PingCAP privacy policy for details: https://pingcap.com/en/privacy-policy/
enable-telemetry = true
enable-telemetry = false

# deprecate-integer-display-length is used to be compatible with MySQL 8.0 in which the integer declared with display length will be returned with
# a warning like `Integer display width is deprecated and will be removed in a future release`.
Expand Down
8 changes: 4 additions & 4 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,23 +341,23 @@ history-size=100`)
require.NoError(t, err)
require.NoError(t, f.Sync())
require.NoError(t, conf.Load(configFile))
require.True(t, conf.EnableTelemetry)
require.False(t, conf.EnableTelemetry)

_, err = f.WriteString(`
enable-table-lock = true
`)
require.NoError(t, err)
require.NoError(t, f.Sync())
require.NoError(t, conf.Load(configFile))
require.True(t, conf.EnableTelemetry)
require.False(t, conf.EnableTelemetry)

_, err = f.WriteString(`
enable-telemetry = false
enable-telemetry = true
`)
require.NoError(t, err)
require.NoError(t, f.Sync())
require.NoError(t, conf.Load(configFile))
require.False(t, conf.EnableTelemetry)
require.True(t, conf.EnableTelemetry)

_, err = f.WriteString(`
[security]
Expand Down
3 changes: 2 additions & 1 deletion ddl/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,8 @@ func needChangeColumnData(oldCol, newCol *model.ColumnInfo) bool {
toUnsigned := mysql.HasUnsignedFlag(newCol.GetFlag())
originUnsigned := mysql.HasUnsignedFlag(oldCol.GetFlag())
needTruncationOrToggleSign := func() bool {
return (newCol.GetFlen() > 0 && newCol.GetFlen() < oldCol.GetFlen()) || (toUnsigned != originUnsigned)
return (newCol.GetFlen() > 0 && (newCol.GetFlen() < oldCol.GetFlen() || newCol.GetDecimal() < oldCol.GetDecimal())) ||
(toUnsigned != originUnsigned)
}
// Ignore the potential max display length represented by integer's flen, use default flen instead.
defaultOldColFlen, _ := mysql.GetDefaultFieldLengthAndDecimal(oldCol.GetType())
Expand Down
6 changes: 5 additions & 1 deletion ddl/column_type_change_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2366,11 +2366,15 @@ func TestColumnTypeChangeBetweenFloatAndDouble(t *testing.T) {
prepare := func(createTableStmt string) {
tk.MustExec("drop table if exists t;")
tk.MustExec(createTableStmt)
tk.MustExec("insert into t values (36.4), (24.1);")
tk.MustExec("insert into t values (36.43), (24.1);")
}

prepare("create table t (a float(6,2));")
tk.MustExec("alter table t modify a double(6,2)")
tk.MustQuery("select a from t;").Check(testkit.Rows("36.43", "24.1"))

prepare("create table t (a float(6,2));")
tk.MustExec("alter table t modify a float(6,1)")
tk.MustQuery("select a from t;").Check(testkit.Rows("36.4", "24.1"))

prepare("create table t (a double(6,2));")
Expand Down
56 changes: 50 additions & 6 deletions ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1419,7 +1419,20 @@ func containsColumnOption(colDef *ast.ColumnDef, opTp ast.ColumnOptionType) bool

// IsAutoRandomColumnID returns true if the given column ID belongs to an auto_random column.
func IsAutoRandomColumnID(tblInfo *model.TableInfo, colID int64) bool {
return tblInfo.PKIsHandle && tblInfo.ContainsAutoRandomBits() && tblInfo.GetPkColInfo().ID == colID
if !tblInfo.ContainsAutoRandomBits() {
return false
}
if tblInfo.PKIsHandle {
return tblInfo.GetPkColInfo().ID == colID
} else if tblInfo.IsCommonHandle {
pk := tables.FindPrimaryIndex(tblInfo)
if pk == nil {
return false
}
offset := pk.Columns[0].Offset
return tblInfo.Columns[offset].ID == colID
}
return false
}

func checkGeneratedColumn(ctx sessionctx.Context, colDefs []*ast.ColumnDef) error {
Expand Down Expand Up @@ -1686,17 +1699,31 @@ func getPrimaryKey(tblInfo *model.TableInfo) *model.IndexInfo {
}

func setTableAutoRandomBits(ctx sessionctx.Context, tbInfo *model.TableInfo, colDefs []*ast.ColumnDef) error {
pkColName := tbInfo.GetPkName()
for _, col := range colDefs {
if containsColumnOption(col, ast.ColumnOptionAutoRandom) {
if col.Tp.GetType() != mysql.TypeLonglong {
return dbterror.ErrInvalidAutoRandom.GenWithStackByArgs(
fmt.Sprintf(autoid.AutoRandomOnNonBigIntColumn, types.TypeStr(col.Tp.GetType())))
}
if !tbInfo.PKIsHandle || col.Name.Name.L != pkColName.L {
errMsg := fmt.Sprintf(autoid.AutoRandomPKisNotHandleErrMsg, col.Name.Name.O)
return dbterror.ErrInvalidAutoRandom.GenWithStackByArgs(errMsg)
switch {
case tbInfo.PKIsHandle:
if tbInfo.GetPkName().L != col.Name.Name.L {
errMsg := fmt.Sprintf(autoid.AutoRandomMustFirstColumnInPK, col.Name.Name.O)
return dbterror.ErrInvalidAutoRandom.GenWithStackByArgs(errMsg)
}
case tbInfo.IsCommonHandle:
pk := tables.FindPrimaryIndex(tbInfo)
if pk == nil {
return dbterror.ErrInvalidAutoRandom.GenWithStackByArgs(autoid.AutoRandomNoClusteredPKErrMsg)
}
if col.Name.Name.L != pk.Columns[0].Name.L {
errMsg := fmt.Sprintf(autoid.AutoRandomMustFirstColumnInPK, col.Name.Name.O)
return dbterror.ErrInvalidAutoRandom.GenWithStackByArgs(errMsg)
}
default:
return dbterror.ErrInvalidAutoRandom.GenWithStackByArgs(autoid.AutoRandomNoClusteredPKErrMsg)
}

if containsColumnOption(col, ast.ColumnOptionAutoIncrement) {
return dbterror.ErrInvalidAutoRandom.GenWithStackByArgs(autoid.AutoRandomIncompatibleWithAutoIncErrMsg)
}
Expand Down Expand Up @@ -4642,9 +4669,26 @@ func checkIndexInModifiableColumns(columns []*model.ColumnInfo, idxColumns []*mo
return nil
}

func isClusteredPKColumn(col *table.Column, tblInfo *model.TableInfo) bool {
switch {
case tblInfo.PKIsHandle:
return mysql.HasPriKeyFlag(col.GetFlag())
case tblInfo.IsCommonHandle:
pk := tables.FindPrimaryIndex(tblInfo)
for _, c := range pk.Columns {
if c.Name.L == col.Name.L {
return true
}
}
return false
default:
return false
}
}

func checkAutoRandom(tableInfo *model.TableInfo, originCol *table.Column, specNewColumn *ast.ColumnDef) (uint64, error) {
var oldRandBits uint64
if originCol.IsPKHandleColumn(tableInfo) {
if isClusteredPKColumn(originCol, tableInfo) {
oldRandBits = tableInfo.AutoRandomBits
}
newRandBits, err := extractAutoRandomBitsFromColDef(specNewColumn)
Expand Down
52 changes: 36 additions & 16 deletions ddl/serial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -838,8 +838,11 @@ func TestAutoRandom(t *testing.T) {
require.EqualError(t, err, dbterror.ErrInvalidAutoRandom.GenWithStackByArgs(fmt.Sprintf(errMsg, args...)).Error())
}

assertPKIsNotHandle := func(sql, errCol string) {
assertInvalidAutoRandomErr(sql, autoid.AutoRandomPKisNotHandleErrMsg, errCol)
assertNotFirstColPK := func(sql, errCol string) {
assertInvalidAutoRandomErr(sql, autoid.AutoRandomMustFirstColumnInPK, errCol)
}
assertNoClusteredPK := func(sql string) {
assertInvalidAutoRandomErr(sql, autoid.AutoRandomNoClusteredPKErrMsg)
}
assertAlterValue := func(sql string) {
assertInvalidAutoRandomErr(sql, autoid.AutoRandomAlterErrMsg)
Expand Down Expand Up @@ -882,36 +885,36 @@ func TestAutoRandom(t *testing.T) {
tk.MustExec("drop table t")
}

// Only bigint column can set auto_random
// Only bigint column can set auto_random.
assertBigIntOnly("create table t (a char primary key auto_random(3), b int)", "char")
assertBigIntOnly("create table t (a varchar(255) primary key auto_random(3), b int)", "varchar")
assertBigIntOnly("create table t (a timestamp primary key auto_random(3), b int)", "timestamp")
assertBigIntOnly("create table t (a timestamp auto_random(3), b int, primary key (a, b) clustered)", "timestamp")

// PKIsHandle, but auto_random is defined on non-primary key.
assertPKIsNotHandle("create table t (a bigint auto_random (3) primary key, b bigint auto_random (3))", "b")
assertPKIsNotHandle("create table t (a bigint auto_random (3), b bigint auto_random(3), primary key(a))", "b")
assertPKIsNotHandle("create table t (a bigint auto_random (3), b bigint auto_random(3) primary key)", "a")
// Clustered, but auto_random is defined on non-primary key.
assertNotFirstColPK("create table t (a bigint auto_random (3) primary key, b bigint auto_random (3))", "b")
assertNotFirstColPK("create table t (a bigint auto_random (3), b bigint auto_random(3), primary key(a))", "b")
assertNotFirstColPK("create table t (a bigint auto_random (3), b bigint auto_random(3) primary key)", "a")
assertNotFirstColPK("create table t (a bigint auto_random, b bigint, primary key (b, a) clustered);", "a")

// PKIsNotHandle: no primary key.
assertPKIsNotHandle("create table t (a bigint auto_random(3), b int)", "a")
// PKIsNotHandle: primary key is not a single column.
assertPKIsNotHandle("create table t (a bigint auto_random(3), b bigint, primary key (a, b))", "a")
assertPKIsNotHandle("create table t (a bigint auto_random(3), b int, c char, primary key (a, c))", "a")
// No primary key.
assertNoClusteredPK("create table t (a bigint auto_random(3), b int)")

// PKIsNotHandle: nonclustered integer primary key.
assertPKIsNotHandle("create table t (a bigint auto_random(3) primary key nonclustered, b int)", "a")
assertPKIsNotHandle("create table t (a bigint auto_random(3) primary key nonclustered, b int)", "a")
assertPKIsNotHandle("create table t (a int, b bigint auto_random(3) primary key nonclustered)", "b")
// No clustered primary key.
assertNoClusteredPK("create table t (a bigint auto_random(3) primary key nonclustered, b int)")
assertNoClusteredPK("create table t (a int, b bigint auto_random(3) primary key nonclustered)")

// Can not set auto_random along with auto_increment.
assertWithAutoInc("create table t (a bigint auto_random(3) primary key auto_increment)")
assertWithAutoInc("create table t (a bigint primary key auto_increment auto_random(3))")
assertWithAutoInc("create table t (a bigint auto_increment primary key auto_random(3))")
assertWithAutoInc("create table t (a bigint auto_random(3) auto_increment, primary key (a))")
assertWithAutoInc("create table t (a bigint auto_random(3) auto_increment, b int, primary key (a, b) clustered)")

// Can not set auto_random along with default.
assertDefault("create table t (a bigint auto_random primary key default 3)")
assertDefault("create table t (a bigint auto_random(2) primary key default 5)")
assertDefault("create table t (a bigint auto_random(2) default 5, b int, primary key (a, b) clustered)")
mustExecAndDrop("create table t (a bigint auto_random primary key)", func() {
assertDefault("alter table t modify column a bigint auto_random default 3")
assertDefault("alter table t alter column a set default 3")
Expand All @@ -920,12 +923,14 @@ func TestAutoRandom(t *testing.T) {
// Overflow data type max length.
assertMaxOverflow("create table t (a bigint auto_random(64) primary key)", "a", 64)
assertMaxOverflow("create table t (a bigint auto_random(16) primary key)", "a", 16)
assertMaxOverflow("create table t (a bigint auto_random(16), b int, primary key (a, b) clustered)", "a", 16)
mustExecAndDrop("create table t (a bigint auto_random(5) primary key)", func() {
assertMaxOverflow("alter table t modify a bigint auto_random(64)", "a", 64)
assertMaxOverflow("alter table t modify a bigint auto_random(16)", "a", 16)
})

assertNonPositive("create table t (a bigint auto_random(0) primary key)")
assertNonPositive("create table t (a bigint auto_random(0), b int, primary key (a, b) clustered)")
tk.MustGetErrMsg("create table t (a bigint auto_random(-1) primary key)",
`[parser:1064]You have an error in your SQL syntax; check the manual that corresponds to your TiDB version for the right syntax to use line 1 column 38 near "-1) primary key)" `)

Expand All @@ -935,18 +940,26 @@ func TestAutoRandom(t *testing.T) {
mustExecAndDrop("create table t (a bigint auto_random(15) primary key)")
mustExecAndDrop("create table t (a bigint primary key auto_random(4))")
mustExecAndDrop("create table t (a bigint auto_random(4), primary key (a))")
mustExecAndDrop("create table t (a bigint auto_random(3), b bigint, primary key (a, b) clustered)")
mustExecAndDrop("create table t (a bigint auto_random(3), b int, c char, primary key (a, c) clustered)")

// Increase auto_random bits.
mustExecAndDrop("create table t (a bigint auto_random(5) primary key)", func() {
tk.MustExec("alter table t modify a bigint auto_random(8)")
tk.MustExec("alter table t modify a bigint auto_random(10)")
tk.MustExec("alter table t modify a bigint auto_random(12)")
})
mustExecAndDrop("create table t (a bigint auto_random(5), b char(255), primary key (a, b) clustered)", func() {
tk.MustExec("alter table t modify a bigint auto_random(8)")
tk.MustExec("alter table t modify a bigint auto_random(10)")
tk.MustExec("alter table t modify a bigint auto_random(12)")
})

// Auto_random can occur multiple times like other column attributes.
mustExecAndDrop("create table t (a bigint auto_random(3) auto_random(2) primary key)")
mustExecAndDrop("create table t (a bigint, b bigint auto_random(3) primary key auto_random(2))")
mustExecAndDrop("create table t (a bigint auto_random(1) auto_random(2) auto_random(3), primary key (a))")
mustExecAndDrop("create table t (a bigint auto_random(1) auto_random(2) auto_random(3), b int, primary key (a, b) clustered)")

// Add/drop the auto_random attribute is not allowed.
mustExecAndDrop("create table t (a bigint auto_random(3) primary key)", func() {
Expand All @@ -958,6 +971,10 @@ func TestAutoRandom(t *testing.T) {
assertAlterValue("alter table t modify column c bigint")
assertAlterValue("alter table t change column c d bigint")
})
mustExecAndDrop("create table t (a bigint, b char, c bigint auto_random(3), primary key(c, a) clustered)", func() {
assertAlterValue("alter table t modify column c bigint")
assertAlterValue("alter table t change column c d bigint")
})
mustExecAndDrop("create table t (a bigint primary key)", func() {
assertOnlyChangeFromAutoIncPK("alter table t modify column a bigint auto_random(3)")
})
Expand Down Expand Up @@ -985,6 +1002,9 @@ func TestAutoRandom(t *testing.T) {
mustExecAndDrop("create table t (a bigint auto_random(10) primary key)", func() {
assertDecreaseBitErr("alter table t modify column a bigint auto_random(1)")
})
mustExecAndDrop("create table t (a bigint auto_random(10), b int, primary key (a, b) clustered)", func() {
assertDecreaseBitErr("alter table t modify column a bigint auto_random(6)")
})

originStep := autoid.GetStep()
autoid.SetStep(1)
Expand Down
5 changes: 4 additions & 1 deletion executor/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2216,7 +2216,10 @@ func (b *executorBuilder) updateForUpdateTSIfNeeded(selectPlan plannercore.Physi
if !txnCtx.IsPessimistic {
return nil
}
if _, ok := selectPlan.(*plannercore.PointGetPlan); ok {

// The `forUpdateTS` should be refreshed for RC, or the `pointGetExecutor` may not read
// the latest data and no pessimistic locks would be acquired, thus the result is unexpected.
if _, ok := selectPlan.(*plannercore.PointGetPlan); ok && !b.ctx.GetSessionVars().IsPessimisticReadConsistency() {
return nil
}
// Activate the invalid txn, use the txn startTS as newForUpdateTS
Expand Down
15 changes: 15 additions & 0 deletions executor/ddl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1162,6 +1162,21 @@ func TestAutoRandomTableOption(t *testing.T) {
require.Contains(t, err.Error(), autoid.AutoRandomRebaseNotApplicable)
}

func TestAutoRandomClusteredPrimaryKey(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("create table t (a bigint auto_random(5), b int, primary key (a, b) clustered);")
tk.MustExec("insert into t (b) values (1);")
tk.MustExec("set @@allow_auto_random_explicit_insert = 0;")
tk.MustGetErrCode("insert into t values (100, 2);", errno.ErrInvalidAutoRandom)
tk.MustExec("set @@allow_auto_random_explicit_insert = 1;")
tk.MustExec("insert into t values (100, 2);")
tk.MustQuery("select b from t order by b;").Check(testkit.Rows("1", "2"))
tk.MustExec("alter table t modify column a bigint auto_random(6);")
}

// Test filter different kind of allocators.
// In special ddl type, for example:
// 1: ActionRenameTable : it will abandon all the old allocators.
Expand Down
2 changes: 1 addition & 1 deletion executor/explainfor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -949,7 +949,7 @@ func TestIndexMerge4PlanCache(t *testing.T) {
tk.MustExec("set @a=9, @b=10, @c=11;")
tk.MustQuery("execute stmt using @a, @a;").Check(testkit.Rows("10 10 10"))
tk.MustQuery("execute stmt using @a, @c;").Check(testkit.Rows("10 10 10", "11 11 11"))
tk.MustQuery("select @@last_plan_from_cache;").Check(testkit.Rows("1"))
tk.MustQuery("select @@last_plan_from_cache;").Check(testkit.Rows("0")) // a>=9 and a<=9 --> a=9
tk.MustQuery("execute stmt using @c, @a;").Check(testkit.Rows("10 10 10"))
tk.MustQuery("select @@last_plan_from_cache;").Check(testkit.Rows("1"))

Expand Down
13 changes: 3 additions & 10 deletions executor/grant.go
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,9 @@ func (e *GrantExec) grantLevelPriv(priv *ast.PrivElem, user *ast.UserSpec, inter
if priv.Priv == mysql.ExtendedPriv {
return e.grantDynamicPriv(priv.Name, user, internalSession)
}
if priv.Priv == mysql.UsagePriv {
return nil
}
switch e.Level.Level {
case ast.GrantLevelGlobal:
return e.grantGlobalLevel(priv, user, internalSession)
Expand Down Expand Up @@ -481,10 +484,6 @@ func (e *GrantExec) grantDynamicPriv(privName string, user *ast.UserSpec, intern

// grantGlobalLevel manipulates mysql.user table.
func (e *GrantExec) grantGlobalLevel(priv *ast.PrivElem, user *ast.UserSpec, internalSession sessionctx.Context) error {
if priv.Priv == 0 || priv.Priv == mysql.UsagePriv {
return nil
}

sql := new(strings.Builder)
sqlexec.MustFormatSQL(sql, `UPDATE %n.%n SET `, mysql.SystemDB, mysql.UserTable)
err := composeGlobalPrivUpdate(sql, priv.Priv, "Y")
Expand All @@ -499,9 +498,6 @@ func (e *GrantExec) grantGlobalLevel(priv *ast.PrivElem, user *ast.UserSpec, int

// grantDBLevel manipulates mysql.db table.
func (e *GrantExec) grantDBLevel(priv *ast.PrivElem, user *ast.UserSpec, internalSession sessionctx.Context) error {
if priv.Priv == mysql.UsagePriv {
return nil
}
for _, v := range mysql.StaticGlobalOnlyPrivs {
if v == priv.Priv {
return ErrWrongUsage.GenWithStackByArgs("DB GRANT", "GLOBAL PRIVILEGES")
Expand Down Expand Up @@ -534,9 +530,6 @@ func (e *GrantExec) grantDBLevel(priv *ast.PrivElem, user *ast.UserSpec, interna

// grantTableLevel manipulates mysql.tables_priv table.
func (e *GrantExec) grantTableLevel(priv *ast.PrivElem, user *ast.UserSpec, internalSession sessionctx.Context) error {
if priv.Priv == mysql.UsagePriv {
return nil
}
dbName := e.Level.DBName
if len(dbName) == 0 {
dbName = e.ctx.GetSessionVars().CurrentDB
Expand Down
Loading

0 comments on commit 24d4db6

Please sign in to comment.