Skip to content

Commit

Permalink
ddl: fill in original default for extra writable columns in batch ins…
Browse files Browse the repository at this point in the history
…ert (#40198) (#53053)

close #40192, close #50993
  • Loading branch information
ti-chi-bot authored May 7, 2024
1 parent ecc2f3e commit 5e74466
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 17 deletions.
6 changes: 4 additions & 2 deletions DEPS.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -4897,14 +4897,16 @@ def go_deps():
name = "com_sourcegraph_sourcegraph_appdash",
build_file_proto_mode = "disable_global",
importpath = "sourcegraph.com/sourcegraph/appdash",
sum = "h1:ucqkfpjg9WzSUubAO62csmucvxl4/JeW3F4I4909XkM=",
replace = "github.com/sourcegraph/appdash",
sum = "h1:IJ3DuWHPTJrsqtIqjfdmPTELdTFGefvrOa2eTeRBleQ=",
version = "v0.0.0-20190731080439-ebfcffb1b5c0",
)
go_repository(
name = "com_sourcegraph_sourcegraph_appdash_data",
build_file_proto_mode = "disable_global",
importpath = "sourcegraph.com/sourcegraph/appdash-data",
sum = "h1:e1sMhtVq9AfcEy8AXNb8eSg6gbzfdpYhoNqnPJa+GzI=",
replace = "github.com/sourcegraph/appdash-data",
sum = "h1:8ZnTA26bBOoPkAbbitKPgNlpw0Bwt7ZlpYgZWHWJR/w=",
version = "v0.0.0-20151005221446-73f23eafcf67",
)
go_repository(
Expand Down
36 changes: 36 additions & 0 deletions ddl/column_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -985,3 +985,39 @@ func TestIssue39080(t *testing.T) {
" UNIQUE KEY `authorIdx` (`authorId`)\n"+
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin"))
}

func TestWriteDataWriteOnlyMode(t *testing.T) {
store, dom := testkit.CreateMockStoreAndDomainWithSchemaLease(t, dbTestLease)

tk := testkit.NewTestKit(t, store)
tk2 := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk2.MustExec("use test")
tk.MustExec("CREATE TABLE t (`col1` bigint(20) DEFAULT 1,`col2` float,UNIQUE KEY `key1` (`col1`))")

originalCallback := dom.DDL().GetHook()
defer dom.DDL().SetHook(originalCallback)

hook := &ddl.TestDDLCallback{Do: dom}
hook.OnJobRunBeforeExported = func(job *model.Job) {
if job.SchemaState != model.StateWriteOnly {
return
}
tk2.MustExec("insert ignore into t values (1, 2)")
tk2.MustExec("insert ignore into t values (2, 2)")
}
dom.DDL().SetHook(hook)
tk.MustExec("alter table t change column `col1` `col1` varchar(20)")

hook = &ddl.TestDDLCallback{Do: dom}
hook.OnJobRunBeforeExported = func(job *model.Job) {
if job.SchemaState != model.StateWriteOnly {
return
}
tk2.MustExec("insert ignore into t values (1)")
tk2.MustExec("insert ignore into t values (2)")
}
dom.DDL().SetHook(hook)
tk.MustExec("alter table t drop column `col1`")
dom.DDL().SetHook(originalCallback)
}
39 changes: 28 additions & 11 deletions executor/batch_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,33 @@ func getKeysNeedCheckOneRow(ctx sessionctx.Context, t table.Table, row []types.D
}
}

