-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into extension_bootstrap
- Loading branch information
Showing
9 changed files
with
596 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
// Copyright 2022 PingCAP, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package expression | ||
|
||
import ( | ||
"strings" | ||
"sync" | ||
|
||
"github.com/pingcap/errors" | ||
"github.com/pingcap/tidb/extension" | ||
"github.com/pingcap/tidb/parser/mysql" | ||
"github.com/pingcap/tidb/privilege" | ||
"github.com/pingcap/tidb/sessionctx" | ||
"github.com/pingcap/tidb/types" | ||
"github.com/pingcap/tidb/util/chunk" | ||
"github.com/pingcap/tidb/util/sem" | ||
) | ||
|
||
var extensionFuncs sync.Map | ||
|
||
func registerExtensionFunc(def *extension.FunctionDef) error { | ||
if def == nil { | ||
return errors.New("extension function def is nil") | ||
} | ||
|
||
if def.Name == "" { | ||
return errors.New("extension function name should not be empty") | ||
} | ||
|
||
lowerName := strings.ToLower(def.Name) | ||
if _, ok := funcs[lowerName]; ok { | ||
return errors.Errorf("extension function name '%s' conflict with builtin", def.Name) | ||
} | ||
|
||
class, err := newExtensionFuncClass(def) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
_, exist := extensionFuncs.LoadOrStore(lowerName, class) | ||
if exist { | ||
return errors.Errorf("duplicated extension function name '%s'", def.Name) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func removeExtensionFunc(name string) { | ||
extensionFuncs.Delete(name) | ||
} | ||
|
||
type extensionFuncClass struct { | ||
baseFunctionClass | ||
funcDef extension.FunctionDef | ||
flen int | ||
} | ||
|
||
func newExtensionFuncClass(def *extension.FunctionDef) (*extensionFuncClass, error) { | ||
var flen int | ||
switch def.EvalTp { | ||
case types.ETString: | ||
flen = mysql.MaxFieldVarCharLength | ||
if def.EvalStringFunc == nil { | ||
return nil, errors.New("eval function is nil") | ||
} | ||
case types.ETInt: | ||
flen = mysql.MaxIntWidth | ||
if def.EvalIntFunc == nil { | ||
return nil, errors.New("eval function is nil") | ||
} | ||
default: | ||
return nil, errors.Errorf("unsupported extension function ret type: '%v'", def.EvalTp) | ||
} | ||
|
||
return &extensionFuncClass{ | ||
baseFunctionClass: baseFunctionClass{def.Name, len(def.ArgTps), len(def.ArgTps)}, | ||
flen: flen, | ||
funcDef: *def, | ||
}, nil | ||
} | ||
|
||
func (c *extensionFuncClass) getFunction(ctx sessionctx.Context, args []Expression) (builtinFunc, error) { | ||
if err := c.checkPrivileges(ctx); err != nil { | ||
return nil, err | ||
} | ||
|
||
if err := c.verifyArgs(args); err != nil { | ||
return nil, err | ||
} | ||
bf, err := newBaseBuiltinFuncWithTp(ctx, c.funcName, args, c.funcDef.EvalTp, c.funcDef.ArgTps...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
bf.tp.SetFlen(c.flen) | ||
sig := &extensionFuncSig{bf, c.funcDef} | ||
return sig, nil | ||
} | ||
|
||
func (c *extensionFuncClass) checkPrivileges(ctx sessionctx.Context) error { | ||
privs := c.funcDef.RequireDynamicPrivileges | ||
if semPrivs := c.funcDef.SemRequireDynamicPrivileges; len(semPrivs) > 0 && sem.IsEnabled() { | ||
privs = semPrivs | ||
} | ||
|
||
if len(privs) == 0 { | ||
return nil | ||
} | ||
|
||
manager := privilege.GetPrivilegeManager(ctx) | ||
activeRoles := ctx.GetSessionVars().ActiveRoles | ||
|
||
for _, priv := range privs { | ||
if !manager.RequestDynamicVerification(activeRoles, priv, false) { | ||
msg := priv | ||
if !sem.IsEnabled() { | ||
msg = "SUPER or " + msg | ||
} | ||
return errSpecificAccessDenied.GenWithStackByArgs(msg) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
var _ extension.FunctionContext = &extensionFuncSig{} | ||
|
||
type extensionFuncSig struct { | ||
baseBuiltinFunc | ||
extension.FunctionDef | ||
} | ||
|
||
func (b *extensionFuncSig) Clone() builtinFunc { | ||
newSig := &extensionFuncSig{} | ||
newSig.cloneFrom(&b.baseBuiltinFunc) | ||
newSig.FunctionDef = b.FunctionDef | ||
return newSig | ||
} | ||
|
||
func (b *extensionFuncSig) evalString(row chunk.Row) (string, bool, error) { | ||
if b.EvalTp == types.ETString { | ||
return b.EvalStringFunc(b, row) | ||
} | ||
return b.baseBuiltinFunc.evalString(row) | ||
} | ||
|
||
func (b *extensionFuncSig) evalInt(row chunk.Row) (int64, bool, error) { | ||
if b.EvalTp == types.ETInt { | ||
return b.EvalIntFunc(b, row) | ||
} | ||
return b.baseBuiltinFunc.evalInt(row) | ||
} | ||
|
||
func (b *extensionFuncSig) EvalArgs(row chunk.Row) ([]types.Datum, error) { | ||
if len(b.args) == 0 { | ||
return nil, nil | ||
} | ||
|
||
result := make([]types.Datum, 0, len(b.args)) | ||
for _, arg := range b.args { | ||
val, err := arg.Eval(row) | ||
if err != nil { | ||
return nil, err | ||
} | ||
result = append(result, val) | ||
} | ||
|
||
return result, nil | ||
} | ||
|
||
func init() { | ||
extension.RegisterExtensionFunc = registerExtensionFunc | ||
extension.RemoveExtensionFunc = removeExtensionFunc | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,5 +11,6 @@ go_library( | |
"//kv", | ||
"//util/chunk", | ||
"//util/sqlexec", | ||
"@com_github_pingcap_errors//:errors", | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright 2022 PingCAP, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package extension | ||
|
||
import ( | ||
"github.com/pingcap/tidb/types" | ||
"github.com/pingcap/tidb/util/chunk" | ||
) | ||
|
||
// FunctionContext is a interface to provide context to the custom function | ||
type FunctionContext interface { | ||
EvalArgs(row chunk.Row) ([]types.Datum, error) | ||
} | ||
|
||
// FunctionDef is the definition for the custom function | ||
type FunctionDef struct { | ||
Name string | ||
EvalTp types.EvalType | ||
ArgTps []types.EvalType | ||
// EvalStringFunc is the eval function when `EvalTp` is `types.ETString` | ||
EvalStringFunc func(ctx FunctionContext, row chunk.Row) (string, bool, error) | ||
// EvalIntFunc is the eval function when `EvalTp` is `types.ETInt` | ||
EvalIntFunc func(ctx FunctionContext, row chunk.Row) (int64, bool, error) | ||
// RequireDynamicPrivileges is the dynamic privileges needed to invoke the function | ||
// If `RequireDynamicPrivileges` is empty, it means every one can invoke this function | ||
RequireDynamicPrivileges []string | ||
// SemRequireDynamicPrivileges is the dynamic privileges needed to invoke the function in sem mode | ||
// If `SemRequireDynamicPrivileges` is empty, `DynamicPrivileges` will be used in sem mode | ||
SemRequireDynamicPrivileges []string | ||
} | ||
|
||
// RegisterExtensionFunc is to avoid dependency cycle | ||
var RegisterExtensionFunc func(*FunctionDef) error | ||
|
||
// RemoveExtensionFunc is to avoid dependency cycle | ||
var RemoveExtensionFunc func(string) |
Oops, something went wrong.