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: Also check column types in ALTER TABLE t PARTITION BY ... #56197

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
27 changes: 27 additions & 0 deletions pkg/ddl/partition.go
Original file line number Diff line number Diff line change
Expand Up @@ -1833,6 +1833,26 @@ func checkPartitionFuncType(ctx sessionctx.Context, expr ast.ExprNode, schema st
return errors.Trace(dbterror.ErrPartitionFuncNotAllowed.GenWithStackByArgs("PARTITION"))
}

func checkPartitionFuncTypeExprString(ctx sessionctx.Context, expr, schema string, tblInfo *model.TableInfo) error {
if len(expr) == 0 {
return nil
}
if schema == "" {
schema = ctx.GetSessionVars().CurrentDB
}
e, err := expression.ParseSimpleExpr(ctx.GetExprCtx(), expr, expression.WithTableInfo(schema, tblInfo))
if err != nil {
return errors.Trace(err)
}
if e.GetType(ctx.GetExprCtx().GetEvalCtx()).EvalType() == types.ETInt {
return nil
}
if col, ok := e.(*expression.Column); ok {
return errors.Trace(dbterror.ErrNotAllowedTypeInPartition.GenWithStackByArgs(col.OrigName))
}
return errors.Trace(dbterror.ErrPartitionFuncNotAllowed.GenWithStackByArgs("PARTITION"))
}

// checkRangePartitionValue checks whether `less than value` is strictly increasing for each partition.
// Side effect: it may simplify the partition range definition from a constant expression to an integer.
func checkRangePartitionValue(ctx sessionctx.Context, tblInfo *model.TableInfo) error {
Expand Down Expand Up @@ -3109,6 +3129,13 @@ func (w *worker) onReorganizePartition(jobCtx *jobContext, t *meta.Meta, job *mo
return ver, err
}

if job.Type == model.ActionAlterTablePartitioning {
// Also verify same things as in CREATE TABLE ... PARTITION BY
if err = checkPartitionFuncTypeExprString(sctx, partInfo.Expr, job.SchemaName, tblInfo); err != nil {
job.State = model.JobStateCancelled
return ver, err
}
}
// move the adding definition into tableInfo.
updateAddingPartitionInfo(partInfo, tblInfo)
orgDefs := tblInfo.Partition.Definitions
Expand Down
11 changes: 11 additions & 0 deletions tests/integrationtest/r/ddl/reorg_partition.result
Original file line number Diff line number Diff line change
Expand Up @@ -830,3 +830,14 @@ a b c
2021-05-04 2021-05-04 10:10:10 4
2022-05-04 2022-05-04 10:10:10 5
2022-05-05 2022-05-06 11:11:11 6
drop table t;
create table t(a varchar(20), b int) partition by hash(a) partitions 2;
Error 1659 (HY000): Field 'a' is of a not allowed type for this type of partitioning
create table t(a varchar(20), b int);
alter table t partition by hash(a) partitions 2;
Error 1659 (HY000): Field 'ddl__reorg_partition.t.a' is of a not allowed type for this type of partitioning
alter table t partition by key() partitions 2;
Error 1105 (HY000): expression should not be an empty string
alter table t partition by key(c) partitions 2;
Error 1488 (HY000): Field in list of fields for partition function not found in table
drop table t;
12 changes: 12 additions & 0 deletions tests/integrationtest/t/ddl/reorg_partition.test
Original file line number Diff line number Diff line change
Expand Up @@ -390,4 +390,16 @@ ADMIN CHECK TABLE t;
SELECT * FROM t PARTITION(pAll);
--sorted_result
SELECT * FROM t;
drop table t;

# Issue 56094
-- error 1659
create table t(a varchar(20), b int) partition by hash(a) partitions 2;
create table t(a varchar(20), b int);
-- error 1659
alter table t partition by hash(a) partitions 2;
-- error 1105
alter table t partition by key() partitions 2;
-- error 1488
alter table t partition by key(c) partitions 2;
drop table t;