Skip to content

Commit cc8357c

Browse files
authored
Merge pull request #3024 from dolthub/fulghum/updatable
Adding skipped tests for `UPDATE ... JOIN` bugs
2 parents cd45932 + c334f59 commit cc8357c

File tree

3 files changed

+153
-5
lines changed

3 files changed

+153
-5
lines changed

enginetest/enginetests.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ func TestReadOnlyDatabases(t *testing.T, harness ReadOnlyDatabaseHarness) {
496496

497497
for _, querySet := range [][]queries.WriteQueryTest{
498498
queries.InsertQueries,
499-
queries.UpdateTests,
499+
queries.UpdateWriteQueryTests,
500500
queries.DeleteTests,
501501
queries.ReplaceQueries,
502502
} {
@@ -1352,9 +1352,12 @@ func TestReplaceIntoErrors(t *testing.T, harness Harness) {
13521352

13531353
func TestUpdate(t *testing.T, harness Harness) {
13541354
harness.Setup(setup.MydbData, setup.MytableData, setup.Mytable_del_idxData, setup.FloattableData, setup.NiltableData, setup.TypestableData, setup.Pk_tablesData, setup.OthertableData, setup.TabletestData)
1355-
for _, tt := range queries.UpdateTests {
1355+
for _, tt := range queries.UpdateWriteQueryTests {
13561356
RunWriteQueryTest(t, harness, tt)
13571357
}
1358+
for _, tt := range queries.UpdateScriptTests {
1359+
TestScript(t, harness, tt)
1360+
}
13581361
}
13591362

13601363
func TestUpdateIgnore(t *testing.T, harness Harness) {
@@ -1421,9 +1424,12 @@ func TestDelete(t *testing.T, harness Harness) {
14211424

14221425
func TestUpdateQueriesPrepared(t *testing.T, harness Harness) {
14231426
harness.Setup(setup.MydbData, setup.MytableData, setup.Mytable_del_idxData, setup.OthertableData, setup.TypestableData, setup.Pk_tablesData, setup.FloattableData, setup.NiltableData, setup.TabletestData)
1424-
for _, tt := range queries.UpdateTests {
1427+
for _, tt := range queries.UpdateWriteQueryTests {
14251428
runWriteQueryTestPrepared(t, harness, tt)
14261429
}
1430+
for _, tt := range queries.UpdateScriptTests {
1431+
TestScriptPrepared(t, harness, tt)
1432+
}
14271433
}
14281434

14291435
func TestDeleteQueriesPrepared(t *testing.T, harness Harness) {

enginetest/queries/check_scripts.go

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ var ChecksOnUpdateScriptTests = []ScriptTest{
495495
},
496496
},
497497
{
498-
Name: "Update join updates",
498+
Name: "Update join - single table",
499499
SetUpScript: []string{
500500
"CREATE TABLE sales (year_built int primary key, CONSTRAINT `valid_year_built` CHECK (year_built <= 2022));",
501501
"INSERT INTO sales VALUES (1981);",
@@ -535,6 +535,45 @@ var ChecksOnUpdateScriptTests = []ScriptTest{
535535
},
536536
},
537537
},
538+
{
539+
Name: "Update join - multiple tables",
540+
SetUpScript: []string{
541+
"CREATE TABLE sales (year_built int primary key, CONSTRAINT `valid_year_built` CHECK (year_built <= 2022));",
542+
"INSERT INTO sales VALUES (1981);",
543+
"CREATE TABLE locations (state char(2) primary key, CONSTRAINT `state` CHECK (state != 'GA'));",
544+
"INSERT INTO locations VALUES ('WA');",
545+
},
546+
Assertions: []ScriptTestAssertion{
547+
{
548+
Query: "UPDATE sales JOIN locations SET sales.year_built = 2000, locations.state = 'GA';",
549+
ExpectedErr: sql.ErrCheckConstraintViolated,
550+
},
551+
{
552+
Query: "UPDATE sales JOIN locations SET sales.year_built = 2025, locations.state = 'CA';",
553+
ExpectedErr: sql.ErrCheckConstraintViolated,
554+
},
555+
{
556+
Query: "select * from sales;",
557+
Expected: []sql.Row{{1981}},
558+
},
559+
{
560+
Query: "select * from locations;",
561+
Expected: []sql.Row{{"WA"}},
562+
},
563+
{
564+
Query: "UPDATE sales JOIN locations SET sales.year_built = 2000, locations.state = 'CA';",
565+
Expected: []sql.Row{{types.OkResult{2, 0, plan.UpdateInfo{2, 2, 0}}}},
566+
},
567+
{
568+
Query: "select * from sales;",
569+
Expected: []sql.Row{{2000}},
570+
},
571+
{
572+
Query: "select * from locations;",
573+
Expected: []sql.Row{{"CA"}},
574+
},
575+
},
576+
},
538577
}
539578

540579
var DisallowedCheckConstraintsScripts = []ScriptTest{

enginetest/queries/update_queries.go

Lines changed: 104 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
"github.com/dolthub/vitess/go/mysql"
2525
)
2626

27-
var UpdateTests = []WriteQueryTest{
27+
var UpdateWriteQueryTests = []WriteQueryTest{
2828
{
2929
WriteQuery: "UPDATE mytable SET s = 'updated';",
3030
ExpectedWriteResult: []sql.Row{{NewUpdateResult(3, 3)}},
@@ -470,6 +470,109 @@ var UpdateTests = []WriteQueryTest{
470470
},
471471
}
472472

473+
var UpdateScriptTests = []ScriptTest{
474+
{
475+
Dialect: "mysql",
476+
Name: "UPDATE join – single table, with FK constraint",
477+
SetUpScript: []string{
478+
"CREATE TABLE customers (id INT PRIMARY KEY, name TEXT);",
479+
"CREATE TABLE orders (id INT PRIMARY KEY, customer_id INT, amount INT, FOREIGN KEY (customer_id) REFERENCES customers(id));",
480+
"INSERT INTO customers VALUES (1, 'Alice'), (2, 'Bob');",
481+
"INSERT INTO orders VALUES (101, 1, 50), (102, 2, 75);",
482+
},
483+
Assertions: []ScriptTestAssertion{
484+
{
485+
// TODO: Foreign key constraints are not honored for UDPATE ... JOIN statements
486+
Skip: true,
487+
Query: "UPDATE orders o JOIN customers c ON o.customer_id = c.id SET o.customer_id = 123 where o.customer_id != 1;",
488+
ExpectedErr: sql.ErrCheckConstraintViolated,
489+
},
490+
{
491+
Query: "SELECT * FROM orders;",
492+
Expected: []sql.Row{
493+
{101, 1, 50}, {102, 2, 75},
494+
},
495+
},
496+
},
497+
},
498+
{
499+
Dialect: "mysql",
500+
Name: "UPDATE join – multiple tables, with FK constraint",
501+
SetUpScript: []string{
502+
"CREATE TABLE parent1 (id INT PRIMARY KEY);",
503+
"CREATE TABLE parent2 (id INT PRIMARY KEY);",
504+
"CREATE TABLE child1 (id INT PRIMARY KEY, p1_id INT, FOREIGN KEY (p1_id) REFERENCES parent1(id));",
505+
"CREATE TABLE child2 (id INT PRIMARY KEY, p2_id INT, FOREIGN KEY (p2_id) REFERENCES parent2(id));",
506+
"INSERT INTO parent1 VALUES (1), (3);",
507+
"INSERT INTO parent2 VALUES (1), (3);",
508+
"INSERT INTO child1 VALUES (10, 1);",
509+
"INSERT INTO child2 VALUES (20, 1);",
510+
},
511+
Assertions: []ScriptTestAssertion{
512+
{
513+
// TODO: Foreign key constraints are not honored for UDPATE ... JOIN statements
514+
Skip: true,
515+
Query: `UPDATE child1 c1
516+
JOIN child2 c2 ON c1.id = 10 AND c2.id = 20
517+
SET c1.p1_id = 999, c2.p2_id = 3;`,
518+
ExpectedErr: sql.ErrForeignKeyChildViolation,
519+
},
520+
{
521+
// TODO: Foreign key constraints are not honored for UDPATE ... JOIN statements
522+
Skip: true,
523+
Query: `UPDATE child1 c1
524+
JOIN child2 c2 ON c1.id = 10 AND c2.id = 20
525+
SET c1.p1_id = 3, c2.p2_id = 999;`,
526+
ExpectedErr: sql.ErrForeignKeyChildViolation,
527+
},
528+
{
529+
Query: "SELECT * FROM child1;",
530+
Expected: []sql.Row{{10, 1}},
531+
},
532+
{
533+
Query: "SELECT * FROM child2;",
534+
Expected: []sql.Row{{20, 1}},
535+
},
536+
},
537+
},
538+
{
539+
Dialect: "mysql",
540+
Name: "UPDATE join – multiple tables, with trigger",
541+
SetUpScript: []string{
542+
"CREATE TABLE a (id INT PRIMARY KEY, x INT);",
543+
"CREATE TABLE b (id INT PRIMARY KEY, y INT);",
544+
"CREATE TABLE logbook (entry TEXT);",
545+
`CREATE TRIGGER trig_a AFTER UPDATE ON a FOR EACH ROW
546+
BEGIN
547+
INSERT INTO logbook VALUES ('a updated');
548+
END;`,
549+
`CREATE TRIGGER trig_b AFTER UPDATE ON b FOR EACH ROW
550+
BEGIN
551+
INSERT INTO logbook VALUES ('b updated');
552+
END;`,
553+
"INSERT INTO a VALUES (5, 100);",
554+
"INSERT INTO b VALUES (6, 200);",
555+
},
556+
Assertions: []ScriptTestAssertion{
557+
{
558+
Query: `UPDATE a
559+
JOIN b ON a.id = 5 AND b.id = 6
560+
SET a.x = 101, b.y = 201;`,
561+
},
562+
{
563+
// TODO: UPDATE ... JOIN does not properly apply triggers when multiple tables are being updated,
564+
// and will currently only apply triggers from one of the tables.
565+
Skip: true,
566+
Query: "SELECT * FROM logbook ORDER BY entry;",
567+
Expected: []sql.Row{
568+
{"a updated"},
569+
{"b updated"},
570+
},
571+
},
572+
},
573+
},
574+
}
575+
473576
var SpatialUpdateTests = []WriteQueryTest{
474577
{
475578
WriteQuery: "UPDATE point_table SET p = point(123.456,789);",

0 commit comments

Comments
 (0)