Skip to content

Commit

Permalink
Add support for multi table deletes with foreign keys (#15081)
Browse files Browse the repository at this point in the history
Signed-off-by: Manan Gupta <[email protected]>
Signed-off-by: Harshit Gangal <[email protected]>
Co-authored-by: Harshit Gangal <[email protected]>
  • Loading branch information
GuptaManan100 and harshit-gangal authored Jan 31, 2024
1 parent 0ce4416 commit aecf793
Show file tree
Hide file tree
Showing 29 changed files with 572 additions and 255 deletions.
23 changes: 21 additions & 2 deletions go/test/endtoend/vtgate/foreignkey/fk_fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,15 +181,34 @@ func (fz *fuzzer) generateUpdateDMLQuery() string {
}
}

// generateDeleteDMLQuery generates a DELETE query from the parameters for the fuzzer.
func (fz *fuzzer) generateDeleteDMLQuery() string {
// generateDeleteDMLQuery generates a DELETE query using 1 table from the parameters for the fuzzer.
func (fz *fuzzer) generateSingleDeleteDMLQuery() string {
tableId := rand.Intn(len(fkTables))
idValue := 1 + rand.Intn(fz.maxValForId)
setVarFkChecksVal := fz.getSetVarFkChecksVal()
query := fmt.Sprintf("delete %vfrom %v where id = %v", setVarFkChecksVal, fkTables[tableId], idValue)
return query
}

// generateMultiDeleteDMLQuery generates a DELETE query using 2 tables from the parameters for the fuzzer.
func (fz *fuzzer) generateMultiDeleteDMLQuery() string {
tableId := rand.Intn(len(fkTables))
tableId2 := rand.Intn(len(fkTables))
idValue := 1 + rand.Intn(fz.maxValForId)
setVarFkChecksVal := fz.getSetVarFkChecksVal()
query := fmt.Sprintf("delete %v%v from %v join %v using (id) where %v.id = %v", setVarFkChecksVal, fkTables[tableId], fkTables[tableId], fkTables[tableId2], fkTables[tableId], idValue)
return query
}

// generateDeleteDMLQuery generates a DELETE query from the parameters for the fuzzer.
func (fz *fuzzer) generateDeleteDMLQuery() string {
multiTableDelete := rand.Intn(2) + 1
if multiTableDelete == 1 {
return fz.generateSingleDeleteDMLQuery()
}
return fz.generateMultiDeleteDMLQuery()
}

// start starts running the fuzzer.
func (fz *fuzzer) start(t *testing.T, sharded bool) {
// We mark the fuzzer thread to be running now.
Expand Down
15 changes: 15 additions & 0 deletions go/test/endtoend/vtgate/foreignkey/fk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,21 @@ func TestFkScenarios(t *testing.T) {
assertionQueries: []string{
"select * from fk_t20 order by id",
},
}, {
name: "Multi Table Delete success",
dataQueries: []string{
"insert into fk_t15(id, col) values (1, 7), (2, 9)",
"insert into fk_t16(id, col) values (1, 7), (2, 9)",
"insert into fk_t17(id, col) values (1, 7)",
"insert into fk_t19(id, col) values (1, 7)",
},
dmlQuery: "delete fk_t15 from fk_t15 join fk_t17 using id",
assertionQueries: []string{
"select * from fk_t15 order by id",
"select * from fk_t16 order by id",
"select * from fk_t17 order by id",
"select * from fk_t19 order by id",
},
},
}

Expand Down
4 changes: 2 additions & 2 deletions go/vt/sqlparser/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ type (
With *With
Comments *ParsedComments
Ignore Ignore
TableExprs TableExprs
TableExprs []TableExpr
Exprs UpdateExprs
Where *Where
OrderBy OrderBy
Expand All @@ -365,7 +365,7 @@ type (
With *With
Ignore Ignore
Comments *ParsedComments
TableExprs TableExprs
TableExprs []TableExpr
Targets TableNames
Partitions Partitions
Where *Where
Expand Down
28 changes: 14 additions & 14 deletions go/vt/sqlparser/ast_clone.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 20 additions & 4 deletions go/vt/sqlparser/ast_copy_on_rewrite.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 15 additions & 15 deletions go/vt/sqlparser/ast_equals.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 14 additions & 4 deletions go/vt/sqlparser/ast_format.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,14 @@ func (node *Update) Format(buf *TrackedBuffer) {
if node.With != nil {
buf.astPrintf(node, "%v", node.With)
}
buf.astPrintf(node, "update %v%s%v set %v%v%v%v",
node.Comments, node.Ignore.ToString(), node.TableExprs,
node.Exprs, node.Where, node.OrderBy, node.Limit)
buf.astPrintf(node, "update %v%s",
node.Comments, node.Ignore.ToString())
prefix := ""
for _, expr := range node.TableExprs {
buf.astPrintf(node, "%s%v", prefix, expr)
prefix = ", "
}
buf.astPrintf(node, " set %v%v%v%v", node.Exprs, node.Where, node.OrderBy, node.Limit)
}

// Format formats the node.
Expand All @@ -175,7 +180,12 @@ func (node *Delete) Format(buf *TrackedBuffer) {
if node.Targets != nil && !node.isSingleAliasExpr() {
buf.astPrintf(node, "%v ", node.Targets)
}
buf.astPrintf(node, "from %v%v%v%v%v", node.TableExprs, node.Partitions, node.Where, node.OrderBy, node.Limit)
prefix := "from "
for _, expr := range node.TableExprs {
buf.astPrintf(node, "%s%v", prefix, expr)
prefix = ", "
}
buf.astPrintf(node, "%v%v%v%v", node.Partitions, node.Where, node.OrderBy, node.Limit)
}

// Format formats the node.
Expand Down
20 changes: 12 additions & 8 deletions go/vt/sqlparser/ast_format_fast.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion go/vt/sqlparser/ast_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2583,7 +2583,7 @@ func (node *Delete) isSingleAliasExpr() bool {
return isAliasExpr
}

func (node TableExprs) MultiTable() bool {
func MultiTable(node []TableExpr) bool {
if len(node) > 1 {
return true
}
Expand Down
24 changes: 16 additions & 8 deletions go/vt/sqlparser/ast_rewrite.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 8 additions & 4 deletions go/vt/sqlparser/ast_visit.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit aecf793

Please sign in to comment.