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: add partition should not take table information (#19671) #19891

Merged
merged 3 commits into from
Sep 9, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
7 changes: 4 additions & 3 deletions ddl/db_partition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/pingcap/errors"
"github.com/pingcap/parser/ast"
"github.com/pingcap/parser/model"
"github.com/pingcap/parser/mysql"
"github.com/pingcap/parser/terror"
"github.com/pingcap/tidb/ddl"
"github.com/pingcap/tidb/ddl/testutil"
Expand Down Expand Up @@ -152,15 +153,15 @@ func (s *testIntegrationSuite3) TestCreateTableWithPartition(c *C) {
);`
tk.MustGetErrCode(sql7, tmysql.ErrPartitionMaxvalue)

_, err = tk.Exec(`create table t8 (
sql18 := `create table t8 (
a int not null,
b int not null
)
partition by range( a ) (
partition p1 values less than (19xx91),
partition p2 values less than maxvalue
);`)
c.Assert(ddl.ErrNotAllowedTypeInPartition.Equal(err), IsTrue)
);`
tk.MustGetErrCode(sql18, mysql.ErrBadField)

sql9 := `create TABLE t9 (
col1 int
Expand Down
28 changes: 16 additions & 12 deletions ddl/partition.go
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,7 @@ func checkCreatePartitionValue(ctx sessionctx.Context, tblInfo *model.TableInfo)
return errors.Trace(ErrPartitionMaxvalue)
}

currentRangeValue, fromExpr, err := getRangeValue(ctx, tblInfo, defs[i].LessThan[0], isUnsignedBigint)
currentRangeValue, fromExpr, err := getRangeValue(ctx, defs[i].LessThan[0], isUnsignedBigint)
if err != nil {
return errors.Trace(err)
}
Expand Down Expand Up @@ -745,18 +745,20 @@ func checkCreatePartitionValue(ctx sessionctx.Context, tblInfo *model.TableInfo)

// getRangeValue gets an integer from the range value string.
// The returned boolean value indicates whether the input string is a constant expression.
func getRangeValue(ctx sessionctx.Context, tblInfo *model.TableInfo, str string, unsignedBigint bool) (interface{}, bool, error) {
func getRangeValue(ctx sessionctx.Context, str string, unsignedBigint bool) (interface{}, bool, error) {
// Unsigned bigint was converted to uint64 handle.
if unsignedBigint {
if value, err := strconv.ParseUint(str, 10, 64); err == nil {
return value, false, nil
}

if e, err1 := expression.ParseSimpleExprWithTableInfo(ctx, str, tblInfo); err1 == nil {
res, isNull, err2 := e.EvalInt(ctx, chunk.Row{})
if err2 == nil && !isNull {
return uint64(res), true, nil
}
e, err1 := expression.ParseSimpleExprWithTableInfo(ctx, str, &model.TableInfo{})
if err1 != nil {
return 0, false, err1
}
res, isNull, err2 := e.EvalInt(ctx, chunk.Row{})
if err2 == nil && !isNull {
return uint64(res), true, nil
}
} else {
if value, err := strconv.ParseInt(str, 10, 64); err == nil {
Expand All @@ -766,11 +768,13 @@ func getRangeValue(ctx sessionctx.Context, tblInfo *model.TableInfo, str string,
// For example, the following two cases are the same:
// PARTITION p0 VALUES LESS THAN (TO_SECONDS('2004-01-01'))
// PARTITION p0 VALUES LESS THAN (63340531200)
if e, err1 := expression.ParseSimpleExprWithTableInfo(ctx, str, tblInfo); err1 == nil {
res, isNull, err2 := e.EvalInt(ctx, chunk.Row{})
if err2 == nil && !isNull {
return res, true, nil
}
e, err1 := expression.ParseSimpleExprWithTableInfo(ctx, str, &model.TableInfo{})
if err1 != nil {
return 0, false, err1
}
res, isNull, err2 := e.EvalInt(ctx, chunk.Row{})
if err2 == nil && !isNull {
return res, true, nil
}
}
return 0, false, ErrNotAllowedTypeInPartition.GenWithStackByArgs(str)
Expand Down
11 changes: 11 additions & 0 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7005,3 +7005,14 @@ func (s *testIntegrationSerialSuite) TestIssue17233(c *C) {
tk.MustQuery("SELECT col_1 FROM test.view_4").Sort().Check(testkit.Rows("8", "8", "8", "8", "8"))
tk.MustQuery("SELECT view_10.col_1 FROM view_4 JOIN view_10").Check(testkit.Rows("16", "16", "16", "16", "16", "18", "18", "18", "18", "18", "19", "19", "19", "19", "19"))
}

func (s *testIntegrationSuite) TestIssue19596(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a int) partition by range(a) (PARTITION p0 VALUES LESS THAN (10));")
tk.MustGetErrMsg("alter table t add partition (partition p1 values less than (a));", "[expression:1054]Unknown column 'a' in 'expression'")
tk.MustQuery("select * from t;")
tk.MustExec("drop table if exists t;")
tk.MustGetErrMsg("create table t (a int) partition by range(a) (PARTITION p0 VALUES LESS THAN (a));", "[expression:1054]Unknown column 'a' in 'expression'")
}