Skip to content

Commit

Permalink
ddl: table meta should store column without db and table name (#50978) (
Browse files Browse the repository at this point in the history
#51020)

close #50972
  • Loading branch information
ti-chi-bot authored Feb 7, 2024
1 parent 25f0109 commit 8f2144a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
2 changes: 1 addition & 1 deletion ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2118,7 +2118,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 ddl/partition.go
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,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 @@ -1502,12 +1504,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
22 changes: 22 additions & 0 deletions ddl/partition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,3 +242,25 @@ func TestReorganizePartitionRollback(t *testing.T) {
// test then add index should success
tk.MustExec("alter table t1 add index idx_kc (k, c)")
}

func TestPartitionExprContainsSchemaName(t *testing.T) {
store, _ := testkit.CreateMockStoreAndDomain(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("CREATE TABLE test.t1 (id1 int) PARTITION BY HASH( test.t1.id1 ) PARTITIONS 4;")
tk.MustExec("CREATE TABLE test.t2 (id2 int) PARTITION BY RANGE (test.t2.id2) (PARTITION p0 VALUES LESS THAN (6))")
tk.MustExec("CREATE TABLE test.t3 (id3 int) PARTITION BY LIST (test.t3.id3) (PARTITION p0 VALUES IN (1, 2))")
tk.MustQuery("show create table test.t1").Check(testkit.Rows("t1 CREATE TABLE `t1` (\n" +
" `id1` int(11) DEFAULT NULL\n" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\n" +
"PARTITION BY HASH (`id1`) PARTITIONS 4"))
tk.MustQuery("show create table test.t2").Check(testkit.Rows("t2 CREATE TABLE `t2` (\n" +
" `id2` int(11) DEFAULT NULL\n" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\n" +
"PARTITION BY RANGE (`id2`)\n" +
"(PARTITION `p0` VALUES LESS THAN (6))"))
tk.MustQuery("show create table test.t3").Check(testkit.Rows("t3 CREATE TABLE `t3` (\n" +
" `id3` int(11) DEFAULT NULL\n" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\n" +
"PARTITION BY LIST (`id3`)\n" +
"(PARTITION `p0` VALUES IN (1,2))"))
}

0 comments on commit 8f2144a

Please sign in to comment.