Skip to content

Commit

Permalink
Merge pull request #6327 from planetscale/insert-lookup-bindvar-clashes
Browse files Browse the repository at this point in the history
Make sure to protect insert lookup queries from bind var name clashes (6.0)
  • Loading branch information
deepthi authored Jun 17, 2020
2 parents a4d5c67 + ef7df0f commit 5ba6549
Show file tree
Hide file tree
Showing 11 changed files with 268 additions and 290 deletions.
14 changes: 7 additions & 7 deletions go/vt/vtgate/autocommit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,11 @@ func TestAutocommitUpdateVindexChange(t *testing.T) {
"keyspace_id": sqltypes.BytesBindVariable([]byte("\026k@\264J\272K\326")),
},
}, {
Sql: "insert into name_lastname_keyspace_id_map(name, lastname, keyspace_id) values (:name0, :lastname0, :keyspace_id0)",
Sql: "insert into name_lastname_keyspace_id_map(name, lastname, keyspace_id) values (:name_0, :lastname_0, :keyspace_id_0)",
BindVariables: map[string]*querypb.BindVariable{
"name0": sqltypes.BytesBindVariable([]byte("myname")),
"lastname0": sqltypes.BytesBindVariable([]byte("mylastname")),
"keyspace_id0": sqltypes.BytesBindVariable([]byte("\026k@\264J\272K\326")),
"name_0": sqltypes.BytesBindVariable([]byte("myname")),
"lastname_0": sqltypes.BytesBindVariable([]byte("mylastname")),
"keyspace_id_0": sqltypes.BytesBindVariable([]byte("\026k@\264J\272K\326")),
},
}})
testAsTransactionCount(t, "sbclookup", sbclookup, 0)
Expand Down Expand Up @@ -269,10 +269,10 @@ func TestAutocommitInsertLookup(t *testing.T) {
require.NoError(t, err)

testQueries(t, "sbclookup", sbclookup, []*querypb.BoundQuery{{
Sql: "insert into name_user_map(name, user_id) values (:name0, :user_id0)",
Sql: "insert into name_user_map(name, user_id) values (:name_0, :user_id_0)",
BindVariables: map[string]*querypb.BindVariable{
"name0": sqltypes.BytesBindVariable([]byte("myname")),
"user_id0": sqltypes.Uint64BindVariable(1),
"name_0": sqltypes.BytesBindVariable([]byte("myname")),
"user_id_0": sqltypes.Uint64BindVariable(1),
},
}})
testAsTransactionCount(t, "sbclookup", sbclookup, 0)
Expand Down
38 changes: 19 additions & 19 deletions go/vt/vtgate/engine/insert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -538,13 +538,13 @@ func TestInsertShardedOwned(t *testing.T) {
t.Fatal(err)
}
vc.ExpectLog(t, []string{
`Execute insert into lkp2(from1, from2, toc) values(:from10, :from20, :toc0), (:from11, :from21, :toc1), (:from12, :from22, :toc2) ` +
`from10: type:INT64 value:"4" from11: type:INT64 value:"5" from12: type:INT64 value:"6" ` +
`from20: type:INT64 value:"7" from21: type:INT64 value:"8" from22: type:INT64 value:"9" ` +
`toc0: type:VARBINARY value:"\026k@\264J\272K\326" toc1: type:VARBINARY value:"\006\347\352\"\316\222p\217" toc2: type:VARBINARY value:"N\261\220\311\242\372\026\234" true`,
`Execute insert into lkp1(from, toc) values(:from0, :toc0), (:from1, :toc1), (:from2, :toc2) ` +
`from0: type:INT64 value:"10" from1: type:INT64 value:"11" from2: type:INT64 value:"12" ` +
`toc0: type:VARBINARY value:"\026k@\264J\272K\326" toc1: type:VARBINARY value:"\006\347\352\"\316\222p\217" toc2: type:VARBINARY value:"N\261\220\311\242\372\026\234" true`,
`Execute insert into lkp2(from1, from2, toc) values(:from1_0, :from2_0, :toc_0), (:from1_1, :from2_1, :toc_1), (:from1_2, :from2_2, :toc_2) ` +
`from1_0: type:INT64 value:"4" from1_1: type:INT64 value:"5" from1_2: type:INT64 value:"6" ` +
`from2_0: type:INT64 value:"7" from2_1: type:INT64 value:"8" from2_2: type:INT64 value:"9" ` +
`toc_0: type:VARBINARY value:"\026k@\264J\272K\326" toc_1: type:VARBINARY value:"\006\347\352\"\316\222p\217" toc_2: type:VARBINARY value:"N\261\220\311\242\372\026\234" true`,
`Execute insert into lkp1(from, toc) values(:from_0, :toc_0), (:from_1, :toc_1), (:from_2, :toc_2) ` +
`from_0: type:INT64 value:"10" from_1: type:INT64 value:"11" from_2: type:INT64 value:"12" ` +
`toc_0: type:VARBINARY value:"\026k@\264J\272K\326" toc_1: type:VARBINARY value:"\006\347\352\"\316\222p\217" toc_2: type:VARBINARY value:"N\261\220\311\242\372\026\234" true`,
// Based on shardForKsid, values returned will be 20-, -20, 20-.
`ResolveDestinations sharded [value:"0" value:"1" value:"2" ] Destinations:DestinationKeyspaceID(166b40b44aba4bd6),DestinationKeyspaceID(06e7ea22ce92708f),DestinationKeyspaceID(4eb190c9a2fa169c)`,
`ExecuteMultiShard ` +
Expand Down Expand Up @@ -636,7 +636,7 @@ func TestInsertShardedOwnedWithNull(t *testing.T) {
t.Fatal(err)
}
vc.ExpectLog(t, []string{
`Execute insert into lkp1(from, toc) values(:from0, :toc0) from0: toc0: type:VARBINARY ` +
`Execute insert into lkp1(from, toc) values(:from_0, :toc_0) from_0: toc_0: type:VARBINARY ` +
`value:"\026k@\264J\272K\326" true`,
`ResolveDestinations sharded [value:"0" ] Destinations:DestinationKeyspaceID(166b40b44aba4bd6)`,
`ExecuteMultiShard sharded.20-: prefix mid1 suffix ` +
Expand Down Expand Up @@ -732,9 +732,9 @@ func TestInsertShardedGeo(t *testing.T) {
t.Fatal(err)
}
vc.ExpectLog(t, []string{
`Execute insert into id_idx(id, keyspace_id) values(:id0, :keyspace_id0), (:id1, :keyspace_id1) ` +
`id0: type:INT64 value:"1" id1: type:INT64 value:"1" ` +
`keyspace_id0: type:VARBINARY value:"\001\026k@\264J\272K\326" keyspace_id1: type:VARBINARY value:"\377\026k@\264J\272K\326" true`,
`Execute insert into id_idx(id, keyspace_id) values(:id_0, :keyspace_id_0), (:id_1, :keyspace_id_1) ` +
`id_0: type:INT64 value:"1" id_1: type:INT64 value:"1" ` +
`keyspace_id_0: type:VARBINARY value:"\001\026k@\264J\272K\326" keyspace_id_1: type:VARBINARY value:"\377\026k@\264J\272K\326" true`,
`ResolveDestinations sharded [value:"0" value:"1" ] Destinations:DestinationKeyspaceID(01166b40b44aba4bd6),DestinationKeyspaceID(ff166b40b44aba4bd6)`,
`ExecuteMultiShard sharded.20-: prefix mid1 suffix ` +
`{_id_0: type:INT64 value:"1" _id_1: type:INT64 value:"1" ` +
Expand Down Expand Up @@ -904,18 +904,18 @@ func TestInsertShardedIgnoreOwned(t *testing.T) {
`Execute select toc from prim where from1 = :from1 from1: type:INT64 value:"2" false`,
`Execute select toc from prim where from1 = :from1 from1: type:INT64 value:"3" false`,
`Execute select toc from prim where from1 = :from1 from1: type:INT64 value:"4" false`,
`Execute insert ignore into lkp2(from1, from2, toc) values(:from10, :from20, :toc0), (:from11, :from21, :toc1), (:from12, :from22, :toc2) ` +
`from10: type:INT64 value:"5" from11: type:INT64 value:"7" from12: type:INT64 value:"8" ` +
`from20: type:INT64 value:"9" from21: type:INT64 value:"11" from22: type:INT64 value:"12" ` +
`toc0: type:VARBINARY value:"\000" toc1: type:VARBINARY value:"\000" toc2: type:VARBINARY value:"\000" ` +
`Execute insert ignore into lkp2(from1, from2, toc) values(:from1_0, :from2_0, :toc_0), (:from1_1, :from2_1, :toc_1), (:from1_2, :from2_2, :toc_2) ` +
`from1_0: type:INT64 value:"5" from1_1: type:INT64 value:"7" from1_2: type:INT64 value:"8" ` +
`from2_0: type:INT64 value:"9" from2_1: type:INT64 value:"11" from2_2: type:INT64 value:"12" ` +
`toc_0: type:VARBINARY value:"\000" toc_1: type:VARBINARY value:"\000" toc_2: type:VARBINARY value:"\000" ` +
`true`,
// row 2 is out because it didn't map to a ksid.
`Execute select from1 from lkp2 where from1 = :from1 and toc = :toc from1: type:INT64 value:"5" toc: type:VARBINARY value:"\000" false`,
`Execute select from1 from lkp2 where from1 = :from1 and toc = :toc from1: type:INT64 value:"7" toc: type:VARBINARY value:"\000" false`,
`Execute select from1 from lkp2 where from1 = :from1 and toc = :toc from1: type:INT64 value:"8" toc: type:VARBINARY value:"\000" false`,
`Execute insert ignore into lkp1(from, toc) values(:from0, :toc0), (:from1, :toc1) ` +
`from0: type:INT64 value:"13" from1: type:INT64 value:"16" ` +
`toc0: type:VARBINARY value:"\000" toc1: type:VARBINARY value:"\000" ` +
`Execute insert ignore into lkp1(from, toc) values(:from_0, :toc_0), (:from_1, :toc_1) ` +
`from_0: type:INT64 value:"13" from_1: type:INT64 value:"16" ` +
`toc_0: type:VARBINARY value:"\000" toc_1: type:VARBINARY value:"\000" ` +
`true`,
// row 3 is out because it failed Verify. Only two verifications from lkp1.
`Execute select from from lkp1 where from = :from and toc = :toc from: type:INT64 value:"13" toc: type:VARBINARY value:"\000" false`,
Expand Down Expand Up @@ -1023,7 +1023,7 @@ func TestInsertShardedIgnoreOwnedWithNull(t *testing.T) {
t.Fatal(err)
}
vc.ExpectLog(t, []string{
`Execute insert ignore into lkp1(from, toc) values(:from0, :toc0) from0: toc0: type:VARBINARY ` +
`Execute insert ignore into lkp1(from, toc) values(:from_0, :toc_0) from_0: toc_0: type:VARBINARY ` +
`value:"\026k@\264J\272K\326" true`,
`Execute select from from lkp1 where from = :from and toc = :toc from: toc: type:VARBINARY value:"\026k@\264J\272K\326" false`,
`ResolveDestinations sharded [value:"0" ] Destinations:DestinationKeyspaceID(166b40b44aba4bd6)`,
Expand Down
Loading

0 comments on commit 5ba6549

Please sign in to comment.