Skip to content

Commit 59aac3c

Browse files
committed
Merge branch 'main' into jennifer/domain
2 parents 92312b3 + 54bd6d6 commit 59aac3c

22 files changed

+422
-141
lines changed

engine.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,23 @@ func (e *Engine) Query(ctx *sql.Context, query string) (sql.Schema, sql.RowIter,
253253
return e.QueryWithBindings(ctx, query, nil, nil, nil)
254254
}
255255

256+
func clearWarnings(ctx *sql.Context, node sql.Node) {
257+
if ctx == nil || ctx.Session == nil {
258+
return
259+
}
260+
261+
switch n := node.(type) {
262+
case *plan.Offset, *plan.Limit:
263+
// `show warning limit x offset y` is valid, so we need to recurse
264+
clearWarnings(ctx, n.Children()[0])
265+
case plan.ShowWarnings:
266+
// ShowWarnings should not clear the warnings, but should still reset the warning count.
267+
ctx.ClearWarningCount()
268+
default:
269+
ctx.ClearWarnings()
270+
}
271+
}
272+
256273
func bindingsToExprs(bindings map[string]*querypb.BindVariable) (map[string]sql.Expression, error) {
257274
res := make(map[string]sql.Expression, len(bindings))
258275
for k, v := range bindings {
@@ -387,10 +404,18 @@ func (e *Engine) QueryWithBindings(ctx *sql.Context, query string, parsed sqlpar
387404
return nil, nil, nil, err
388405
}
389406

407+
// planbuilding can produce warnings, so we need to preserve them
408+
numPrevWarnings := len(ctx.Session.Warnings())
390409
bound, qFlags, err := e.bindQuery(ctx, query, parsed, bindings, binder, qFlags)
391410
if err != nil {
392411
return nil, nil, nil, err
393412
}
413+
newWarnings := ctx.Session.Warnings()[numPrevWarnings:]
414+
clearWarnings(ctx, bound)
415+
// restore new warnings (backwards because they are in reverse order)
416+
for i := len(newWarnings) - 1; i >= 0; i-- {
417+
ctx.Session.Warn(newWarnings[i])
418+
}
394419

395420
analyzed, err := e.analyzeNode(ctx, query, bound, qFlags)
396421
if err != nil {

enginetest/queries/foreign_key_queries.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2581,6 +2581,41 @@ var ForeignKeyTests = []ScriptTest{
25812581
},
25822582
},
25832583
},
2584+
{
2585+
// https://github.com/dolthub/dolt/issues/7857
2586+
Name: "partial foreign key update",
2587+
SetUpScript: []string{
2588+
"create table parent1 (i int primary key);",
2589+
"create table child1 (" +
2590+
"i int primary key, " +
2591+
"j int, " +
2592+
"k int, " +
2593+
"index(j), " +
2594+
"index(k), " +
2595+
"foreign key (j) references parent1 (i)," +
2596+
"foreign key (k) references parent1 (i));",
2597+
"insert into parent1 values (1);",
2598+
"insert into child1 values (100, 1, 1);",
2599+
"set foreign_key_checks = 0;",
2600+
"insert into child1 values (101, 2, 2);",
2601+
"set foreign_key_checks = 1;",
2602+
},
2603+
Assertions: []ScriptTestAssertion{
2604+
{
2605+
Query: "update child1 set j = 1 where i = 101;",
2606+
Expected: []sql.Row{
2607+
{types.OkResult{RowsAffected: 1, Info: plan.UpdateInfo{Matched: 1, Updated: 1}}},
2608+
},
2609+
},
2610+
{
2611+
Query: "select * from child1",
2612+
Expected: []sql.Row{
2613+
{100, 1, 1},
2614+
{101, 1, 2},
2615+
},
2616+
},
2617+
},
2618+
},
25842619
}
25852620

