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 2 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.ErrWrongExprInPartitionFunc)

sql2 := `CREATE TABLE t1(c0 INT) PARTITION BY HASH(!c0) PARTITIONS 2;`
tk.MustGetErrCode(sql2, tmysql.ErrWrongExprInPartitionFunc)
}
71 changes: 38 additions & 33 deletions ddl/partition.go
Original file line number Diff line number Diff line change
Expand Up @@ -1690,42 +1690,47 @@ func checkPartitionExprFuncAllowed(_ sessionctx.Context, _ *model.TableInfo, e a
}

func checkPartitionExprArgs(_ sessionctx.Context, tblInfo *model.TableInfo, e ast.ExprNode) error {
expr, ok := e.(*ast.FuncCallExpr)
if !ok {
return nil
}
switch expr.FnName.L {
case ast.ToDays, ast.ToSeconds, ast.DayOfMonth, ast.Month, ast.DayOfYear, ast.Quarter, ast.YearWeek,
ast.Year, ast.Weekday, ast.DayOfWeek, ast.Day:
return errors.Trace(checkResultOK(hasDateArgs(tblInfo, expr)))
case ast.Hour, ast.Minute, ast.Second, ast.TimeToSec, ast.MicroSecond:
return errors.Trace(checkResultOK(hasTimeArgs(tblInfo, expr)))
case ast.UnixTimestamp:
return errors.Trace(checkResultOK(hasTimestampArgs(tblInfo, expr)))
case ast.FromDays:
if err := checkResultOK(hasDateArgs(tblInfo, expr)); err != nil {
return errors.Trace(err)
}
return errors.Trace(checkResultOK(hasTimeArgs(tblInfo, expr)))
case ast.Extract:
switch expr.Args[0].(*ast.TimeUnitExpr).Unit {
case ast.TimeUnitYear, ast.TimeUnitYearMonth, ast.TimeUnitQuarter, ast.TimeUnitMonth, ast.TimeUnitDay:
switch expr := e.(type) {
Reminiscent marked this conversation as resolved.
Show resolved Hide resolved
case *ast.FuncCallExpr:
switch expr.FnName.L {
case ast.ToDays, ast.ToSeconds, ast.DayOfMonth, ast.Month, ast.DayOfYear, ast.Quarter, ast.YearWeek,
ast.Year, ast.Weekday, ast.DayOfWeek, ast.Day:
return errors.Trace(checkResultOK(hasDateArgs(tblInfo, expr)))
case ast.TimeUnitDayMicrosecond, ast.TimeUnitDayHour, ast.TimeUnitDayMinute, ast.TimeUnitDaySecond:
return errors.Trace(checkResultOK(hasDatetimeArgs(tblInfo, expr)))
case ast.TimeUnitHour, ast.TimeUnitHourMinute, ast.TimeUnitHourSecond, ast.TimeUnitMinute, ast.TimeUnitMinuteSecond,
ast.TimeUnitSecond, ast.TimeUnitMicrosecond, ast.TimeUnitHourMicrosecond, ast.TimeUnitMinuteMicrosecond, ast.TimeUnitSecondMicrosecond:
case ast.Hour, ast.Minute, ast.Second, ast.TimeToSec, ast.MicroSecond:
return errors.Trace(checkResultOK(hasTimeArgs(tblInfo, expr)))
default:
return errors.Trace(ErrWrongExprInPartitionFunc)
}
case ast.Abs, ast.Ceiling, ast.DateDiff, ast.Floor, ast.Mod:
has, err := hasTimestampArgs(tblInfo, expr)
if err != nil {
return errors.Trace(err)
case ast.UnixTimestamp:
return errors.Trace(checkResultOK(hasTimestampArgs(tblInfo, expr)))
case ast.FromDays:
if err := checkResultOK(hasDateArgs(tblInfo, expr)); err != nil {
return errors.Trace(err)
}
return errors.Trace(checkResultOK(hasTimeArgs(tblInfo, expr)))
case ast.Extract:
switch expr.Args[0].(*ast.TimeUnitExpr).Unit {
case ast.TimeUnitYear, ast.TimeUnitYearMonth, ast.TimeUnitQuarter, ast.TimeUnitMonth, ast.TimeUnitDay:
return errors.Trace(checkResultOK(hasDateArgs(tblInfo, expr)))
case ast.TimeUnitDayMicrosecond, ast.TimeUnitDayHour, ast.TimeUnitDayMinute, ast.TimeUnitDaySecond:
return errors.Trace(checkResultOK(hasDatetimeArgs(tblInfo, expr)))
case ast.TimeUnitHour, ast.TimeUnitHourMinute, ast.TimeUnitHourSecond, ast.TimeUnitMinute, ast.TimeUnitMinuteSecond,
ast.TimeUnitSecond, ast.TimeUnitMicrosecond, ast.TimeUnitHourMicrosecond, ast.TimeUnitMinuteMicrosecond, ast.TimeUnitSecondMicrosecond:
return errors.Trace(checkResultOK(hasTimeArgs(tblInfo, expr)))
default:
return errors.Trace(ErrWrongExprInPartitionFunc)
}
case ast.Abs, ast.Ceiling, ast.DateDiff, ast.Floor, ast.Mod:
has, err := hasTimestampArgs(tblInfo, expr)
if err != nil {
return errors.Trace(err)
}
if has {
return errors.Trace(ErrWrongExprInPartitionFunc)
}
}
if has {
return errors.Trace(ErrWrongExprInPartitionFunc)
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:
return errors.Trace(ErrPartitionFunctionIsNotAllowed)
}
}
return nil
Expand Down