diff --git a/pkg/ddl/constraint.go b/pkg/ddl/constraint.go index f8b28efb1ab6d..85a9dd83ea87b 100644 --- a/pkg/ddl/constraint.go +++ b/pkg/ddl/constraint.go @@ -314,7 +314,7 @@ func buildConstraintInfo(tblInfo *model.TableInfo, dependedCols []model.CIStr, c // Restore check constraint expression to string. var sb strings.Builder restoreFlags := format.RestoreStringSingleQuotes | format.RestoreKeyWordLowercase | format.RestoreNameBackQuotes | - format.RestoreSpacesAroundBinaryOperation + format.RestoreSpacesAroundBinaryOperation | format.RestoreWithoutSchemaName | format.RestoreWithoutTableName restoreCtx := format.NewRestoreCtx(restoreFlags, &sb) sb.Reset() diff --git a/pkg/ddl/ddl_api.go b/pkg/ddl/ddl_api.go index 31c4c8f5d1bfb..8cc286a874730 100644 --- a/pkg/ddl/ddl_api.go +++ b/pkg/ddl/ddl_api.go @@ -2304,7 +2304,7 @@ func checkTableInfoValidWithStmt(ctx sessionctx.Context, tbInfo *model.TableInfo return errors.Trace(err) } if s.Partition != nil { - if err := checkPartitionFuncType(ctx, s.Partition.Expr, tbInfo); err != nil { + if err := checkPartitionFuncType(ctx, s.Partition.Expr, s.Table.Schema, tbInfo); err != nil { return errors.Trace(err) } if err := checkPartitioningKeysConstraints(ctx, s, tbInfo); err != nil { diff --git a/pkg/ddl/partition.go b/pkg/ddl/partition.go index f2446dd713ef5..7c8dbc82e5c7c 100644 --- a/pkg/ddl/partition.go +++ b/pkg/ddl/partition.go @@ -560,7 +560,9 @@ func buildTablePartitionInfo(ctx sessionctx.Context, s *ast.PartitionOptions, tb return errors.Trace(err) } buf := new(bytes.Buffer) - restoreCtx := format.NewRestoreCtx(format.DefaultRestoreFlags|format.RestoreBracketAroundBinaryOperation, buf) + restoreFlags := format.DefaultRestoreFlags | format.RestoreBracketAroundBinaryOperation | + format.RestoreWithoutSchemaName | format.RestoreWithoutTableName + restoreCtx := format.NewRestoreCtx(restoreFlags, buf) if err := s.Expr.Restore(restoreCtx); err != nil { return err } @@ -1578,12 +1580,21 @@ func checkResultOK(ok bool) error { } // checkPartitionFuncType checks partition function return type. -func checkPartitionFuncType(ctx sessionctx.Context, expr ast.ExprNode, tblInfo *model.TableInfo) error { +func checkPartitionFuncType(ctx sessionctx.Context, expr ast.ExprNode, dbName model.CIStr, tblInfo *model.TableInfo) error { if expr == nil { return nil } - e, err := expression.RewriteSimpleExprWithTableInfo(ctx, tblInfo, expr, false) + if dbName.L == "" { + dbName = model.NewCIStr(ctx.GetSessionVars().CurrentDB) + } + + columns, names, err := expression.ColumnInfos2ColumnsAndNames(ctx, dbName, tblInfo.Name, tblInfo.Cols(), tblInfo) + if err != nil { + return err + } + + e, err := expression.RewriteAstExpr(ctx, expr, expression.NewSchema(columns...), names, false) if err != nil { return errors.Trace(err) } diff --git a/tests/integrationtest/r/ddl/constraint.result b/tests/integrationtest/r/ddl/constraint.result index 54840371c81a9..1f676c2dc1f16 100644 --- a/tests/integrationtest/r/ddl/constraint.result +++ b/tests/integrationtest/r/ddl/constraint.result @@ -821,4 +821,22 @@ Table Create Table t CREATE TABLE `t` ( `a` int(11) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +set @@global.tidb_enable_check_constraint = 1; +drop table if exists t; +create table t(a int, check((test.t.a > 1))); +show create table t; +Table Create Table +t CREATE TABLE `t` ( + `a` int(11) DEFAULT NULL, +CONSTRAINT `t_chk_1` CHECK (((`a` > 1))) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +alter table t add constraint chk check((test.t.a < 100)); +show create table t; +Table Create Table +t CREATE TABLE `t` ( + `a` int(11) DEFAULT NULL, +CONSTRAINT `t_chk_1` CHECK (((`a` > 1))), +CONSTRAINT `chk` CHECK (((`a` < 100))) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +drop table if exists t; set @@global.tidb_enable_check_constraint = 0; diff --git a/tests/integrationtest/r/ddl/partition.result b/tests/integrationtest/r/ddl/partition.result index d037541f96ba1..7a9f98a95db62 100644 --- a/tests/integrationtest/r/ddl/partition.result +++ b/tests/integrationtest/r/ddl/partition.result @@ -177,3 +177,55 @@ t CREATE TABLE `t` ( KEY `b` (`b`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin PARTITION BY KEY (`a`) PARTITIONS 5 +drop table if exists test.issue50972_1, test.issue50972_2, test.issue50972_3; +CREATE TABLE test.issue50972_1 (id1 int) PARTITION BY HASH( test.issue50972_1.id1 ) PARTITIONS 4; +CREATE TABLE test.issue50972_2 (id2 int) PARTITION BY RANGE (test.issue50972_2.id2) ( PARTITION p0 VALUES LESS THAN (6)); +CREATE TABLE test.issue50972_3 (id3 int) PARTITION BY LIST (test.issue50972_3.id3) ( PARTITION p0 VALUES IN (1, 2) ); +show create table test.issue50972_1; +Table Create Table +issue50972_1 CREATE TABLE `issue50972_1` ( + `id1` int(11) DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY HASH (`id1`) PARTITIONS 4 +show create table test.issue50972_2; +Table Create Table +issue50972_2 CREATE TABLE `issue50972_2` ( + `id2` int(11) DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY RANGE (`id2`) +(PARTITION `p0` VALUES LESS THAN (6)) +show create table test.issue50972_3; +Table Create Table +issue50972_3 CREATE TABLE `issue50972_3` ( + `id3` int(11) DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY LIST (`id3`) +(PARTITION `p0` VALUES IN (1,2)) +drop table if exists test.issue50972_1, test.issue50972_2, test.issue50972_3; +CREATE TABLE test.issue50972_1 (id1 int); +CREATE TABLE test.issue50972_2 (id2 int); +CREATE TABLE test.issue50972_3 (id3 int); +ALTER TABLE test.issue50972_1 PARTITION BY HASH( test.issue50972_1.id1 ) PARTITIONS 4; +ALTER TABLE test.issue50972_2 PARTITION BY RANGE (test.issue50972_2.id2) ( PARTITION p0 VALUES LESS THAN (6)); +ALTER TABLE test.issue50972_3 PARTITION BY LIST (test.issue50972_3.id3) ( PARTITION p0 VALUES IN (1, 2) ); +show create table test.issue50972_1; +Table Create Table +issue50972_1 CREATE TABLE `issue50972_1` ( + `id1` int(11) DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY HASH (`id1`) PARTITIONS 4 +show create table test.issue50972_2; +Table Create Table +issue50972_2 CREATE TABLE `issue50972_2` ( + `id2` int(11) DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY RANGE (`id2`) +(PARTITION `p0` VALUES LESS THAN (6)) +show create table test.issue50972_3; +Table Create Table +issue50972_3 CREATE TABLE `issue50972_3` ( + `id3` int(11) DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY LIST (`id3`) +(PARTITION `p0` VALUES IN (1,2)) +drop table if exists test.issue50972_1, test.issue50972_2, test.issue50972_3; diff --git a/tests/integrationtest/t/ddl/constraint.test b/tests/integrationtest/t/ddl/constraint.test index 4becc4152e911..6b9c60f0d7996 100644 --- a/tests/integrationtest/t/ddl/constraint.test +++ b/tests/integrationtest/t/ddl/constraint.test @@ -676,4 +676,13 @@ show create table t; alter table t drop CONSTRAINT chk; show create table t; +# Related issue TiDB#50972, constraint expression should ignore schema and table name when restore +set @@global.tidb_enable_check_constraint = 1; +drop table if exists t; +create table t(a int, check((test.t.a > 1))); +show create table t; +alter table t add constraint chk check((test.t.a < 100)); +show create table t; +drop table if exists t; + set @@global.tidb_enable_check_constraint = 0; diff --git a/tests/integrationtest/t/ddl/partition.test b/tests/integrationtest/t/ddl/partition.test index 4622da78575ad..7e1185635c1e7 100644 --- a/tests/integrationtest/t/ddl/partition.test +++ b/tests/integrationtest/t/ddl/partition.test @@ -135,3 +135,23 @@ show create table t; alter table t partition by key(a) partitions 5; show create table t; +# Related issue TiDB#50972, partition expression should ignore schema and table name when restore +drop table if exists test.issue50972_1, test.issue50972_2, test.issue50972_3; +CREATE TABLE test.issue50972_1 (id1 int) PARTITION BY HASH( test.issue50972_1.id1 ) PARTITIONS 4; +CREATE TABLE test.issue50972_2 (id2 int) PARTITION BY RANGE (test.issue50972_2.id2) ( PARTITION p0 VALUES LESS THAN (6)); +CREATE TABLE test.issue50972_3 (id3 int) PARTITION BY LIST (test.issue50972_3.id3) ( PARTITION p0 VALUES IN (1, 2) ); +show create table test.issue50972_1; +show create table test.issue50972_2; +show create table test.issue50972_3; +drop table if exists test.issue50972_1, test.issue50972_2, test.issue50972_3; +CREATE TABLE test.issue50972_1 (id1 int); +CREATE TABLE test.issue50972_2 (id2 int); +CREATE TABLE test.issue50972_3 (id3 int); +ALTER TABLE test.issue50972_1 PARTITION BY HASH( test.issue50972_1.id1 ) PARTITIONS 4; +ALTER TABLE test.issue50972_2 PARTITION BY RANGE (test.issue50972_2.id2) ( PARTITION p0 VALUES LESS THAN (6)); +ALTER TABLE test.issue50972_3 PARTITION BY LIST (test.issue50972_3.id3) ( PARTITION p0 VALUES IN (1, 2) ); +show create table test.issue50972_1; +show create table test.issue50972_2; +show create table test.issue50972_3; +drop table if exists test.issue50972_1, test.issue50972_2, test.issue50972_3; +