Skip to content

Commit

Permalink
ddl: check partition definition correctly in list partition (pingcap#…
Browse files Browse the repository at this point in the history
  • Loading branch information
xiongjiwei authored Dec 8, 2020
1 parent 1b12071 commit a3a2481
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 118 deletions.
42 changes: 35 additions & 7 deletions ddl/db_partition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -552,11 +552,11 @@ func (s *testIntegrationSuite1) TestCreateTableWithListPartition(c *C) {
},
{
"create table t (id timestamp) partition by list (id) (partition p0 values in ('2019-01-09 11:23:34'));",
ddl.ErrNotAllowedTypeInPartition,
ddl.ErrValuesIsNotIntType,
},
{
"create table t (id decimal) partition by list (id) (partition p0 values in ('2019-01-09 11:23:34'));",
ddl.ErrNotAllowedTypeInPartition,
ddl.ErrValuesIsNotIntType,
},
{
"create table t (id float) partition by list (id) (partition p0 values in (1));",
Expand All @@ -568,19 +568,19 @@ func (s *testIntegrationSuite1) TestCreateTableWithListPartition(c *C) {
},
{
"create table t (id text) partition by list (id) (partition p0 values in ('abc'));",
ddl.ErrNotAllowedTypeInPartition,
ddl.ErrValuesIsNotIntType,
},
{
"create table t (id blob) partition by list (id) (partition p0 values in ('abc'));",
ddl.ErrNotAllowedTypeInPartition,
ddl.ErrValuesIsNotIntType,
},
{
"create table t (id enum('a','b')) partition by list (id) (partition p0 values in ('a'));",
ddl.ErrNotAllowedTypeInPartition,
ddl.ErrValuesIsNotIntType,
},
{
"create table t (id set('a','b')) partition by list (id) (partition p0 values in ('a'));",
ddl.ErrNotAllowedTypeInPartition,
ddl.ErrValuesIsNotIntType,
},
{
"create table t (a int) partition by list (a) (partition p0 values in (1), partition p0 values in (2));",
Expand Down Expand Up @@ -798,6 +798,10 @@ func (s *testIntegrationSuite1) TestCreateTableWithListColumnsPartition(c *C) {
);`,
ddl.ErrUniqueKeyNeedAllFieldsInPf,
},
{
"create table t (a date) partition by list columns (a) (partition p0 values in ('2020-02-02'), partition p1 values in ('20200202'));",
ddl.ErrMultipleDefConstInListPart,
},
{
"create table t (a int, b varchar(10)) partition by list columns (a,b) (partition p0 values in (1));",
ast.ErrPartitionColumnList,
Expand Down Expand Up @@ -846,6 +850,7 @@ func (s *testIntegrationSuite1) TestCreateTableWithListColumnsPartition(c *C) {
"partition by list columns (c1,c2,c3,c4,c5,c6,c7,c8) (" +
"partition p0 values in ((1,2,3,4,'2020-11-30 00:00:01', '2020-11-30','abc','a')));",
"create table t (a int, b int generated always as (a+1) virtual) partition by list columns (b) (partition p0 values in (1));",
"create table t(a int,b char(10)) partition by list columns (a, b) (partition p1 values in ((2, 'a'), (1, 'b')), partition p2 values in ((2, 'b')));",
}

for _, sql := range validCases {
Expand Down Expand Up @@ -910,7 +915,7 @@ func (s *testIntegrationSuite5) TestAlterTableAddPartitionByList(c *C) {
ddl.ErrMultipleDefConstInListPart,
},
{"alter table t add partition (partition p6 values in ('a'))",
ddl.ErrNotAllowedTypeInPartition,
ddl.ErrValuesIsNotIntType,
},
}

Expand Down Expand Up @@ -3156,3 +3161,26 @@ func (s *testIntegrationSuite7) TestAddPartitionForTableWithWrongType(c *C) {
c.Assert(err, NotNil)
c.Assert(ddl.ErrWrongTypeColumnValue.Equal(err), IsTrue)
}

func (s *testIntegrationSuite7) TestPartitionListWithTimeType(c *C) {
tk := testkit.NewTestKitWithInit(c, s.store)
tk.MustExec("use test;")
tk.MustExec("create table t_list1(a date) partition by list columns (a) (partition p0 values in ('2010-02-02', '20180203'), partition p1 values in ('20200202'));")
tk.MustExec("insert into t_list1(a) values (20180203);")
tk.MustQuery(`select * from t_list1 partition (p0);`).Check(testkit.Rows("2018-02-03"))
}

func (s *testIntegrationSuite7) TestPartitionListWithNewCollation(c *C) {
collate.SetNewCollationEnabledForTest(true)
defer collate.SetNewCollationEnabledForTest(false)
tk := testkit.NewTestKitWithInit(c, s.store)
tk.MustExec("use test;")
tk.MustGetErrCode(`create table t (a char(10) collate utf8mb4_general_ci) partition by list columns (a) (partition p0 values in ('a', 'A'));`, mysql.ErrMultipleDefConstInListPart)
tk.MustExec("create table t11(a char(10) collate utf8mb4_general_ci) partition by list columns (a) (partition p0 values in ('a', 'b'), partition p1 values in ('C', 'D'));")
tk.MustExec("insert into t11(a) values ('A'), ('c'), ('C'), ('d'), ('B');")
tk.MustQuery(`select * from t11 order by a;`).Check(testkit.Rows("A", "B", "c", "C", "d"))
tk.MustQuery(`select * from t11 partition (p0);`).Check(testkit.Rows("A", "B"))
tk.MustQuery(`select * from t11 partition (p1);`).Check(testkit.Rows("c", "C", "d"))
str := tk.MustQuery(`desc select * from t11 where a = 'b';`).Rows()[0][3].(string)
c.Assert(strings.Contains(str, "partition:p0"), IsTrue)
}
53 changes: 3 additions & 50 deletions ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -5197,19 +5197,12 @@ func checkColumnsTypeAndValuesMatch(ctx sessionctx.Context, meta *model.TableInf
// create table ... partition by range columns (cols)
// partition p0 values less than (expr)
// check the type of cols[i] and expr is consistent.
colNames := meta.Partition.Columns
colTypes := collectColumnsType(meta)
for i, colExpr := range exprs {
if _, ok := colExpr.(*ast.MaxValueExpr); ok {
continue
}

colName := colNames[i]
colInfo := getColumnInfoByName(meta, colName.L)
if colInfo == nil {
return errors.Trace(ErrFieldNotFoundPart)
}
colType := &colInfo.FieldType

colType := colTypes[i]
val, err := expression.EvalAstExpr(ctx, colExpr)
if err != nil {
return err
Expand Down Expand Up @@ -5243,54 +5236,14 @@ func checkColumnsTypeAndValuesMatch(ctx sessionctx.Context, meta *model.TableInf
return ErrWrongTypeColumnValue.GenWithStackByArgs()
}
}
_, err = val.ConvertTo(ctx.GetSessionVars().StmtCtx, colType)
_, err = val.ConvertTo(ctx.GetSessionVars().StmtCtx, &colType)
if err != nil {
return ErrWrongTypeColumnValue.GenWithStackByArgs()
}
}
return nil
}

func formatListPartitionValue(ctx sessionctx.Context, tblInfo *model.TableInfo) error {
defs := tblInfo.Partition.Definitions
pi := tblInfo.Partition
var colTps []*types.FieldType
if len(pi.Columns) == 0 {
tp := types.NewFieldType(mysql.TypeLonglong)
if isRangePartitionColUnsignedBigint(tblInfo.Columns, tblInfo.Partition) {
tp.Flag |= mysql.UnsignedFlag
}
colTps = []*types.FieldType{tp}
} else {
colTps = make([]*types.FieldType, 0, len(pi.Columns))
for _, colName := range pi.Columns {
colInfo := findColumnByName(colName.L, tblInfo)
if colInfo == nil {
return errors.Trace(ErrFieldNotFoundPart)
}
colTps = append(colTps, &colInfo.FieldType)
}
}
for i := range defs {
for j, vs := range defs[i].InValues {
for k, v := range vs {
if colTps[k].EvalType() != types.ETInt {
continue
}
isUnsigned := mysql.HasUnsignedFlag(colTps[k].Flag)
currentRangeValue, isNull, err := getListPartitionValue(ctx, v, isUnsigned)
if err != nil {
return errors.Trace(err)
}
if !isNull {
defs[i].InValues[j][k] = fmt.Sprintf("%d", currentRangeValue)
}
}
}
}
return nil
}

// LockTables uses to execute lock tables statement.
func (d *ddl) LockTables(ctx sessionctx.Context, stmt *ast.LockTablesStmt) error {
lockTables := make([]model.TableLockTpInfo, 0, len(stmt.TableLocks))
Expand Down
2 changes: 2 additions & 0 deletions ddl/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ var (
ErrFieldNotFoundPart = dbterror.ClassDDL.NewStd(mysql.ErrFieldNotFoundPart)
// ErrWrongTypeColumnValue returns 'Partition column values of incorrect type'
ErrWrongTypeColumnValue = dbterror.ClassDDL.NewStd(mysql.ErrWrongTypeColumnValue)
// ErrValuesIsNotIntType returns 'VALUES value for partition '%-.64s' must have type INT'
ErrValuesIsNotIntType = dbterror.ClassDDL.NewStd(mysql.ErrValuesIsNotIntType)
// ErrFunctionalIndexPrimaryKey returns 'The primary key cannot be a functional index'
ErrFunctionalIndexPrimaryKey = dbterror.ClassDDL.NewStd(mysql.ErrFunctionalIndexPrimaryKey)
// ErrFunctionalIndexOnField returns 'Functional index on a column is not supported. Consider using a regular index instead'
Expand Down
Loading

0 comments on commit a3a2481

Please sign in to comment.