Skip to content

Commit

Permalink
sql: support RESET ALL statement
Browse files Browse the repository at this point in the history
Release note (sql change): Support for the RESET ALL statement was
added. This statement resets the values of all session variables to
their default values.
  • Loading branch information
rafiss committed Feb 2, 2022
1 parent be1b6c4 commit 85ea19d
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/generated/sql/bnf/reset_session_stmt.bnf
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
reset_session_stmt ::=
'RESET' session_var
| 'RESET' 'SESSION' session_var
| 'RESET_ALL' 'ALL'
1 change: 1 addition & 0 deletions docs/generated/sql/bnf/stmt_block.bnf
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,7 @@ pause_all_jobs_stmt ::=
reset_session_stmt ::=
'RESET' session_var
| 'RESET' 'SESSION' session_var
| 'RESET_ALL' 'ALL'

reset_csetting_stmt ::=
'RESET' 'CLUSTER' 'SETTING' var_name
Expand Down
14 changes: 14 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/set
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,20 @@ SHOW tracing.custom
----
ijk

# Test that RESET ALL changes custom options to empty strings.
statement ok
RESET ALL

query T
SHOW tracing.custom
----
·

query T
SHOW custom_option.set_sql
----
·

statement error unrecognized configuration parameter "custom_option.does_not_yet_exist"
SHOW custom_option.does_not_yet_exist

Expand Down
9 changes: 9 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/set_role
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,15 @@ root root root root
statement ok
SET ROLE testuser

# Verify that RESET ALL does *not* affect role.
statement ok
RESET ALL

query TTTT
SELECT current_user(), current_user, session_user(), session_user
----
testuser testuser root root

query T
SELECT user_name FROM crdb_internal.node_sessions
WHERE active_queries LIKE 'SELECT user_name%'
Expand Down
4 changes: 4 additions & 0 deletions pkg/sql/parser/sql.y
Original file line number Diff line number Diff line change
Expand Up @@ -4600,6 +4600,10 @@ reset_session_stmt:
{
$$.val = &tree.SetVar{Name: $3, Values:tree.Exprs{tree.DefaultVal{}}, Reset: true}
}
| RESET_ALL ALL
{
$$.val = &tree.SetVar{ResetAll: true, Reset: true}
}
| RESET error // SHOW HELP: RESET

// %Help: RESET CLUSTER SETTING - reset a cluster setting to its default value
Expand Down
13 changes: 13 additions & 0 deletions pkg/sql/pgwire/conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1754,6 +1754,19 @@ func TestRoleDefaultSettings(t *testing.T) {
setupStmt: "ALTER ROLE testuser IN DATABASE defaultdb SET search_path = 'f'",
expectedSearchPath: "f",
},
{
// RESET after connecting should go back to the per-role default setting.
setupStmt: "",
postConnectStmt: "SET search_path = 'new'; RESET search_path;",
expectedSearchPath: "f",
},
{
// RESET should use the query param as the default if it was provided.
setupStmt: "",
searchPathOptOverride: "g",
postConnectStmt: "SET search_path = 'new'; RESET ALL;",
expectedSearchPath: "g",
},
{
setupStmt: "ALTER ROLE testuser IN DATABASE defaultdb SET search_path = DEFAULT",
expectedSearchPath: "c",
Expand Down
45 changes: 45 additions & 0 deletions pkg/sql/set_var.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,16 @@ type setVarNode struct {
typedValues []tree.TypedExpr
}

// resetAllNode represents a RESET ALL statement.
type resetAllNode struct{}

// SetVar sets session variables.
// Privileges: None.
// Notes: postgres/mysql do not require privileges for session variables (some exceptions).
func (p *planner) SetVar(ctx context.Context, n *tree.SetVar) (planNode, error) {
if n.ResetAll {
return &resetAllNode{}, nil
}
if n.Name == "" {
// A client has sent the reserved internal syntax SET ROW ...,
// or the user entered `SET "" = foo`. Reject it.
Expand Down Expand Up @@ -182,6 +188,45 @@ func (n *setVarNode) Next(_ runParams) (bool, error) { return false, nil }
func (n *setVarNode) Values() tree.Datums { return nil }
func (n *setVarNode) Close(_ context.Context) {}

func (n *resetAllNode) startExec(params runParams) error {
for varName, v := range varGen {
if v.Set == nil && v.RuntimeSet == nil && v.SetWithPlanner == nil {
continue
}
// For Postgres compatibility, Don't reset `role` here.
if varName == "role" {
continue
}
_, defVal := getSessionVarDefaultString(
varName,
v,
params.p.sessionDataMutatorIterator.sessionDataMutatorBase,
)
if err := params.p.SetSessionVar(params.ctx, varName, defVal, false /* isLocal */); err != nil {
return err
}
}
for varName := range params.SessionData().CustomOptions {
_, v, err := getSessionVar(varName, false /* missingOK */)
if err != nil {
return err
}
_, defVal := getSessionVarDefaultString(
varName,
v,
params.p.sessionDataMutatorIterator.sessionDataMutatorBase,
)
if err := params.p.SetSessionVar(params.ctx, varName, defVal, false /* isLocal */); err != nil {
return err
}
}
return nil
}

func (n *resetAllNode) Next(_ runParams) (bool, error) { return false, nil }
func (n *resetAllNode) Values() tree.Datums { return nil }
func (n *resetAllNode) Close(_ context.Context) {}

func getStringVal(evalCtx *tree.EvalContext, name string, values []tree.TypedExpr) (string, error) {
if len(values) != 1 {
return "", newSingleArgVarError(name)
Expand Down
2 changes: 2 additions & 0 deletions pkg/sql/walk.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ func (v *planVisitor) visitInternal(plan planNode, name string) {
case *createViewNode:
case *setVarNode:
case *setClusterSettingNode:
case *resetAllNode:

case *delayedNode:
if n.plan != nil {
Expand Down Expand Up @@ -416,6 +417,7 @@ var planNodeNames = map[reflect.Type]string{
reflect.TypeOf(&renameTableNode{}): "rename table",
reflect.TypeOf(&reparentDatabaseNode{}): "reparent database",
reflect.TypeOf(&renderNode{}): "render",
reflect.TypeOf(&resetAllNode{}): "reset all",
reflect.TypeOf(&RevokeRoleNode{}): "revoke role",
reflect.TypeOf(&rowCountNode{}): "count",
reflect.TypeOf(&rowSourceToPlanNode{}): "row source to plan node",
Expand Down

0 comments on commit 85ea19d

Please sign in to comment.