Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for multi table deletes with foreign keys #15081

Merged
merged 4 commits into from
Jan 31, 2024
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
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 @@
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)

Check warning on line 168 in go/vt/sqlparser/ast_format.go

View check run for this annotation

Codecov / codecov/patch

go/vt/sqlparser/ast_format.go#L161-L168

Added lines #L161 - L168 were not covered by tests
}

// Format formats the node.
Expand All @@ -175,7 +180,12 @@
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)

Check warning on line 188 in go/vt/sqlparser/ast_format.go

View check run for this annotation

Codecov / codecov/patch

go/vt/sqlparser/ast_format.go#L183-L188

Added lines #L183 - L188 were not covered by tests
}

// 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 @@ -2151,7 +2151,7 @@
}

// RemoveKeyspaceInCol removes the Qualifier.Qualifier on all ColNames in the AST
func RemoveKeyspaceInCol(in SQLNode) {

Check warning on line 2154 in go/vt/sqlparser/ast_funcs.go

View check run for this annotation

Codecov / codecov/patch

go/vt/sqlparser/ast_funcs.go#L2154

Added line #L2154 was not covered by tests
// Walk will only return an error if we return an error from the inner func. safe to ignore here
_ = Walk(func(node SQLNode) (kontinue bool, err error) {
if col, ok := node.(*ColName); ok && col.Qualifier.Qualifier.NotEmpty() {
Expand All @@ -2176,20 +2176,20 @@
}

// RemoveKeyspace removes the Qualifier.Qualifier on all ColNames and Qualifier on all TableNames in the AST
func RemoveKeyspace(in SQLNode) {
Rewrite(in, nil, func(cursor *Cursor) bool {
switch expr := cursor.Node().(type) {
case *ColName:
if expr.Qualifier.Qualifier.NotEmpty() {
expr.Qualifier.Qualifier = NewIdentifierCS("")
}
case TableName:
if expr.Qualifier.NotEmpty() {
expr.Qualifier = NewIdentifierCS("")
cursor.Replace(expr)
}

Check warning on line 2190 in go/vt/sqlparser/ast_funcs.go

View check run for this annotation

Codecov / codecov/patch

go/vt/sqlparser/ast_funcs.go#L2179-L2190

Added lines #L2179 - L2190 were not covered by tests
}
return true

Check warning on line 2192 in go/vt/sqlparser/ast_funcs.go

View check run for this annotation

Codecov / codecov/patch

go/vt/sqlparser/ast_funcs.go#L2192

Added line #L2192 was not covered by tests
})
}

Expand Down Expand Up @@ -2566,7 +2566,7 @@
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
Loading