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: table meta should store column without db and table name (#50978) #51018

Merged
Merged
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
2 changes: 1 addition & 1 deletion pkg/ddl/constraint.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,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()
Expand Down
2 changes: 1 addition & 1 deletion pkg/ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2285,7 +2285,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 {
Expand Down
17 changes: 14 additions & 3 deletions pkg/ddl/partition.go
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,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
}
Expand Down Expand Up @@ -1575,12 +1577,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)
}
Expand Down
18 changes: 18 additions & 0 deletions tests/integrationtest/r/ddl/constraint.result
Original file line number Diff line number Diff line change
Expand Up @@ -819,4 +819,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;
52 changes: 52 additions & 0 deletions tests/integrationtest/r/ddl/partition.result
Original file line number Diff line number Diff line change
Expand Up @@ -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;
9 changes: 9 additions & 0 deletions tests/integrationtest/t/ddl/constraint.test
Original file line number Diff line number Diff line change
Expand Up @@ -677,4 +677,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;
20 changes: 20 additions & 0 deletions tests/integrationtest/t/ddl/partition.test
Original file line number Diff line number Diff line change
Expand Up @@ -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;