Skip to content

Commit 52bb933

Browse files
author
James Cor
committed
clear warnings better, and separate warning count from actual warnings
1 parent f9668b4 commit 52bb933

File tree

3 files changed

+21
-16
lines changed

3 files changed

+21
-16
lines changed

sql/analyzer/warnings.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,25 @@ import (
2020
"github.com/dolthub/go-mysql-server/sql/transform"
2121
)
2222

23+
// clearWarnings resets the warning count to 0 and removes any warnings from the current context.
2324
func clearWarnings(ctx *sql.Context, a *Analyzer, node sql.Node, scope *plan.Scope, sel RuleSelector, qFlags *sql.QueryFlags) (sql.Node, transform.TreeIdentity, error) {
25+
// This is an analyzer rule because we need to clear the warnings from the previous query before executing
26+
// the current query, except for the case of `show warnings` which should not clear the warnings.
2427
children := node.Children()
2528
if len(children) == 0 {
2629
return node, transform.SameTree, nil
2730
}
2831

2932
switch ch := children[0].(type) {
33+
case *plan.Offset, *plan.Limit:
34+
// `show warning limit x offset y` is valid, so we need to recurse
35+
return clearWarnings(ctx, a, ch, scope, sel, qFlags)
3036
case plan.ShowWarnings:
37+
// ShowWarnings should not clear the warnings, but should still reset the warning count.
38+
ctx.ClearWarningCount()
3139
return node, transform.SameTree, nil
32-
case *plan.Offset:
33-
clearWarnings(ctx, a, ch, scope, sel, qFlags)
34-
return node, transform.SameTree, nil
35-
case *plan.Limit:
36-
clearWarnings(ctx, a, ch, scope, sel, qFlags)
40+
default:
41+
ctx.ClearWarnings()
3742
return node, transform.SameTree, nil
3843
}
39-
40-
ctx.ClearWarnings()
41-
return node, transform.SameTree, nil
4244
}

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+
// ClearWarnCount 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)