From e06c87d3c1705dfd3557f77b502519c7445c5e6e Mon Sep 17 00:00:00 2001 From: Jack Yu Date: Wed, 2 Jan 2019 11:30:53 +0800 Subject: [PATCH] add an option for AddRecord and Create (#8884) --- ddl/column_change_test.go | 8 ++++---- ddl/column_test.go | 16 ++++++++-------- ddl/ddl_worker_test.go | 14 +++++++------- ddl/fail_test.go | 2 +- ddl/index_change_test.go | 20 ++++++++++---------- ddl/reorg_test.go | 2 +- ddl/schema_test.go | 4 ++-- ddl/table_test.go | 2 +- executor/executor_test.go | 4 ++-- executor/insert_common.go | 2 +- executor/write.go | 3 ++- executor/write_test.go | 2 +- infoschema/tables.go | 2 +- perfschema/tables.go | 2 +- table/index.go | 8 +++++++- table/table.go | 8 ++++++-- table/tables/index.go | 3 ++- table/tables/partition.go | 6 +++--- table/tables/tables.go | 15 ++++++++++----- table/tables/tables_test.go | 36 ++++++++++++++++++------------------ util/admin/admin_test.go | 4 ++-- 21 files changed, 90 insertions(+), 73 deletions(-) diff --git a/ddl/column_change_test.go b/ddl/column_change_test.go index 0ab16a8f06f32..5b8bcf9b86134 100644 --- a/ddl/column_change_test.go +++ b/ddl/column_change_test.go @@ -73,7 +73,7 @@ func (s *testColumnChangeSuite) TestColumnChange(c *C) { // insert t values (1, 2); originTable := testGetTable(c, d, s.dbInfo.ID, tblInfo.ID) row := types.MakeDatums(1, 2) - h, err := originTable.AddRecord(ctx, row, false) + h, err := originTable.AddRecord(ctx, row) c.Assert(err, IsNil) txn, err := ctx.Txn(true) c.Assert(err, IsNil) @@ -180,7 +180,7 @@ func (s *testColumnChangeSuite) testAddColumnNoDefault(c *C, ctx sessionctx.Cont if err != nil { checkErr = errors.Trace(err) } - _, err = writeOnlyTable.AddRecord(hookCtx, types.MakeDatums(10, 10), false) + _, err = writeOnlyTable.AddRecord(hookCtx, types.MakeDatums(10, 10)) if err != nil { checkErr = errors.Trace(err) } @@ -235,7 +235,7 @@ func (s *testColumnChangeSuite) checkAddWriteOnly(ctx sessionctx.Context, d *ddl if err != nil { return errors.Trace(err) } - _, err = writeOnlyTable.AddRecord(ctx, types.MakeDatums(2, 3), false) + _, err = writeOnlyTable.AddRecord(ctx, types.MakeDatums(2, 3)) if err != nil { return errors.Trace(err) } @@ -310,7 +310,7 @@ func (s *testColumnChangeSuite) checkAddPublic(sctx sessionctx.Context, d *ddl, if err != nil { return errors.Trace(err) } - h, err := publicTable.AddRecord(sctx, types.MakeDatums(4, 4, 4), false) + h, err := publicTable.AddRecord(sctx, types.MakeDatums(4, 4, 4)) if err != nil { return errors.Trace(err) } diff --git a/ddl/column_test.go b/ddl/column_test.go index 51e8bf8bf9df0..b31532ea74b2f 100644 --- a/ddl/column_test.go +++ b/ddl/column_test.go @@ -124,7 +124,7 @@ func (s *testColumnSuite) TestColumn(c *C) { num := 10 for i := 0; i < num; i++ { - _, err := t.AddRecord(ctx, types.MakeDatums(i, 10*i, 100*i), false) + _, err := t.AddRecord(ctx, types.MakeDatums(i, 10*i, 100*i)) c.Assert(err, IsNil) } @@ -164,7 +164,7 @@ func (s *testColumnSuite) TestColumn(c *C) { c.Assert(err, IsNil) c.Assert(i, Equals, int64(num)) - h, err := t.AddRecord(ctx, types.MakeDatums(11, 12, 13, 14), false) + h, err := t.AddRecord(ctx, types.MakeDatums(11, 12, 13, 14)) c.Assert(err, IsNil) err = ctx.NewTxn(context.Background()) c.Assert(err, IsNil) @@ -359,7 +359,7 @@ func (s *testColumnSuite) checkDeleteOnlyColumn(ctx sessionctx.Context, d *ddl, } newRow := types.MakeDatums(int64(11), int64(22), int64(33)) - newHandle, err := t.AddRecord(ctx, newRow, false) + newHandle, err := t.AddRecord(ctx, newRow) if err != nil { return errors.Trace(err) } @@ -463,7 +463,7 @@ func (s *testColumnSuite) checkWriteOnlyColumn(ctx sessionctx.Context, d *ddl, t } newRow := types.MakeDatums(int64(11), int64(22), int64(33)) - newHandle, err := t.AddRecord(ctx, newRow, false) + newHandle, err := t.AddRecord(ctx, newRow) if err != nil { return errors.Trace(err) } @@ -563,7 +563,7 @@ func (s *testColumnSuite) checkReorganizationColumn(ctx sessionctx.Context, d *d } newRow := types.MakeDatums(int64(11), int64(22), int64(33)) - newHandle, err := t.AddRecord(ctx, newRow, false) + newHandle, err := t.AddRecord(ctx, newRow) if err != nil { return errors.Trace(err) } @@ -660,7 +660,7 @@ func (s *testColumnSuite) checkPublicColumn(ctx sessionctx.Context, d *ddl, tblI } newRow := types.MakeDatums(int64(11), int64(22), int64(33), int64(44)) - handle, err = t.AddRecord(ctx, newRow, false) + handle, err = t.AddRecord(ctx, newRow) if err != nil { return errors.Trace(err) } @@ -765,7 +765,7 @@ func (s *testColumnSuite) TestAddColumn(c *C) { t := testGetTable(c, d, s.dbInfo.ID, tblInfo.ID) oldRow := types.MakeDatums(int64(1), int64(2), int64(3)) - handle, err := t.AddRecord(ctx, oldRow, false) + handle, err := t.AddRecord(ctx, oldRow) c.Assert(err, IsNil) txn, err := ctx.Txn(true) @@ -856,7 +856,7 @@ func (s *testColumnSuite) TestDropColumn(c *C) { colName := "c4" defaultColValue := int64(4) row := types.MakeDatums(int64(1), int64(2), int64(3)) - _, err = t.AddRecord(ctx, append(row, types.NewDatum(defaultColValue)), false) + _, err = t.AddRecord(ctx, append(row, types.NewDatum(defaultColValue))) c.Assert(err, IsNil) txn, err := ctx.Txn(true) diff --git a/ddl/ddl_worker_test.go b/ddl/ddl_worker_test.go index daa0df731416f..a9648bd05aa67 100644 --- a/ddl/ddl_worker_test.go +++ b/ddl/ddl_worker_test.go @@ -436,7 +436,7 @@ func (s *testDDLSuite) TestCancelJob(c *C) { // insert t values (1, 2); originTable := testGetTable(c, d, dbInfo.ID, tblInfo.ID) row := types.MakeDatums(1, 2) - _, err = originTable.AddRecord(ctx, row, false) + _, err = originTable.AddRecord(ctx, row) c.Assert(err, IsNil) txn, err := ctx.Txn(true) c.Assert(err, IsNil) @@ -709,9 +709,9 @@ func (s *testDDLSuite) TestParallelDDL(c *C) { testCreateTable(c, ctx, d, dbInfo1, tblInfo1) // insert t1 values (10, 10), (20, 20) tbl1 := testGetTable(c, d, dbInfo1.ID, tblInfo1.ID) - _, err = tbl1.AddRecord(ctx, types.MakeDatums(1, 1), false) + _, err = tbl1.AddRecord(ctx, types.MakeDatums(1, 1)) c.Assert(err, IsNil) - _, err = tbl1.AddRecord(ctx, types.MakeDatums(2, 2), false) + _, err = tbl1.AddRecord(ctx, types.MakeDatums(2, 2)) c.Assert(err, IsNil) // create table t2 (c1 int primary key, c2 int, c3 int); tblInfo2 := testTableInfo(c, d, "t2", 3) @@ -720,11 +720,11 @@ func (s *testDDLSuite) TestParallelDDL(c *C) { testCreateTable(c, ctx, d, dbInfo1, tblInfo2) // insert t2 values (1, 1), (2, 2), (3, 3) tbl2 := testGetTable(c, d, dbInfo1.ID, tblInfo2.ID) - _, err = tbl2.AddRecord(ctx, types.MakeDatums(1, 1, 1), false) + _, err = tbl2.AddRecord(ctx, types.MakeDatums(1, 1, 1)) c.Assert(err, IsNil) - _, err = tbl2.AddRecord(ctx, types.MakeDatums(2, 2, 2), false) + _, err = tbl2.AddRecord(ctx, types.MakeDatums(2, 2, 2)) c.Assert(err, IsNil) - _, err = tbl2.AddRecord(ctx, types.MakeDatums(3, 3, 3), false) + _, err = tbl2.AddRecord(ctx, types.MakeDatums(3, 3, 3)) c.Assert(err, IsNil) // create database test_parallel_ddl_2; dbInfo2 := testSchemaInfo(c, d, "test_parallel_ddl_2") @@ -734,7 +734,7 @@ func (s *testDDLSuite) TestParallelDDL(c *C) { testCreateTable(c, ctx, d, dbInfo2, tblInfo3) // insert t3 values (11, 22, 33, 44) tbl3 := testGetTable(c, d, dbInfo2.ID, tblInfo3.ID) - _, err = tbl3.AddRecord(ctx, types.MakeDatums(11, 22, 33, 44), false) + _, err = tbl3.AddRecord(ctx, types.MakeDatums(11, 22, 33, 44)) c.Assert(err, IsNil) // set hook to execute jobs after all jobs are in queue. diff --git a/ddl/fail_test.go b/ddl/fail_test.go index 59b423be49446..b8f6eb54bd5fb 100644 --- a/ddl/fail_test.go +++ b/ddl/fail_test.go @@ -35,7 +35,7 @@ func (s *testColumnChangeSuite) TestFailBeforeDecodeArgs(c *C) { // insert t_fail values (1, 2); originTable := testGetTable(c, d, s.dbInfo.ID, tblInfo.ID) row := types.MakeDatums(1, 2) - _, err = originTable.AddRecord(ctx, row, false) + _, err = originTable.AddRecord(ctx, row) c.Assert(err, IsNil) txn, err := ctx.Txn(true) c.Assert(err, IsNil) diff --git a/ddl/index_change_test.go b/ddl/index_change_test.go index 8836faef9df52..d567861afe072 100644 --- a/ddl/index_change_test.go +++ b/ddl/index_change_test.go @@ -68,11 +68,11 @@ func (s *testIndexChangeSuite) TestIndexChange(c *C) { originTable := testGetTable(c, d, s.dbInfo.ID, tblInfo.ID) // insert t values (1, 1), (2, 2), (3, 3) - _, err = originTable.AddRecord(ctx, types.MakeDatums(1, 1), false) + _, err = originTable.AddRecord(ctx, types.MakeDatums(1, 1)) c.Assert(err, IsNil) - _, err = originTable.AddRecord(ctx, types.MakeDatums(2, 2), false) + _, err = originTable.AddRecord(ctx, types.MakeDatums(2, 2)) c.Assert(err, IsNil) - _, err = originTable.AddRecord(ctx, types.MakeDatums(3, 3), false) + _, err = originTable.AddRecord(ctx, types.MakeDatums(3, 3)) c.Assert(err, IsNil) txn, err := ctx.Txn(true) @@ -199,7 +199,7 @@ func (s *testIndexChangeSuite) checkAddWriteOnly(d *ddl, ctx sessionctx.Context, if err != nil { return errors.Trace(err) } - _, err = delOnlyTbl.AddRecord(ctx, types.MakeDatums(4, 4), false) + _, err = delOnlyTbl.AddRecord(ctx, types.MakeDatums(4, 4)) if err != nil { return errors.Trace(err) } @@ -209,7 +209,7 @@ func (s *testIndexChangeSuite) checkAddWriteOnly(d *ddl, ctx sessionctx.Context, } // WriteOnlyTable: insert t values (5, 5); - _, err = writeOnlyTbl.AddRecord(ctx, types.MakeDatums(5, 5), false) + _, err = writeOnlyTbl.AddRecord(ctx, types.MakeDatums(5, 5)) if err != nil { return errors.Trace(err) } @@ -272,7 +272,7 @@ func (s *testIndexChangeSuite) checkAddPublic(d *ddl, ctx sessionctx.Context, wr if err != nil { return errors.Trace(err) } - _, err = writeTbl.AddRecord(ctx, types.MakeDatums(6, 6), false) + _, err = writeTbl.AddRecord(ctx, types.MakeDatums(6, 6)) if err != nil { return errors.Trace(err) } @@ -281,7 +281,7 @@ func (s *testIndexChangeSuite) checkAddPublic(d *ddl, ctx sessionctx.Context, wr return errors.Trace(err) } // PublicTable: insert t values (7, 7) - _, err = publicTbl.AddRecord(ctx, types.MakeDatums(7, 7), false) + _, err = publicTbl.AddRecord(ctx, types.MakeDatums(7, 7)) if err != nil { return errors.Trace(err) } @@ -343,7 +343,7 @@ func (s *testIndexChangeSuite) checkDropWriteOnly(d *ddl, ctx sessionctx.Context if err != nil { return errors.Trace(err) } - _, err = writeTbl.AddRecord(ctx, types.MakeDatums(8, 8), false) + _, err = writeTbl.AddRecord(ctx, types.MakeDatums(8, 8)) if err != nil { return errors.Trace(err) } @@ -387,7 +387,7 @@ func (s *testIndexChangeSuite) checkDropDeleteOnly(d *ddl, ctx sessionctx.Contex if err != nil { return errors.Trace(err) } - _, err = writeTbl.AddRecord(ctx, types.MakeDatums(9, 9), false) + _, err = writeTbl.AddRecord(ctx, types.MakeDatums(9, 9)) if err != nil { return errors.Trace(err) } @@ -398,7 +398,7 @@ func (s *testIndexChangeSuite) checkDropDeleteOnly(d *ddl, ctx sessionctx.Contex } // DeleteOnlyTable insert t values (10, 10) - _, err = delTbl.AddRecord(ctx, types.MakeDatums(10, 10), false) + _, err = delTbl.AddRecord(ctx, types.MakeDatums(10, 10)) if err != nil { return errors.Trace(err) } diff --git a/ddl/reorg_test.go b/ddl/reorg_test.go index eb92aa758dfed..0d5e8763d0570 100644 --- a/ddl/reorg_test.go +++ b/ddl/reorg_test.go @@ -176,7 +176,7 @@ func (s *testDDLSuite) TestReorgOwner(c *C) { num := 10 for i := 0; i < num; i++ { - _, err := t.AddRecord(ctx, types.MakeDatums(i, i, i), false) + _, err := t.AddRecord(ctx, types.MakeDatums(i, i, i)) c.Assert(err, IsNil) } diff --git a/ddl/schema_test.go b/ddl/schema_test.go index 74c8cff4b4208..31b8cb839a8a4 100644 --- a/ddl/schema_test.go +++ b/ddl/schema_test.go @@ -146,7 +146,7 @@ func (s *testSchemaSuite) TestSchema(c *C) { testCheckJobDone(c, d, tJob1, true) tbl1 := testGetTable(c, d, dbInfo.ID, tblInfo1.ID) for i := 1; i <= 100; i++ { - _, err := tbl1.AddRecord(ctx, types.MakeDatums(i, i, i), false) + _, err := tbl1.AddRecord(ctx, types.MakeDatums(i, i, i)) c.Assert(err, IsNil) } // create table t1 with 1034 records. @@ -156,7 +156,7 @@ func (s *testSchemaSuite) TestSchema(c *C) { testCheckJobDone(c, d, tJob2, true) tbl2 := testGetTable(c, d, dbInfo.ID, tblInfo2.ID) for i := 1; i <= 1034; i++ { - _, err := tbl2.AddRecord(ctx, types.MakeDatums(i, i, i), false) + _, err := tbl2.AddRecord(ctx, types.MakeDatums(i, i, i)) c.Assert(err, IsNil) } job, v := testDropSchema(c, ctx, d, dbInfo) diff --git a/ddl/table_test.go b/ddl/table_test.go index d4dd046164530..9e117c9799507 100644 --- a/ddl/table_test.go +++ b/ddl/table_test.go @@ -277,7 +277,7 @@ func (s *testTableSuite) TestTable(c *C) { count := 2000 tbl := testGetTable(c, d, s.dbInfo.ID, tblInfo.ID) for i := 1; i <= count; i++ { - _, err := tbl.AddRecord(ctx, types.MakeDatums(i, i, i), false) + _, err := tbl.AddRecord(ctx, types.MakeDatums(i, i, i)) c.Assert(err, IsNil) } diff --git a/executor/executor_test.go b/executor/executor_test.go index 8e5610df50c57..f57b1376810f3 100644 --- a/executor/executor_test.go +++ b/executor/executor_test.go @@ -2813,9 +2813,9 @@ func (s *testSuite) TestCheckIndex(c *C) { recordVal1 := types.MakeDatums(int64(1), int64(10), int64(11)) recordVal2 := types.MakeDatums(int64(2), int64(20), int64(21)) c.Assert(s.ctx.NewTxn(context.Background()), IsNil) - _, err = tb.AddRecord(s.ctx, recordVal1, false) + _, err = tb.AddRecord(s.ctx, recordVal1) c.Assert(err, IsNil) - _, err = tb.AddRecord(s.ctx, recordVal2, false) + _, err = tb.AddRecord(s.ctx, recordVal2) c.Assert(err, IsNil) txn, err := s.ctx.Txn(true) c.Assert(err, IsNil) diff --git a/executor/insert_common.go b/executor/insert_common.go index e3b79c857bf30..03d666ced463c 100644 --- a/executor/insert_common.go +++ b/executor/insert_common.go @@ -585,7 +585,7 @@ func (e *InsertValues) addRecord(row []types.Datum) (int64, error) { if !e.ctx.GetSessionVars().ConstraintCheckInPlace { txn.SetOption(kv.PresumeKeyNotExists, nil) } - h, err := e.Table.AddRecord(e.ctx, row, false) + h, err := e.Table.AddRecord(e.ctx, row) txn.DelOption(kv.PresumeKeyNotExists) if err != nil { return 0, errors.Trace(err) diff --git a/executor/write.go b/executor/write.go index b514601d7d641..4e231355b4c54 100644 --- a/executor/write.go +++ b/executor/write.go @@ -141,7 +141,8 @@ func updateRecord(ctx sessionctx.Context, h int64, oldData, newData []types.Datu return false, false, 0, errors.Trace(err) } // the `affectedRows` is increased when adding new record. - newHandle, err = t.AddRecord(ctx, newData, sc.DupKeyAsWarning) + newHandle, err = t.AddRecord(ctx, newData, + &table.AddRecordOpt{CreateIdxOpt: table.CreateIdxOpt{SkipHandleCheck: sc.DupKeyAsWarning}}) if err != nil { return false, false, 0, errors.Trace(err) } diff --git a/executor/write_test.go b/executor/write_test.go index 71ac33b43ae64..e01aaaa4fac48 100644 --- a/executor/write_test.go +++ b/executor/write_test.go @@ -2336,7 +2336,7 @@ func (s *testSuite2) TestRebaseIfNeeded(c *C) { c.Assert(s.ctx.NewTxn(context.Background()), IsNil) // AddRecord directly here will skip to rebase the auto ID in the insert statement, // which could simulate another TiDB adds a large auto ID. - _, err = tbl.AddRecord(s.ctx, types.MakeDatums(30001, 2), false) + _, err = tbl.AddRecord(s.ctx, types.MakeDatums(30001, 2)) c.Assert(err, IsNil) txn, err := s.ctx.Txn(true) c.Assert(err, IsNil) diff --git a/infoschema/tables.go b/infoschema/tables.go index 64e9583a7b303..02e60854ac291 100644 --- a/infoschema/tables.go +++ b/infoschema/tables.go @@ -1498,7 +1498,7 @@ func (it *infoschemaTable) RecordKey(h int64) kv.Key { return nil } -func (it *infoschemaTable) AddRecord(ctx sessionctx.Context, r []types.Datum, skipHandleCheck bool) (recordID int64, err error) { +func (it *infoschemaTable) AddRecord(ctx sessionctx.Context, r []types.Datum, opts ...*table.AddRecordOpt) (recordID int64, err error) { return 0, table.ErrUnsupportedOp } diff --git a/perfschema/tables.go b/perfschema/tables.go index 307042c144174..2288311cd9bea 100644 --- a/perfschema/tables.go +++ b/perfschema/tables.go @@ -111,7 +111,7 @@ func (vt *perfSchemaTable) RecordKey(h int64) kv.Key { } // AddRecord implements table.Table Type interface. -func (vt *perfSchemaTable) AddRecord(ctx sessionctx.Context, r []types.Datum, skipHandleCheck bool) (recordID int64, err error) { +func (vt *perfSchemaTable) AddRecord(ctx sessionctx.Context, r []types.Datum, opts ...*table.AddRecordOpt) (recordID int64, err error) { return 0, table.ErrUnsupportedOp } diff --git a/table/index.go b/table/index.go index 1b7d247c6ed13..5553614822718 100644 --- a/table/index.go +++ b/table/index.go @@ -27,12 +27,18 @@ type IndexIterator interface { Close() } +// CreateIdxOpt contains the options will be used when creating an index. +type CreateIdxOpt struct { + SkipHandleCheck bool // If true, skip the handle constraint check. + SkipCheck bool // If true, skip all the unique indices constraint check. +} + // Index is the interface for index data on KV store. type Index interface { // Meta returns IndexInfo. Meta() *model.IndexInfo // Create supports insert into statement. - Create(ctx sessionctx.Context, rm kv.RetrieverMutator, indexedValues []types.Datum, h int64) (int64, error) + Create(ctx sessionctx.Context, rm kv.RetrieverMutator, indexedValues []types.Datum, h int64, opts ...*CreateIdxOpt) (int64, error) // Delete supports delete from statement. Delete(sc *stmtctx.StatementContext, m kv.Mutator, indexedValues []types.Datum, h int64) error // Drop supports drop table, drop index statements. diff --git a/table/table.go b/table/table.go index 021bb6740ad4d..b23a77dce57c8 100644 --- a/table/table.go +++ b/table/table.go @@ -86,6 +86,11 @@ var ( // RecordIterFunc is used for low-level record iteration. type RecordIterFunc func(h int64, rec []types.Datum, cols []*Column) (more bool, err error) +// AddRecordOpt contains the options will be used when adding a record. +type AddRecordOpt struct { + CreateIdxOpt +} + // Table is used to retrieve and modify rows in table. type Table interface { // IterRecords iterates records in the table and calls fn. @@ -126,8 +131,7 @@ type Table interface { RecordKey(h int64) kv.Key // AddRecord inserts a row which should contain only public columns - // skipHandleCheck indicates that recordID in r has been checked as not duplicate already. - AddRecord(ctx sessionctx.Context, r []types.Datum, skipHandleCheck bool) (recordID int64, err error) + AddRecord(ctx sessionctx.Context, r []types.Datum, opts ...*AddRecordOpt) (recordID int64, err error) // UpdateRecord updates a row which should contain only writable columns. UpdateRecord(ctx sessionctx.Context, h int64, currData, newData []types.Datum, touched []bool) error diff --git a/table/tables/index.go b/table/tables/index.go index d86c45c6c1d72..e6f67006aac7a 100644 --- a/table/tables/index.go +++ b/table/tables/index.go @@ -196,7 +196,8 @@ func (c *index) GenIndexKey(sc *stmtctx.StatementContext, indexedValues []types. // Create creates a new entry in the kvIndex data. // If the index is unique and there is an existing entry with the same key, // Create will return the existing entry's handle as the first return value, ErrKeyExists as the second return value. -func (c *index) Create(ctx sessionctx.Context, rm kv.RetrieverMutator, indexedValues []types.Datum, h int64) (int64, error) { +func (c *index) Create(ctx sessionctx.Context, rm kv.RetrieverMutator, indexedValues []types.Datum, h int64, + opts ...*table.CreateIdxOpt) (int64, error) { writeBufs := ctx.GetSessionVars().GetWriteStmtBufs() skipCheck := ctx.GetSessionVars().LightningMode || ctx.GetSessionVars().StmtCtx.BatchCheck key, distinct, err := c.GenIndexKey(ctx.GetSessionVars().StmtCtx, indexedValues, h, writeBufs.IndexKeyBuf) diff --git a/table/tables/partition.go b/table/tables/partition.go index 45fe09c076830..5de6a7fb764ed 100644 --- a/table/tables/partition.go +++ b/table/tables/partition.go @@ -296,7 +296,7 @@ func (t *partitionedTable) GetPartitionByRow(ctx sessionctx.Context, r []types.D } // AddRecord implements the AddRecord method for the table.Table interface. -func (t *partitionedTable) AddRecord(ctx sessionctx.Context, r []types.Datum, skipHandleCheck bool) (recordID int64, err error) { +func (t *partitionedTable) AddRecord(ctx sessionctx.Context, r []types.Datum, opts ...*table.AddRecordOpt) (recordID int64, err error) { partitionInfo := t.meta.GetPartitionInfo() pid, err := t.locatePartition(ctx, partitionInfo, r) if err != nil { @@ -304,7 +304,7 @@ func (t *partitionedTable) AddRecord(ctx sessionctx.Context, r []types.Datum, sk } tbl := t.GetPartition(pid) - return tbl.AddRecord(ctx, r, skipHandleCheck) + return tbl.AddRecord(ctx, r, opts...) } // RemoveRecord implements table.Table RemoveRecord interface. @@ -336,7 +336,7 @@ func (t *partitionedTable) UpdateRecord(ctx sessionctx.Context, h int64, currDat // The old and new data locate in different partitions. // Remove record from old partition and add record to new partition. if from != to { - _, err = t.GetPartition(to).AddRecord(ctx, newData, false) + _, err = t.GetPartition(to).AddRecord(ctx, newData) if err != nil { return errors.Trace(err) } diff --git a/table/tables/tables.go b/table/tables/tables.go index 985a3cb465c20..e46ee22091b7e 100644 --- a/table/tables/tables.go +++ b/table/tables/tables.go @@ -418,7 +418,11 @@ func (t *tableCommon) getRollbackableMemStore(ctx sessionctx.Context) (kv.Retrie } // AddRecord implements table.Table AddRecord interface. -func (t *tableCommon) AddRecord(ctx sessionctx.Context, r []types.Datum, skipHandleCheck bool) (recordID int64, err error) { +func (t *tableCommon) AddRecord(ctx sessionctx.Context, r []types.Datum, opts ...*table.AddRecordOpt) (recordID int64, err error) { + opt := &table.AddRecordOpt{} + if len(opts) != 0 { + opt = opts[0] + } var hasRecordID bool cols := t.Cols() if len(r) > len(cols) { @@ -450,7 +454,7 @@ func (t *tableCommon) AddRecord(ctx sessionctx.Context, r []types.Datum, skipHan rm, err := t.getRollbackableMemStore(ctx) // Insert new entries into indices. - h, err := t.addIndices(ctx, recordID, r, rm, skipHandleCheck) + h, err := t.addIndices(ctx, recordID, r, rm, &opt.CreateIdxOpt) if err != nil { return h, errors.Trace(err) } @@ -536,7 +540,8 @@ func (t *tableCommon) genIndexKeyStr(colVals []types.Datum) (string, error) { } // addIndices adds data into indices. If any key is duplicated, returns the original handle. -func (t *tableCommon) addIndices(ctx sessionctx.Context, recordID int64, r []types.Datum, rm kv.RetrieverMutator, skipHandleCheck bool) (int64, error) { +func (t *tableCommon) addIndices(ctx sessionctx.Context, recordID int64, r []types.Datum, rm kv.RetrieverMutator, + opt *table.CreateIdxOpt) (int64, error) { txn, err := ctx.Txn(true) if err != nil { return 0, errors.Trace(err) @@ -544,7 +549,7 @@ func (t *tableCommon) addIndices(ctx sessionctx.Context, recordID int64, r []typ // Clean up lazy check error environment defer txn.DelOption(kv.PresumeKeyNotExistsError) skipCheck := ctx.GetSessionVars().LightningMode || ctx.GetSessionVars().StmtCtx.BatchCheck - if t.meta.PKIsHandle && !skipCheck && !skipHandleCheck { + if t.meta.PKIsHandle && !skipCheck && !opt.SkipHandleCheck { if err := CheckHandleExists(ctx, t, recordID, nil); err != nil { return recordID, errors.Trace(err) } @@ -567,7 +572,7 @@ func (t *tableCommon) addIndices(ctx sessionctx.Context, recordID int64, r []typ dupKeyErr = kv.ErrKeyExists.FastGen("Duplicate entry '%s' for key '%s'", entryKey, v.Meta().Name) txn.SetOption(kv.PresumeKeyNotExistsError, dupKeyErr) } - if dupHandle, err := v.Create(ctx, rm, indexVals, recordID); err != nil { + if dupHandle, err := v.Create(ctx, rm, indexVals, recordID, opt); err != nil { if kv.ErrKeyExists.Equal(err) { return dupHandle, errors.Trace(dupKeyErr) } diff --git a/table/tables/tables_test.go b/table/tables/tables_test.go index e2ada17a35bf8..d9307bb307882 100644 --- a/table/tables/tables_test.go +++ b/table/tables/tables_test.go @@ -87,7 +87,7 @@ func (ts *testSuite) TestBasic(c *C) { ctx := ts.se ctx.GetSessionVars().BinlogClient = binloginfo.GetPumpsClient() ctx.GetSessionVars().InRestrictedSQL = false - rid, err := tb.AddRecord(ctx, types.MakeDatums(1, "abc"), false) + rid, err := tb.AddRecord(ctx, types.MakeDatums(1, "abc")) c.Assert(err, IsNil) c.Assert(rid, Greater, int64(0)) row, err := tb.Row(ctx, rid) @@ -95,9 +95,9 @@ func (ts *testSuite) TestBasic(c *C) { c.Assert(len(row), Equals, 2) c.Assert(row[0].GetInt64(), Equals, int64(1)) - _, err = tb.AddRecord(ctx, types.MakeDatums(1, "aba"), false) + _, err = tb.AddRecord(ctx, types.MakeDatums(1, "aba")) c.Assert(err, NotNil) - _, err = tb.AddRecord(ctx, types.MakeDatums(2, "abc"), false) + _, err = tb.AddRecord(ctx, types.MakeDatums(2, "abc")) c.Assert(err, NotNil) c.Assert(tb.UpdateRecord(ctx, rid, types.MakeDatums(1, "abc"), types.MakeDatums(1, "cba"), []bool{false, true}), IsNil) @@ -128,7 +128,7 @@ func (ts *testSuite) TestBasic(c *C) { c.Assert(tb.RemoveRecord(ctx, rid, types.MakeDatums(1, "cba")), IsNil) // Make sure index data is also removed after tb.RemoveRecord(). c.Assert(indexCnt(), Equals, 0) - _, err = tb.AddRecord(ctx, types.MakeDatums(1, "abc"), false) + _, err = tb.AddRecord(ctx, types.MakeDatums(1, "abc")) c.Assert(err, IsNil) c.Assert(indexCnt(), Greater, 0) handle, found, err := tb.Seek(ctx, 0) @@ -231,9 +231,9 @@ func (ts *testSuite) TestUniqueIndexMultipleNullEntries(c *C) { c.Assert(err, IsNil) c.Assert(autoid, Greater, int64(0)) c.Assert(sctx.NewTxn(ctx), IsNil) - _, err = tb.AddRecord(sctx, types.MakeDatums(1, nil), false) + _, err = tb.AddRecord(sctx, types.MakeDatums(1, nil)) c.Assert(err, IsNil) - _, err = tb.AddRecord(sctx, types.MakeDatums(2, nil), false) + _, err = tb.AddRecord(sctx, types.MakeDatums(2, nil)) c.Assert(err, IsNil) txn, err := sctx.Txn(true) c.Assert(err, IsNil) @@ -290,7 +290,7 @@ func (ts *testSuite) TestUnsignedPK(c *C) { tb, err := ts.dom.InfoSchema().TableByName(model.NewCIStr("test"), model.NewCIStr("tPK")) c.Assert(err, IsNil) c.Assert(ts.se.NewTxn(context.Background()), IsNil) - rid, err := tb.AddRecord(ts.se, types.MakeDatums(1, "abc"), false) + rid, err := tb.AddRecord(ts.se, types.MakeDatums(1, "abc")) c.Assert(err, IsNil) row, err := tb.Row(ts.se, rid) c.Assert(err, IsNil) @@ -364,7 +364,7 @@ PARTITION BY RANGE ( id ) ( p0 := tbInfo.Partition.Definitions[0] c.Assert(p0.Name, Equals, model.NewCIStr("p0")) c.Assert(ts.se.NewTxn(ctx), IsNil) - rid, err := tb.AddRecord(ts.se, types.MakeDatums(1), false) + rid, err := tb.AddRecord(ts.se, types.MakeDatums(1)) c.Assert(err, IsNil) // Check that add record writes to the partition, rather than the table. @@ -377,11 +377,11 @@ PARTITION BY RANGE ( id ) ( c.Assert(kv.ErrNotExist.Equal(err), IsTrue) // Cover more code. - _, err = tb.AddRecord(ts.se, types.MakeDatums(7), false) + _, err = tb.AddRecord(ts.se, types.MakeDatums(7)) c.Assert(err, IsNil) - _, err = tb.AddRecord(ts.se, types.MakeDatums(12), false) + _, err = tb.AddRecord(ts.se, types.MakeDatums(12)) c.Assert(err, IsNil) - _, err = tb.AddRecord(ts.se, types.MakeDatums(16), false) + _, err = tb.AddRecord(ts.se, types.MakeDatums(16)) c.Assert(err, IsNil) // Make the changes visible. @@ -395,7 +395,7 @@ PARTITION BY RANGE ( id ) ( tk.MustQuery("select count(*) from t1 use index(id) where id > 6").Check(testkit.Rows("3")) // Value must locates in one partition. - _, err = tb.AddRecord(ts.se, types.MakeDatums(22), false) + _, err = tb.AddRecord(ts.se, types.MakeDatums(22)) c.Assert(table.ErrTrgInvalidCreationCtx.Equal(err), IsTrue) ts.se.Execute(context.Background(), "rollback") @@ -410,7 +410,7 @@ PARTITION BY RANGE ( id ) ( tb, err = ts.dom.InfoSchema().TableByName(model.NewCIStr("test"), model.NewCIStr("t2")) c.Assert(err, IsNil) tbInfo = tb.Meta() - _, err = tb.AddRecord(ts.se, types.MakeDatums(22), false) + _, err = tb.AddRecord(ts.se, types.MakeDatums(22)) c.Assert(err, IsNil) // Insert into maxvalue partition. } @@ -428,7 +428,7 @@ func (ts *testSuite) TestHashPartitionAddRecord(c *C) { tbInfo := tb.Meta() p0 := tbInfo.Partition.Definitions[0] c.Assert(ts.se.NewTxn(context.Background()), IsNil) - rid, err := tb.AddRecord(ts.se, types.MakeDatums(8), false) + rid, err := tb.AddRecord(ts.se, types.MakeDatums(8)) c.Assert(err, IsNil) // Check that add record writes to the partition, rather than the table. @@ -441,11 +441,11 @@ func (ts *testSuite) TestHashPartitionAddRecord(c *C) { c.Assert(kv.ErrNotExist.Equal(err), IsTrue) // Cover more code. - _, err = tb.AddRecord(ts.se, types.MakeDatums(-1), false) + _, err = tb.AddRecord(ts.se, types.MakeDatums(-1)) c.Assert(err, IsNil) - _, err = tb.AddRecord(ts.se, types.MakeDatums(3), false) + _, err = tb.AddRecord(ts.se, types.MakeDatums(3)) c.Assert(err, IsNil) - _, err = tb.AddRecord(ts.se, types.MakeDatums(6), false) + _, err = tb.AddRecord(ts.se, types.MakeDatums(6)) c.Assert(err, IsNil) // Make the changes visible. @@ -466,7 +466,7 @@ func (ts *testSuite) TestHashPartitionAddRecord(c *C) { tbInfo = tb.Meta() for i := 0; i < 11; i++ { c.Assert(ts.se.NewTxn(context.Background()), IsNil) - rid, err = tb.AddRecord(ts.se, types.MakeDatums(-i), false) + rid, err = tb.AddRecord(ts.se, types.MakeDatums(-i)) c.Assert(err, IsNil) txn, err = ts.se.Txn(true) c.Assert(err, IsNil) diff --git a/util/admin/admin_test.go b/util/admin/admin_test.go index e4da87fa9a806..113977d91ae1a 100644 --- a/util/admin/admin_test.go +++ b/util/admin/admin_test.go @@ -292,7 +292,7 @@ func (s *testSuite) TestScan(c *C) { c.Assert(err, IsNil) indices := tb.Indices() c.Assert(s.ctx.NewTxn(context.Background()), IsNil) - _, err = tb.AddRecord(s.ctx, types.MakeDatums(1, 10, 11), false) + _, err = tb.AddRecord(s.ctx, types.MakeDatums(1, 10, 11)) c.Assert(err, IsNil) txn, err := s.ctx.Txn(true) c.Assert(err, IsNil) @@ -307,7 +307,7 @@ func (s *testSuite) TestScan(c *C) { c.Assert(records, DeepEquals, []*RecordData{record1}) c.Assert(s.ctx.NewTxn(context.Background()), IsNil) - _, err = tb.AddRecord(s.ctx, record2.Values, false) + _, err = tb.AddRecord(s.ctx, record2.Values) c.Assert(err, IsNil) txn, err = s.ctx.Txn(true) c.Assert(err, IsNil)