From 6df6270d977143248dece68db54d8eec0eecf6a6 Mon Sep 17 00:00:00 2001 From: lihaowei Date: Thu, 27 May 2021 14:31:34 +0800 Subject: [PATCH 1/5] auto random && shard_row_id_bits Signed-off-by: lihaowei --- ddl/db_test.go | 9 +++++++++ ddl/serial_test.go | 8 ++++++++ planner/core/preprocess.go | 16 ++++++++++++++-- 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/ddl/db_test.go b/ddl/db_test.go index 80a821a40e387..bd3d0130cd275 100644 --- a/ddl/db_test.go +++ b/ddl/db_test.go @@ -43,6 +43,7 @@ import ( "github.com/pingcap/tidb/kv" "github.com/pingcap/tidb/meta" "github.com/pingcap/tidb/meta/autoid" + "github.com/pingcap/tidb/planner/core" "github.com/pingcap/tidb/session" "github.com/pingcap/tidb/sessionctx" "github.com/pingcap/tidb/sessionctx/variable" @@ -5358,6 +5359,14 @@ func (s *testSerialDBSuite) TestAlterShardRowIDBits(c *C) { c.Assert(err.Error(), Equals, "[autoid:1467]Failed to read auto-increment value from storage engine") } +func (s *testSerialDBSuite) TestShardRowIDBitsOnTemporaryTable(c *C) { + tk := testkit.NewTestKit(c, s.store) + tk.MustExec("use test") + tk.MustExec("drop table if exists shard_row_id_temporary") + _, err := tk.Exec("create global temporary table shard_row_id_temporary (a int) shard_row_id_bits = 5 on commit delete rows;") + c.Assert(err.Error(), Equals, core.ErrOptOnTemporaryTable.GenWithStackByArgs("shard_row_id_bits").Error()) +} + // port from mysql // https://github.com/mysql/mysql-server/blob/124c7ab1d6f914637521fd4463a993aa73403513/mysql-test/t/lock.test func (s *testDBSuite2) TestLock(c *C) { diff --git a/ddl/serial_test.go b/ddl/serial_test.go index f78ff3d16577b..678791ec1848b 100644 --- a/ddl/serial_test.go +++ b/ddl/serial_test.go @@ -935,6 +935,14 @@ func (s *testSerialSuite) TestTableLocksEnable(c *C) { checkTableLock(c, tk.Se, "test", "t1", model.TableLockNone) } +func (s *testSerialDBSuite) TestAutoRandomOnTemporaryTable(c *C) { + tk := testkit.NewTestKit(c, s.store) + tk.MustExec("use test") + tk.MustExec("drop table if exists auto_random_temporary") + _, err := tk.Exec("create global temporary table auto_random_temporary (a bigint primary key auto_random(3), b varchar(255)) on commit delete rows;") + c.Assert(err.Error(), Equals, core.ErrOptOnTemporaryTable.GenWithStackByArgs("auto_random").Error()) +} + func (s *testSerialDBSuite) TestAutoRandom(c *C) { tk := testkit.NewTestKit(c, s.store) tk.MustExec("create database if not exists auto_random_db") diff --git a/planner/core/preprocess.go b/planner/core/preprocess.go index ca436189c0d41..f557ceaa4a38c 100644 --- a/planner/core/preprocess.go +++ b/planner/core/preprocess.go @@ -600,6 +600,14 @@ func (p *preprocessor) checkCreateTableGrammar(stmt *ast.CreateTableStmt) { return } } + if stmt.TemporaryKeyword != ast.TemporaryNone { + for _, opt := range stmt.Options { + if opt.Tp == ast.TableOptionShardRowID { + p.err = ErrOptOnTemporaryTable.GenWithStackByArgs("shard_row_id_bits") + return + } + } + } tName := stmt.Table.Name.String() if isIncorrectName(tName) { p.err = ddl.ErrWrongTableName.GenWithStackByArgs(tName) @@ -616,7 +624,7 @@ func (p *preprocessor) checkCreateTableGrammar(stmt *ast.CreateTableStmt) { p.err = err return } - isPrimary, err := checkColumnOptions(colDef.Options) + isPrimary, err := checkColumnOptions(stmt.TemporaryKeyword != ast.TemporaryNone, colDef.Options) if err != nil { p.err = err return @@ -773,7 +781,7 @@ func isTableAliasDuplicate(node ast.ResultSetNode, tableAliases map[string]inter return nil } -func checkColumnOptions(ops []*ast.ColumnOption) (int, error) { +func checkColumnOptions(isTempTable bool, ops []*ast.ColumnOption) (int, error) { isPrimary, isGenerated, isStored := 0, 0, false for _, op := range ops { @@ -783,6 +791,10 @@ func checkColumnOptions(ops []*ast.ColumnOption) (int, error) { case ast.ColumnOptionGenerated: isGenerated = 1 isStored = op.Stored + case ast.ColumnOptionAutoRandom: + if isTempTable { + return isPrimary, ErrOptOnTemporaryTable.GenWithStackByArgs("auto_random") + } } } From ea18d9b94a0b89c46a6cc6a04ab12f4302117889 Mon Sep 17 00:00:00 2001 From: lihaowei Date: Fri, 28 May 2021 20:07:20 +0800 Subject: [PATCH 2/5] add check for local temporary table Signed-off-by: lihaowei --- ddl/serial_test.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/ddl/serial_test.go b/ddl/serial_test.go index 678791ec1848b..30c81bf2f2e7b 100644 --- a/ddl/serial_test.go +++ b/ddl/serial_test.go @@ -941,6 +941,9 @@ func (s *testSerialDBSuite) TestAutoRandomOnTemporaryTable(c *C) { tk.MustExec("drop table if exists auto_random_temporary") _, err := tk.Exec("create global temporary table auto_random_temporary (a bigint primary key auto_random(3), b varchar(255)) on commit delete rows;") c.Assert(err.Error(), Equals, core.ErrOptOnTemporaryTable.GenWithStackByArgs("auto_random").Error()) + tk.MustExec("set @@tidb_enable_noop_functions = 1") + _, err = tk.Exec("create temporary table t(a bigint key auto_random);") + c.Assert(err.Error(), Equals, core.ErrOptOnTemporaryTable.GenWithStackByArgs("auto_random").Error()) } func (s *testSerialDBSuite) TestAutoRandom(c *C) { From 77c513dc84465a886a0237a14efb0edacb1773c2 Mon Sep 17 00:00:00 2001 From: lihaowei Date: Mon, 31 May 2021 21:26:48 +0800 Subject: [PATCH 3/5] add check Signed-off-by: lihaowei --- ddl/db_test.go | 4 ++++ planner/core/preprocess.go | 24 ++++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/ddl/db_test.go b/ddl/db_test.go index bd3d0130cd275..713e69393876d 100644 --- a/ddl/db_test.go +++ b/ddl/db_test.go @@ -5365,6 +5365,10 @@ func (s *testSerialDBSuite) TestShardRowIDBitsOnTemporaryTable(c *C) { tk.MustExec("drop table if exists shard_row_id_temporary") _, err := tk.Exec("create global temporary table shard_row_id_temporary (a int) shard_row_id_bits = 5 on commit delete rows;") c.Assert(err.Error(), Equals, core.ErrOptOnTemporaryTable.GenWithStackByArgs("shard_row_id_bits").Error()) + tk.MustExec("create global temporary table shard_row_id_temporary (a int) on commit delete rows;") + defer tk.MustExec("drop table if exists shard_row_id_temporary") + _, err = tk.Exec("alter table shard_row_id_temporary shard_row_id_bits = 4;") + c.Assert(err.Error(), Equals, core.ErrOptOnTemporaryTable.GenWithStackByArgs("shard_row_id_bits").Error()) } // port from mysql diff --git a/planner/core/preprocess.go b/planner/core/preprocess.go index d4d01a1bd41f9..44f0fd9b202d8 100644 --- a/planner/core/preprocess.go +++ b/planner/core/preprocess.go @@ -903,7 +903,31 @@ func (p *preprocessor) checkAlterTableGrammar(stmt *ast.AlterTableStmt) { return } specs := stmt.Specs + currentDB := p.ctx.GetSessionVars().CurrentDB + if stmt.Table.Schema.String() != "" { + currentDB = stmt.Table.Schema.L + } + if currentDB == "" { + p.err = errors.Trace(ErrNoDB) + return + } + sName := model.NewCIStr(currentDB) + tableInfo, err := p.ensureInfoSchema().TableByName(sName, stmt.Table.Name) + if err != nil { + p.err = err + return + } + tempTableType := tableInfo.Meta().TempTableType for _, spec := range specs { + if tempTableType != model.TempTableNone { + for _, option := range spec.Options { + if option.Tp == ast.TableOptionShardRowID { + p.err = ErrOptOnTemporaryTable.GenWithStackByArgs("shard_row_id_bits") + return + } + } + } + if spec.NewTable != nil { ntName := spec.NewTable.Name.String() if isIncorrectName(ntName) { From d97feb7ad08c1de739959281b104b0461fd2eb76 Mon Sep 17 00:00:00 2001 From: lihaowei Date: Tue, 1 Jun 2021 13:11:56 +0800 Subject: [PATCH 4/5] fix tests Signed-off-by: lihaowei --- planner/core/planbuilder.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/planner/core/planbuilder.go b/planner/core/planbuilder.go index 2189f645e32b1..0a043de19e74d 100644 --- a/planner/core/planbuilder.go +++ b/planner/core/planbuilder.go @@ -3393,7 +3393,11 @@ func (b *PlanBuilder) buildDDL(ctx context.Context, node ast.DDLNode) (Plan, err authErr = ErrTableaccessDenied.GenWithStackByArgs("ALTER", b.ctx.GetSessionVars().User.AuthUsername, b.ctx.GetSessionVars().User.AuthHostname, v.Table.Name.L) } - b.visitInfo = appendVisitInfo(b.visitInfo, mysql.AlterPriv, v.Table.Schema.L, + dbName := v.Table.Schema.L + if dbName == "" { + dbName = b.ctx.GetSessionVars().CurrentDB + } + b.visitInfo = appendVisitInfo(b.visitInfo, mysql.AlterPriv, dbName, v.Table.Name.L, "", authErr) for _, spec := range v.Specs { if spec.Tp == ast.AlterTableRenameTable || spec.Tp == ast.AlterTableExchangePartition { @@ -3401,21 +3405,21 @@ func (b *PlanBuilder) buildDDL(ctx context.Context, node ast.DDLNode) (Plan, err authErr = ErrTableaccessDenied.GenWithStackByArgs("DROP", b.ctx.GetSessionVars().User.AuthUsername, b.ctx.GetSessionVars().User.AuthHostname, v.Table.Name.L) } - b.visitInfo = appendVisitInfo(b.visitInfo, mysql.DropPriv, v.Table.Schema.L, + b.visitInfo = appendVisitInfo(b.visitInfo, mysql.DropPriv, dbName, v.Table.Name.L, "", authErr) if b.ctx.GetSessionVars().User != nil { authErr = ErrTableaccessDenied.GenWithStackByArgs("CREATE", b.ctx.GetSessionVars().User.AuthUsername, b.ctx.GetSessionVars().User.AuthHostname, spec.NewTable.Name.L) } - b.visitInfo = appendVisitInfo(b.visitInfo, mysql.CreatePriv, spec.NewTable.Schema.L, + b.visitInfo = appendVisitInfo(b.visitInfo, mysql.CreatePriv, dbName, spec.NewTable.Name.L, "", authErr) if b.ctx.GetSessionVars().User != nil { authErr = ErrTableaccessDenied.GenWithStackByArgs("INSERT", b.ctx.GetSessionVars().User.AuthUsername, b.ctx.GetSessionVars().User.AuthHostname, spec.NewTable.Name.L) } - b.visitInfo = appendVisitInfo(b.visitInfo, mysql.InsertPriv, spec.NewTable.Schema.L, + b.visitInfo = appendVisitInfo(b.visitInfo, mysql.InsertPriv, dbName, spec.NewTable.Name.L, "", authErr) } else if spec.Tp == ast.AlterTableDropPartition { if b.ctx.GetSessionVars().User != nil { From d0e03dc42d20c94e9f7fbb41d3279cde72d43525 Mon Sep 17 00:00:00 2001 From: lihaowei Date: Wed, 2 Jun 2021 13:45:52 +0800 Subject: [PATCH 5/5] change pos Signed-off-by: lihaowei --- ddl/db_test.go | 2 +- ddl/ddl_api.go | 3 +++ planner/core/preprocess.go | 24 ------------------------ 3 files changed, 4 insertions(+), 25 deletions(-) diff --git a/ddl/db_test.go b/ddl/db_test.go index 713e69393876d..9271642302bbd 100644 --- a/ddl/db_test.go +++ b/ddl/db_test.go @@ -5368,7 +5368,7 @@ func (s *testSerialDBSuite) TestShardRowIDBitsOnTemporaryTable(c *C) { tk.MustExec("create global temporary table shard_row_id_temporary (a int) on commit delete rows;") defer tk.MustExec("drop table if exists shard_row_id_temporary") _, err = tk.Exec("alter table shard_row_id_temporary shard_row_id_bits = 4;") - c.Assert(err.Error(), Equals, core.ErrOptOnTemporaryTable.GenWithStackByArgs("shard_row_id_bits").Error()) + c.Assert(err.Error(), Equals, ddl.ErrOptOnTemporaryTable.GenWithStackByArgs("shard_row_id_bits").Error()) } // port from mysql diff --git a/ddl/ddl_api.go b/ddl/ddl_api.go index 9c3eca13c7fee..2c87a51bc12af 100644 --- a/ddl/ddl_api.go +++ b/ddl/ddl_api.go @@ -2647,6 +2647,9 @@ func (d *ddl) ShardRowID(ctx sessionctx.Context, tableIdent ast.Ident, uVal uint if err != nil { return errors.Trace(err) } + if t.Meta().TempTableType != model.TempTableNone { + return ErrOptOnTemporaryTable.GenWithStackByArgs("shard_row_id_bits") + } if uVal == t.Meta().ShardRowIDBits { // Nothing need to do. return nil diff --git a/planner/core/preprocess.go b/planner/core/preprocess.go index a94cb2cd4ff61..f4ac13af5707a 100644 --- a/planner/core/preprocess.go +++ b/planner/core/preprocess.go @@ -907,31 +907,7 @@ func (p *preprocessor) checkAlterTableGrammar(stmt *ast.AlterTableStmt) { return } specs := stmt.Specs - currentDB := p.ctx.GetSessionVars().CurrentDB - if stmt.Table.Schema.String() != "" { - currentDB = stmt.Table.Schema.L - } - if currentDB == "" { - p.err = errors.Trace(ErrNoDB) - return - } - sName := model.NewCIStr(currentDB) - tableInfo, err := p.ensureInfoSchema().TableByName(sName, stmt.Table.Name) - if err != nil { - p.err = err - return - } - tempTableType := tableInfo.Meta().TempTableType for _, spec := range specs { - if tempTableType != model.TempTableNone { - for _, option := range spec.Options { - if option.Tp == ast.TableOptionShardRowID { - p.err = ErrOptOnTemporaryTable.GenWithStackByArgs("shard_row_id_bits") - return - } - } - } - if spec.NewTable != nil { ntName := spec.NewTable.Name.String() if isIncorrectName(ntName) {