Skip to content

Commit

Permalink
expression, planner: check function exists with schema name (#46767)
Browse files Browse the repository at this point in the history
close #46744
  • Loading branch information
Defined2014 authored Sep 8, 2023
1 parent c6eb7bb commit fa0c5ce
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 15 deletions.
5 changes: 5 additions & 0 deletions errors.toml
Original file line number Diff line number Diff line change
Expand Up @@ -1916,6 +1916,11 @@ error = '''
Illegal mix of collations for operation '%s'
'''

["expression:1305"]
error = '''
%s %s does not exist
'''

["expression:1365"]
error = '''
Division by 0
Expand Down
8 changes: 4 additions & 4 deletions expression/builtin_encryption.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,15 +423,15 @@ type desDecryptFunctionClass struct {
}

func (c *desDecryptFunctionClass) getFunction(ctx sessionctx.Context, args []Expression) (builtinFunc, error) {
return nil, errFunctionNotExists.GenWithStackByArgs("FUNCTION", "DES_DECRYPT")
return nil, ErrFunctionNotExists.GenWithStackByArgs("FUNCTION", "DES_DECRYPT")
}

type desEncryptFunctionClass struct {
baseFunctionClass
}

func (c *desEncryptFunctionClass) getFunction(ctx sessionctx.Context, args []Expression) (builtinFunc, error) {
return nil, errFunctionNotExists.GenWithStackByArgs("FUNCTION", "DES_ENCRYPT")
return nil, ErrFunctionNotExists.GenWithStackByArgs("FUNCTION", "DES_ENCRYPT")
}

type encodeFunctionClass struct {
Expand Down Expand Up @@ -486,15 +486,15 @@ type encryptFunctionClass struct {
}

func (c *encryptFunctionClass) getFunction(ctx sessionctx.Context, args []Expression) (builtinFunc, error) {
return nil, errFunctionNotExists.GenWithStackByArgs("FUNCTION", "ENCRYPT")
return nil, ErrFunctionNotExists.GenWithStackByArgs("FUNCTION", "ENCRYPT")
}

type oldPasswordFunctionClass struct {
baseFunctionClass
}

func (c *oldPasswordFunctionClass) getFunction(ctx sessionctx.Context, args []Expression) (builtinFunc, error) {
return nil, errFunctionNotExists.GenWithStackByArgs("FUNCTION", "OLD_PASSWORD")
return nil, ErrFunctionNotExists.GenWithStackByArgs("FUNCTION", "OLD_PASSWORD")
}

type passwordFunctionClass struct {
Expand Down
6 changes: 3 additions & 3 deletions expression/builtin_miscellaneous.go
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ type defaultFunctionClass struct {
}

func (c *defaultFunctionClass) getFunction(ctx sessionctx.Context, args []Expression) (builtinFunc, error) {
return nil, errFunctionNotExists.GenWithStackByArgs("FUNCTION", "DEFAULT")
return nil, ErrFunctionNotExists.GenWithStackByArgs("FUNCTION", "DEFAULT")
}

type inetAtonFunctionClass struct {
Expand Down Expand Up @@ -1084,7 +1084,7 @@ type masterPosWaitFunctionClass struct {
}

func (c *masterPosWaitFunctionClass) getFunction(ctx sessionctx.Context, args []Expression) (builtinFunc, error) {
return nil, errFunctionNotExists.GenWithStackByArgs("FUNCTION", "MASTER_POS_WAIT")
return nil, ErrFunctionNotExists.GenWithStackByArgs("FUNCTION", "MASTER_POS_WAIT")
}

type nameConstFunctionClass struct {
Expand Down Expand Up @@ -1308,7 +1308,7 @@ type uuidShortFunctionClass struct {
}

func (c *uuidShortFunctionClass) getFunction(ctx sessionctx.Context, args []Expression) (builtinFunc, error) {
return nil, errFunctionNotExists.GenWithStackByArgs("FUNCTION", "UUID_SHORT")
return nil, ErrFunctionNotExists.GenWithStackByArgs("FUNCTION", "UUID_SHORT")
}

type vitessHashFunctionClass struct {
Expand Down
2 changes: 1 addition & 1 deletion expression/builtin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func TestDisplayName(t *testing.T) {
func newFunctionForTest(ctx sessionctx.Context, funcName string, args ...Expression) (Expression, error) {
fc, ok := funcs[funcName]
if !ok {
return nil, errFunctionNotExists.GenWithStackByArgs("FUNCTION", funcName)
return nil, ErrFunctionNotExists.GenWithStackByArgs("FUNCTION", funcName)
}
funcArgs := make([]Expression, len(args))
copy(funcArgs, args)
Expand Down
2 changes: 1 addition & 1 deletion expression/distsql_builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -1086,7 +1086,7 @@ func getSignatureByPB(ctx sessionctx.Context, sigCode tipb.ScalarFuncSig, tp *ti
f = &builtinInternalFromBinarySig{base}

default:
e = errFunctionNotExists.GenWithStackByArgs("FUNCTION", sigCode)
e = ErrFunctionNotExists.GenWithStackByArgs("FUNCTION", sigCode)
return nil, e
}
f.setPbCode(sigCode)
Expand Down
2 changes: 1 addition & 1 deletion expression/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ var (
ErrInvalidJSONForFuncIndex = dbterror.ClassExpression.NewStd(mysql.ErrInvalidJSONValueForFuncIndex)
ErrDataOutOfRangeFuncIndex = dbterror.ClassExpression.NewStd(mysql.ErrDataOutOfRangeFunctionalIndex)
ErrFuncIndexDataIsTooLong = dbterror.ClassExpression.NewStd(mysql.ErrFunctionalIndexDataIsTooLong)
ErrFunctionNotExists = dbterror.ClassExpression.NewStd(mysql.ErrSpDoesNotExist)

// All the un-exported errors are defined here:
errFunctionNotExists = dbterror.ClassExpression.NewStd(mysql.ErrSpDoesNotExist)
errZlibZData = dbterror.ClassExpression.NewStd(mysql.ErrZlibZData)
errZlibZBuf = dbterror.ClassExpression.NewStd(mysql.ErrZlibZBuf)
errIncorrectArgs = dbterror.ClassExpression.NewStd(mysql.ErrWrongArguments)
Expand Down
5 changes: 2 additions & 3 deletions expression/integration_test/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3717,15 +3717,14 @@ func TestNotExistFunc(t *testing.T) {

// current db is empty
tk.MustGetErrMsg("SELECT xxx(1)", "[planner:1046]No database selected")

tk.MustGetErrMsg("SELECT yyy()", "[planner:1046]No database selected")
tk.MustGetErrMsg("SELECT T.upper(1)", "[expression:1305]FUNCTION t.upper does not exist")

// current db is not empty
tk.MustExec("use test")
tk.MustGetErrMsg("SELECT xxx(1)", "[expression:1305]FUNCTION test.xxx does not exist")
tk.MustGetErrMsg("SELECT yyy()", "[expression:1305]FUNCTION test.yyy does not exist")

tk.MustExec("use test")
tk.MustGetErrMsg("SELECT t.upper(1)", "[expression:1305]FUNCTION t.upper does not exist")
tk.MustGetErrMsg("SELECT timestampliteral(rand())", "[expression:1305]FUNCTION test.timestampliteral does not exist")
}

Expand Down
2 changes: 1 addition & 1 deletion expression/scalar_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ func newFunctionImpl(ctx sessionctx.Context, fold int, funcName string, retType
if db == "" {
return nil, errors.Trace(ErrNoDB)
}
return nil, errFunctionNotExists.GenWithStackByArgs("FUNCTION", db+"."+funcName)
return nil, ErrFunctionNotExists.GenWithStackByArgs("FUNCTION", db+"."+funcName)
}
noopFuncsMode := ctx.GetSessionVars().NoopFuncsMode
if noopFuncsMode != variable.OnInt {
Expand Down
5 changes: 4 additions & 1 deletion planner/core/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,10 @@ func (p *preprocessor) Enter(in ast.Node) (out ast.Node, skipChildren bool) {
if node.FnName.L == ast.NextVal || node.FnName.L == ast.LastVal || node.FnName.L == ast.SetVal {
p.flag |= inSequenceFunction
}

// not support procedure right now.
if node.Schema.L != "" {
p.err = expression.ErrFunctionNotExists.GenWithStackByArgs("FUNCTION", node.Schema.L+"."+node.FnName.L)
}
case *ast.BRIEStmt:
if node.Kind == ast.BRIEKindRestore {
p.flag |= inCreateOrDropTable
Expand Down

0 comments on commit fa0c5ce

Please sign in to comment.