Skip to content

Commit 5e74466

Browse files
authored
ddl: fill in original default for extra writable columns in batch insert (pingcap#40198) (pingcap#53053)
close pingcap#40192, close pingcap#50993
1 parent ecc2f3e commit 5e74466

File tree

5 files changed

+76
-17
lines changed

5 files changed

+76
-17
lines changed

DEPS.bzl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4897,14 +4897,16 @@ def go_deps():
48974897
name = "com_sourcegraph_sourcegraph_appdash",
48984898
build_file_proto_mode = "disable_global",
48994899
importpath = "sourcegraph.com/sourcegraph/appdash",
4900-
sum = "h1:ucqkfpjg9WzSUubAO62csmucvxl4/JeW3F4I4909XkM=",
4900+
replace = "github.com/sourcegraph/appdash",
4901+
sum = "h1:IJ3DuWHPTJrsqtIqjfdmPTELdTFGefvrOa2eTeRBleQ=",
49014902
version = "v0.0.0-20190731080439-ebfcffb1b5c0",
49024903
)
49034904
go_repository(
49044905
name = "com_sourcegraph_sourcegraph_appdash_data",
49054906
build_file_proto_mode = "disable_global",
49064907
importpath = "sourcegraph.com/sourcegraph/appdash-data",
4907-
sum = "h1:e1sMhtVq9AfcEy8AXNb8eSg6gbzfdpYhoNqnPJa+GzI=",
4908+
replace = "github.com/sourcegraph/appdash-data",
4909+
sum = "h1:8ZnTA26bBOoPkAbbitKPgNlpw0Bwt7ZlpYgZWHWJR/w=",
49084910
version = "v0.0.0-20151005221446-73f23eafcf67",
49094911
)
49104912
go_repository(

ddl/column_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -985,3 +985,39 @@ func TestIssue39080(t *testing.T) {
985985
" UNIQUE KEY `authorIdx` (`authorId`)\n"+
986986
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin"))
987987
}
988+
989+
func TestWriteDataWriteOnlyMode(t *testing.T) {
990+
store, dom := testkit.CreateMockStoreAndDomainWithSchemaLease(t, dbTestLease)
991+
992+
tk := testkit.NewTestKit(t, store)
993+
tk2 := testkit.NewTestKit(t, store)
994+
tk.MustExec("use test")
995+
tk2.MustExec("use test")
996+
tk.MustExec("CREATE TABLE t (`col1` bigint(20) DEFAULT 1,`col2` float,UNIQUE KEY `key1` (`col1`))")
997+
998+
originalCallback := dom.DDL().GetHook()
999+
defer dom.DDL().SetHook(originalCallback)
1000+
1001+
hook := &ddl.TestDDLCallback{Do: dom}
1002+
hook.OnJobRunBeforeExported = func(job *model.Job) {
1003+
if job.SchemaState != model.StateWriteOnly {
1004+
return
1005+
}
1006+
tk2.MustExec("insert ignore into t values (1, 2)")
1007+
tk2.MustExec("insert ignore into t values (2, 2)")
1008+
}
1009+
dom.DDL().SetHook(hook)
1010+
tk.MustExec("alter table t change column `col1` `col1` varchar(20)")
1011+
1012+
hook = &ddl.TestDDLCallback{Do: dom}
1013+
hook.OnJobRunBeforeExported = func(job *model.Job) {
1014+
if job.SchemaState != model.StateWriteOnly {
1015+
return
1016+
}
1017+
tk2.MustExec("insert ignore into t values (1)")
1018+
tk2.MustExec("insert ignore into t values (2)")
1019+
}
1020+
dom.DDL().SetHook(hook)
1021+
tk.MustExec("alter table t drop column `col1`")
1022+
dom.DDL().SetHook(originalCallback)
1023+
}

executor/batch_checker.go

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,33 @@ func getKeysNeedCheckOneRow(ctx sessionctx.Context, t table.Table, row []types.D
146146
}
147147
}
148148

