Skip to content

Commit

Permalink
ddl: forbid invalid usage of window function in the generated column (#…
Browse files Browse the repository at this point in the history
…20855)

Signed-off-by: wjhuang2016 <huangwenjun1997@gmail.com>
  • Loading branch information
wjhuang2016 authored Nov 6, 2020
1 parent c243a0b commit 2c8b28c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
8 changes: 8 additions & 0 deletions ddl/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6361,3 +6361,11 @@ func (s *testSerialDBSuite) TestModifyColumnTypeWhenInterception(c *C) {
res := tk.MustQuery("show warnings")
c.Assert(len(res.Rows()), Equals, count)
}

func (s *testDBSuite4) TestGeneratedColumnWindowFunction(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test_db")
tk.MustExec("DROP TABLE IF EXISTS t")
tk.MustGetErrCode("CREATE TABLE t (a INT , b INT as (ROW_NUMBER() OVER (ORDER BY a)))", errno.ErrWindowInvalidWindowFuncUse)
tk.MustGetErrCode("CREATE TABLE t (a INT , index idx ((ROW_NUMBER() OVER (ORDER BY a))))", errno.ErrWindowInvalidWindowFuncUse)
}
1 change: 1 addition & 0 deletions ddl/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ var (
errUnsupportedCreatePartition = dbterror.ClassDDL.NewStdErr(mysql.ErrUnsupportedDDLOperation, parser_mysql.Message(fmt.Sprintf(mysql.MySQLErrName[mysql.ErrUnsupportedDDLOperation].Raw, "partition type, treat as normal table"), nil), "", "")
errTablePartitionDisabled = dbterror.ClassDDL.NewStdErr(mysql.ErrUnsupportedDDLOperation, parser_mysql.Message("Partitions are ignored because Table Partition is disabled, please set 'tidb_enable_table_partition' if you need to need to enable it", nil), "", "")
errUnsupportedIndexType = dbterror.ClassDDL.NewStdErr(mysql.ErrUnsupportedDDLOperation, parser_mysql.Message(fmt.Sprintf(mysql.MySQLErrName[mysql.ErrUnsupportedDDLOperation].Raw, "index type"), nil), "", "")
errWindowInvalidWindowFuncUse = dbterror.ClassDDL.NewStd(mysql.ErrWindowInvalidWindowFuncUse)

// ErrDupKeyName returns for duplicated key name
ErrDupKeyName = dbterror.ClassDDL.NewStd(mysql.ErrDupKeyName)
Expand Down
7 changes: 7 additions & 0 deletions ddl/generated_column.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ type illegalFunctionChecker struct {
hasIllegalFunc bool
hasAggFunc bool
hasRowVal bool // hasRowVal checks whether the functional index refers to a row value
hasWindowFunc bool
}

func (c *illegalFunctionChecker) Enter(inNode ast.Node) (outNode ast.Node, skipChildren bool) {
Expand All @@ -251,6 +252,9 @@ func (c *illegalFunctionChecker) Enter(inNode ast.Node) (outNode ast.Node, skipC
case *ast.RowExpr:
c.hasRowVal = true
return inNode, true
case *ast.WindowFuncExpr:
c.hasWindowFunc = true
return inNode, true
}
return inNode, false
}
Expand Down Expand Up @@ -289,6 +293,9 @@ func checkIllegalFn4Generated(name string, genType int, expr ast.ExprNode) error
return ErrFunctionalIndexRowValueIsNotAllowed.GenWithStackByArgs(name)
}
}
if c.hasWindowFunc {
return errWindowInvalidWindowFuncUse.GenWithStackByArgs(name)
}
return nil
}

Expand Down

0 comments on commit 2c8b28c

Please sign in to comment.