Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

executor: add an variable to compatible with MySQL insert for OGG #7863

Merged
merged 10 commits into from
Oct 17, 2018
4 changes: 3 additions & 1 deletion executor/insert_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,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().CompatibleInsert {
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_compatible_insert = 1")
tk.MustQuery(`select @@session.tidb_compatible_insert;`).Check(testkit.Rows("1"))
tk.MustExec("set global tidb_compatible_insert = 0")
tk.MustQuery(`select @@global.tidb_compatible_insert;`).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 @@ -2079,3 +2079,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) TestCompatibleInsert(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_compatible_insert = 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 @@ -1275,6 +1275,7 @@ const loadCommonGlobalVarsSQL = "select HIGH_PRIORITY * from mysql.global_variab
variable.TiDBHashAggPartialConcurrency + quoteCommaQuote +
variable.TiDBHashAggFinalConcurrency + quoteCommaQuote +
variable.TiDBBackoffLockFast + quoteCommaQuote +
variable.TiDBCompatibleInsert + 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 @@ -304,6 +304,9 @@ type SessionVars struct {
// EnableRadixJoin indicates whether to use radix hash join to execute
// HashJoin.
EnableRadixJoin bool

// CompatibleInsert indicates whether to check key exists when execute insert.
CompatibleInsert bool
}

// NewSessionVars creates a session vars object.
Expand Down Expand Up @@ -566,6 +569,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 TiDBCompatibleInsert:
s.CompatibleInsert = 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 @@ -657,6 +657,7 @@ var defaultSysVars = []*SysVar{
{ScopeGlobal | ScopeSession, TiDBBackoffLockFast, strconv.Itoa(kv.DefBackoffLockFast)},
{ScopeGlobal | ScopeSession, TiDBRetryLimit, strconv.Itoa(DefTiDBRetryLimit)},
{ScopeGlobal | ScopeSession, TiDBDisableTxnAutoRetry, boolToIntStr(DefTiDBDisableTxnAutoRetry)},
{ScopeGlobal | ScopeSession, TiDBCompatibleInsert, boolToIntStr(DefTiDBCompatibleInsert)},
{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 @@ -195,6 +195,10 @@ const (
// tidb_enable_radix_join indicates to use radix hash join algorithm to execute
// HashJoin.
TiDBEnableRadixJoin = "tidb_enable_radix_join"

// tidb_compatible_insert indicates to check the to-be-insert key exists or not when SQL is
// executing. It could hurt the performance of insert statement.
TiDBCompatibleInsert = "tidb_compatible_insert"
)

// Default TiDB system variable values.
Expand Down Expand Up @@ -228,6 +232,7 @@ const (
DefTiDBGeneralLog = 0
DefTiDBRetryLimit = 10
DefTiDBDisableTxnAutoRetry = false
DefTiDBCompatibleInsert = 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, TiDBCompatibleInsert:
if strings.EqualFold(value, "ON") || value == "1" {
return "1", nil
} else if strings.EqualFold(value, "OFF") || value == "0" {
Expand Down