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

go.mod: update parser to support TABLES and VALUES statement #20272

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
5 changes: 2 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# Contribution Guide
# Contributing Guide

See the [Contribution Guide](https://github.com/pingcap/community/blob/master/CONTRIBUTING.md) in the
[community](https://github.com/pingcap/community) repo.
See the [Contributing Guide](https://github.com/pingcap/community/blob/master/contributors/README.md) in the [community](https://github.com/pingcap/community) repository.
45 changes: 38 additions & 7 deletions ddl/backfilling.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/pingcap/errors"
"github.com/pingcap/failpoint"
"github.com/pingcap/parser/model"
"github.com/pingcap/parser/terror"
ddlutil "github.com/pingcap/tidb/ddl/util"
"github.com/pingcap/tidb/expression"
"github.com/pingcap/tidb/kv"
Expand Down Expand Up @@ -71,10 +72,12 @@ type backfillResult struct {
// backfillTaskContext is the context of the batch adding indices or updating column values.
// After finishing the batch adding indices or updating column values, result in backfillTaskContext will be merged into backfillResult.
type backfillTaskContext struct {
nextHandle kv.Handle
done bool
addedCount int
scanCount int
nextHandle kv.Handle
done bool
addedCount int
scanCount int
warnings map[errors.ErrorID]*terror.Error
warningsCount map[errors.ErrorID]int64
}

type backfillWorker struct {
Expand Down Expand Up @@ -150,10 +153,26 @@ func mergeBackfillCtxToResult(taskCtx *backfillTaskContext, result *backfillResu
result.scanCount += taskCtx.scanCount
}

func mergeWarningsAndWarningsCount(partWarnings, totalWarnings map[errors.ErrorID]*terror.Error, partWarningsCount, totalWarningsCount map[errors.ErrorID]int64) (map[errors.ErrorID]*terror.Error, map[errors.ErrorID]int64) {
for _, warn := range partWarnings {
if _, ok := totalWarningsCount[warn.ID()]; ok {
totalWarningsCount[warn.ID()] += partWarningsCount[warn.ID()]
} else {
totalWarningsCount[warn.ID()] = partWarningsCount[warn.ID()]
totalWarnings[warn.ID()] = warn
}
}
return totalWarnings, totalWarningsCount
}

// handleBackfillTask backfills range [task.startHandle, task.endHandle) handle's index to table.
func (w *backfillWorker) handleBackfillTask(d *ddlCtx, task *reorgBackfillTask, bf backfiller) *backfillResult {
handleRange := *task
result := &backfillResult{addedCount: 0, nextHandle: handleRange.startHandle, err: nil}
result := &backfillResult{
err: nil,
addedCount: 0,
nextHandle: handleRange.startHandle,
}
lastLogCount := 0
lastLogTime := time.Now()
startTime := lastLogTime
Expand All @@ -177,7 +196,17 @@ func (w *backfillWorker) handleBackfillTask(d *ddlCtx, task *reorgBackfillTask,

bf.AddMetricInfo(float64(taskCtx.addedCount))
mergeBackfillCtxToResult(&taskCtx, result)

// Although `handleRange` is for data in one region, but back fill worker still split it into many
// small reorg batch size slices and reorg them in many different kv txn.
// If a task failed, it may contained some committed small kv txn which has already finished the
// small range reorganization.
// In the next round of reorganization, the target handle range may overlap with last committed
// small ranges. This will cause the `redo` action in reorganization.
// So for added count and warnings collection, it is recommended to collect the statistics in every
// successfully committed small ranges rather than fetching it in the total result.
w.ddlWorker.reorgCtx.increaseRowCount(int64(taskCtx.addedCount))
w.ddlWorker.reorgCtx.mergeWarnings(taskCtx.warnings, taskCtx.warningsCount)

if num := result.scanCount - lastLogCount; num >= 30000 {
lastLogCount = result.scanCount
Expand Down Expand Up @@ -386,6 +415,8 @@ var (
TestCheckWorkerNumCh = make(chan struct{})
// TestCheckWorkerNumber use for test adjust backfill worker.
TestCheckWorkerNumber = int32(16)
// TestCheckReorgTimeout is used to mock timeout when reorg data.
TestCheckReorgTimeout = int32(0)
)

func loadDDLReorgVars(w *worker) error {
Expand Down Expand Up @@ -482,12 +513,12 @@ func (w *worker) writePhysicalTableRecord(t table.PhysicalTable, bfWorkerType ba
sessCtx.GetSessionVars().StmtCtx.IsDDLJobInQueue = true

if bfWorkerType == typeAddIndexWorker {
idxWorker := newAddIndexWorker(sessCtx, w, i, t, indexInfo, decodeColMap)
idxWorker := newAddIndexWorker(sessCtx, w, i, t, indexInfo, decodeColMap, reorgInfo.ReorgMeta.SQLMode)
idxWorker.priority = job.Priority
backfillWorkers = append(backfillWorkers, idxWorker.backfillWorker)
go idxWorker.backfillWorker.run(reorgInfo.d, idxWorker)
} else {
updateWorker := newUpdateColumnWorker(sessCtx, w, i, t, oldColInfo, colInfo, decodeColMap)
updateWorker := newUpdateColumnWorker(sessCtx, w, i, t, oldColInfo, colInfo, decodeColMap, reorgInfo.ReorgMeta.SQLMode)
updateWorker.priority = job.Priority
backfillWorkers = append(backfillWorkers, updateWorker.backfillWorker)
go updateWorker.backfillWorker.run(reorgInfo.d, updateWorker)
Expand Down
70 changes: 64 additions & 6 deletions ddl/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/pingcap/parser/ast"
"github.com/pingcap/parser/model"
"github.com/pingcap/parser/mysql"
"github.com/pingcap/parser/terror"
ddlutil "github.com/pingcap/tidb/ddl/util"
"github.com/pingcap/tidb/infoschema"
"github.com/pingcap/tidb/kv"
Expand Down Expand Up @@ -645,6 +646,11 @@ func onSetDefaultValue(t *meta.Meta, job *model.Job) (ver int64, _ error) {
func needChangeColumnData(oldCol, newCol *model.ColumnInfo) bool {
toUnsigned := mysql.HasUnsignedFlag(newCol.Flag)
originUnsigned := mysql.HasUnsignedFlag(oldCol.Flag)
if oldCol.Tp == newCol.Tp && oldCol.Tp == mysql.TypeNewDecimal {
// Since type decimal will encode the precision, frac, negative(signed) and wordBuf into storage together, there is no short
// cut to eliminate data reorg change for column type change between decimal.
return oldCol.Flen != newCol.Flen || oldCol.Decimal != newCol.Decimal || toUnsigned != originUnsigned
}
if newCol.Flen > 0 && newCol.Flen < oldCol.Flen || toUnsigned != originUnsigned {
return true
}
Expand Down Expand Up @@ -1036,9 +1042,12 @@ type updateColumnWorker struct {
rowDecoder *decoder.RowDecoder

rowMap map[int64]types.Datum

// For SQL Mode and warnings.
sqlMode mysql.SQLMode
}

func newUpdateColumnWorker(sessCtx sessionctx.Context, worker *worker, id int, t table.PhysicalTable, oldCol, newCol *model.ColumnInfo, decodeColMap map[int64]decoder.Column) *updateColumnWorker {
func newUpdateColumnWorker(sessCtx sessionctx.Context, worker *worker, id int, t table.PhysicalTable, oldCol, newCol *model.ColumnInfo, decodeColMap map[int64]decoder.Column, sqlMode mysql.SQLMode) *updateColumnWorker {
rowDecoder := decoder.NewRowDecoder(t, t.WritableCols(), decodeColMap)
return &updateColumnWorker{
backfillWorker: newBackfillWorker(sessCtx, worker, id, t),
Expand All @@ -1047,6 +1056,7 @@ func newUpdateColumnWorker(sessCtx sessionctx.Context, worker *worker, id int, t
metricCounter: metrics.BackfillTotalCounter.WithLabelValues("update_col_speed"),
rowDecoder: rowDecoder,
rowMap: make(map[int64]types.Datum, len(decodeColMap)),
sqlMode: sqlMode,
}
}

Expand All @@ -1055,8 +1065,9 @@ func (w *updateColumnWorker) AddMetricInfo(cnt float64) {
}

type rowRecord struct {
key []byte // It's used to lock a record. Record it to reduce the encoding time.
vals []byte // It's the record.
key []byte // It's used to lock a record. Record it to reduce the encoding time.
vals []byte // It's the record.
warning *terror.Error // It's used to record the cast warning of a record.
}

// getNextHandle gets next handle of entry that we are going to process.
Expand Down Expand Up @@ -1133,11 +1144,25 @@ func (w *updateColumnWorker) getRowRecord(handle kv.Handle, recordKey []byte, ra
return nil
}

var recordWarning *terror.Error
newColVal, err := table.CastValue(w.sessCtx, w.rowMap[w.oldColInfo.ID], w.newColInfo, false, false)
// TODO: Consider sql_mode and the error msg(encounter this error check whether to rollback).
if err != nil {
return errors.Trace(err)
if IsNormalWarning(err) || (!w.sqlMode.HasStrictMode() && IsStrictWarning(err)) {
// Keep the warnings.
recordWarning = errors.Cause(err).(*terror.Error)
} else {
return errors.Trace(err)
}
}

failpoint.Inject("MockReorgTimeoutInOneRegion", func(val failpoint.Value) {
if val.(bool) {
if handle.IntValue() == 3000 && atomic.CompareAndSwapInt32(&TestCheckReorgTimeout, 0, 1) {
failpoint.Return(errors.Trace(errWaitReorgTimeout))
}
}
})

w.rowMap[w.newColInfo.ID] = newColVal
newColumnIDs := make([]int64, 0, len(w.rowMap))
newRow := make([]types.Datum, 0, len(w.rowMap))
Expand All @@ -1151,11 +1176,31 @@ func (w *updateColumnWorker) getRowRecord(handle kv.Handle, recordKey []byte, ra
return errors.Trace(err)
}

w.rowRecords = append(w.rowRecords, &rowRecord{key: recordKey, vals: newRowVal})
w.rowRecords = append(w.rowRecords, &rowRecord{key: recordKey, vals: newRowVal, warning: recordWarning})
w.cleanRowMap()
return nil
}

// IsNormalWarning is used to check the normal warnings, for example data-truncated warnings.
// This kind of warning will be always thrown out regard less of what kind of the sql mode is.
func IsNormalWarning(err error) bool {
// TODO: there are more errors here can be identified as normal warnings.
if types.ErrTruncatedWrongVal.Equal(err) {
return true
}
return false
}

// IsStrictWarning is used to check whether the error can be transferred as a warning under a
// non-strict SQL Mode.
func IsStrictWarning(err error) bool {
// TODO: there are more errors here can be identified as warnings under non-strict SQL mode.
if types.ErrOverflow.Equal(err) {
return true
}
return false
}

func (w *updateColumnWorker) cleanRowMap() {
for id := range w.rowMap {
delete(w.rowMap, id)
Expand All @@ -1177,6 +1222,8 @@ func (w *updateColumnWorker) BackfillDataInTxn(handleRange reorgBackfillTask) (t
taskCtx.nextHandle = nextHandle
taskCtx.done = taskDone

warningsMap := make(map[errors.ErrorID]*terror.Error, len(rowRecords))
warningsCountMap := make(map[errors.ErrorID]int64, len(rowRecords))
for _, rowRecord := range rowRecords {
taskCtx.scanCount++

Expand All @@ -1185,8 +1232,19 @@ func (w *updateColumnWorker) BackfillDataInTxn(handleRange reorgBackfillTask) (t
return errors.Trace(err)
}
taskCtx.addedCount++
if rowRecord.warning != nil {
if _, ok := warningsCountMap[rowRecord.warning.ID()]; ok {
warningsCountMap[rowRecord.warning.ID()]++
} else {
warningsCountMap[rowRecord.warning.ID()] = 1
warningsMap[rowRecord.warning.ID()] = rowRecord.warning
}
}
}

// Collect the warnings.
taskCtx.warnings, taskCtx.warningsCount = warningsMap, warningsCountMap

return nil
})
logSlowOperations(time.Since(oprStartTime), "BackfillDataInTxn", 3000)
Expand Down
4 changes: 2 additions & 2 deletions ddl/column_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1159,8 +1159,8 @@ func (s *testColumnSuite) TestModifyColumn(c *C) {
{"varchar(10)", "varchar(8)", errUnsupportedModifyColumn.GenWithStackByArgs("length 8 is less than origin 10")},
{"varchar(10)", "varchar(11)", nil},
{"varchar(10) character set utf8 collate utf8_bin", "varchar(10) character set utf8", nil},
{"decimal(2,1)", "decimal(3,2)", errUnsupportedModifyColumn.GenWithStackByArgs("can't change decimal column precision")},
{"decimal(2,1)", "decimal(2,2)", errUnsupportedModifyColumn.GenWithStackByArgs("can't change decimal column precision")},
{"decimal(2,1)", "decimal(3,2)", errUnsupportedModifyColumn.GenWithStackByArgs("decimal change from decimal(2, 1) to decimal(3, 2), and tidb_enable_change_column_type is false")},
{"decimal(2,1)", "decimal(2,2)", errUnsupportedModifyColumn.GenWithStackByArgs("decimal change from decimal(2, 1) to decimal(2, 2), and tidb_enable_change_column_type is false")},
{"decimal(2,1)", "decimal(2,1)", nil},
{"decimal(2,1)", "int", errUnsupportedModifyColumn.GenWithStackByArgs("type int(11) not match origin decimal(2,1)")},
{"decimal", "int", errUnsupportedModifyColumn.GenWithStackByArgs("type int(11) not match origin decimal(11,0)")},
Expand Down
101 changes: 101 additions & 0 deletions ddl/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ var _ = Suite(&testDBSuite7{&testDBSuite{}})
var _ = SerialSuites(&testSerialDBSuite{&testDBSuite{}})

const defaultBatchSize = 1024
const defaultReorgBatchSize = 256

type testDBSuite struct {
cluster cluster.Cluster
Expand Down Expand Up @@ -5767,3 +5768,103 @@ func (s *testSerialDBSuite) TestColumnTypeChangeGenUniqueChangingName(c *C) {

tk.MustExec("drop table if exists t")
}

func (s *testSerialDBSuite) TestModifyColumnTypeWithWarnings(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
// Enable column change variable.
tk.Se.GetSessionVars().EnableChangeColumnType = true

// Test normal warnings.
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a decimal(5,2))")
tk.MustExec("insert into t values(111.22),(111.22),(111.22),(111.22),(333.4)")
// 111.22 will be truncated the fraction .22 as .2 with truncated warning for each row.
tk.MustExec("alter table t modify column a decimal(4,1)")
// there should 4 rows of warnings corresponding to the origin rows.
tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1292 Truncated incorrect DECIMAL value: '111.22'",
"Warning 1292 Truncated incorrect DECIMAL value: '111.22'",
"Warning 1292 Truncated incorrect DECIMAL value: '111.22'",
"Warning 1292 Truncated incorrect DECIMAL value: '111.22'"))

// Test the strict warnings is treated as errors under the strict mode.
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a decimal(5,2))")
tk.MustExec("insert into t values(111.22),(111.22),(111.22),(33.4)")
// Since modify column a from decimal(5,2) to decimal(3,1), the first three rows with 111.22 will overflows the target types.
_, err := tk.Exec("alter table t modify column a decimal(3,1)")
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "[types:1690]DECIMAL value is out of range in '(3, 1)'")

// Test the strict warnings is treated as warnings under the non-strict mode.
tk.MustExec("set @@sql_mode=\"\"")
tk.MustExec("alter table t modify column a decimal(3,1)")
tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1690 DECIMAL value is out of range in '(3, 1)'",
"Warning 1690 DECIMAL value is out of range in '(3, 1)'",
"Warning 1690 DECIMAL value is out of range in '(3, 1)'"))
}

// TestModifyColumnTypeWhenInterception is to test modifying column type with warnings intercepted by
// reorg timeout, not owner error and so on.
func (s *testSerialDBSuite) TestModifyColumnTypeWhenInterception(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
// Enable column change variable.
tk.Se.GetSessionVars().EnableChangeColumnType = true

// Test normal warnings.
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a int primary key, b decimal(4,2))")

count := defaultBatchSize * 4
// Add some rows.
dml := fmt.Sprintf("insert into t values")
for i := 1; i <= count; i++ {
dml += fmt.Sprintf("(%d, %f)", i, 11.22)
if i != count {
dml += ","
}
}
tk.MustExec(dml)
// Make the regions scale like: [1, 1024), [1024, 2048), [2048, 3072), [3072, 4096]
tk.MustQuery("split table t between(0) and (4096) regions 4")

d := s.dom.DDL()
hook := &ddl.TestDDLCallback{}
var checkMiddleWarningCount bool
var checkMiddleAddedCount bool
// Since the `DefTiDBDDLReorgWorkerCount` is 4, every worker will be assigned with one region
// for the first time. Here we mock the insert failure/reorg timeout in region [2048, 3072)
// which will lead next handle be set to 2048 and partial warnings be stored into ddl job.
// Since the existence of reorg batch size, only the last reorg batch [2816, 3072) of kv
// range [2048, 3072) fail to commit, the rest of them all committed successfully. So the
// addedCount and warnings count in the job are all equal to `4096 - reorg batch size`.
// In the next round of this ddl job, the last reorg batch will be finished.
var middleWarningsCount = int64(defaultBatchSize*4 - defaultReorgBatchSize)
hook.OnJobUpdatedExported = func(job *model.Job) {
if job.SchemaState == model.StateWriteReorganization || job.SnapshotVer != 0 {
if len(job.ReorgMeta.WarningsCount) == len(job.ReorgMeta.Warnings) {
for _, v := range job.ReorgMeta.WarningsCount {
if v == middleWarningsCount {
checkMiddleWarningCount = true
}
}
}
if job.RowCount == middleWarningsCount {
checkMiddleAddedCount = true
}
}
}
originHook := d.GetHook()
d.(ddl.DDLForTest).SetHook(hook)
defer d.(ddl.DDLForTest).SetHook(originHook)
c.Assert(failpoint.Enable("github.com/pingcap/tidb/ddl/MockReorgTimeoutInOneRegion", `return(true)`), IsNil)
defer func() {
c.Assert(failpoint.Disable("github.com/pingcap/tidb/ddl/MockReorgTimeoutInOneRegion"), IsNil)
}()
tk.MustExec("alter table t modify column b decimal(3,1)")
c.Assert(checkMiddleWarningCount, Equals, true)
c.Assert(checkMiddleAddedCount, Equals, true)
res := tk.MustQuery("show warnings")
c.Assert(len(res.Rows()), Equals, count)
}
12 changes: 12 additions & 0 deletions ddl/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,18 @@ func (d *ddl) doDDLJob(ctx sessionctx.Context, job *model.Job) error {

// If a job is a history job, the state must be JobStateSynced or JobStateRollbackDone or JobStateCancelled.
if historyJob.IsSynced() {
// Judge whether there are some warnings when executing DDL under the certain SQL mode.
if historyJob.ReorgMeta != nil && len(historyJob.ReorgMeta.Warnings) != 0 {
if len(historyJob.ReorgMeta.Warnings) != len(historyJob.ReorgMeta.WarningsCount) {
logutil.BgLogger().Info("[ddl] DDL warnings doesn't match the warnings count", zap.Int64("jobID", jobID))
} else {
for key, warning := range historyJob.ReorgMeta.Warnings {
for j := int64(0); j < historyJob.ReorgMeta.WarningsCount[key]; j++ {
ctx.GetSessionVars().StmtCtx.AppendWarning(warning)
}
}
}
}
logutil.BgLogger().Info("[ddl] DDL job is finished", zap.Int64("jobID", jobID))
return nil
}
Expand Down
Loading