Skip to content

Commit 9ee86c8

Browse files
authored
clear warnings better and separate warning count from actual warnings (#2696)
1 parent f9668b4 commit 9ee86c8

File tree

9 files changed

+42
-66
lines changed

9 files changed

+42
-66
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/script_queries.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8809,21 +8809,17 @@ var CreateDatabaseScripts = []ScriptTest{
88098809
Expected: []sql.Row{{types.NewOkResult(1)}},
88108810
},
88118811
{
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"}},
8812+
Query: "SHOW WARNINGS /* 1 */",
8813+
Expected: []sql.Row{{"Warning", 1235, "Setting CHARACTER SET, COLLATION and ENCRYPTION are not supported yet"}},
88158814
},
88168815
{
88178816
Query: "CREATE DATABASE newtest1db DEFAULT COLLATE binary ENCRYPTION='Y'",
88188817
Expected: []sql.Row{{types.NewOkResult(1)}},
88198818
},
88208819
{
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
88238820
Query: "SHOW WARNINGS /* 2 */",
88248821
Expected: []sql.Row{
88258822
{"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"},
88278823
},
88288824
},
88298825
{
@@ -8939,10 +8935,8 @@ var DropDatabaseScripts = []ScriptTest{
89398935
Expected: []sql.Row{{types.OkResult{RowsAffected: 1}}},
89408936
},
89418937
{
8942-
// TODO: there should not be warning
8943-
// https://github.com/dolthub/dolt/issues/6921
89448938
Query: "SHOW WARNINGS",
8945-
Expected: []sql.Row{{"Note", 1008, "Can't drop database mydb; database doesn't exist "}},
8939+
Expected: []sql.Row{},
89468940
},
89478941
{
89488942
Query: "SELECT DATABASE()",

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/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

sql/analyzer/warnings.go

Lines changed: 0 additions & 42 deletions
This file was deleted.

sql/base_session.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ type BaseSession struct {
4141
viewReg *ViewRegistry
4242
warnings []*Warning
4343
warningLock bool
44-
warncnt uint16
44+
warningCount uint16 // num of warnings from recent query; does not always equal to len(warnings)
4545
locks map[string]bool
4646
queriedDb string
4747
lastQueryInfo map[string]*atomic.Value
@@ -331,6 +331,7 @@ func (s *BaseSession) SetConnectionId(id uint32) {
331331
// Warn stores the warning in the session.
332332
func (s *BaseSession) Warn(warn *Warning) {
333333
s.warnings = append(s.warnings, warn)
334+
s.warningCount = uint16(len(s.warnings))
334335
}
335336

336337
// Warnings returns a copy of session warnings (from the most recent - the last one)
@@ -354,25 +355,25 @@ func (s *BaseSession) UnlockWarnings() {
354355
s.warningLock = false
355356
}
356357

358+
// ClearWarningCount cleans up session warnings
359+
func (s *BaseSession) ClearWarningCount() {
360+
s.warningCount = 0
361+
}
362+
357363
// ClearWarnings cleans up session warnings
358364
func (s *BaseSession) ClearWarnings() {
359365
if s.warningLock {
360366
return
361367
}
362-
cnt := uint16(len(s.warnings))
363-
if s.warncnt != cnt {
364-
s.warncnt = cnt
365-
return
366-
}
367368
if s.warnings != nil {
368369
s.warnings = s.warnings[:0]
369370
}
370-
s.warncnt = 0
371+
s.ClearWarningCount()
371372
}
372373

373374
// WarningCount returns a number of session warnings
374375
func (s *BaseSession) WarningCount() uint16 {
375-
return uint16(len(s.warnings))
376+
return s.warningCount
376377
}
377378

378379
// AddLock adds a lock to the set of locks owned by this user which will need to be released if this session terminates

sql/session.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ type Session interface {
104104
Warn(warn *Warning)
105105
// Warnings returns a copy of session warnings (from the most recent).
106106
Warnings() []*Warning
107+
// ClearWarningCount clears the warning count without clearing the actual warnings.
108+
ClearWarningCount()
107109
// ClearWarnings cleans up session warnings.
108110
ClearWarnings()
109111
// WarningCount returns a number of session warnings

0 commit comments

Comments
 (0)