Skip to content

Commit

Permalink
executor: add an variable to compatible with MySQL insert for OGG (#7863
Browse files Browse the repository at this point in the history
)
  • Loading branch information
jackysp authored and zz-jason committed Oct 17, 2018
1 parent 458c0d1 commit f3148da
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 2 deletions.
4 changes: 3 additions & 1 deletion executor/insert_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,9 @@ func (e *InsertValues) batchCheckAndInsert(rows [][]types.Datum, addRecord func(
}

func (e *InsertValues) addRecord(row []types.Datum) (int64, error) {
e.ctx.Txn().SetOption(kv.PresumeKeyNotExists, nil)
if !e.ctx.GetSessionVars().ConstraintCheckInPlace {
e.ctx.Txn().SetOption(kv.PresumeKeyNotExists, nil)
}
h, err := e.Table.AddRecord(e.ctx, row, false)
e.ctx.Txn().DelOption(kv.PresumeKeyNotExists)
if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions executor/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,11 @@ func (s *testSuite) TestSetVar(c *C) {
tk.MustQuery(`select @@tidb_force_priority;`).Check(testkit.Rows("NO_PRIORITY"))
_, err = tk.Exec(`set global tidb_force_priority = ""`)
c.Assert(err, NotNil)

tk.MustExec("set tidb_constraint_check_in_place = 1")
tk.MustQuery(`select @@session.tidb_constraint_check_in_place;`).Check(testkit.Rows("1"))
tk.MustExec("set global tidb_constraint_check_in_place = 0")
tk.MustQuery(`select @@global.tidb_constraint_check_in_place;`).Check(testkit.Rows("0"))
}

func (s *testSuite) TestSetCharset(c *C) {
Expand Down
14 changes: 14 additions & 0 deletions executor/write_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2091,3 +2091,17 @@ func (s *testSuite) TestRebaseIfNeeded(c *C) {
tk.MustExec(`insert into t (b) values (6);`)
tk.MustQuery(`select a from t where b = 6;`).Check(testkit.Rows("30003"))
}

func (s *testSuite) TestDeferConstraintCheckForInsert(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec(`use test`)
tk.MustExec(`drop table if exists t;create table t (i int key);`)
tk.MustExec(`insert t values (1);`)
tk.MustExec(`set tidb_constraint_check_in_place = 1;`)
tk.MustExec(`begin;`)
_, err := tk.Exec(`insert t values (1);`)
c.Assert(err, NotNil)
tk.MustExec(`update t set i = 2 where i = 1;`)
tk.MustExec(`commit;`)
tk.MustQuery(`select * from t;`).Check(testkit.Rows("2"))
}
1 change: 1 addition & 0 deletions session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -1281,6 +1281,7 @@ const loadCommonGlobalVarsSQL = "select HIGH_PRIORITY * from mysql.global_variab
variable.TiDBHashAggPartialConcurrency + quoteCommaQuote +
variable.TiDBHashAggFinalConcurrency + quoteCommaQuote +
variable.TiDBBackoffLockFast + quoteCommaQuote +
variable.TiDBConstraintCheckInPlace + quoteCommaQuote +
variable.TiDBDDLReorgWorkerCount + quoteCommaQuote +
variable.TiDBOptInSubqUnFolding + quoteCommaQuote +
variable.TiDBDistSQLScanConcurrency + quoteCommaQuote +
Expand Down
5 changes: 5 additions & 0 deletions sessionctx/variable/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,9 @@ type SessionVars struct {
// HashJoin.
EnableRadixJoin bool

// ConstraintCheckInPlace indicates whether to check the constraint when the SQL executing.
ConstraintCheckInPlace bool

// CommandValue indicates which command current session is doing.
CommandValue uint32
}
Expand Down Expand Up @@ -573,6 +576,8 @@ func (s *SessionVars) SetSystemVar(name string, val string) error {
s.IndexSerialScanConcurrency = tidbOptPositiveInt32(val, DefIndexSerialScanConcurrency)
case TiDBBackoffLockFast:
s.KVVars.BackoffLockFast = tidbOptPositiveInt32(val, kv.DefBackoffLockFast)
case TiDBConstraintCheckInPlace:
s.ConstraintCheckInPlace = TiDBOptOn(val)
case TiDBBatchInsert:
s.BatchInsert = TiDBOptOn(val)
case TiDBBatchDelete:
Expand Down
1 change: 1 addition & 0 deletions sessionctx/variable/sysvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,7 @@ var defaultSysVars = []*SysVar{
{ScopeGlobal | ScopeSession, TiDBBackoffLockFast, strconv.Itoa(kv.DefBackoffLockFast)},
{ScopeGlobal | ScopeSession, TiDBRetryLimit, strconv.Itoa(DefTiDBRetryLimit)},
{ScopeGlobal | ScopeSession, TiDBDisableTxnAutoRetry, boolToIntStr(DefTiDBDisableTxnAutoRetry)},
{ScopeGlobal | ScopeSession, TiDBConstraintCheckInPlace, boolToIntStr(DefTiDBConstraintCheckInPlace)},
{ScopeSession, TiDBOptimizerSelectivityLevel, strconv.Itoa(DefTiDBOptimizerSelectivityLevel)},
/* The following variable is defined as session scope but is actually server scope. */
{ScopeSession, TiDBGeneralLog, strconv.Itoa(DefTiDBGeneralLog)},
Expand Down
5 changes: 5 additions & 0 deletions sessionctx/variable/tidb_vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,10 @@ const (
// tidb_enable_radix_join indicates to use radix hash join algorithm to execute
// HashJoin.
TiDBEnableRadixJoin = "tidb_enable_radix_join"

// tidb_constraint_check_in_place indicates to check the constraint when the SQL executing.
// It could hurt the performance of bulking insert when it is ON.
TiDBConstraintCheckInPlace = "tidb_constraint_check_in_place"
)

// Default TiDB system variable values.
Expand Down Expand Up @@ -231,6 +235,7 @@ const (
DefTiDBGeneralLog = 0
DefTiDBRetryLimit = 10
DefTiDBDisableTxnAutoRetry = false
DefTiDBConstraintCheckInPlace = false
DefTiDBHashJoinConcurrency = 5
DefTiDBProjectionConcurrency = 4
DefTiDBOptimizerSelectivityLevel = 0
Expand Down
2 changes: 1 addition & 1 deletion sessionctx/variable/varsutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ func ValidateSetSystemVar(vars *SessionVars, name string, value string) (string,
case WarningCount, ErrorCount:
return value, ErrReadOnly.GenWithStackByArgs(name)
case GeneralLog, TiDBGeneralLog, AvoidTemporalUpgrade, BigTables, CheckProxyUsers, CoreFile, EndMakersInJSON, SQLLogBin, OfflineMode,
PseudoSlaveMode, LowPriorityUpdates, SkipNameResolve, ForeignKeyChecks, SQLSafeUpdates:
PseudoSlaveMode, LowPriorityUpdates, SkipNameResolve, ForeignKeyChecks, SQLSafeUpdates, TiDBConstraintCheckInPlace:
if strings.EqualFold(value, "ON") || value == "1" {
return "1", nil
} else if strings.EqualFold(value, "OFF") || value == "0" {
Expand Down

0 comments on commit f3148da

Please sign in to comment.