Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/pingcap/tidb into forbidB…
Browse files Browse the repository at this point in the history
…aselineEvolution
  • Loading branch information
Reminiscent committed Aug 4, 2021
2 parents 5756178 + 47514c2 commit 98ba826
Show file tree
Hide file tree
Showing 83 changed files with 1,878 additions and 695 deletions.
3 changes: 3 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,8 @@ type Security struct {
SpilledFileEncryptionMethod string `toml:"spilled-file-encryption-method" json:"spilled-file-encryption-method"`
// EnableSEM prevents SUPER users from having full access.
EnableSEM bool `toml:"enable-sem" json:"enable-sem"`
// Allow automatic TLS certificate generation
AutoTLS bool `toml:"auto-tls" json:"auto-tls"`
}

// The ErrConfigValidationFailed error is used so that external callers can do a type assertion
Expand Down Expand Up @@ -674,6 +676,7 @@ var defaultConf = Config{
Security: Security{
SpilledFileEncryptionMethod: SpilledFileEncryptionMethodPlaintext,
EnableSEM: false,
AutoTLS: true,
},
DeprecateIntegerDisplayWidth: false,
EnableEnumLengthLimit: true,
Expand Down
3 changes: 3 additions & 0 deletions config/config.toml.example
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,9 @@ spilled-file-encryption-method = "plaintext"
# Security Enhanced Mode (SEM) restricts the "SUPER" privilege and requires fine-grained privileges instead.
enable-sem = false

# Automatic creation of TLS certificates
auto-tls = true

[status]
# If enable status report HTTP service.
report-status = true
Expand Down
11 changes: 11 additions & 0 deletions ddl/db_change_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/pingcap/parser/terror"
"github.com/pingcap/tidb/ddl"
"github.com/pingcap/tidb/domain"
"github.com/pingcap/tidb/errno"
"github.com/pingcap/tidb/executor"
"github.com/pingcap/tidb/infoschema"
"github.com/pingcap/tidb/kv"
Expand Down Expand Up @@ -1871,3 +1872,13 @@ func (s *serialTestStateChangeSuite) TestCreateExpressionIndex(c *C) {
tk.MustExec("admin check table t")
tk.MustQuery("select * from t order by a, b").Check(testkit.Rows("0 9", "0 11", "0 11", "1 7", "2 7", "5 7", "8 8", "10 10", "10 10"))
}

func (s *testStateChangeSuite) TestExpressionIndexDDLError(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test_db_state")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a int, b int, index idx((a+b)))")
tk.MustGetErrCode("alter table t rename column b to b2", errno.ErrDependentByFunctionalIndex)
tk.MustGetErrCode("alter table t drop column b", errno.ErrDependentByFunctionalIndex)
tk.MustExec("drop table t")
}
97 changes: 97 additions & 0 deletions ddl/db_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3134,3 +3134,100 @@ func (s *testIntegrationSuite3) TestDropTemporaryTable(c *C) {
c.Assert(err.Error(), Equals, "[schema:1051]Unknown table 'test.a_local_temp_table_9_not_exist'")
tk.MustQuery("select * from a_local_temp_table_8").Check(testkit.Rows())
}

