Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ func clearWarnings(ctx *sql.Context, node sql.Node) {
case *plan.Offset, *plan.Limit:
// `show warning limit x offset y` is valid, so we need to recurse
clearWarnings(ctx, n.Children()[0])
case plan.ShowWarnings:
case plan.ShowWarnings, *plan.Set:
// ShowWarnings should not clear the warnings, but should still reset the warning count.
ctx.ClearWarningCount()
default:
Expand Down Expand Up @@ -410,6 +410,15 @@ func (e *Engine) QueryWithBindings(ctx *sql.Context, query string, parsed sqlpar
return nil, nil, nil, err
}

shouldLock, err := ctx.GetSessionVariable(ctx, "lock_warnings")
if err != nil {
return nil, nil, nil, err
}
if shouldLock.(int8) == 1 {
ctx.LockWarnings()
defer ctx.UnlockWarnings()
}

// planbuilding can produce warnings, so we need to preserve them
numPrevWarnings := len(ctx.Session.Warnings())
bound, qFlags, err := e.bindQuery(ctx, query, parsed, bindings, binder, qFlags)
Expand Down
30 changes: 30 additions & 0 deletions enginetest/queries/variable_queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,36 @@ var VariableQueries = []ScriptTest{
},
},
},
{
Name: "locked warnings stay after query",
SetUpScript: []string{
"set @@lock_warnings = 1",
"select 1/0,1/0",
"select 1/1",
},
Assertions: []ScriptTestAssertion{
{
Query: "show warnings",
Expected: []sql.Row{
{"Warning", 1365, "Division by 0"},
{"Warning", 1365, "Division by 0"}},
},
},
},
{
Name: "unlocked warnings clear after query",
SetUpScript: []string{
"set @@lock_warnings = 0",
"select 1/0,1/0",
"select 1/1",
},
Assertions: []ScriptTestAssertion{
{
Query: "show warnings",
Expected: []sql.Row{},
},
},
},
//TODO: do not override tables with user-var-like names...but why would you do this??
//{
// Name: "user var table name no conflict",
Expand Down
8 changes: 8 additions & 0 deletions sql/variables/system_variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -1253,6 +1253,14 @@ var systemVars = map[string]sql.SystemVariable{
Type: types.NewSystemIntType("lock_wait_timeout", 1, 31536000, false),
Default: int64(31536000),
},
"lock_warnings": &sql.MysqlSystemVariable{
Name: "lock_warnings",
Scope: sql.GetMysqlScope(sql.SystemVariableScope_Both),
Dynamic: true,
SetVarHintApplies: false,
Type: types.NewSystemBoolType("lock_warnings"),
Default: int8(0),
},
"log_bin": &sql.MysqlSystemVariable{
Name: "log_bin",
Scope: sql.GetMysqlScope(sql.SystemVariableScope_Persist),
Expand Down
Loading