diff --git a/expression/integration_test.go b/expression/integration_test.go index 72ceafe42a83e..199d008f9521a 100644 --- a/expression/integration_test.go +++ b/expression/integration_test.go @@ -10338,3 +10338,17 @@ func (s *testIntegrationSuite) TestLastInsertId(c *C) { tk.MustExec(`INSERT INTO lastinsertid VALUES (NULL);`) tk.MustQuery("SELECT @@last_insert_id, LAST_INSERT_ID()").Check(testkit.Rows("3 3")) } + +func (s *testIntegrationSuite) TestIdentity(c *C) { + tk := testkit.NewTestKit(c, s.store) + tk.MustExec(`use test;`) + tk.MustExec(`drop table if exists identity;`) + tk.MustExec(`create table identity (id int not null primary key auto_increment);`) + tk.MustQuery("SELECT @@identity;").Check(testkit.Rows("0")) + tk.MustExec(`INSERT INTO identity VALUES (NULL);`) + tk.MustQuery("SELECT @@identity, LAST_INSERT_ID()").Check(testkit.Rows("1 1")) + tk.MustExec(`INSERT INTO identity VALUES (NULL);`) + tk.MustQuery("SELECT @@identity, LAST_INSERT_ID()").Check(testkit.Rows("2 2")) + tk.MustExec(`INSERT INTO identity VALUES (NULL);`) + tk.MustQuery("SELECT @@identity, LAST_INSERT_ID()").Check(testkit.Rows("3 3")) +} diff --git a/sessionctx/variable/noop.go b/sessionctx/variable/noop.go index d41689f75af7d..fe4fe12acb951 100644 --- a/sessionctx/variable/noop.go +++ b/sessionctx/variable/noop.go @@ -485,7 +485,6 @@ var noopSysVars = []*SysVar{ {Scope: ScopeGlobal, Name: AvoidTemporalUpgrade, Value: Off, Type: TypeBool}, {Scope: ScopeGlobal, Name: "key_cache_age_threshold", Value: "300"}, {Scope: ScopeGlobal, Name: InnodbStatusOutput, Value: Off, Type: TypeBool, AutoConvertNegativeBool: true}, - {Scope: ScopeSession, Name: "identity", Value: ""}, {Scope: ScopeGlobal | ScopeSession, Name: "min_examined_row_limit", Value: "0"}, {Scope: ScopeGlobal, Name: "sync_frm", Type: TypeBool, Value: On}, {Scope: ScopeGlobal, Name: "innodb_online_alter_log_max_size", Value: "134217728"}, diff --git a/sessionctx/variable/sysvar.go b/sessionctx/variable/sysvar.go index 46ba7d030e0c4..53478bbc49732 100644 --- a/sessionctx/variable/sysvar.go +++ b/sessionctx/variable/sysvar.go @@ -807,6 +807,9 @@ var defaultSysVars = []*SysVar{ {Scope: ScopeSession, Name: LastInsertID, Value: "", skipInit: true, GetSession: func(s *SessionVars) (string, error) { return strconv.FormatUint(s.StmtCtx.PrevLastInsertID, 10), nil }}, + {Scope: ScopeSession, Name: Identity, Value: "", skipInit: true, GetSession: func(s *SessionVars) (string, error) { + return strconv.FormatUint(s.StmtCtx.PrevLastInsertID, 10), nil + }}, {Scope: ScopeNone, Name: "have_ssl", Value: "DISABLED"}, {Scope: ScopeNone, Name: "have_openssl", Value: "DISABLED"}, {Scope: ScopeNone, Name: "ssl_ca", Value: ""}, @@ -2136,6 +2139,8 @@ const ( DefaultAuthPlugin = "default_authentication_plugin" // LastInsertID is the name of 'last_insert_id' system variable. LastInsertID = "last_insert_id" + // Identity is the name of 'identity' system variable. + Identity = "identity" ) // GlobalVarAccessor is the interface for accessing global scope system and status variables. diff --git a/sessionctx/variable/sysvar_test.go b/sessionctx/variable/sysvar_test.go index c0bb1769c607a..45715c0be789c 100644 --- a/sessionctx/variable/sysvar_test.go +++ b/sessionctx/variable/sysvar_test.go @@ -734,3 +734,15 @@ func TestLastInsertID(t *testing.T) { require.NoError(t, err) require.Equal(t, val, "21") } + +func TestIdentity(t *testing.T) { + vars := NewSessionVars() + val, err := GetSessionOrGlobalSystemVar(vars, Identity) + require.NoError(t, err) + require.Equal(t, val, "0") + + vars.StmtCtx.PrevLastInsertID = 21 + val, err = GetSessionOrGlobalSystemVar(vars, Identity) + require.NoError(t, err) + require.Equal(t, val, "21") +}