25862621
var CreateForeignKeyTests = []ScriptTest{

enginetest/queries/information_schema_queries.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,18 +1158,27 @@ FROM INFORMATION_SCHEMA.TRIGGERS WHERE trigger_schema = 'mydb'`,
11581158
SetUpScript: []string{
11591159
"USE foo",
11601160
"drop table othertable",
1161-
"CREATE TABLE t (i int primary key, j int)",
1162-
"CREATE VIEW v as select i + 1, j * 2, mod(i, j) from t",
1161+
"CREATE TABLE t (i int primary key, j int default (uuid_to_bin(uuid())));",
1162+
"CREATE VIEW v as select i + 1, j, j * 2, mod(i, j) from t;",
1163+
"create table tt (ii int primary key, jj int default (pow(1, 2)));",
1164+
"create view vv as select * from t join tt where i = ii;",
11631165
},
11641166
Assertions: []ScriptTestAssertion{
11651167
{
11661168
Query: "SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema = 'foo'",
11671169
Expected: []sql.Row{
11681170
{"def", "foo", "t", "i", uint32(1), nil, "NO", "int", nil, nil, 10, 0, nil, nil, nil, "int", "PRI", "", "insert,references,select,update", "", "", nil},
1169-
{"def", "foo", "t", "j", uint32(2), nil, "YES", "int", nil, nil, 10, 0, nil, nil, nil, "int", "", "", "insert,references,select,update", "", "", nil},
1171+
{"def", "foo", "t", "j", uint32(2), "UUID_TO_BIN(uuid())", "YES", "int", nil, nil, 10, 0, nil, nil, nil, "int", "", "DEFAULT_GENERATED", "insert,references,select,update", "", "", nil},
1172+
{"def", "foo", "tt", "ii", uint32(1), nil, "NO", "int", nil, nil, 10, 0, nil, nil, nil, "int", "PRI", "", "insert,references,select,update", "", "", nil},
1173+
{"def", "foo", "tt", "jj", uint32(2), "power(1, 2)", "YES", "int", nil, nil, 10, 0, nil, nil, nil, "int", "", "DEFAULT_GENERATED", "insert,references,select,update", "", "", nil},
11701174
{"def", "foo", "v", "i + 1", uint32(1), nil, "NO", "bigint", nil, nil, 19, 0, nil, nil, nil, "bigint", "", "", "insert,references,select,update", "", "", nil},
1171-
{"def", "foo", "v", "j * 2", uint32(2), nil, "YES", "bigint", nil, nil, 19, 0, nil, nil, nil, "bigint", "", "", "insert,references,select,update", "", "", nil},
1172-
{"def", "foo", "v", "mod(i, j)", uint32(3), nil, "YES", "decimal", nil, nil, 10, 0, nil, nil, nil, "decimal(10,0)", "", "", "insert,references,select,update", "", "", nil},
1175+
{"def", "foo", "v", "j", uint32(2), "UUID_TO_BIN(uuid())", "YES", "int", nil, nil, 10, 0, nil, nil, nil, "int", "", "DEFAULT_GENERATED", "insert,references,select,update", "", "", nil},
1176+
{"def", "foo", "v", "j * 2", uint32(3), nil, "YES", "bigint", nil, nil, 19, 0, nil, nil, nil, "bigint", "", "", "insert,references,select,update", "", "", nil},
1177+
{"def", "foo", "v", "mod(i, j)", uint32(4), nil, "YES", "decimal", nil, nil, 10, 0, nil, nil, nil, "decimal(10,0)", "", "", "insert,references,select,update", "", "", nil},
1178+
{"def", "foo", "vv", "i", uint32(1), nil, "NO", "int", nil, nil, 10, 0, nil, nil, nil, "int", "", "", "insert,references,select,update", "", "", nil},
1179+
{"def", "foo", "vv", "j", uint32(2), "UUID_TO_BIN(uuid())", "YES", "int", nil, nil, 10, 0, nil, nil, nil, "int", "", "DEFAULT_GENERATED", "insert,references,select,update", "", "", nil},
1180+
{"def", "foo", "vv", "ii", uint32(3), nil, "NO", "int", nil, nil, 10, 0, nil, nil, nil, "int", "", "", "insert,references,select,update", "", "", nil},
1181+
{"def", "foo", "vv", "jj", uint32(4), "power(1, 2)", "YES", "int", nil, nil, 10, 0, nil, nil, nil, "int", "", "DEFAULT_GENERATED", "insert,references,select,update", "", "", nil},
11731182
},
11741183
},
11751184
},

enginetest/queries/script_queries.go

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7499,6 +7499,22 @@ where
74997499
},
75007500
},
75017501
},
7502+
{
7503+
Name: "coalesce with system types",
7504+
SetUpScript: []string{
7505+
"create table t as select @@admin_port as port1, @@port as port2, COALESCE(@@admin_port, @@port) as\n port3;",
7506+
},
7507+
Assertions: []ScriptTestAssertion{
7508+
{
7509+
Query: "describe t;",
7510+
Expected: []sql.Row{
7511+
{"port1", "bigint", "NO", "", nil, ""},
7512+
{"port2", "bigint", "NO", "", nil, ""},
7513+
{"port3", "bigint", "NO", "", nil, ""},
7514+
},
7515+
},
7516+
},
7517+
},
75027518
}
75037519

75047520
var SpatialScriptTests = []ScriptTest{
@@ -8809,21 +8825,17 @@ var CreateDatabaseScripts = []ScriptTest{
88098825
Expected: []sql.Row{{types.NewOkResult(1)}},
88108826
},
88118827
{
8812-
SkipResultCheckOnServerEngine: true, // tracking issue here, https://github.com/dolthub/dolt/issues/6921. Also for when run with prepares, the warning is added twice
8813-
Query: "SHOW WARNINGS /* 1 */",
8814-
Expected: []sql.Row{{"Warning", 1235, "Setting CHARACTER SET, COLLATION and ENCRYPTION are not supported yet"}},
8828+
Query: "SHOW WARNINGS /* 1 */",
8829+
Expected: []sql.Row{{"Warning", 1235, "Setting CHARACTER SET, COLLATION and ENCRYPTION are not supported yet"}},
88158830
},
88168831
{
88178832
Query: "CREATE DATABASE newtest1db DEFAULT COLLATE binary ENCRYPTION='Y'",
88188833
Expected: []sql.Row{{types.NewOkResult(1)}},
88198834
},
88208835
{
8821-
SkipResultCheckOnServerEngine: true, // tracking issue here, https://github.com/dolthub/dolt/issues/6921.
8822-
// TODO: There should only be one warning (the warnings are not clearing for create database query) AND 'PREPARE' statements should not create warning from its query
88238836
Query: "SHOW WARNINGS /* 2 */",
88248837
Expected: []sql.Row{
88258838
{"Warning", 1235, "Setting CHARACTER SET, COLLATION and ENCRYPTION are not supported yet"},
8826-
{"Warning", 1235, "Setting CHARACTER SET, COLLATION and ENCRYPTION are not supported yet"},
88278839
},
88288840
},
88298841
{
@@ -8939,10 +8951,8 @@ var DropDatabaseScripts = []ScriptTest{
89398951
Expected: []sql.Row{{types.OkResult{RowsAffected: 1}}},
89408952
},
89418953
{
8942-
// TODO: there should not be warning
8943-
// https://github.com/dolthub/dolt/issues/6921
89448954
Query: "SHOW WARNINGS",
8945-
Expected: []sql.Row{{"Note", 1008, "Can't drop database mydb; database doesn't exist "}},
8955+
Expected: []sql.Row{},
89468956
},
89478957
{
89488958
Query: "SELECT DATABASE()",

enginetest/queries/view_queries.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,11 @@ CREATE TABLE tab1 (
203203
SetUpScript: []string{
204204
"create table t (i int primary key, j int default 100);",
205205
"insert into t(i) values (1);",
206+
"create table tt (ii int primary key, jj int default (pow(11, 2)));",
207+
"insert into tt values (1, default), (3, 4);",
206208
"create view v as select * from t;",
207209
"create view v1 as select i, j + 10 as jj from t;",
210+
"create view vv as select i, ii, j, jj, i + ii + j + jj from t join tt where i = ii;",
208211
},
209212
Assertions: []ScriptTestAssertion{
210213
{
@@ -228,6 +231,12 @@ CREATE TABLE tab1 (
228231
{"j", "int", "YES", "", "100", ""},
229232
},
230233
},
234+
{
235+
Query: "select * from v",
236+
Expected: []sql.Row{
237+
{1, 100},
238+
},
239+
},
231240
{
232241
Query: "show full columns from v1;",
233242
Expected: []sql.Row{
@@ -249,6 +258,48 @@ CREATE TABLE tab1 (
249258
{"jj", "bigint", "YES", "", nil, ""},
250259
},
251260
},
261+
{
262+
Query: "select * from v1",
263+
Expected: []sql.Row{
264+
{1, 110},
265+
},
266+
},
267+
{
268+
Query: "show full columns from vv;",
269+
Expected: []sql.Row{
270+
{"i", "int", nil, "NO", "", nil, "", "", ""},
271+
{"ii", "int", nil, "NO", "", nil, "", "", ""},
272+
{"j", "int", nil, "YES", "", "100", "", "", ""},
273+
{"jj", "int", nil, "YES", "", "(power(11, 2))", "DEFAULT_GENERATED", "", ""},
274+
{"i + ii + j + jj", "bigint", nil, "YES", "", nil, "", "", ""},
275+
},
276+
},
277+
{
278+
Query: "show columns from vv;",
279+
Expected: []sql.Row{
280+
{"i", "int", "NO", "", nil, ""},
281+
{"ii", "int", "NO", "", nil, ""},
282+
{"j", "int", "YES", "", "100", ""},
283+
{"jj", "int", "YES", "", "(power(11, 2))", "DEFAULT_GENERATED"},
284+
{"i + ii + j + jj", "bigint", "YES", "", nil, ""},
285+
},
286+
},
287+
{
288+
Query: "describe vv;",
289+
Expected: []sql.Row{
290+
{"i", "int", "NO", "", nil, ""},
291+
{"ii", "int", "NO", "", nil, ""},
292+
{"j", "int", "YES", "", "100", ""},
293+
{"jj", "int", "YES", "", "(power(11, 2))", "DEFAULT_GENERATED"},
294+
{"i + ii + j + jj", "bigint", "YES", "", nil, ""},
295+
},
296+
},
297+
{
298+
Query: "select * from vv",
299+
Expected: []sql.Row{
300+
{1, 1, 100, 121, 223},
301+
},
302+
},
252303
},
253304
},
254305
}

sql/analyzer/analyzer.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -406,8 +406,7 @@ func NewProcRuleSelector(sel RuleSelector) RuleSelector {
406406
// once after default rules should only be run once
407407
AutocommitId,
408408
TrackProcessId,
409-
parallelizeId,
410-
clearWarningsId:
409+
parallelizeId:
411410
return false
412411
}
413412
return sel(id)

sql/analyzer/resolve_create_select.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ func resolveCreateSelect(ctx *sql.Context, a *Analyzer, n sql.Node, scope *plan.
3131
for i, col := range mergedSchema {
3232
tempCol := *col
3333
tempCol.Source = ct.Name()
34+
// replace system variable types with their underlying types
35+
if sysType, isSysTyp := tempCol.Type.(sql.SystemVariableType); isSysTyp {
36+
tempCol.Type = sysType.UnderlyingType()
37+
}
3438
newSch[i] = &tempCol
3539
}
3640

sql/analyzer/rule_ids.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,5 +90,4 @@ const (
9090
AutocommitId // addAutocommit
9191
TrackProcessId // trackProcess
9292
parallelizeId // parallelize
93-
clearWarningsId // clearWarnings
9493
)

sql/analyzer/ruleid_string.go

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

sql/analyzer/rules.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ func init() {
2929
{AutocommitId, addAutocommit},
3030
{TrackProcessId, trackProcess},
3131
{parallelizeId, parallelize},
32-
{clearWarningsId, clearWarnings},
3332
}
3433
}
3534

0 commit comments

Comments
 (0)