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

ddl: add the not expression check when creating a partition table #21497

Merged
merged 21 commits into from
Dec 24, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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
13 changes: 13 additions & 0 deletions ddl/db_partition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3156,3 +3156,16 @@ func (s *testIntegrationSuite7) TestAddPartitionForTableWithWrongType(c *C) {
c.Assert(err, NotNil)
c.Assert(ddl.ErrWrongTypeColumnValue.Equal(err), IsTrue)
}

func (s *testIntegrationSuite5) TestBuildPartitionWithExpr(c *C) {
tk := testkit.NewTestKit(c, s.store)
Reminiscent marked this conversation as resolved.
Show resolved Hide resolved
tk.MustExec("set @@session.tidb_enable_table_partition = 1;")
tk.MustExec("use test;")
tk.MustExec("drop table if exists t;")

sql1 := `CREATE TABLE t1(c0 INT) PARTITION BY HASH((NOT c0)) PARTITIONS 2;`
tk.MustGetErrCode(sql1, tmysql.ErrPartitionFunctionIsNotAllowed)

sql2 := `CREATE TABLE t1(c0 INT) PARTITION BY HASH(!c0) PARTITIONS 2;`
tk.MustGetErrCode(sql2, tmysql.ErrPartitionFunctionIsNotAllowed)
}
34 changes: 20 additions & 14 deletions ddl/partition.go
Original file line number Diff line number Diff line change
Expand Up @@ -1672,21 +1672,27 @@ func checkPartitionExprAllowed(_ sessionctx.Context, _ *model.TableInfo, e ast.E
}

func checkPartitionExprFuncAllowed(_ sessionctx.Context, _ *model.TableInfo, e ast.ExprNode) error {
Reminiscent marked this conversation as resolved.
Show resolved Hide resolved
expr, ok := e.(*ast.FuncCallExpr)
if !ok {
return nil
}
allowedFuncMap := map[string]struct{}{
ast.ToDays: {}, ast.ToSeconds: {}, ast.DayOfMonth: {}, ast.Month: {}, ast.DayOfYear: {},
ast.Quarter: {}, ast.YearWeek: {}, ast.Year: {}, ast.Weekday: {}, ast.DayOfWeek: {}, ast.Day: {},
ast.Hour: {}, ast.Minute: {}, ast.Second: {}, ast.TimeToSec: {}, ast.MicroSecond: {},
ast.UnixTimestamp: {}, ast.FromDays: {}, ast.Extract: {}, ast.Abs: {}, ast.Ceiling: {},
ast.DateDiff: {}, ast.Floor: {}, ast.Mod: {},
}
if _, ok := allowedFuncMap[expr.FnName.L]; ok {
return nil
var err error
switch expr := e.(type) {
Reminiscent marked this conversation as resolved.
Show resolved Hide resolved
case *ast.FuncCallExpr:
allowedFuncMap := map[string]struct{}{
ast.ToDays: {}, ast.ToSeconds: {}, ast.DayOfMonth: {}, ast.Month: {}, ast.DayOfYear: {},
ast.Quarter: {}, ast.YearWeek: {}, ast.Year: {}, ast.Weekday: {}, ast.DayOfWeek: {}, ast.Day: {},
ast.Hour: {}, ast.Minute: {}, ast.Second: {}, ast.TimeToSec: {}, ast.MicroSecond: {},
ast.UnixTimestamp: {}, ast.FromDays: {}, ast.Extract: {}, ast.Abs: {}, ast.Ceiling: {},
ast.DateDiff: {}, ast.Floor: {}, ast.Mod: {},
}
if _, ok := allowedFuncMap[expr.FnName.L]; !ok {
err = errors.Trace(ErrPartitionFunctionIsNotAllowed)
}
case *ast.UnaryOperationExpr:
switch expr.Op {
// We can not build partition with `not` expression. But in TiDB, `not` will be regard as a operator.
case opcode.Not, opcode.Not2:
err = errors.Trace(ErrPartitionFunctionIsNotAllowed)
}
}
return errors.Trace(ErrPartitionFunctionIsNotAllowed)
return err
}

func checkPartitionExprArgs(_ sessionctx.Context, tblInfo *model.TableInfo, e ast.ExprNode) error {
Expand Down