Skip to content

Commit d287d2d

Browse files
author
James Cor
committed
feedback
2 parents ec71386 + 9d8e43b commit d287d2d

File tree

18 files changed

+512
-129
lines changed

18 files changed

+512
-129
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/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: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8712,6 +8712,26 @@ 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+
},
87158735
{
87168736
Name: "subquery with case insensitive collation",
87178737
Dialect: "mysql",

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: 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);",

server/handler_test.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ func TestHandlerOutput(t *testing.T) {
212212
})
213213
require.NoError(t, err)
214214
require.Equal(t, 1, len(result.Rows))
215-
require.Equal(t, sqltypes.Int64, result.Rows[0][0].Type())
215+
require.Equal(t, sqltypes.Int16, result.Rows[0][0].Type())
216216
require.Equal(t, []byte("456"), result.Rows[0][0].ToBytes())
217217
})
218218
}
@@ -471,7 +471,8 @@ func TestHandlerComPrepareExecute(t *testing.T) {
471471
},
472472
},
473473
schema: []*query.Field{
474-
{Name: "c1", OrgName: "c1", Table: "test", OrgTable: "test", Database: "test", Type: query.Type_INT32, Charset: uint32(sql.CharacterSet_utf8mb4), ColumnLength: 11, Flags: uint32(query.MySqlFlag_NOT_NULL_FLAG)},
474+
{Name: "c1", OrgName: "c1", Table: "test", OrgTable: "test", Database: "test", Type: query.Type_INT32,
475+
Charset: uint32(sql.CharacterSet_utf8mb4), ColumnLength: 11, Flags: uint32(query.MySqlFlag_NOT_NULL_FLAG)},
475476
},
476477
expected: []sql.Row{
477478
{0}, {1}, {2}, {3}, {4},
@@ -550,7 +551,8 @@ func TestHandlerComPrepareExecuteWithPreparedDisabled(t *testing.T) {
550551
},
551552
},
552553
schema: []*query.Field{
553-
{Name: "c1", OrgName: "c1", Table: "test", OrgTable: "test", Database: "test", Type: query.Type_INT32, Charset: uint32(sql.CharacterSet_utf8mb4), ColumnLength: 11, Flags: uint32(query.MySqlFlag_NOT_NULL_FLAG)},
554+
{Name: "c1", OrgName: "c1", Table: "test", OrgTable: "test", Database: "test", Type: query.Type_INT32,
555+
Charset: uint32(sql.CharacterSet_utf8mb4), ColumnLength: 11, Flags: uint32(query.MySqlFlag_NOT_NULL_FLAG)},
554556
},
555557
expected: []sql.Row{
556558
{0}, {1}, {2}, {3}, {4},
@@ -567,7 +569,8 @@ func TestHandlerComPrepareExecuteWithPreparedDisabled(t *testing.T) {
567569
BindVars: nil,
568570
},
569571
schema: []*query.Field{
570-
{Name: "a", OrgName: "a", Table: "", OrgTable: "", Database: "", Type: query.Type_INT16, Charset: uint32(sql.CharacterSet_utf8mb4), ColumnLength: 6, Flags: uint32(query.MySqlFlag_NOT_NULL_FLAG)},
572+
{Name: "a", OrgName: "a", Table: "", OrgTable: "", Database: "", Type: query.Type_INT16,
573+
Charset: uint32(sql.CharacterSet_utf8mb4), ColumnLength: 6, Flags: uint32(query.MySqlFlag_NOT_NULL_FLAG)},
571574
},
572575
expected: []sql.Row{
573576
{1000},
@@ -584,7 +587,8 @@ func TestHandlerComPrepareExecuteWithPreparedDisabled(t *testing.T) {
584587
BindVars: nil,
585588
},
586589
schema: []*query.Field{
587-
{Name: "a", OrgName: "a", Table: "", OrgTable: "", Database: "", Type: query.Type_INT16, Charset: uint32(sql.CharacterSet_utf8mb4), ColumnLength: 6, Flags: uint32(query.MySqlFlag_NOT_NULL_FLAG)},
590+
{Name: "a", OrgName: "a", Table: "", OrgTable: "", Database: "", Type: query.Type_INT16,
591+
Charset: uint32(sql.CharacterSet_utf8mb4), ColumnLength: 6, Flags: uint32(query.MySqlFlag_NOT_NULL_FLAG)},
588592
},
589593
expected: []sql.Row{
590594
{-129},

0 commit comments

Comments
 (0)