Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into issue29705
Browse files Browse the repository at this point in the history
  • Loading branch information
time-and-fate committed Dec 1, 2021
2 parents 139229c + fa686a9 commit f2a35a7
Show file tree
Hide file tree
Showing 57 changed files with 780 additions and 194 deletions.
2 changes: 1 addition & 1 deletion br/pkg/backup/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,7 @@ func OnBackupResponse(
if lockErr := v.KvError.Locked; lockErr != nil {
// Try to resolve lock.
log.Warn("backup occur kv error", zap.Reflect("error", v))
msBeforeExpired, _, err1 := lockResolver.ResolveLocks(
msBeforeExpired, err1 := lockResolver.ResolveLocks(
bo, backupTS, []*txnlock.Lock{txnlock.NewLock(lockErr)})
if err1 != nil {
return nil, 0, errors.Trace(err1)
Expand Down
6 changes: 1 addition & 5 deletions cmd/ddltest/ddl_serial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import (
_ "github.com/go-sql-driver/mysql"
"github.com/pingcap/errors"
"github.com/pingcap/log"
zaplog "github.com/pingcap/log"
"github.com/pingcap/tidb/ddl"
"github.com/pingcap/tidb/domain"
"github.com/pingcap/tidb/kv"
Expand All @@ -48,7 +47,6 @@ import (
"github.com/pingcap/tidb/table/tables"
"github.com/pingcap/tidb/testkit"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/logutil"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
goctx "golang.org/x/net/context"
Expand Down Expand Up @@ -91,11 +89,9 @@ type ddlSuite struct {
}

func createDDLSuite(t *testing.T) (s *ddlSuite) {
var err error
s = new(ddlSuite)

err := logutil.InitLogger(&logutil.LogConfig{Config: zaplog.Config{Level: *logLevel}})
require.NoError(t, err)

s.quit = make(chan struct{})

s.store, err = store.New(fmt.Sprintf("tikv://%s%s", *etcd, *tikvPath))
Expand Down
36 changes: 36 additions & 0 deletions cmd/ddltest/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2021 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package ddltest

import (
"fmt"
"os"
"testing"

zaplog "github.com/pingcap/log"
"github.com/pingcap/tidb/util/logutil"
"github.com/pingcap/tidb/util/testbridge"
"go.uber.org/goleak"
)

func TestMain(m *testing.M) {
testbridge.WorkaroundGoCheckFlags()
err := logutil.InitLogger(&logutil.LogConfig{Config: zaplog.Config{Level: *logLevel}})
if err != nil {
fmt.Fprint(os.Stderr, err.Error())
os.Exit(1)
}
goleak.VerifyTestMain(m)
}
2 changes: 2 additions & 0 deletions ddl/backfilling.go
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,8 @@ func (w *worker) writePhysicalTableRecord(t table.PhysicalTable, bfWorkerType ba
backfillWorkers = append(backfillWorkers, idxWorker.backfillWorker)
go idxWorker.backfillWorker.run(reorgInfo.d, idxWorker, job)
case typeUpdateColumnWorker:
// Setting InCreateOrAlterStmt tells the difference between SELECT casting and ALTER COLUMN casting.
sessCtx.GetSessionVars().StmtCtx.InCreateOrAlterStmt = true
updateWorker := newUpdateColumnWorker(sessCtx, w, i, t, oldColInfo, colInfo, decodeColMap, reorgInfo.ReorgMeta.SQLMode)
updateWorker.priority = job.Priority
backfillWorkers = append(backfillWorkers, updateWorker.backfillWorker)
Expand Down
46 changes: 46 additions & 0 deletions ddl/column_type_change_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2128,6 +2128,52 @@ func (s *testColumnTypeChangeSuite) TestCastToTimeStampDecodeError(c *C) {
tk.MustQuery("select timestamp(cast('1000-11-11 12-3-1' as date));").Check(testkit.Rows("1000-11-11 00:00:00"))
}

// https://github.com/pingcap/tidb/issues/25285.
func (s *testColumnTypeChangeSuite) TestCastFromZeroIntToTimeError(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test;")

prepare := func() {
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a int);")
tk.MustExec("insert into t values (0);")
}
const errCodeNone = -1
testCases := []struct {
sqlMode string
errCode int
}{
{"STRICT_TRANS_TABLES", mysql.ErrTruncatedWrongValue},
{"STRICT_ALL_TABLES", mysql.ErrTruncatedWrongValue},
{"NO_ZERO_IN_DATE", errCodeNone},
{"NO_ZERO_DATE", errCodeNone},
{"ALLOW_INVALID_DATES", errCodeNone},
{"", errCodeNone},
}
for _, tc := range testCases {
prepare()
tk.MustExec(fmt.Sprintf("set @@sql_mode = '%s';", tc.sqlMode))
if tc.sqlMode == "NO_ZERO_DATE" {
tk.MustQuery(`select date(0);`).Check(testkit.Rows("<nil>"))
} else {
tk.MustQuery(`select date(0);`).Check(testkit.Rows("0000-00-00"))
}
tk.MustQuery(`select time(0);`).Check(testkit.Rows("00:00:00"))
if tc.errCode == errCodeNone {
tk.MustExec("alter table t modify column a date;")
prepare()
tk.MustExec("alter table t modify column a datetime;")
prepare()
tk.MustExec("alter table t modify column a timestamp;")
} else {
tk.MustGetErrCode("alter table t modify column a date;", mysql.ErrTruncatedWrongValue)
tk.MustGetErrCode("alter table t modify column a datetime;", mysql.ErrTruncatedWrongValue)
tk.MustGetErrCode("alter table t modify column a timestamp;", mysql.ErrTruncatedWrongValue)
}
}
tk.MustExec("drop table if exists t;")
}

func (s *testColumnTypeChangeSuite) TestChangeFromTimeToYear(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test;")
Expand Down
11 changes: 6 additions & 5 deletions ddl/db_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1537,7 +1537,7 @@ func (s *testSerialDBSuite1) TestCreateSecondaryIndexInCluster(c *C) {
tk.MustExec("use test")

// test create table with non-unique key
tk.MustGetErrCode(`
tk.MustExec(`
CREATE TABLE t (
c01 varchar(255) NOT NULL,
c02 varchar(255) NOT NULL,
Expand All @@ -1547,7 +1547,8 @@ CREATE TABLE t (
c06 varchar(255) DEFAULT NULL,
PRIMARY KEY (c01,c02,c03) clustered,
KEY c04 (c04)
)`, errno.ErrTooLongKey)
)`)
tk.MustExec("drop table t")

// test create long clustered primary key.
tk.MustGetErrCode(`
Expand Down Expand Up @@ -1587,7 +1588,7 @@ CREATE TABLE t (
PRIMARY KEY (c01,c02) clustered
)`)
tk.MustExec("create index idx1 on t(c03)")
tk.MustGetErrCode("create index idx2 on t(c03, c04)", errno.ErrTooLongKey)
tk.MustExec("create index idx2 on t(c03, c04)")
tk.MustExec("create unique index uk2 on t(c03, c04)")
tk.MustExec("drop table t")

Expand All @@ -1606,9 +1607,9 @@ CREATE TABLE t (
)`)
tk.MustExec("alter table t change c03 c10 varchar(256) default null")
tk.MustGetErrCode("alter table t change c10 c100 varchar(1024) default null", errno.ErrTooLongKey)
tk.MustGetErrCode("alter table t modify c10 varchar(600) default null", errno.ErrTooLongKey)
tk.MustExec("alter table t modify c10 varchar(600) default null")
tk.MustExec("alter table t modify c06 varchar(600) default null")
tk.MustGetErrCode("alter table t modify c01 varchar(510)", errno.ErrTooLongKey)
tk.MustExec("alter table t modify c01 varchar(510)")
tk.MustExec("create table t2 like t")
}

Expand Down
9 changes: 3 additions & 6 deletions ddl/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5474,8 +5474,7 @@ func (s *testDBSuite1) TestModifyColumnTime_YearToDate(c *C) {
{"year", `"69"`, "date", "", errno.ErrTruncatedWrongValue},
{"year", `"70"`, "date", "", errno.ErrTruncatedWrongValue},
{"year", `"99"`, "date", "", errno.ErrTruncatedWrongValue},
// MySQL will get "Data truncation: Incorrect date value: '0000'", but TiDB treat 00 as valid datetime.
{"year", `00`, "date", "0000-00-00", 0},
{"year", `00`, "date", "", errno.ErrTruncatedWrongValue},
{"year", `69`, "date", "", errno.ErrTruncatedWrongValue},
{"year", `70`, "date", "", errno.ErrTruncatedWrongValue},
{"year", `99`, "date", "", errno.ErrTruncatedWrongValue},
Expand All @@ -5492,8 +5491,7 @@ func (s *testDBSuite1) TestModifyColumnTime_YearToDatetime(c *C) {
{"year", `"69"`, "datetime", "", errno.ErrTruncatedWrongValue},
{"year", `"70"`, "datetime", "", errno.ErrTruncatedWrongValue},
{"year", `"99"`, "datetime", "", errno.ErrTruncatedWrongValue},
// MySQL will get "Data truncation: Incorrect date value: '0000'", but TiDB treat 00 as valid datetime.
{"year", `00`, "datetime", "0000-00-00 00:00:00", 0},
{"year", `00`, "datetime", "", errno.ErrTruncatedWrongValue},
{"year", `69`, "datetime", "", errno.ErrTruncatedWrongValue},
{"year", `70`, "datetime", "", errno.ErrTruncatedWrongValue},
{"year", `99`, "datetime", "", errno.ErrTruncatedWrongValue},
Expand All @@ -5510,8 +5508,7 @@ func (s *testDBSuite1) TestModifyColumnTime_YearToTimestamp(c *C) {
{"year", `"69"`, "timestamp", "", errno.ErrTruncatedWrongValue},
{"year", `"70"`, "timestamp", "", errno.ErrTruncatedWrongValue},
{"year", `"99"`, "timestamp", "", errno.ErrTruncatedWrongValue},
// MySQL will get "Data truncation: Incorrect date value: '0000'", but TiDB treat 00 as valid datetime.
{"year", `00`, "timestamp", "0000-00-00 00:00:00", 0},
{"year", `00`, "timestamp", "", errno.ErrTruncatedWrongValue},
{"year", `69`, "timestamp", "", errno.ErrTruncatedWrongValue},
{"year", `70`, "timestamp", "", errno.ErrTruncatedWrongValue},
{"year", `99`, "timestamp", "", errno.ErrTruncatedWrongValue},
Expand Down
24 changes: 11 additions & 13 deletions ddl/ddl_algorithm_test.go → ddl/ddl_algorithm_serial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,25 @@
package ddl_test

import (
. "github.com/pingcap/check"
"testing"

"github.com/pingcap/tidb/ddl"
"github.com/pingcap/tidb/parser/ast"
"github.com/stretchr/testify/require"
)

var _ = Suite(&testDDLAlgorithmSuite{})

var (
allAlgorithm = []ast.AlgorithmType{ast.AlgorithmTypeCopy,
ast.AlgorithmTypeInplace, ast.AlgorithmTypeInstant}
)

type testDDLAlgorithmSuite struct{}

type testCase struct {
alterSpec ast.AlterTableSpec
supportedAlgorithm []ast.AlgorithmType
expectedAlgorithm []ast.AlgorithmType
}

func (s *testDDLAlgorithmSuite) TestFindAlterAlgorithm(c *C) {
func TestFindAlterAlgorithm(t *testing.T) {
supportedInstantAlgorithms := []ast.AlgorithmType{ast.AlgorithmTypeDefault, ast.AlgorithmTypeCopy, ast.AlgorithmTypeInplace, ast.AlgorithmTypeInstant}
expectedInstantAlgorithms := []ast.AlgorithmType{ast.AlgorithmTypeInstant, ast.AlgorithmTypeInstant, ast.AlgorithmTypeInstant, ast.AlgorithmTypeInstant}

Expand Down Expand Up @@ -77,11 +75,11 @@ func (s *testDDLAlgorithmSuite) TestFindAlterAlgorithm(c *C) {
}

for _, tc := range testCases {
runAlterAlgorithmTestCases(c, &tc)
runAlterAlgorithmTestCases(t, &tc)
}
}

func runAlterAlgorithmTestCases(c *C, tc *testCase) {
func runAlterAlgorithmTestCases(t *testing.T, tc *testCase) {
unsupported := make([]ast.AlgorithmType, 0, len(allAlgorithm))
Loop:
for _, alm := range allAlgorithm {
Expand All @@ -101,16 +99,16 @@ Loop:
for i, alm := range tc.supportedAlgorithm {
algorithm, err = ddl.ResolveAlterAlgorithm(&tc.alterSpec, alm)
if err != nil {
c.Assert(ddl.ErrAlterOperationNotSupported.Equal(err), IsTrue)
require.True(t, ddl.ErrAlterOperationNotSupported.Equal(err))
}
c.Assert(algorithm, Equals, tc.expectedAlgorithm[i])
require.Equal(t, tc.expectedAlgorithm[i], algorithm)
}

// Test unsupported.
for _, alm := range unsupported {
algorithm, err = ddl.ResolveAlterAlgorithm(&tc.alterSpec, alm)
c.Assert(algorithm, Equals, ast.AlgorithmTypeDefault)
c.Assert(err, NotNil, Commentf("Tp:%v, alm:%s", tc.alterSpec.Tp, alm))
c.Assert(ddl.ErrAlterOperationNotSupported.Equal(err), IsTrue)
require.Equal(t, ast.AlgorithmTypeDefault, algorithm)
require.Error(t, err)
require.True(t, ddl.ErrAlterOperationNotSupported.Equal(err))
}
}
Loading

0 comments on commit f2a35a7

Please sign in to comment.