149-
// addChangingColTimes is used to fetch values while processing "modify/change column" operation.
150-
addChangingColTimes := 0
149+
// extraColumns is used to fetch values while processing "add/drop/modify/change column" operation.
150+
extraColumns := 0
151+
for _, col := range t.WritableCols() {
152+
// if there is a changing column, append the dependency column for index fetch values
153+
if col.ChangeStateInfo != nil && col.State != model.StatePublic {
154+
value, err := table.CastValue(ctx, row[col.DependencyColumnOffset], col.ColumnInfo, false, false)
155+
if err != nil {
156+
return nil, err
157+
}
158+
row = append(row, value)
159+
extraColumns++
160+
continue
161+
}
162+
163+
if col.State != model.StatePublic {
164+
// only append origin default value for index fetch values
165+
if col.Offset >= len(row) {
166+
value, err := table.GetColOriginDefaultValue(ctx, col.ToInfo())
167+
if err != nil {
168+
return nil, err
169+
}
170+
171+
row = append(row, value)
172+
extraColumns++
173+
}
174+
}
175+
}
151176
// append unique keys and errors
152177
for _, v := range t.Indices() {
153178
if !tables.IsIndexWritable(v) {
@@ -159,12 +184,6 @@ func getKeysNeedCheckOneRow(ctx sessionctx.Context, t table.Table, row []types.D
159184
if t.Meta().IsCommonHandle && v.Meta().Primary {
160185
continue
161186
}
162-
if len(row) < len(t.WritableCols()) && addChangingColTimes == 0 {
163-
if col := tables.FindChangingCol(t.WritableCols(), v.Meta()); col != nil {
164-
row = append(row, row[col.DependencyColumnOffset])
165-
addChangingColTimes++
166-
}
167-
}
168187
colVals, err1 := v.FetchValues(row, nil)
169188
if err1 != nil {
170189
return nil, err1
@@ -194,9 +213,7 @@ func getKeysNeedCheckOneRow(ctx sessionctx.Context, t table.Table, row []types.D
194213
commonHandle: t.Meta().IsCommonHandle,
195214
})
196215
}
197-
if addChangingColTimes == 1 {
198-
row = row[:len(row)-1]
199-
}
216+
row = row[:len(row)-extraColumns]
200217
result = append(result, toBeCheckedRow{
201218
row: row,
202219
handleKey: handleKey,

go.mod

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,4 +265,8 @@ replace (
265265
github.com/dgrijalva/jwt-go => github.com/form3tech-oss/jwt-go v3.2.6-0.20210809144907-32ab6a8243d7+incompatible
266266
github.com/pingcap/tidb/parser => ./parser
267267
go.opencensus.io => go.opencensus.io v0.23.1-0.20220331163232-052120675fac
268+
// TODO: `sourcegraph.com/sourcegraph/appdash` has been archived, and the original host has been removed.
269+
// Please remove these dependencies.
270+
sourcegraph.com/sourcegraph/appdash => github.com/sourcegraph/appdash v0.0.0-20190731080439-ebfcffb1b5c0
271+
sourcegraph.com/sourcegraph/appdash-data => github.com/sourcegraph/appdash-data v0.0.0-20151005221446-73f23eafcf67
268272
)

go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -920,6 +920,10 @@ github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9
920920
github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM=
921921
github.com/soheilhy/cmux v0.1.5 h1:jjzc5WVemNEDTLwv9tlmemhC73tI08BNOIGwBOo10Js=
922922
github.com/soheilhy/cmux v0.1.5/go.mod h1:T7TcVDs9LWfQgPlPsdngu6I6QIoyIFZDDC6sNE1GqG0=
923+
github.com/sourcegraph/appdash v0.0.0-20190731080439-ebfcffb1b5c0 h1:IJ3DuWHPTJrsqtIqjfdmPTELdTFGefvrOa2eTeRBleQ=
924+
github.com/sourcegraph/appdash v0.0.0-20190731080439-ebfcffb1b5c0/go.mod h1:V952P4GGl1v/MMynLwxVdWEbSZJx+n0oOO3ljnez+WU=
925+
github.com/sourcegraph/appdash-data v0.0.0-20151005221446-73f23eafcf67 h1:8ZnTA26bBOoPkAbbitKPgNlpw0Bwt7ZlpYgZWHWJR/w=
926+
github.com/sourcegraph/appdash-data v0.0.0-20151005221446-73f23eafcf67/go.mod h1:tNZjgbYncKL5HxvDULAr/mWDmFz4B7H8yrXEDlnoIiw=
923927
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
924928
github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI=
925929
github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
@@ -1727,8 +1731,4 @@ sigs.k8s.io/structured-merge-diff/v4 v4.2.1/go.mod h1:j/nl6xW8vLS49O8YvXW1ocPhZa
17271731
sigs.k8s.io/yaml v1.1.0/go.mod h1:UJmg0vDUVViEyp3mgSv9WPwZCDxu4rQW1olrI1uml+o=
17281732
sigs.k8s.io/yaml v1.2.0 h1:kr/MCeFWJWTwyaHoR9c8EjH9OumOmoF9YGiZd7lFm/Q=
17291733
sigs.k8s.io/yaml v1.2.0/go.mod h1:yfXDCHCao9+ENCvLSE62v9VSji2MKu5jeNfTrofGhJc=
1730-
sourcegraph.com/sourcegraph/appdash v0.0.0-20190731080439-ebfcffb1b5c0 h1:ucqkfpjg9WzSUubAO62csmucvxl4/JeW3F4I4909XkM=
1731-
sourcegraph.com/sourcegraph/appdash v0.0.0-20190731080439-ebfcffb1b5c0/go.mod h1:hI742Nqp5OhwiqlzhgfbWU4mW4yO10fP+LoT9WOswdU=
1732-
sourcegraph.com/sourcegraph/appdash-data v0.0.0-20151005221446-73f23eafcf67 h1:e1sMhtVq9AfcEy8AXNb8eSg6gbzfdpYhoNqnPJa+GzI=
1733-
sourcegraph.com/sourcegraph/appdash-data v0.0.0-20151005221446-73f23eafcf67/go.mod h1:L5q+DGLGOQFpo1snNEkLOJT2d1YTW66rWNzatr3He1k=
17341734
stathat.com/c/consistent v1.0.0 h1:ezyc51EGcRPJUxfHGSgJjWzJdj3NiMU9pNfLNGiXV0c=

0 commit comments

Comments
 (0)