Skip to content
Merged
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
2 changes: 1 addition & 1 deletion go/vt/vtgate/planbuilder/operators/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ func buildChildUpdOpForSetNull(ctx *plancontext.PlanningContext, fk vindexes.Chi
if !colSetToNull {
childWhereExpr = &sqlparser.AndExpr{
Left: childWhereExpr,
Right: sqlparser.NewComparisonExpr(sqlparser.NotInOp, valTuple, updateValues, nil),
Right: sqlparser.NewComparisonExpr(sqlparser.NotInOp, valTuple, sqlparser.ValTuple{updateValues}, nil),
}
}
childUpdStmt := &sqlparser.Update{
Expand Down
5 changes: 5 additions & 0 deletions go/vt/vtgate/planbuilder/plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ func setFks(t *testing.T, vschema *vindexes.VSchema) {
// u_tbl8(col8) -> u_tbl6(col6) Cascade Null.
// u_tbl4(col4) -> u_tbl7(col7) Cascade Cascade.
// u_tbl9(col9) -> u_tbl4(col4) Restrict Restrict.
// u_multicol_tbl2(cola, colb) -> u_multicol_tbl1(cola, colb) Null Null.
// u_multicol_tbl3(cola, colb) -> u_multicol_tbl2(cola, colb) Cascade Cascade.

_ = vschema.AddForeignKey("unsharded_fk_allow", "u_tbl2", createFkDefinition([]string{"col2"}, "u_tbl1", []string{"col1"}, sqlparser.Cascade, sqlparser.Cascade))
_ = vschema.AddForeignKey("unsharded_fk_allow", "u_tbl9", createFkDefinition([]string{"col9"}, "u_tbl1", []string{"col1"}, sqlparser.SetNull, sqlparser.NoAction))
Expand All @@ -167,6 +169,9 @@ func setFks(t *testing.T, vschema *vindexes.VSchema) {
_ = vschema.AddForeignKey("unsharded_fk_allow", "u_tbl4", createFkDefinition([]string{"col4"}, "u_tbl7", []string{"col7"}, sqlparser.Cascade, sqlparser.Cascade))
_ = vschema.AddForeignKey("unsharded_fk_allow", "u_tbl9", createFkDefinition([]string{"col9"}, "u_tbl4", []string{"col4"}, sqlparser.Restrict, sqlparser.Restrict))
_ = vschema.AddForeignKey("unsharded_fk_allow", "u_tbl", createFkDefinition([]string{"col"}, "sharded_fk_allow.s_tbl", []string{"col"}, sqlparser.Restrict, sqlparser.Restrict))

_ = vschema.AddForeignKey("unsharded_fk_allow", "u_multicol_tbl2", createFkDefinition([]string{"cola", "colb"}, "u_multicol_tbl1", []string{"cola", "colb"}, sqlparser.SetNull, sqlparser.SetNull))
_ = vschema.AddForeignKey("unsharded_fk_allow", "u_multicol_tbl3", createFkDefinition([]string{"cola", "colb"}, "u_multicol_tbl2", []string{"cola", "colb"}, sqlparser.Cascade, sqlparser.Cascade))
}
}

Expand Down
112 changes: 103 additions & 9 deletions go/vt/vtgate/planbuilder/testdata/foreignkey_cases.json
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@
"Cols": [
0
],
"Query": "update u_tbl3 set col3 = null where (col3) in ::fkc_vals and (col3) not in ('bar')",
"Query": "update u_tbl3 set col3 = null where (col3) in ::fkc_vals and (col3) not in (('bar'))",
"Table": "u_tbl3"
},
{
Expand Down Expand Up @@ -439,7 +439,7 @@
"Cols": [
0
],
"Query": "update tbl4 set t4col4 = null where (t4col4) in ::fkc_vals and (t4col4) not in ('foo')",
"Query": "update tbl4 set t4col4 = null where (t4col4) in ::fkc_vals and (t4col4) not in (('foo'))",
"Table": "tbl4"
},
{
Expand Down Expand Up @@ -693,7 +693,7 @@
"Cols": [
0
],
"Query": "update u_tbl3 set col3 = null where (col3) in ::fkc_vals1 and (col3) not in ('foo')",
"Query": "update u_tbl3 set col3 = null where (col3) in ::fkc_vals1 and (col3) not in (('foo'))",
"Table": "u_tbl3"
},
{
Expand Down Expand Up @@ -727,7 +727,7 @@
"Sharded": false
},
"FieldQuery": "select col9 from u_tbl9 where 1 != 1",
"Query": "select col9 from u_tbl9 where (col9) in ::fkc_vals2 and (col9) not in ('foo') for update",
"Query": "select col9 from u_tbl9 where (col9) in ::fkc_vals2 and (col9) not in (('foo')) for update",
"Table": "u_tbl9"
},
{
Expand Down Expand Up @@ -755,7 +755,7 @@
"Sharded": false
},
"TargetTabletType": "PRIMARY",
"Query": "update u_tbl9 set col9 = null where (col9) in ::fkc_vals2 and (col9) not in ('foo')",
"Query": "update u_tbl9 set col9 = null where (col9) in ::fkc_vals2 and (col9) not in (('foo'))",
"Table": "u_tbl9"
}
]
Expand Down Expand Up @@ -832,7 +832,7 @@
"Cols": [
0
],
"Query": "update u_tbl3 set col3 = null where (col3) in ::fkc_vals and (col3) not in (2)",
"Query": "update u_tbl3 set col3 = null where (col3) in ::fkc_vals and (col3) not in ((2))",
"Table": "u_tbl3"
},
{
Expand Down Expand Up @@ -909,7 +909,7 @@
"Cols": [
0
],
"Query": "update u_tbl3 set col3 = null where (col3) in ::fkc_vals1 and (col3) not in (2)",
"Query": "update u_tbl3 set col3 = null where (col3) in ::fkc_vals1 and (col3) not in ((2))",
"Table": "u_tbl3"
},
{
Expand Down Expand Up @@ -943,7 +943,7 @@
"Sharded": false
},
"FieldQuery": "select col9 from u_tbl9 where 1 != 1",
"Query": "select col9 from u_tbl9 where (col9) in ::fkc_vals2 and (col9) not in (2) for update",
"Query": "select col9 from u_tbl9 where (col9) in ::fkc_vals2 and (col9) not in ((2)) for update",
"Table": "u_tbl9"
},
{
Expand Down Expand Up @@ -971,7 +971,7 @@
"Sharded": false
},
"TargetTabletType": "PRIMARY",
"Query": "update u_tbl9 set col9 = null where (col9) in ::fkc_vals2 and (col9) not in (2)",
"Query": "update u_tbl9 set col9 = null where (col9) in ::fkc_vals2 and (col9) not in ((2))",
"Table": "u_tbl9"
}
]
Expand Down Expand Up @@ -1295,5 +1295,99 @@
"comment": "replace with fk reference unsupported",
"query": "replace into u_tbl1 (id, col1) values (1, 2)",
"plan": "VT12001: unsupported: REPLACE INTO with foreign keys"
},
{
"comment": "update on a multicol foreign key that set nulls and then cascades",
"query": "update u_multicol_tbl1 set cola = 1, colb = 2 where id = 3",
"plan": {
"QueryType": "UPDATE",
"Original": "update u_multicol_tbl1 set cola = 1, colb = 2 where id = 3",
"Instructions": {
"OperatorType": "FkCascade",
"Inputs": [
{
"InputName": "Selection",
"OperatorType": "Route",
"Variant": "Unsharded",
"Keyspace": {
"Name": "unsharded_fk_allow",
"Sharded": false
},
"FieldQuery": "select cola, colb from u_multicol_tbl1 where 1 != 1",
"Query": "select cola, colb from u_multicol_tbl1 where id = 3 lock in share mode",
"Table": "u_multicol_tbl1"
},
{
"InputName": "CascadeChild-1",
"OperatorType": "FkCascade",
"BvName": "fkc_vals",
"Cols": [
0,
1
],
"Inputs": [
{
"InputName": "Selection",
"OperatorType": "Route",
"Variant": "Unsharded",
"Keyspace": {
"Name": "unsharded_fk_allow",
"Sharded": false
},
"FieldQuery": "select cola, colb from u_multicol_tbl2 where 1 != 1",
"Query": "select cola, colb from u_multicol_tbl2 where (cola, colb) in ::fkc_vals and (cola, colb) not in ((1, 2)) for update",
"Table": "u_multicol_tbl2"
},
{
"InputName": "CascadeChild-1",
"OperatorType": "Update",
"Variant": "Unsharded",
"Keyspace": {
"Name": "unsharded_fk_allow",
"Sharded": false
},
"TargetTabletType": "PRIMARY",
"BvName": "fkc_vals1",
"Cols": [
0,
1
],
"Query": "update /*+ SET_VAR(foreign_key_checks=OFF) */ u_multicol_tbl3 set cola = null, colb = null where (cola, colb) in ::fkc_vals1",
"Table": "u_multicol_tbl3"
},
{
"InputName": "Parent",
"OperatorType": "Update",
"Variant": "Unsharded",
"Keyspace": {
"Name": "unsharded_fk_allow",
"Sharded": false
},
"TargetTabletType": "PRIMARY",
"Query": "update u_multicol_tbl2 set cola = null, colb = null where (cola, colb) in ::fkc_vals and (cola, colb) not in ((1, 2))",
"Table": "u_multicol_tbl2"
}
]
},
{
"InputName": "Parent",
"OperatorType": "Update",
"Variant": "Unsharded",
"Keyspace": {
"Name": "unsharded_fk_allow",
"Sharded": false
},
"TargetTabletType": "PRIMARY",
"Query": "update u_multicol_tbl1 set cola = 1, colb = 2 where id = 3",
"Table": "u_multicol_tbl1"
}
]
},
"TablesUsed": [
"unsharded_fk_allow.u_multicol_tbl1",
"unsharded_fk_allow.u_multicol_tbl2",
"unsharded_fk_allow.u_multicol_tbl3"
]
}
}
]
5 changes: 4 additions & 1 deletion go/vt/vtgate/planbuilder/testdata/vschemas/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -757,7 +757,10 @@
"u_tbl7": {},
"u_tbl8": {},
"u_tbl9": {},
"u_tbl": {}
"u_tbl": {},
"u_multicol_tbl1": {},
"u_multicol_tbl2": {},
"u_multicol_tbl3": {}
}
}
}
Expand Down