// addChangingColTimes is used to fetch values while processing "modify/change column" operation.
addChangingColTimes := 0
// extraColumns is used to fetch values while processing "add/drop/modify/change column" operation.
extraColumns := 0
for _, col := range t.WritableCols() {
// if there is a changing column, append the dependency column for index fetch values
if col.ChangeStateInfo != nil && col.State != model.StatePublic {
value, err := table.CastValue(ctx, row[col.DependencyColumnOffset], col.ColumnInfo, false, false)
if err != nil {
return nil, err
}
row = append(row, value)
extraColumns++
continue
}

if col.State != model.StatePublic {
// only append origin default value for index fetch values
if col.Offset >= len(row) {
value, err := table.GetColOriginDefaultValue(ctx, col.ToInfo())
if err != nil {
return nil, err
}

row = append(row, value)
extraColumns++
}
}
}
// append unique keys and errors
for _, v := range t.Indices() {
if !tables.IsIndexWritable(v) {
Expand All @@ -159,12 +184,6 @@ func getKeysNeedCheckOneRow(ctx sessionctx.Context, t table.Table, row []types.D
if t.Meta().IsCommonHandle && v.Meta().Primary {
continue
}
if len(row) < len(t.WritableCols()) && addChangingColTimes == 0 {
if col := tables.FindChangingCol(t.WritableCols(), v.Meta()); col != nil {
row = append(row, row[col.DependencyColumnOffset])
addChangingColTimes++
}
}
colVals, err1 := v.FetchValues(row, nil)
if err1 != nil {
return nil, err1
Expand Down Expand Up @@ -194,9 +213,7 @@ func getKeysNeedCheckOneRow(ctx sessionctx.Context, t table.Table, row []types.D
commonHandle: t.Meta().IsCommonHandle,
})
}
if addChangingColTimes == 1 {
row = row[:len(row)-1]
}
row = row[:len(row)-extraColumns]
result = append(result, toBeCheckedRow{
row: row,
handleKey: handleKey,
Expand Down
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -265,4 +265,8 @@ replace (
github.com/dgrijalva/jwt-go => github.com/form3tech-oss/jwt-go v3.2.6-0.20210809144907-32ab6a8243d7+incompatible
github.com/pingcap/tidb/parser => ./parser
go.opencensus.io => go.opencensus.io v0.23.1-0.20220331163232-052120675fac
// TODO: `sourcegraph.com/sourcegraph/appdash` has been archived, and the original host has been removed.
// Please remove these dependencies.
sourcegraph.com/sourcegraph/appdash => github.com/sourcegraph/appdash v0.0.0-20190731080439-ebfcffb1b5c0
sourcegraph.com/sourcegraph/appdash-data => github.com/sourcegraph/appdash-data v0.0.0-20151005221446-73f23eafcf67
)
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -920,6 +920,10 @@ github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9
github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM=
github.com/soheilhy/cmux v0.1.5 h1:jjzc5WVemNEDTLwv9tlmemhC73tI08BNOIGwBOo10Js=
github.com/soheilhy/cmux v0.1.5/go.mod h1:T7TcVDs9LWfQgPlPsdngu6I6QIoyIFZDDC6sNE1GqG0=
github.com/sourcegraph/appdash v0.0.0-20190731080439-ebfcffb1b5c0 h1:IJ3DuWHPTJrsqtIqjfdmPTELdTFGefvrOa2eTeRBleQ=
github.com/sourcegraph/appdash v0.0.0-20190731080439-ebfcffb1b5c0/go.mod h1:V952P4GGl1v/MMynLwxVdWEbSZJx+n0oOO3ljnez+WU=
github.com/sourcegraph/appdash-data v0.0.0-20151005221446-73f23eafcf67 h1:8ZnTA26bBOoPkAbbitKPgNlpw0Bwt7ZlpYgZWHWJR/w=
github.com/sourcegraph/appdash-data v0.0.0-20151005221446-73f23eafcf67/go.mod h1:tNZjgbYncKL5HxvDULAr/mWDmFz4B7H8yrXEDlnoIiw=
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI=
github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
Expand Down Expand Up @@ -1727,8 +1731,4 @@ sigs.k8s.io/structured-merge-diff/v4 v4.2.1/go.mod h1:j/nl6xW8vLS49O8YvXW1ocPhZa
sigs.k8s.io/yaml v1.1.0/go.mod h1:UJmg0vDUVViEyp3mgSv9WPwZCDxu4rQW1olrI1uml+o=
sigs.k8s.io/yaml v1.2.0 h1:kr/MCeFWJWTwyaHoR9c8EjH9OumOmoF9YGiZd7lFm/Q=
sigs.k8s.io/yaml v1.2.0/go.mod h1:yfXDCHCao9+ENCvLSE62v9VSji2MKu5jeNfTrofGhJc=
sourcegraph.com/sourcegraph/appdash v0.0.0-20190731080439-ebfcffb1b5c0 h1:ucqkfpjg9WzSUubAO62csmucvxl4/JeW3F4I4909XkM=
sourcegraph.com/sourcegraph/appdash v0.0.0-20190731080439-ebfcffb1b5c0/go.mod h1:hI742Nqp5OhwiqlzhgfbWU4mW4yO10fP+LoT9WOswdU=
sourcegraph.com/sourcegraph/appdash-data v0.0.0-20151005221446-73f23eafcf67 h1:e1sMhtVq9AfcEy8AXNb8eSg6gbzfdpYhoNqnPJa+GzI=
sourcegraph.com/sourcegraph/appdash-data v0.0.0-20151005221446-73f23eafcf67/go.mod h1:L5q+DGLGOQFpo1snNEkLOJT2d1YTW66rWNzatr3He1k=
stathat.com/c/consistent v1.0.0 h1:ezyc51EGcRPJUxfHGSgJjWzJdj3NiMU9pNfLNGiXV0c=

0 comments on commit 5e74466

Please sign in to comment.