From 8f2144a92c38e84cf0cfc3a38a77e4b50f712351 Mon Sep 17 00:00:00 2001 From: Ti Chi Robot Date: Wed, 7 Feb 2024 13:01:15 +0800 Subject: [PATCH] ddl: table meta should store column without db and table name (#50978) (#51020) close pingcap/tidb#50972 --- ddl/ddl_api.go | 2 +- ddl/partition.go | 17 ++++++++++++++--- ddl/partition_test.go | 22 ++++++++++++++++++++++ 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/ddl/ddl_api.go b/ddl/ddl_api.go index 74b31cf5c16de..fb7d0bf01ff1b 100644 --- a/ddl/ddl_api.go +++ b/ddl/ddl_api.go @@ -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 { diff --git a/ddl/partition.go b/ddl/partition.go index 3c503de5012ef..e80c672f8cb7a 100644 --- a/ddl/partition.go +++ b/ddl/partition.go @@ -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 } @@ -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) } diff --git a/ddl/partition_test.go b/ddl/partition_test.go index 6fad26630edd8..122d81244f347 100644 --- a/ddl/partition_test.go +++ b/ddl/partition_test.go @@ -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))")) +}