Skip to content

Commit

Permalink
Merge branch 'master' into view-hint1
Browse files Browse the repository at this point in the history
  • Loading branch information
fzzf678 authored Nov 10, 2022
2 parents c8567df + f51227c commit ed82a4e
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 20 deletions.
9 changes: 6 additions & 3 deletions br/pkg/lightning/restore/meta_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -1186,9 +1186,12 @@ func getGlobalAutoIDAlloc(store kv.Storage, dbID int64, tblInfo *model.TableInfo
return nil, errors.New("internal error: dbID should not be 0")
}

// We don't need the cache here because we allocate all IDs at once.
// The argument for CustomAutoIncCacheOption is the cache step. step 1 means no cache.
noCache := autoid.CustomAutoIncCacheOption(1)
// We don't need autoid cache here because we allocate all IDs at once.
// The argument for CustomAutoIncCacheOption is the cache step. Step 1 means no cache,
// but step 1 will enable an experimental feature, so we use step 2 here.
//
// See https://github.com/pingcap/tidb/issues/38442 for more details.
noCache := autoid.CustomAutoIncCacheOption(2)
tblVer := autoid.AllocOptionTableInfoVersion(tblInfo.Version)

hasRowID := common.TableHasAutoRowID(tblInfo)
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
create table `exotic``table````name` (a varchar(6) primary key, b int unique auto_increment) auto_increment=80000;
create table `exotic``table````name` (a varchar(6) primary key /*T![clustered_index] NONCLUSTERED */, b int unique auto_increment) auto_increment=80000;
Original file line number Diff line number Diff line change
@@ -1 +1 @@
create table 中文表(a int primary key);
create table 中文表(a int primary key /*T![clustered_index] NONCLUSTERED */);
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ CREATE TABLE `test` (
`s1` char(10) NOT NULL,
`s2` char(10) NOT NULL,
`s3` char(10) DEFAULT NULL,
PRIMARY KEY (`s1`,`s2`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin/*!90000 SHARD_ROW_ID_BITS=3 PRE_SPLIT_REGIONS=3 */;
PRIMARY KEY (`s1`,`s2`) /*T![clustered_index] NONCLUSTERED */
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin/*!90000 SHARD_ROW_ID_BITS=3 PRE_SPLIT_REGIONS=3 */;
2 changes: 1 addition & 1 deletion br/tests/lightning_tidb_rowid/data/rowid.non_pk-schema.sql
Original file line number Diff line number Diff line change
@@ -1 +1 @@
create table non_pk (pk varchar(6) primary key);
create table non_pk (pk varchar(6) primary key /*T![clustered_index] NONCLUSTERED */);
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
CREATE TABLE `non_pk_auto_inc` (
`pk` char(36) NOT NULL,
`id` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`pk`),
PRIMARY KEY (`pk`) /*T![clustered_index] NONCLUSTERED */,
UNIQUE KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
18 changes: 18 additions & 0 deletions expression/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ import (

"github.com/pingcap/errors"
"github.com/pingcap/tidb/extension"
"github.com/pingcap/tidb/parser/auth"
"github.com/pingcap/tidb/parser/mysql"
"github.com/pingcap/tidb/privilege"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/chunk"
"github.com/pingcap/tidb/util/sem"
Expand Down Expand Up @@ -183,6 +185,22 @@ func (b *extensionFuncSig) EvalArgs(row chunk.Row) ([]types.Datum, error) {
return result, nil
}

func (b *extensionFuncSig) ConnectionInfo() *variable.ConnectionInfo {
return b.ctx.GetSessionVars().ConnectionInfo
}

func (b *extensionFuncSig) User() *auth.UserIdentity {
return b.ctx.GetSessionVars().User
}

func (b *extensionFuncSig) ActiveRoles() []*auth.RoleIdentity {
return b.ctx.GetSessionVars().ActiveRoles
}

func (b *extensionFuncSig) CurrentDB() string {
return b.ctx.GetSessionVars().CurrentDB
}

func init() {
extension.RegisterExtensionFunc = registerExtensionFunc
extension.RemoveExtensionFunc = removeExtensionFunc
Expand Down
8 changes: 7 additions & 1 deletion extension/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,19 @@ import (
"context"

"github.com/pingcap/errors"
"github.com/pingcap/tidb/parser/auth"
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/chunk"
)

// FunctionContext is a interface to provide context to the custom function
// FunctionContext is an interface to provide context to the custom function
type FunctionContext interface {
context.Context
User() *auth.UserIdentity
ActiveRoles() []*auth.RoleIdentity
CurrentDB() string
ConnectionInfo() *variable.ConnectionInfo
EvalArgs(row chunk.Row) ([]types.Datum, error)
}

Expand Down
73 changes: 63 additions & 10 deletions extension/function_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/pingcap/tidb/expression"
"github.com/pingcap/tidb/extension"
"github.com/pingcap/tidb/parser/auth"
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/testkit"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/chunk"
Expand Down Expand Up @@ -68,10 +69,64 @@ var customFunc2 = &extension.FunctionDef{
},
}

func TestInvokeFunc(t *testing.T) {
defer func() {
extension.Reset()
}()
func TestExtensionFuncCtx(t *testing.T) {
defer extension.Reset()
extension.Reset()

invoked := false
var user *auth.UserIdentity
var currentDB string
var activeRoles []*auth.RoleIdentity
var conn *variable.ConnectionInfo

require.NoError(t, extension.Register("test", extension.WithCustomFunctions([]*extension.FunctionDef{
{
Name: "custom_get_ctx",
EvalTp: types.ETString,
EvalStringFunc: func(ctx extension.FunctionContext, row chunk.Row) (string, bool, error) {
require.False(t, invoked)
invoked = true
user = ctx.User()
currentDB = ctx.CurrentDB()
activeRoles = ctx.ActiveRoles()
conn = ctx.ConnectionInfo()
return "done", false, nil
},
},
})))

store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("create user u1@localhost")
tk.MustExec("create role r1")
tk.MustExec("grant r1 to u1@localhost")
tk.MustExec("grant ALL ON test.* to u1@localhost")

tk1 := testkit.NewTestKit(t, store)
require.NoError(t, tk1.Session().Auth(&auth.UserIdentity{Username: "u1", Hostname: "localhost"}, nil, nil))
tk1.MustExec("set role r1")
tk1.MustExec("use test")
tk1.Session().GetSessionVars().ConnectionInfo = &variable.ConnectionInfo{
ConnectionID: 12345,
User: "u1",
}

tk1.MustQuery("select custom_get_ctx()").Check(testkit.Rows("done"))

require.True(t, invoked)
require.NotNil(t, user)
require.Equal(t, *tk1.Session().GetSessionVars().User, *user)
require.Equal(t, "test", currentDB)
require.NotNil(t, conn)
require.Equal(t, *tk1.Session().GetSessionVars().ConnectionInfo, *conn)
require.Equal(t, 1, len(activeRoles))
require.Equal(t, auth.RoleIdentity{Username: "r1", Hostname: "%"}, *activeRoles[0])
}

func TestInvokeExtensionFunc(t *testing.T) {
defer extension.Reset()
extension.Reset()

extension.Reset()
orgFuncList := expression.GetBuiltinList()
Expand Down Expand Up @@ -99,7 +154,7 @@ func TestInvokeFunc(t *testing.T) {
require.EqualError(t, tk2.ExecToErr("select custom_func2(1, 2)"), "[expression:1305]FUNCTION test.custom_func2 does not exist")
}

func TestFuncDynamicArgLen(t *testing.T) {
func TestExtensionFuncDynamicArgLen(t *testing.T) {
defer extension.Reset()
extension.Reset()

Expand Down Expand Up @@ -142,10 +197,8 @@ func TestFuncDynamicArgLen(t *testing.T) {
require.EqualError(t, tk.ExecToErr("select dynamic_arg_func(1, 2)"), expectedErrMsg)
}

func TestRegisterFunc(t *testing.T) {
defer func() {
extension.Reset()
}()
func TestRegisterExtensionFunc(t *testing.T) {
defer extension.Reset()

// nil func
extension.Reset()
Expand Down Expand Up @@ -213,7 +266,7 @@ func checkFuncList(t *testing.T, orgList []string, customFuncs ...string) {
require.Equal(t, checkList, expression.GetBuiltinList())
}

func TestFuncPrivilege(t *testing.T) {
func TestExtensionFuncPrivilege(t *testing.T) {
defer func() {
extension.Reset()
sem.Disable()
Expand Down

0 comments on commit ed82a4e

Please sign in to comment.