Skip to content

Commit 40050c1

Browse files
author
James Cor
committed
Merge branch 'main' into james/check
2 parents 71458fc + 220353c commit 40050c1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1230
-434
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/integration_plans.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

enginetest/queries/join_queries.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1161,6 +1161,45 @@ var JoinScriptTests = []ScriptTest{
11611161
},
11621162
},
11631163
},
1164+
{
1165+
// After this change: https://github.com/dolthub/go-mysql-server/pull/3038
1166+
// hash.HashOf takes in a sql.Schema to convert and hash keys, so
1167+
// we need to pass in the schema of the join key.
1168+
// This tests a bug introduced in that same PR where we incorrectly pass in the entire schema,
1169+
// resulting in incorrect conversions.
1170+
Name: "HashLookup on multiple columns with tables with different schemas",
1171+
SetUpScript: []string{
1172+
"create table t1 (i int primary key, k int);",
1173+
"create table t2 (i int primary key, j varchar(1), k int);",
1174+
"insert into t1 values (111111, 111111);",
1175+
"insert into t2 values (111111, 'a', 111111);",
1176+
},
1177+
Assertions: []ScriptTestAssertion{
1178+
{
1179+
Query: "select /*+ HASH_JOIN(t1, t2) */ * from t1 join t2 on t1.i = t2.i and t1.k = t2.k;",
1180+
Expected: []sql.Row{
1181+
{111111, 111111, 111111, "a", 111111},
1182+
},
1183+
},
1184+
},
1185+
},
1186+
{
1187+
Name: "HashLookup on multiple columns with collations",
1188+
SetUpScript: []string{
1189+
"create table t1 (i int primary key, j varchar(128) collate utf8mb4_0900_ai_ci);",
1190+
"create table t2 (i int primary key, j varchar(128) collate utf8mb4_0900_ai_ci);",
1191+
"insert into t1 values (1, 'ABCDE');",
1192+
"insert into t2 values (1, 'abcde');",
1193+
},
1194+
Assertions: []ScriptTestAssertion{
1195+
{
1196+
Query: "select /*+ HASH_JOIN(t1, t2) */ * from t1 join t2 on t1.i = t2.i and t1.j = t2.j;",
1197+
Expected: []sql.Row{
1198+
{1, "ABCDE", 1, "abcde"},
1199+
},
1200+
},
1201+
},
1202+
},
11641203
}
11651204

11661205
var LateralJoinScriptTests = []ScriptTest{

enginetest/queries/queries.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -813,7 +813,7 @@ var QueryTests = []QueryTest{
813813
{
814814
// Assert that SYSDATE() returns different times on each call in a query (unlike NOW())
815815
// Using the maximum precision for fractional seconds, lets us see a difference.
816-
Query: "select now() = sysdate(), sleep(0.5), now(6) < sysdate(6);",
816+
Query: "select sysdate() - now() <= 1, sleep(2), sysdate() - now() > 0;",
817817
Expected: []sql.Row{{true, 0, true}},
818818
},
819819
{
@@ -6092,7 +6092,7 @@ SELECT * FROM cte WHERE d = 2;`,
60926092
Query: `SELECT if(123 = 123, NULL, NULL = 1)`,
60936093
Expected: []sql.Row{{nil}},
60946094
ExpectedColumns: []*sql.Column{
6095-
{Name: "if(123 = 123, NULL, NULL = 1)", Type: types.Int64}, // TODO: this should be getting coerced to bool
6095+
{Name: "if(123 = 123, NULL, NULL = 1)", Type: types.Boolean},
60966096
},
60976097
},
60986098
{

enginetest/queries/script_queries.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8712,6 +8712,42 @@ where
87128712
},
87138713
},
87148714
},
8715+
{
8716+
Name: "tinyint column does not restrict IF or IFNULL output",
8717+
// https://github.com/dolthub/dolt/issues/9321
8718+
SetUpScript: []string{
8719+
"create table t0 (c0 tinyint);",
8720+
"insert into t0 values (null);",
8721+
},
8722+
Assertions: []ScriptTestAssertion{
8723+
{
8724+
Query: "select ifnull(t0.c0, 128) as ref0 from t0",
8725+
Expected: []sql.Row{
8726+
{128},
8727+
},
8728+
},
8729+
{
8730+
Query: "select if(t0.c0 = 1, t0.c0, 128) as ref0 from t0",
8731+
Expected: []sql.Row{{128}},
8732+
},
8733+
},
8734+
},
8735+
{
8736+
Name: "subquery with case insensitive collation",
8737+
Dialect: "mysql",
8738+
SetUpScript: []string{
8739+
"create table tbl (t text) collate=utf8mb4_0900_ai_ci;",
8740+
"insert into tbl values ('abcdef');",
8741+
},
8742+
Assertions: []ScriptTestAssertion{
8743+
{
8744+
Query: "select 'AbCdEf' in (select t from tbl);",
8745+
Expected: []sql.Row{
8746+
{true},
8747+
},
8748+
},
8749+
},
8750+
},
87158751
}
87168752

87178753
var SpatialScriptTests = []ScriptTest{

enginetest/queries/trigger_queries.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3784,6 +3784,42 @@ end;
37843784
},
37853785
},
37863786
},
3787+
3788+
// Invalid triggers
3789+
{
3790+
Name: "insert trigger with subquery projections",
3791+
SetUpScript: []string{
3792+
"create table t (i int);",
3793+
"create trigger trig before insert on t for each row begin replace into t select 1; end;",
3794+
"alter table t add column j int;",
3795+
},
3796+
Assertions: []ScriptTestAssertion{
3797+
{
3798+
Query: "show create trigger trig",
3799+
Expected: []sql.Row{
3800+
{
3801+
"trig",
3802+
"",
3803+
"create trigger trig before insert on t for each row begin replace into t select 1; end",
3804+
sql.Collation_Default.CharacterSet().String(),
3805+
sql.Collation_Default.String(),
3806+
sql.Collation_Default.String(),
3807+
time.Unix(0, 0).UTC(),
3808+
},
3809+
},
3810+
},
3811+
{
3812+
Query: "insert into t values (1, 2);",
3813+
ExpectedErr: sql.ErrInsertIntoMismatchValueCount,
3814+
},
3815+
{
3816+
Query: "drop trigger trig;",
3817+
Expected: []sql.Row{
3818+
{types.NewOkResult(0)},
3819+
},
3820+
},
3821+
},
3822+
},
37873823
}
37883824

37893825
var TriggerCreateInSubroutineTests = []ScriptTest{

enginetest/queries/update_queries.go

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

0 commit comments

Comments
 (0)