func (s *testIntegrationSuite3) TestTruncateLocalTemporaryTable(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("set @@tidb_enable_noop_functions = 1")

tk.MustExec("drop table if exists t1, tn")
tk.MustExec("create table t1 (id int)")
tk.MustExec("create table tn (id int)")
tk.MustExec("insert into t1 values(10), (11), (12)")
tk.MustExec("create temporary table t1 (id int primary key auto_increment)")
tk.MustExec("create temporary table t2 (id int primary key)")
tk.MustExec("create database test2")
tk.MustExec("create temporary table test2.t2 (id int)")

// truncate table out of txn
tk.MustExec("insert into t1 values(1), (2), (3)")
tk.MustExec("insert into t2 values(4), (5), (6)")
tk.MustExec("insert into test2.t2 values(7), (8), (9)")
tk.MustExec("truncate table t1")
tk.MustQuery("select * from t1").Check(testkit.Rows())
tk.MustExec("insert into t1 values()")
// auto_increment will be reset for truncate
tk.MustQuery("select * from t1").Check(testkit.Rows("1"))
tk.MustQuery("select * from t2").Check(testkit.Rows("4", "5", "6"))
tk.MustExec("truncate table t2")
tk.MustQuery("select * from t2").Check(testkit.Rows())
tk.MustQuery("select * from test2.t2").Check(testkit.Rows("7", "8", "9"))
tk.MustExec("drop table t1")
tk.MustQuery("select * from t1").Check(testkit.Rows("10", "11", "12"))
tk.MustExec("create temporary table t1 (id int primary key auto_increment)")

// truncate table with format dbName.tableName
tk.MustExec("insert into t2 values(4), (5), (6)")
tk.MustExec("insert into test2.t2 values(7), (8), (9)")
tk.MustExec("truncate table test2.t2")
tk.MustQuery("select * from test2.t2").Check(testkit.Rows())
tk.MustQuery("select * from t2").Check(testkit.Rows("4", "5", "6"))
tk.MustExec("truncate table test.t2")
tk.MustQuery("select * from t2").Check(testkit.Rows())

// truncate table in txn
tk.MustExec("insert into t1 values(1), (2), (3)")
tk.MustExec("insert into t2 values(4), (5), (6)")
tk.MustExec("begin")
tk.MustExec("insert into t1 values(11), (12)")
tk.MustExec("insert into t2 values(24), (25)")
tk.MustExec("delete from t1 where id=2")
tk.MustExec("delete from t2 where id=4")
tk.MustExec("truncate table t1")
tk.MustQuery("select * from t1").Check(testkit.Rows())
tk.MustExec("insert into t1 values()")
// auto_increment will be reset for truncate
tk.MustQuery("select * from t1").Check(testkit.Rows("1"))
tk.MustQuery("select * from t2").Check(testkit.Rows("5", "6", "24", "25"))

// since transaction already committed by truncate, so query after rollback will get same result
tk.MustExec("rollback")
tk.MustQuery("select * from t1").Check(testkit.Rows("1"))
tk.MustQuery("select * from t2").Check(testkit.Rows("5", "6", "24", "25"))

// truncate a temporary table will not effect the normal table with the same name
tk.MustExec("drop table t1")
tk.MustQuery("select * from t1").Check(testkit.Rows("10", "11", "12"))
tk.MustExec("create temporary table t1 (id int primary key auto_increment)")

// truncate temporary table will clear session data
localTemporaryTables := tk.Se.GetSessionVars().LocalTemporaryTables.(*infoschema.LocalTemporaryTables)
tb1, exist := localTemporaryTables.TableByName(model.NewCIStr("test"), model.NewCIStr("t1"))
tbl1Info := tb1.Meta()
tablePrefix := tablecodec.EncodeTablePrefix(tbl1Info.ID)
endTablePrefix := tablecodec.EncodeTablePrefix(tbl1Info.ID + 1)
c.Assert(exist, IsTrue)
tk.MustExec("insert into t1 values(1), (2), (3)")
tk.MustExec("begin")
tk.MustExec("insert into t1 values(5), (6), (7)")
tk.MustExec("truncate table t1")
iter, err := tk.Se.GetSessionVars().TemporaryTableData.Iter(tablePrefix, endTablePrefix)
c.Assert(err, IsNil)
for iter.Valid() {
key := iter.Key()
if !bytes.HasPrefix(key, tablePrefix) {
break
}
value := iter.Value()
c.Assert(len(value), Equals, 0)
_ = iter.Next()
}
c.Assert(iter.Valid(), IsFalse)

// truncate after drop database should be successful
tk.MustExec("create temporary table test2.t3 (id int)")
tk.MustExec("insert into test2.t3 values(1)")
tk.MustExec("drop database test2")
tk.MustExec("truncate table test2.t3")
tk.MustQuery("select * from test2.t3").Check(testkit.Rows())
}
2 changes: 1 addition & 1 deletion ddl/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2520,7 +2520,7 @@ func (s *testDBSuite5) TestRenameColumn(c *C) {
assertColNames("test_rename_column", "id", "col2")
s.mustExec(tk, c, "alter table test_rename_column rename column col2 to col1")
assertColNames("test_rename_column", "id", "col1")
tk.MustGetErrCode("alter table test_rename_column rename column id to id1", errno.ErrBadField)
tk.MustGetErrCode("alter table test_rename_column rename column id to id1", errno.ErrDependentByGeneratedColumn)

// Test renaming view columns.
tk.MustExec("drop table test_rename_column")
Expand Down
10 changes: 8 additions & 2 deletions ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -4207,7 +4207,10 @@ func (d *ddl) RenameColumn(ctx sessionctx.Context, ident ast.Ident, spec *ast.Al
dependedColNames := findColumnNamesInExpr(col.GeneratedExpr)
for _, name := range dependedColNames {
if name.Name.L == oldColName.L {
return ErrBadField.GenWithStackByArgs(oldColName.O, "generated column function")
if col.Hidden {
return errDependentByFunctionalIndex.GenWithStackByArgs(oldColName.O)
}
return errDependentByGeneratedColumn.GenWithStackByArgs(oldColName.O)
}
}
}
Expand Down Expand Up @@ -5439,7 +5442,10 @@ func (d *ddl) DropIndex(ctx sessionctx.Context, ti ast.Ident, indexName model.CI
}

func isDroppableColumn(tblInfo *model.TableInfo, colName model.CIStr) error {
if ok, dep := hasDependentByGeneratedColumn(tblInfo, colName); ok {
if ok, dep, isHidden := hasDependentByGeneratedColumn(tblInfo, colName); ok {
if isHidden {
return errDependentByFunctionalIndex.GenWithStackByArgs(dep)
}
return errDependentByGeneratedColumn.GenWithStackByArgs(dep)
}

Expand Down
8 changes: 6 additions & 2 deletions ddl/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,12 @@ var (
errUnsupportedEngineTemporary = dbterror.ClassDDL.NewStdErr(mysql.ErrUnsupportedDDLOperation, parser_mysql.Message("TiDB doesn't support this kind of engine for temporary table", nil))
errUnsupportedClusteredSecondaryKey = dbterror.ClassDDL.NewStdErr(mysql.ErrUnsupportedDDLOperation, parser_mysql.Message("CLUSTERED/NONCLUSTERED keyword is only supported for primary key", nil))

// ErrUnsupportedLocalTempTableDDL returns when ddl operation unsupported for local temporary table
ErrUnsupportedLocalTempTableDDL = dbterror.ClassDDL.NewStdErr(mysql.ErrUnsupportedDDLOperation, parser_mysql.Message("TiDB doesn't support %s for local temporary table", nil))
// ErrInvalidAttributesSpec is returned when meeting invalid attributes.
ErrInvalidAttributesSpec = dbterror.ClassDDL.NewStd(mysql.ErrInvalidAttributesSpec)
// ErrFunctionalIndexOnJSONOrGeometryFunction returns when creating expression index and the type of the expression is JSON.
ErrFunctionalIndexOnJSONOrGeometryFunction = dbterror.ClassDDL.NewStd(mysql.ErrFunctionalIndexOnJSONOrGeometryFunction)
// errFunctionalIndexOnJSONOrGeometryFunction returns when creating expression index and the type of the expression is JSON.
errFunctionalIndexOnJSONOrGeometryFunction = dbterror.ClassDDL.NewStd(mysql.ErrFunctionalIndexOnJSONOrGeometryFunction)
// errDependentByFunctionalIndex returns when the dropped column depends by expression index.
errDependentByFunctionalIndex = dbterror.ClassDDL.NewStd(mysql.ErrDependentByFunctionalIndex)
)
8 changes: 4 additions & 4 deletions ddl/generated_column.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,15 @@ func findColumnNamesInExpr(expr ast.ExprNode) []*ast.ColumnName {
}

// hasDependentByGeneratedColumn checks whether there are other columns depend on this column or not.
func hasDependentByGeneratedColumn(tblInfo *model.TableInfo, colName model.CIStr) (bool, string) {
func hasDependentByGeneratedColumn(tblInfo *model.TableInfo, colName model.CIStr) (bool, string, bool) {
for _, col := range tblInfo.Columns {
for dep := range col.Dependences {
if dep == colName.L {
return true, dep
return true, dep, col.Hidden
}
}
}
return false, ""
return false, "", false
}

func isGeneratedRelatedColumn(tblInfo *model.TableInfo, newCol, col *model.ColumnInfo) error {
Expand All @@ -158,7 +158,7 @@ func isGeneratedRelatedColumn(tblInfo *model.TableInfo, newCol, col *model.Colum
msg := fmt.Sprintf("newCol IsGenerated %v, oldCol IsGenerated %v", newCol.IsGenerated(), col.IsGenerated())
return errUnsupportedModifyColumn.GenWithStackByArgs(msg)
}
if ok, dep := hasDependentByGeneratedColumn(tblInfo, col.Name); ok {
if ok, dep, _ := hasDependentByGeneratedColumn(tblInfo, col.Name); ok {
msg := fmt.Sprintf("oldCol is a dependent column '%s' for generated column", dep)
return errUnsupportedModifyColumn.GenWithStackByArgs(msg)
}
Expand Down
2 changes: 1 addition & 1 deletion ddl/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func checkIndexColumn(col *model.ColumnInfo, indexColumnLen int) error {
// JSON column cannot index.
if col.FieldType.Tp == mysql.TypeJSON {
if col.Hidden {
return ErrFunctionalIndexOnJSONOrGeometryFunction
return errFunctionalIndexOnJSONOrGeometryFunction
}
return errors.Trace(errJSONUsedAsKey.GenWithStackByArgs(col.Name.O))
}
Expand Down
26 changes: 26 additions & 0 deletions ddl/serial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1691,3 +1691,29 @@ func (s *testSerialSuite) TestCheckEnumLength(c *C) {
tk.MustGetErrCode("create table t5 (a set('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'))", errno.ErrTooLongValueForType)
tk.MustExec("drop table if exists t1,t2,t3,t4,t5")
}

func (s *testSerialDBSuite) TestLocalTemporaryTableBlockedDDL(c *C) {
tk := testkit.NewTestKitWithInit(c, s.store)
tk.MustExec("set @@tidb_enable_noop_functions = 1")
tk.MustExec("use test")
tk.MustExec("create table t1 (id int)")
tk.MustExec("create temporary table tmp1 (id int primary key, a int unique, b int)")
err := tk.ExecToErr("rename table tmp1 to tmp2")
c.Assert(ddl.ErrUnsupportedLocalTempTableDDL.Equal(err), IsTrue)
err = tk.ExecToErr("alter table tmp1 add column c int")
c.Assert(ddl.ErrUnsupportedLocalTempTableDDL.Equal(err), IsTrue)
err = tk.ExecToErr("alter table tmp1 add index b(b)")
c.Assert(ddl.ErrUnsupportedLocalTempTableDDL.Equal(err), IsTrue)
err = tk.ExecToErr("create index a on tmp1(b)")
c.Assert(ddl.ErrUnsupportedLocalTempTableDDL.Equal(err), IsTrue)
err = tk.ExecToErr("drop index a on tmp1")
c.Assert(ddl.ErrUnsupportedLocalTempTableDDL.Equal(err), IsTrue)
err = tk.ExecToErr("lock tables tmp1 read")
c.Assert(ddl.ErrUnsupportedLocalTempTableDDL.Equal(err), IsTrue)
err = tk.ExecToErr("lock tables tmp1 write")
c.Assert(ddl.ErrUnsupportedLocalTempTableDDL.Equal(err), IsTrue)
err = tk.ExecToErr("lock tables t1 read, tmp1 read")
c.Assert(ddl.ErrUnsupportedLocalTempTableDDL.Equal(err), IsTrue)
err = tk.ExecToErr("admin cleanup table lock tmp1")
c.Assert(ddl.ErrUnsupportedLocalTempTableDDL.Equal(err), IsTrue)
}
11 changes: 8 additions & 3 deletions domain/sysvar_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ import (

// SysVarCache represents the cache of system variables broken up into session and global scope.
type SysVarCache struct {
sync.RWMutex
global map[string]string
session map[string]string
sync.RWMutex // protects global and session maps
global map[string]string
session map[string]string
rebuildLock sync.Mutex // protects concurrent rebuild
}

// GetSysVarCache gets the global variable cache.
Expand Down Expand Up @@ -115,6 +116,10 @@ func (svc *SysVarCache) fetchTableValues(ctx sessionctx.Context) (map[string]str
func (svc *SysVarCache) RebuildSysVarCache(ctx sessionctx.Context) error {
newSessionCache := make(map[string]string)
newGlobalCache := make(map[string]string)
// Only one rebuild can be in progress at a time, this prevents a lost update race
// where an earlier fetchTableValues() finishes last.
svc.rebuildLock.Lock()
defer svc.rebuildLock.Unlock()
tableContents, err := svc.fetchTableValues(ctx)
if err != nil {
return err
Expand Down
5 changes: 0 additions & 5 deletions errors.toml
Original file line number Diff line number Diff line change
Expand Up @@ -351,11 +351,6 @@ error = '''
A primary key index cannot be invisible
'''

["ddl:3753"]
error = '''
Cannot create an expression index on a function that returns a JSON or GEOMETRY value
'''

["ddl:3754"]
error = '''
Expression index '%s' cannot refer to an auto-increment column
Expand Down
22 changes: 22 additions & 0 deletions executor/admin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,28 @@ func (s *testSuite5) TestAdminCheckIndexInTemporaryMode(c *C) {
tk.MustExec("drop table if exists temporary_admin_checksum_table_with_index_test,temporary_admin_checksum_table_without_index_test;")
}

func (s *testSuite5) TestAdminCheckIndexInLocalTemporaryMode(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("set @@tidb_enable_noop_functions = 1")
tk.MustExec("drop table if exists local_temporary_admin_test;")
tk.MustExec("create temporary table local_temporary_admin_test (c1 int, c2 int, c3 int default 1, primary key (c1), index (c1), unique key(c2))")
tk.MustExec("insert local_temporary_admin_test (c1, c2) values (1,1), (2,2), (3,3);")
_, err := tk.Exec("admin check table local_temporary_admin_test;")
c.Assert(err.Error(), Equals, core.ErrOptOnTemporaryTable.GenWithStackByArgs("admin check table").Error())
tk.MustExec("drop table if exists temporary_admin_test;")

tk.MustExec("drop table if exists local_temporary_admin_checksum_table_with_index_test;")
tk.MustExec("drop table if exists local_temporary_admin_checksum_table_without_index_test;")
tk.MustExec("create temporary table local_temporary_admin_checksum_table_with_index_test (id int, count int, PRIMARY KEY(id), KEY(count))")
tk.MustExec("create temporary table local_temporary_admin_checksum_table_without_index_test (id int, count int, PRIMARY KEY(id))")
_, err = tk.Exec("admin checksum table local_temporary_admin_checksum_table_with_index_test;")
c.Assert(err.Error(), Equals, core.ErrOptOnTemporaryTable.GenWithStackByArgs("admin checksum table").Error())
_, err = tk.Exec("admin checksum table local_temporary_admin_checksum_table_without_index_test;")
c.Assert(err.Error(), Equals, core.ErrOptOnTemporaryTable.GenWithStackByArgs("admin checksum table").Error())
tk.MustExec("drop table if exists local_temporary_admin_checksum_table_with_index_test,local_temporary_admin_checksum_table_without_index_test;")
}

func (s *testSuite5) TestAdminRecoverIndex(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
Expand Down
Loading

0 comments on commit 98ba826

Please sign in to comment.