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

expression, ddl: check the argument count for the generated column (#22154) #22174

Merged
merged 14 commits into from
Jan 27, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions ddl/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2952,6 +2952,10 @@ func (s *testDBSuite3) TestGeneratedColumnDDL(c *C) {
" `full_name` varchar(255) GENERATED ALWAYS AS (concat(`last_name`, _utf8mb4' ', `first_name`)) VIRTUAL\n" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin"))

// Test incorrect parameter count.
tk.MustGetErrCode("create table test_gv_incorrect_pc(a double, b int as (lower(a, 2)))", errno.ErrWrongParamcountToNativeFct)
tk.MustGetErrCode("create table test_gv_incorrect_pc(a double, b int as (lower(a, 2)) stored)", errno.ErrWrongParamcountToNativeFct)

genExprTests := []struct {
stmt string
err int
Expand Down Expand Up @@ -2982,6 +2986,14 @@ func (s *testDBSuite3) TestGeneratedColumnDDL(c *C) {
// Add stored generated column through alter table.
{`alter table test_gv_ddl add column d int as (b+2) stored`, errno.ErrUnsupportedOnGeneratedColumn},
{`alter table test_gv_ddl modify column b int as (a + 8) stored`, errno.ErrUnsupportedOnGeneratedColumn},

// Add generated column with incorrect parameter count.
{`alter table test_gv_ddl add column z int as (lower(a, 2))`, errno.ErrWrongParamcountToNativeFct},
{`alter table test_gv_ddl add column z int as (lower(a, 2)) stored`, errno.ErrWrongParamcountToNativeFct},

// Modify generated column with incorrect parameter count.
{`alter table test_gv_ddl modify column b int as (lower(a, 2))`, errno.ErrWrongParamcountToNativeFct},
{`alter table test_gv_ddl change column b b int as (lower(a, 2))`, errno.ErrWrongParamcountToNativeFct},
}
for _, tt := range genExprTests {
s.tk.MustGetErrCode(tt.stmt, tt.err)
Expand Down
6 changes: 6 additions & 0 deletions ddl/generated_column.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ type illegalFunctionChecker struct {
hasIllegalFunc bool
hasAggFunc bool
hasWindowFunc bool
otherErr error
}

func (c *illegalFunctionChecker) Enter(inNode ast.Node) (outNode ast.Node, skipChildren bool) {
Expand All @@ -248,6 +249,11 @@ func (c *illegalFunctionChecker) Enter(inNode ast.Node) (outNode ast.Node, skipC
c.hasIllegalFunc = true
return inNode, true
}
err := expression.VerifyArgsWrapper(node.FnName.L, len(node.Args))
if err != nil {
c.otherErr = err
return inNode, true
}
case *ast.SubqueryExpr, *ast.ValuesExpr, *ast.VariableExpr:
// Subquery & `values(x)` & variable is not allowed
c.hasIllegalFunc = true
Expand Down
17 changes: 16 additions & 1 deletion expression/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -542,17 +542,32 @@ type baseFunctionClass struct {
}

func (b *baseFunctionClass) verifyArgs(args []Expression) error {
l := len(args)
return b.verifyArgsByCount(len(args))
}

func (b *baseFunctionClass) verifyArgsByCount(l int) error {
if l < b.minArgs || (b.maxArgs != -1 && l > b.maxArgs) {
return ErrIncorrectParameterCount.GenWithStackByArgs(b.funcName)
}
return nil
}

// VerifyArgsWrapper verifies a function by its name and the count of the arguments.
// Note that this function assumes that the function is supported.
func VerifyArgsWrapper(name string, l int) error {
f, ok := funcs[name]
if !ok {
return nil
}
return f.verifyArgsByCount(l)
}

// functionClass is the interface for a function which may contains multiple functions.
type functionClass interface {
// getFunction gets a function signature by the types and the counts of given arguments.
getFunction(ctx sessionctx.Context, args []Expression) (builtinFunc, error)
// verifyArgsByCount verifies the count of parameters.
verifyArgsByCount(l int) error
}

// funcs holds all registered builtin functions. When new function is added,
Expand Down