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: fix change partition key type to a not allowed type #38735

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
20 changes: 20 additions & 0 deletions ddl/db_partition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4539,6 +4539,7 @@ func TestAlterModifyColumnOnPartitionedTable(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("create database AlterPartTable")
defer tk.MustExec("drop database AlterPartTable")
tk.MustExec("use AlterPartTable")
tk.MustExec(`create table t (a int unsigned PRIMARY KEY, b varchar(255), key (b))`)
tk.MustExec(`insert into t values (7, "07"), (8, "08"),(23,"23"),(34,"34💥"),(46,"46"),(57,"57")`)
Expand Down Expand Up @@ -4662,4 +4663,23 @@ func TestAlterModifyColumnOnPartitionedTable(t *testing.T) {
"34 34💥",
"46 46",
"57 57"))
tk.MustExec("create table t1 (a tinyint, b char) partition by range (a) ( partition p0 values less than (10) )")
err := tk.ExecToErr("alter table t1 modify a char")
require.Error(t, err)
require.Equal(t, "[ddl:1659]Field 'a' is of a not allowed type for this type of partitioning", err.Error())

tk.MustExec("create table t2 (a tinyint, b char) partition by range (a-1) ( partition p0 values less than (10) )")
err = tk.ExecToErr("alter table t2 modify a char")
require.Error(t, err)
require.Equal(t, "[ddl:1491]The PARTITION function returns the wrong type", err.Error())

tk.MustExec("create table t3 (a tinyint, b char) partition by hash(a) partitions 4;")
err = tk.ExecToErr("alter table t3 modify a char")
require.Error(t, err)
require.Equal(t, "[ddl:1659]Field 'a' is of a not allowed type for this type of partitioning", err.Error())

tk.MustExec("create table t4 (a tinyint, b char) partition by hash(a-1) partitions 4;")
err = tk.ExecToErr("alter table t4 modify a char")
require.Error(t, err)
require.Equal(t, "[ddl:1491]The PARTITION function returns the wrong type", err.Error())
}
48 changes: 48 additions & 0 deletions ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import (
"github.com/pingcap/tidb/infoschema"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/meta/autoid"
"github.com/pingcap/tidb/parser"
"github.com/pingcap/tidb/parser/ast"
"github.com/pingcap/tidb/parser/charset"
"github.com/pingcap/tidb/parser/format"
Expand Down Expand Up @@ -4614,6 +4615,11 @@ func GetModifiableColumnJob(
return nil, err
}

err = checkColumnWithPartitionConstraint(sctx, t.Meta().Clone(), newCol.ColumnInfo.Clone(), col.ColumnInfo.Clone(), spec.Position)
if err != nil {
return nil, err
}

// As same with MySQL, we don't support modifying the stored status for generated columns.
if err = checkModifyGeneratedColumn(sctx, t, col, newCol, specNewColumn, spec.Position); err != nil {
return nil, errors.Trace(err)
Expand Down Expand Up @@ -4644,6 +4650,48 @@ func GetModifiableColumnJob(
return job, nil
}

// checkColumnWithPartitionConstraint is used to check the partition constraint of the modified column.
func checkColumnWithPartitionConstraint(sctx sessionctx.Context, tbInfo *model.TableInfo, newCol, oldCol *model.ColumnInfo, pos *ast.ColumnPosition) error {
pi := tbInfo.GetPartitionInfo()
if pi == nil || pi.Expr == "" {
return nil
}

err := adjustTableInfoAfterModifyColumn(tbInfo, newCol, oldCol, pos)
if err != nil {
return errors.Trace(err)
}

exprStr := "select " + pi.Expr
var stmts []ast.StmtNode
if p, ok := sctx.(interface {
ParseSQL(context.Context, string, ...parser.ParseParam) ([]ast.StmtNode, []error, error)
}); ok {
stmts, _, err = p.ParseSQL(context.Background(), exprStr)
} else {
stmts, _, err = parser.New().ParseSQL(exprStr)
}

if err != nil {
return errors.Trace(err)
}

expr := stmts[0].(*ast.SelectStmt).Fields.Fields[0].Expr
e, err := expression.RewriteSimpleExprWithTableInfo(sctx, tbInfo, expr)
if err != nil {
return errors.Trace(err)
}

if e.GetType().EvalType() == types.ETInt {
return nil
}
if col, ok := expr.(*ast.ColumnNameExpr); ok {
return errors.Trace(dbterror.ErrNotAllowedTypeInPartition.GenWithStackByArgs(col.Name.Name.L))
}

return errors.Trace(dbterror.ErrPartitionFuncNotAllowed.GenWithStackByArgs("PARTITION"))
}

// checkColumnWithIndexConstraint is used to check the related index constraint of the modified column.
// Index has a max-prefix-length constraint. eg: a varchar(100), index idx(a), modifying column a to a varchar(4000)
// will cause index idx to break the max-prefix-length constraint.
Expand Down