Skip to content

Commit

Permalink
Merge branch 'master' into concurrent_create
Browse files Browse the repository at this point in the history
  • Loading branch information
qw4990 authored Dec 1, 2020
2 parents 92c747c + fb80822 commit e99e6b5
Show file tree
Hide file tree
Showing 47 changed files with 1,290 additions and 469 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ tools/bin/failpoint-ctl: go.mod
$(GO) build -o $@ github.com/pingcap/failpoint/failpoint-ctl

tools/bin/errdoc-gen: go.mod
$(GO) build -o $@ github.com/pingcap/tiup/components/errdoc/errdoc-gen
$(GO) build -o $@ github.com/pingcap/errors/errdoc-gen

tools/bin/golangci-lint:
curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh| sh -s -- -b ./tools/bin v1.29.0
Expand Down
17 changes: 16 additions & 1 deletion bindinfo/bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,7 @@ func (s *testSuite) TestDMLSQLBind(c *C) {
tk := testkit.NewTestKit(c, s.store)
s.cleanBindingEnv(tk)
tk.MustExec("use test")
tk.MustExec("drop table if exists t")
tk.MustExec("drop table if exists t1, t2")
tk.MustExec("create table t1(a int, b int, c int, key idx_b(b), key idx_c(c))")
tk.MustExec("create table t2(a int, b int, c int, key idx_b(b), key idx_c(c))")

Expand Down Expand Up @@ -1717,3 +1717,18 @@ func (s *testSuite) TestReCreateBind(c *C) {
c.Assert(rows[0][0], Equals, "select * from t")
c.Assert(rows[0][3], Equals, "using")
}

func (s *testSuite) TestDMLIndexHintBind(c *C) {
tk := testkit.NewTestKit(c, s.store)
s.cleanBindingEnv(tk)
tk.MustExec("use test")
tk.MustExec("create table t(a int, b int, c int, key idx_b(b), key idx_c(c))")

tk.MustExec("delete from t where b = 1 and c > 1")
c.Assert(tk.Se.GetSessionVars().StmtCtx.IndexNames[0], Equals, "t:idx_b")
c.Assert(tk.MustUseIndex("delete from t where b = 1 and c > 1", "idx_b(b)"), IsTrue)
tk.MustExec("create global binding for delete from t where b = 1 and c > 1 using delete from t use index(idx_c) where b = 1 and c > 1")
tk.MustExec("delete from t where b = 1 and c > 1")
c.Assert(tk.Se.GetSessionVars().StmtCtx.IndexNames[0], Equals, "t:idx_c")
c.Assert(tk.MustUseIndex("delete from t where b = 1 and c > 1", "idx_c(c)"), IsTrue)
}
15 changes: 14 additions & 1 deletion ddl/db_partition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,10 @@ create table log_message_1 (
"create table t (a int) partition by range columns (b) (partition p0 values less than (1));",
ddl.ErrFieldNotFoundPart,
},
{
"create table t (a date) partition by range (to_days(to_days(a))) (partition p0 values less than (1));",
ddl.ErrWrongExprInPartitionFunc,
},
{
"create table t (id timestamp) partition by range columns (id) (partition p0 values less than ('2019-01-09 11:23:34'));",
ddl.ErrNotAllowedTypeInPartition,
Expand Down Expand Up @@ -562,6 +566,14 @@ func (s *testIntegrationSuite1) TestCreateTableWithListPartition(c *C) {
"create table t (a int) partition by list (a) (partition p0 values in (1), partition P0 values in (2));",
ddl.ErrSameNamePartition,
},
{
"create table t(b char(10)) partition by range columns (b) (partition p1 values less than ('G' collate utf8mb4_unicode_ci));",
ddl.ErrPartitionFunctionIsNotAllowed,
},
{
"create table t (a date) partition by list (to_days(to_days(a))) (partition p0 values in (1), partition P1 values in (2));",
ddl.ErrWrongExprInPartitionFunc,
},
{
"create table t (a int) partition by list (a) (partition p0 values in (1), partition p1 values in (1));",
ddl.ErrMultipleDefConstInListPart,
Expand Down Expand Up @@ -2183,6 +2195,7 @@ func (s *testIntegrationSuite5) TestPartitionUniqueKeyNeedAllFieldsInPf(c *C) {
)`
tk.MustGetErrCode(sql10, tmysql.ErrUniqueKeyNeedAllFieldsInPf)

// after we support multiple columns partition, this sql should fail. For now, it will be a normal table.
sql11 := `create table part9 (
a int not null,
b int not null,
Expand All @@ -2197,7 +2210,7 @@ func (s *testIntegrationSuite5) TestPartitionUniqueKeyNeedAllFieldsInPf(c *C) {
partition p1 values less than (7, 9),
partition p2 values less than (11, 22)
)`
tk.MustGetErrCode(sql11, tmysql.ErrUniqueKeyNeedAllFieldsInPf)
tk.MustExec(sql11)

sql12 := `create table part12 (a varchar(20), b binary, unique index (a(5))) partition by range columns (a) (
partition p0 values less than ('aaaaa'),
Expand Down
8 changes: 8 additions & 0 deletions ddl/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6471,3 +6471,11 @@ func (s *testDBSuite4) TestGeneratedColumnWindowFunction(c *C) {
tk.MustGetErrCode("CREATE TABLE t (a INT , b INT as (ROW_NUMBER() OVER (ORDER BY a)))", errno.ErrWindowInvalidWindowFuncUse)
tk.MustGetErrCode("CREATE TABLE t (a INT , index idx ((ROW_NUMBER() OVER (ORDER BY a))))", errno.ErrWindowInvalidWindowFuncUse)
}

func (s *testDBSuite4) TestUnsupportedAlterTableOption(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test_db")
tk.MustExec("DROP TABLE IF EXISTS t")
tk.MustExec("create table t(a char(10) not null,b char(20)) shard_row_id_bits=6;")
tk.MustGetErrCode("alter table t pre_split_regions=6;", errno.ErrUnsupportedDDLOperation)
}
83 changes: 28 additions & 55 deletions ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1522,37 +1522,40 @@ func checkTableInfoValidWithStmt(ctx sessionctx.Context, tbInfo *model.TableInfo
if err := checkGeneratedColumn(s.Cols); err != nil {
return errors.Trace(err)
}
if s.Partition != nil {
if err := checkPartitionFuncValid(ctx, tbInfo, s.Partition.Expr); err != nil {
if tbInfo.Partition != nil && s.Partition != nil {
if err := checkPartitionDefinitionConstraints(ctx, tbInfo); err != nil {
return errors.Trace(err)
}

var err error
pi := tbInfo.Partition
if pi != nil {
switch pi.Type {
case model.PartitionTypeRange:
err = checkPartitionByRange(ctx, tbInfo)
case model.PartitionTypeHash:
err = checkPartitionByHash(ctx, tbInfo)
case model.PartitionTypeList:
err = checkPartitionByList(ctx, tbInfo)
}
if err != nil {
return errors.Trace(err)
}
}

if err := checkPartitionFuncType(ctx, s, tbInfo); err != nil {
if err := checkPartitionFuncType(ctx, s.Partition.Expr, tbInfo); err != nil {
return errors.Trace(err)
}
if err = checkPartitioningKeysConstraints(ctx, s, tbInfo); err != nil {
if err := checkPartitioningKeysConstraints(ctx, s, tbInfo); err != nil {
return errors.Trace(err)
}
}
return nil
}

func checkPartitionDefinitionConstraints(ctx sessionctx.Context, tbInfo *model.TableInfo) error {
var err error
if err = checkPartitionNameUnique(tbInfo.Partition); err != nil {
return errors.Trace(err)
}
if err = checkAddPartitionTooManyPartitions(uint64(len(tbInfo.Partition.Definitions))); err != nil {
return errors.Trace(err)
}

switch tbInfo.Partition.Type {
case model.PartitionTypeRange:
err = checkPartitionByRange(ctx, tbInfo)
case model.PartitionTypeHash:
err = checkPartitionByHash(ctx, tbInfo)
case model.PartitionTypeList:
err = checkPartitionByList(ctx, tbInfo)
}
return errors.Trace(err)
}

// checkTableInfoValid uses to check table info valid. This is used to validate table info.
func checkTableInfoValid(tblInfo *model.TableInfo) error {
_, err := tables.TableFromMeta(nil, tblInfo)
Expand Down Expand Up @@ -1944,15 +1947,7 @@ func buildViewInfo(ctx sessionctx.Context, s *ast.CreateViewStmt) (*model.ViewIn
}

func checkPartitionByHash(ctx sessionctx.Context, tbInfo *model.TableInfo) error {
pi := tbInfo.Partition
if err := checkPartitionNameUnique(pi); err != nil {
return err
}
if err := checkAddPartitionTooManyPartitions(pi.Num); err != nil {
return err
}

return checkNoHashPartitions(ctx, pi.Num)
return checkNoHashPartitions(ctx, tbInfo.Partition.Num)
}

// checkPartitionByRange checks validity of a "BY RANGE" partition.
Expand All @@ -1961,17 +1956,6 @@ func checkPartitionByRange(ctx sessionctx.Context, tbInfo *model.TableInfo) erro
panic("Out Of Memory Quota!")
})
pi := tbInfo.Partition
if err := checkPartitionNameUnique(pi); err != nil {
return err
}

if err := checkAddPartitionTooManyPartitions(uint64(len(pi.Definitions))); err != nil {
return err
}

if err := checkNoRangePartitions(len(pi.Definitions)); err != nil {
return err
}

if len(pi.Columns) == 0 {
return checkRangePartitionValue(ctx, tbInfo)
Expand All @@ -1988,13 +1972,6 @@ func checkPartitionByRange(ctx sessionctx.Context, tbInfo *model.TableInfo) erro
// checkPartitionByList checks validity of a "BY LIST" partition.
func checkPartitionByList(ctx sessionctx.Context, tbInfo *model.TableInfo) error {
pi := tbInfo.Partition
if err := checkPartitionNameUnique(pi); err != nil {
return err
}

if err := checkAddPartitionTooManyPartitions(uint64(len(pi.Definitions))); err != nil {
return err
}

if err := checkListPartitionValue(ctx, tbInfo); err != nil {
return err
Expand Down Expand Up @@ -2479,6 +2456,8 @@ func (d *ddl) AlterTable(ctx sessionctx.Context, ident ast.Ident, specs []*ast.A
needsOverwriteCols := needToOverwriteColCharset(spec.Options)
err = d.AlterTableCharsetAndCollate(ctx, ident, toCharset, toCollate, needsOverwriteCols)
handledCharsetOrCollate = true
default:
err = errUnsupportedAlterTableOption
}

if err != nil {
Expand Down Expand Up @@ -2870,13 +2849,7 @@ func (d *ddl) AddTablePartitions(ctx sessionctx.Context, ident ast.Ident, spec *
tmp := *partInfo
tmp.Definitions = append(pi.Definitions, tmp.Definitions...)
clonedMeta.Partition = &tmp
switch pi.Type {
case model.PartitionTypeRange:
err = checkPartitionByRange(ctx, clonedMeta)
case model.PartitionTypeList:
err = checkPartitionByList(ctx, clonedMeta)
}
if err != nil {
if err := checkPartitionDefinitionConstraints(ctx, clonedMeta); err != nil {
if ErrSameNamePartition.Equal(err) && spec.IfNotExists {
ctx.GetSessionVars().StmtCtx.AppendNote(err)
return nil
Expand Down
2 changes: 1 addition & 1 deletion ddl/ddl_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func (w *worker) start(d *ddlCtx) {

err := w.handleDDLJobQueue(d)
if err != nil {
logutil.Logger(w.logCtx).Error("[ddl] handle DDL job failed", zap.Error(err))
logutil.Logger(w.logCtx).Warn("[ddl] handle DDL job failed", zap.Error(err))
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion ddl/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ var (
errUnsupportedShardRowIDBits = dbterror.ClassDDL.NewStdErr(mysql.ErrUnsupportedDDLOperation, parser_mysql.Message(fmt.Sprintf(mysql.MySQLErrName[mysql.ErrUnsupportedDDLOperation].Raw, "shard_row_id_bits for table with primary key as row id"), nil))
errUnsupportedAlterTableWithValidation = dbterror.ClassDDL.NewStdErr(mysql.ErrUnsupportedDDLOperation, parser_mysql.Message("ALTER TABLE WITH VALIDATION is currently unsupported", nil))
errUnsupportedAlterTableWithoutValidation = dbterror.ClassDDL.NewStdErr(mysql.ErrUnsupportedDDLOperation, parser_mysql.Message("ALTER TABLE WITHOUT VALIDATION is currently unsupported", nil))
errUnsupportedAlterTableOption = dbterror.ClassDDL.NewStdErr(mysql.ErrUnsupportedDDLOperation, parser_mysql.Message("This type of ALTER TABLE is currently unsupported", nil))
errBlobKeyWithoutLength = dbterror.ClassDDL.NewStd(mysql.ErrBlobKeyWithoutLength)
errKeyPart0 = dbterror.ClassDDL.NewStd(mysql.ErrKeyPart0)
errIncorrectPrefixKey = dbterror.ClassDDL.NewStd(mysql.ErrWrongSubKey)
Expand Down Expand Up @@ -184,7 +185,8 @@ var (
ErrPartitionFuncNotAllowed = dbterror.ClassDDL.NewStd(mysql.ErrPartitionFuncNotAllowed)
// ErrUniqueKeyNeedAllFieldsInPf returns must include all columns in the table's partitioning function.
ErrUniqueKeyNeedAllFieldsInPf = dbterror.ClassDDL.NewStd(mysql.ErrUniqueKeyNeedAllFieldsInPf)
errWrongExprInPartitionFunc = dbterror.ClassDDL.NewStd(mysql.ErrWrongExprInPartitionFunc)
// ErrWrongExprInPartitionFunc Constant, random or timezone-dependent expressions in (sub)partitioning function are not allowed.
ErrWrongExprInPartitionFunc = dbterror.ClassDDL.NewStd(mysql.ErrWrongExprInPartitionFunc)
// ErrWarnDataTruncated returns data truncated error.
ErrWarnDataTruncated = dbterror.ClassDDL.NewStd(mysql.WarnDataTruncated)
// ErrCoalesceOnlyOnHashPartition returns coalesce partition can only be used on hash/key partitions.
Expand Down
Loading

0 comments on commit e99e6b5

Please sign in to comment.