Skip to content

Commit

Permalink
Merge #71915
Browse files Browse the repository at this point in the history
71915: sql: implement custom session options r=rafiss a=otan

Resolves #70033

See individual commit for details. I've left out memory accounting for now.

Co-authored-by: Oliver Tan <[email protected]>
  • Loading branch information
craig[bot] and otan committed Nov 2, 2021
2 parents b180b4d + d9afaf5 commit 3f56647
Show file tree
Hide file tree
Showing 27 changed files with 802 additions and 327 deletions.
4 changes: 4 additions & 0 deletions docs/generated/sql/bnf/stmt_block.bnf
Original file line number Diff line number Diff line change
Expand Up @@ -1556,6 +1556,7 @@ insert_column_item ::=

session_var ::=
'identifier'
| 'identifier' session_var_parts
| 'ALL'
| 'DATABASE'
| 'NAMES'
Expand Down Expand Up @@ -2118,6 +2119,9 @@ like_table_option_list ::=
column_name ::=
name

session_var_parts ::=
( '.' 'identifier' ) ( ( '.' 'identifier' ) )*

attrs ::=
( '.' unrestricted_name ) ( ( '.' unrestricted_name ) )*

Expand Down
1 change: 1 addition & 0 deletions pkg/sql/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ go_library(
"show_stats.go",
"show_trace.go",
"show_trace_replica.go",
"show_var.go",
"show_zone_config.go",
"sort.go",
"split.go",
Expand Down
4 changes: 4 additions & 0 deletions pkg/sql/conn_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -684,8 +684,12 @@ func (s *Server) newSessionData(args SessionArgs) *sessiondata.SessionData {
LocalOnlySessionData: sessiondatapb.LocalOnlySessionData{
ResultsBufferSize: args.ConnResultsBufferSize,
IsSuperuser: args.IsSuperuser,
CustomOptions: make(map[string]string),
},
}
for k, v := range args.CustomOptionSessionDefaults {
sd.CustomOptions[k] = v
}
s.populateMinimalSessionData(sd)
return sd
}
Expand Down
5 changes: 4 additions & 1 deletion pkg/sql/crdb_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -1452,7 +1452,10 @@ CREATE TABLE crdb_internal.session_variables (
populate: func(ctx context.Context, p *planner, _ catalog.DatabaseDescriptor, addRow func(...tree.Datum) error) error {
for _, vName := range varNames {
gen := varGen[vName]
value := gen.Get(&p.extendedEvalCtx)
value, err := gen.Get(&p.extendedEvalCtx)
if err != nil {
return err
}
if err := addRow(
tree.NewDString(vName),
tree.NewDString(value),
Expand Down
4 changes: 4 additions & 0 deletions pkg/sql/delegate/show_var.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ func (d *delegator) delegateShowVar(n *tree.ShowVar) (tree.Statement, error) {
}

if _, ok := ValidVars[name]; !ok {
// Custom options go to planNode.
if strings.Contains(name, ".") {
return nil, nil
}
return nil, pgerror.Newf(pgcode.UndefinedObject,
"unrecognized configuration parameter %q", origName)
}
Expand Down
14 changes: 11 additions & 3 deletions pkg/sql/exec_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -1750,9 +1750,10 @@ type SessionDefaults map[string]string

// SessionArgs contains arguments for serving a client connection.
type SessionArgs struct {
User security.SQLUsername
IsSuperuser bool
SessionDefaults SessionDefaults
User security.SQLUsername
IsSuperuser bool
SessionDefaults SessionDefaults
CustomOptionSessionDefaults SessionDefaults
// RemoteAddr is the client's address. This is nil iff this is an internal
// client.
RemoteAddr net.Addr
Expand Down Expand Up @@ -2807,6 +2808,13 @@ func (m *sessionDataMutator) SetLocation(loc *time.Location) {
m.bufferParamStatusUpdate("TimeZone", sessionDataTimeZoneFormat(loc))
}

func (m *sessionDataMutator) SetCustomOption(name, val string) {
if m.data.CustomOptions == nil {
m.data.CustomOptions = make(map[string]string)
}
m.data.CustomOptions[name] = val
}

func (m *sessionDataMutator) SetReadOnly(val bool) {
// The read-only state is special; it's set as a session variable (SET
// transaction_read_only=<>), but it represents per-txn state, not
Expand Down
5 changes: 4 additions & 1 deletion pkg/sql/information_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -1528,7 +1528,10 @@ var informationSchemaSessionVariables = virtualSchemaTable{
populate: func(ctx context.Context, p *planner, _ catalog.DatabaseDescriptor, addRow func(...tree.Datum) error) error {
for _, vName := range varNames {
gen := varGen[vName]
value := gen.Get(&p.extendedEvalCtx)
value, err := gen.Get(&p.extendedEvalCtx)
if err != nil {
return err
}
if err := addRow(
tree.NewDString(vName),
tree.NewDString(value),
Expand Down
17 changes: 9 additions & 8 deletions pkg/sql/logictest/testdata/logic_test/alter_role_set
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@ statement ok
ALTER ROLE test_set_role SET application_name = 'a';
ALTER ROLE test_set_role IN DATABASE test_set_db SET application_name = 'b';
ALTER ROLE ALL IN DATABASE test_set_db SET application_name = 'c';
ALTER ROLE ALL SET application_name = 'd'
ALTER ROLE ALL SET application_name = 'd';
ALTER ROLE test_set_role SET custom_option.setting = 'e'

# Verify that the defaults were stored.
query OTT colnames
SELECT database_id, role_name, settings FROM system.database_role_settings ORDER BY 1, 2
----
database_id role_name settings
0 · {application_name=d}
0 test_set_role {application_name=a}
0 test_set_role {application_name=a,custom_option.setting=e}
53 · {application_name=c}
53 test_set_role {application_name=b}

Expand All @@ -32,7 +33,7 @@ ORDER BY 1, 2
----
setdatabase setrole datname rolname setconfig
0 0 NULL NULL {application_name=d}
0 265380634 NULL test_set_role {application_name=a}
0 265380634 NULL test_set_role {application_name=a,custom_option.setting=e}
53 0 test_set_db NULL {application_name=c}
53 265380634 test_set_db test_set_role {application_name=b}

Expand All @@ -44,7 +45,7 @@ query T
SELECT settings FROM system.database_role_settings
WHERE database_id = 0 AND role_name = 'test_set_role'
----
{application_name=a,backslash_quote=safe_encoding}
{application_name=a,custom_option.setting=e,backslash_quote=safe_encoding}

statement ok
ALTER ROLE test_set_role SET application_name = 'f'
Expand All @@ -54,7 +55,7 @@ query T
SELECT settings FROM system.database_role_settings
WHERE database_id = 0 AND role_name = 'test_set_role'
----
{backslash_quote=safe_encoding,application_name=f}
{custom_option.setting=e,backslash_quote=safe_encoding,application_name=f}

statement ok
ALTER ROLE test_set_role SET serial_normalization = 'sql_sequence';
Expand All @@ -65,7 +66,7 @@ query T
SELECT settings FROM system.database_role_settings
WHERE database_id = 0 AND role_name = 'test_set_role'
----
{backslash_quote=safe_encoding,serial_normalization=sql_sequence}
{custom_option.setting=e,backslash_quote=safe_encoding,serial_normalization=sql_sequence}

# Resetting something that isn't there anymore is fine.
statement ok
Expand Down Expand Up @@ -187,7 +188,7 @@ query OTT colnames
SELECT database_id, role_name, settings FROM system.database_role_settings ORDER BY 1, 2
----
database_id role_name settings
0 test_set_role {backslash_quote=safe_encoding,serial_normalization=sql_sequence}
0 test_set_role {custom_option.setting=e,backslash_quote=safe_encoding,serial_normalization=sql_sequence}
53 · {application_name=c}
53 test_set_role {application_name=b}

Expand All @@ -199,7 +200,7 @@ query OTT colnames
SELECT database_id, role_name, settings FROM system.database_role_settings ORDER BY 1, 2
----
database_id role_name settings
0 test_set_role {backslash_quote=safe_encoding,serial_normalization=sql_sequence}
0 test_set_role {custom_option.setting=e,backslash_quote=safe_encoding,serial_normalization=sql_sequence}

statement ok
DROP ROLE test_set_role
Expand Down
59 changes: 50 additions & 9 deletions pkg/sql/logictest/testdata/logic_test/set
Original file line number Diff line number Diff line change
Expand Up @@ -414,15 +414,6 @@ SHOW idle_in_transaction_session_timeout
statement ok
SET idle_in_transaction_session_timeout = 0

# Test that composite variable names get rejected properly, especially
# when "tracing" is used as prefix.

statement error unrecognized configuration parameter "blah.blah"
SET blah.blah = 123

statement error unrecognized configuration parameter "tracing.blah"
SET tracing.blah = 123

statement error invalid value for parameter "ssl_renegotiation_limit"
SET ssl_renegotiation_limit = 123

Expand Down Expand Up @@ -626,3 +617,53 @@ SET LC_NUMERIC = 'en_US.UTF-8'

statement error invalid value for parameter "lc_time": "en_US.UTF-8"
SET LC_TIME = 'en_US.UTF-8'

# Test custom session variables

statement ok
SET custom_option.set_SQL = 'abc';
SELECT set_config('custom_option.set_config', 'def', false);
RESET custom_option.use_default;
SET tracing.custom = 'ijk'

# Custom options are case insensitive.
query T colnames
SHOW Custom_option.set_sql
----
custom_option.set_sql
abc

query T
SHOW custom_option.set_config
----
def

query T
SELECT current_setting('custom_option.use_default')
----
·

statement ok
RESET custom_option.set_config

query T
SHOW custom_option.set_config
----
·

# Ensure it does not show up on SHOW ALL or pg_settings.
query T
SELECT variable FROM [SHOW ALL] WHERE variable LIKE 'custom_option.%'
----

query T
SELECT name FROM pg_catalog.pg_settings WHERE name LIKE 'custom_option.%'
----

query T
SHOW tracing.custom
----
ijk

statement error unrecognized configuration parameter "custom_option.does_not_yet_exist"
SHOW custom_option.does_not_yet_exist
33 changes: 33 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/set_local
Original file line number Diff line number Diff line change
Expand Up @@ -508,3 +508,36 @@ query TT
SELECT * FROM tbl
----
2020-08-25 15:16:17.123456 +0000 UTC 1 day 15:16:17.123456

# Verify that custom session variables are handled correctly.
statement ok
SET custom_option.local_setting = 'abc';
SET custom_option.session_setting = 'abc'

statement ok
BEGIN;
SET LOCAL custom_option.local_setting = 'def';
SET custom_option.session_setting = 'def'

query T
SHOW custom_option.local_setting
----
def

query T
SHOW custom_option.session_setting
----
def

statement ok
COMMIT

query T
SHOW custom_option.local_setting
----
abc

query T
SHOW custom_option.session_setting
----
def
3 changes: 3 additions & 0 deletions pkg/sql/opaque.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,8 @@ func planOpaque(ctx context.Context, p *planner, stmt tree.Statement) (planNode,
return p.ShowTableStats(ctx, n)
case *tree.ShowTraceForSession:
return p.ShowTrace(ctx, n)
case *tree.ShowVar:
return p.ShowVar(ctx, n)
case *tree.ShowZoneConfig:
return p.ShowZoneConfig(ctx, n)
case *tree.ShowFingerprints:
Expand Down Expand Up @@ -293,6 +295,7 @@ func init() {
&tree.ShowTraceForSession{},
&tree.ShowZoneConfig{},
&tree.ShowFingerprints{},
&tree.ShowVar{},
&tree.Truncate{},

// CCL statements (without Export which has an optimizer operator).
Expand Down
15 changes: 15 additions & 0 deletions pkg/sql/parser/sql.y
Original file line number Diff line number Diff line change
Expand Up @@ -1216,6 +1216,7 @@ func (u *sqlSymUnion) setVar() *tree.SetVar {
%type <tree.Exprs> expr_list opt_expr_list tuple1_ambiguous_values tuple1_unambiguous_values
%type <*tree.Tuple> expr_tuple1_ambiguous expr_tuple_unambiguous
%type <tree.NameList> attrs
%type <[]string> session_var_parts
%type <tree.SelectExprs> target_list
%type <tree.UpdateExprs> set_clause_list
%type <*tree.UpdateExpr> set_clause multiple_set_clause
Expand Down Expand Up @@ -4978,6 +4979,10 @@ show_session_stmt:

session_var:
IDENT
| IDENT session_var_parts
{
$$ = $1 + "." + strings.Join($2.strs(), ".")
}
// Although ALL, SESSION_USER, DATABASE, LC_COLLATE, and LC_CTYPE are
// identifiers for the purpose of SHOW, they lex as separate token types, so
// they need separate rules.
Expand All @@ -4994,6 +4999,16 @@ session_var:
| TIME ZONE { $$ = "timezone" }
| TIME error // SHOW HELP: SHOW SESSION

session_var_parts:
'.' IDENT
{
$$.val = []string{$2}
}
| session_var_parts '.' IDENT
{
$$.val = append($1.strs(), $3)
}

// %Help: SHOW STATISTICS - display table statistics (experimental)
// %Category: Experimental
// %Text: SHOW STATISTICS [USING JSON] FOR TABLE <table_name>
Expand Down
32 changes: 32 additions & 0 deletions pkg/sql/parser/testdata/set
Original file line number Diff line number Diff line change
Expand Up @@ -547,3 +547,35 @@ SET LOCAL intervalstyle = 'postgres' -- normalized!
SET LOCAL intervalstyle = ('postgres') -- fully parenthesized
SET LOCAL intervalstyle = '_' -- literals removed
SET LOCAL intervalstyle = 'postgres' -- identifiers removed

parse
SET a.b = 'd'
----
SET "a.b" = 'd' -- normalized!
SET "a.b" = ('d') -- fully parenthesized
SET "a.b" = '_' -- literals removed
SET "a.b" = 'd' -- identifiers removed

parse
SET a.b.c = 'd'
----
SET "a.b.c" = 'd' -- normalized!
SET "a.b.c" = ('d') -- fully parenthesized
SET "a.b.c" = '_' -- literals removed
SET "a.b.c" = 'd' -- identifiers removed

parse
RESET a.b.c
----
RESET "a.b.c" -- normalized!
RESET "a.b.c" -- fully parenthesized
RESET "a.b.c" -- literals removed
RESET "a.b.c" -- identifiers removed

parse
SHOW a.b.c
----
SHOW "a.b.c" -- normalized!
SHOW "a.b.c" -- fully parenthesized
SHOW "a.b.c" -- literals removed
SHOW "a.b.c" -- identifiers removed
5 changes: 4 additions & 1 deletion pkg/sql/pg_catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -2494,7 +2494,10 @@ https://www.postgresql.org/docs/9.5/catalog-pg-settings.html`,
if gen.Hidden {
continue
}
value := gen.Get(&p.extendedEvalCtx)
value, err := gen.Get(&p.extendedEvalCtx)
if err != nil {
return err
}
valueDatum := tree.NewDString(value)
var bootDatum tree.Datum = tree.DNull
var resetDatum tree.Datum = tree.DNull
Expand Down
1 change: 0 additions & 1 deletion pkg/sql/pgwire/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,6 @@ func (c *conn) handleAuthentication(
if _, ok := c.sessionArgs.SessionDefaults[keyVal[0]]; !ok {
c.sessionArgs.SessionDefaults[keyVal[0]] = keyVal[1]
}

}
}

Expand Down
Loading

0 comments on commit 3f56647

Please sign in to comment.