Skip to content

Commit 9aece20

Browse files
committed
sql/parser: require parens around resource group options
The grammar for `with_resource_group_options` previously accepted both `WITH <opt> = <val>, ...` and `WITH (<opt> = <val>, ...)`. Because `BUCKET_COUNT` is in the lexer's `WITH_LA` lookahead set, supporting the unparenthesized form required four grammar alternatives (the `WITH` and `WITH_LA` variants of each), and the generated BNF surfaced this as duplicate `'WITH' ...` lines in `stmt_block.bnf`. PR #170894 made the `Format` methods always emit the parenthesized form, so the unparenthesized grammar paths are no longer needed for the parse/format round-trip. Drop them and require parentheses, matching CRDB convention for arbitrary KV-style option lists (`CREATE TABLE` storage params, `BACKUP ... WITH OPTIONS (...)`, etc.). Resolves: #170788 Release note: None
1 parent a081a00 commit 9aece20

4 files changed

Lines changed: 44 additions & 79 deletions

File tree

docs/generated/sql/bnf/stmt_block.bnf

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1946,10 +1946,7 @@ label_spec ::=
19461946
| 'IF' 'NOT' 'EXISTS' string_or_placeholder
19471947

19481948
with_resource_group_options ::=
1949-
'WITH' resource_group_option_list
1950-
| 'WITH' resource_group_option_list
1951-
| 'WITH' '(' resource_group_option_list ')'
1952-
| 'WITH' '(' resource_group_option_list ')'
1949+
'WITH' '(' resource_group_option_list ')'
19531950

19541951
role_or_group_or_user ::=
19551952
'ROLE'

pkg/sql/logictest/testdata/logic_test/resource_group

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
subtest disabled_by_default
88

99
statement error pq: resource group SQL is disabled
10-
CREATE RESOURCE GROUP g WITH cpu_weight = 100
10+
CREATE RESOURCE GROUP g WITH (cpu_weight = 100)
1111

1212
statement error pq: resource group SQL is disabled
13-
ALTER RESOURCE GROUP g WITH cpu_weight = 100
13+
ALTER RESOURCE GROUP g WITH (cpu_weight = 100)
1414

1515
statement error pq: resource group SQL is disabled
1616
DROP RESOURCE GROUP g
@@ -34,58 +34,55 @@ statement error at or near "EOF": syntax error
3434
CREATE RESOURCE GROUP missing_weight
3535

3636
statement error pq: cpu_weight is required and must be a positive integer
37-
CREATE RESOURCE GROUP only_max_cpu WITH max_cpu = true
37+
CREATE RESOURCE GROUP only_max_cpu WITH (max_cpu = true)
3838

3939
statement error pq: cpu_weight must be a positive integer
40-
CREATE RESOURCE GROUP zero_weight WITH cpu_weight = 0
40+
CREATE RESOURCE GROUP zero_weight WITH (cpu_weight = 0)
4141

4242
statement error pq: cpu_weight must be a positive integer
43-
CREATE RESOURCE GROUP negative_weight WITH cpu_weight = -1
43+
CREATE RESOURCE GROUP negative_weight WITH (cpu_weight = -1)
4444

4545
# Unknown options are rejected.
4646
statement error pq: unknown resource group option "bogus"
47-
CREATE RESOURCE GROUP unknown_opt WITH cpu_weight = 1, bogus = 1
47+
CREATE RESOURCE GROUP unknown_opt WITH (cpu_weight = 1, bogus = 1)
4848

4949
# Repeating an option is rejected (rather than silently last-writer-wins).
5050
statement error pq: cpu_weight specified multiple times
51-
CREATE RESOURCE GROUP dup_weight WITH cpu_weight = 10, cpu_weight = 20
51+
CREATE RESOURCE GROUP dup_weight WITH (cpu_weight = 10, cpu_weight = 20)
5252

5353
statement error pq: max_cpu specified multiple times
54-
CREATE RESOURCE GROUP dup_max_cpu WITH cpu_weight = 1, max_cpu = true, max_cpu = false
54+
CREATE RESOURCE GROUP dup_max_cpu WITH (cpu_weight = 1, max_cpu = true, max_cpu = false)
55+
56+
# The no-parens form is rejected; parentheses are required around the option list.
57+
statement error at or near "cpu_weight": syntax error
58+
CREATE RESOURCE GROUP no_parens WITH cpu_weight = 100
5559

5660
# IDs < 16 are reserved; the sequence's first allocated id is 17.
5761
statement ok
58-
CREATE RESOURCE GROUP high WITH cpu_weight = 100, max_cpu = true
62+
CREATE RESOURCE GROUP high WITH (cpu_weight = 100, max_cpu = true)
5963

6064
query ITIB colnames
6165
SHOW RESOURCE GROUP high
6266
----
6367
id name cpu_weight max_cpu
6468
17 high 100 true
6569

66-
# The parens form is also accepted.
67-
statement ok
68-
CREATE RESOURCE GROUP parens WITH (cpu_weight = 50, max_cpu = false)
69-
70-
statement ok
71-
DROP RESOURCE GROUP parens
72-
7370
# Defaults: max_cpu omitted on CREATE -> false.
7471
statement ok
75-
CREATE RESOURCE GROUP medium WITH cpu_weight = 50
72+
CREATE RESOURCE GROUP medium WITH (cpu_weight = 50)
7673

7774
query ITIB colnames
7875
SHOW RESOURCE GROUP medium
7976
----
8077
id name cpu_weight max_cpu
81-
19 medium 50 false
78+
18 medium 50 false
8279

8380
# Duplicate name is rejected; IF NOT EXISTS swallows the conflict.
8481
statement error pq: resource group "high" already exists
85-
CREATE RESOURCE GROUP high WITH cpu_weight = 999
82+
CREATE RESOURCE GROUP high WITH (cpu_weight = 999)
8683

8784
statement ok
88-
CREATE RESOURCE GROUP IF NOT EXISTS high WITH cpu_weight = 999
85+
CREATE RESOURCE GROUP IF NOT EXISTS high WITH (cpu_weight = 999)
8986

9087
# The existing row was not modified.
9188
query I
@@ -103,7 +100,7 @@ SHOW RESOURCE GROUPS
103100
----
104101
id name cpu_weight max_cpu
105102
17 high 100 true
106-
19 medium 50 false
103+
18 medium 50 false
107104

108105
# SHOW RESOURCE GROUP <name> errors when the named group does not exist.
109106
statement error pq: resource group "ghost" does not exist
@@ -115,7 +112,7 @@ subtest alter
115112

116113
# ALTER updates only specified options (partial update).
117114
statement ok
118-
ALTER RESOURCE GROUP high WITH cpu_weight = 200
115+
ALTER RESOURCE GROUP high WITH (cpu_weight = 200)
119116

120117
query ITIB colnames
121118
SHOW RESOURCE GROUP high
@@ -124,7 +121,7 @@ id name cpu_weight max_cpu
124121
17 high 200 true
125122

126123
statement ok
127-
ALTER RESOURCE GROUP high WITH max_cpu = false
124+
ALTER RESOURCE GROUP high WITH (max_cpu = false)
128125

129126
query ITIB colnames
130127
SHOW RESOURCE GROUP high
@@ -134,14 +131,14 @@ id name cpu_weight max_cpu
134131

135132
# ALTER on a missing group: error without IF EXISTS, no-op with.
136133
statement error pq: resource group "ghost" does not exist
137-
ALTER RESOURCE GROUP ghost WITH cpu_weight = 1
134+
ALTER RESOURCE GROUP ghost WITH (cpu_weight = 1)
138135

139136
statement ok
140-
ALTER RESOURCE GROUP IF EXISTS ghost WITH cpu_weight = 1
137+
ALTER RESOURCE GROUP IF EXISTS ghost WITH (cpu_weight = 1)
141138

142139
# Repeating an option is rejected on ALTER as well.
143140
statement error pq: cpu_weight specified multiple times
144-
ALTER RESOURCE GROUP high WITH cpu_weight = 10, cpu_weight = 20
141+
ALTER RESOURCE GROUP high WITH (cpu_weight = 10, cpu_weight = 20)
145142

146143
subtest end
147144

@@ -170,10 +167,10 @@ subtest non_admin
170167
user testuser
171168

172169
statement error pq: only users with the admin role may use resource group statements
173-
CREATE RESOURCE GROUP unauth WITH cpu_weight = 1
170+
CREATE RESOURCE GROUP unauth WITH (cpu_weight = 1)
174171

175172
statement error pq: only users with the admin role may use resource group statements
176-
ALTER RESOURCE GROUP high WITH cpu_weight = 1
173+
ALTER RESOURCE GROUP high WITH (cpu_weight = 1)
177174

178175
statement error pq: only users with the admin role may use resource group statements
179176
DROP RESOURCE GROUP high

pkg/sql/parser/sql.y

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4283,19 +4283,7 @@ drop_external_connection_stmt:
42834283
| DROP EXTERNAL CONNECTION error // SHOW HELP: DROP EXTERNAL CONNECTION
42844284

42854285
with_resource_group_options:
4286-
WITH resource_group_option_list
4287-
{
4288-
$$.val = $2.kvOptions()
4289-
}
4290-
| WITH_LA resource_group_option_list
4291-
{
4292-
$$.val = $2.kvOptions()
4293-
}
4294-
| WITH '(' resource_group_option_list ')'
4295-
{
4296-
$$.val = $3.kvOptions()
4297-
}
4298-
| WITH_LA '(' resource_group_option_list ')'
4286+
WITH '(' resource_group_option_list ')'
42994287
{
43004288
$$.val = $3.kvOptions()
43014289
}

pkg/sql/parser/testdata/resource_group

Lines changed: 17 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,6 @@ CREATE RESOURCE GROUP my_group WITH (cpu_weight = (100), max_cpu = (false)) -- f
1515
CREATE RESOURCE GROUP my_group WITH (cpu_weight = _, max_cpu = _) -- literals removed
1616
CREATE RESOURCE GROUP _ WITH (cpu_weight = 100, max_cpu = false) -- identifiers removed
1717

18-
# No-parens form is also accepted; the canonical form adds them.
19-
parse
20-
CREATE RESOURCE GROUP my_group WITH cpu_weight = 100, max_cpu = false
21-
----
22-
CREATE RESOURCE GROUP my_group WITH (cpu_weight = 100, max_cpu = false) -- normalized!
23-
CREATE RESOURCE GROUP my_group WITH (cpu_weight = (100), max_cpu = (false)) -- fully parenthesized
24-
CREATE RESOURCE GROUP my_group WITH (cpu_weight = _, max_cpu = _) -- literals removed
25-
CREATE RESOURCE GROUP _ WITH (cpu_weight = 100, max_cpu = false) -- identifiers removed
26-
2718
parse
2819
CREATE RESOURCE GROUP IF NOT EXISTS my_group WITH (cpu_weight = 1, max_cpu = true)
2920
----
@@ -32,7 +23,6 @@ CREATE RESOURCE GROUP IF NOT EXISTS my_group WITH (cpu_weight = (1), max_cpu = (
3223
CREATE RESOURCE GROUP IF NOT EXISTS my_group WITH (cpu_weight = _, max_cpu = _) -- literals removed
3324
CREATE RESOURCE GROUP IF NOT EXISTS _ WITH (cpu_weight = 1, max_cpu = true) -- identifiers removed
3425

35-
# Ensure BUCKET_COUNT (which triggers WITH_LA in the lexer) works correctly.
3626
parse
3727
CREATE RESOURCE GROUP my_group WITH (bucket_count = 10)
3828
----
@@ -41,14 +31,6 @@ CREATE RESOURCE GROUP my_group WITH (bucket_count = (10)) -- fully parenthesized
4131
CREATE RESOURCE GROUP my_group WITH (bucket_count = _) -- literals removed
4232
CREATE RESOURCE GROUP _ WITH (bucket_count = 10) -- identifiers removed
4333

44-
parse
45-
CREATE RESOURCE GROUP my_group WITH bucket_count = 10
46-
----
47-
CREATE RESOURCE GROUP my_group WITH (bucket_count = 10) -- normalized!
48-
CREATE RESOURCE GROUP my_group WITH (bucket_count = (10)) -- fully parenthesized
49-
CREATE RESOURCE GROUP my_group WITH (bucket_count = _) -- literals removed
50-
CREATE RESOURCE GROUP _ WITH (bucket_count = 10) -- identifiers removed
51-
5234
parse
5335
ALTER RESOURCE GROUP my_group WITH (cpu_weight = 200)
5436
----
@@ -57,15 +39,6 @@ ALTER RESOURCE GROUP my_group WITH (cpu_weight = (200)) -- fully parenthesized
5739
ALTER RESOURCE GROUP my_group WITH (cpu_weight = _) -- literals removed
5840
ALTER RESOURCE GROUP _ WITH (cpu_weight = 200) -- identifiers removed
5941

60-
# No-parens form is also accepted; the canonical form adds them.
61-
parse
62-
ALTER RESOURCE GROUP my_group WITH cpu_weight = 200
63-
----
64-
ALTER RESOURCE GROUP my_group WITH (cpu_weight = 200) -- normalized!
65-
ALTER RESOURCE GROUP my_group WITH (cpu_weight = (200)) -- fully parenthesized
66-
ALTER RESOURCE GROUP my_group WITH (cpu_weight = _) -- literals removed
67-
ALTER RESOURCE GROUP _ WITH (cpu_weight = 200) -- identifiers removed
68-
6942
parse
7043
ALTER RESOURCE GROUP IF EXISTS my_group WITH (max_cpu = true)
7144
----
@@ -74,14 +47,24 @@ ALTER RESOURCE GROUP IF EXISTS my_group WITH (max_cpu = (true)) -- fully parenth
7447
ALTER RESOURCE GROUP IF EXISTS my_group WITH (max_cpu = _) -- literals removed
7548
ALTER RESOURCE GROUP IF EXISTS _ WITH (max_cpu = true) -- identifiers removed
7649

77-
# Ensure BUCKET_COUNT (which triggers WITH_LA in the lexer) works correctly.
78-
parse
79-
ALTER RESOURCE GROUP my_group WITH bucket_count = 10
50+
# The no-parens form is rejected; parentheses are required.
51+
error
52+
CREATE RESOURCE GROUP my_group WITH cpu_weight = 100
53+
----
54+
at or near "cpu_weight": syntax error
55+
DETAIL: source SQL:
56+
CREATE RESOURCE GROUP my_group WITH cpu_weight = 100
57+
^
58+
HINT: try \h CREATE RESOURCE GROUP
59+
60+
error
61+
ALTER RESOURCE GROUP my_group WITH cpu_weight = 200
8062
----
81-
ALTER RESOURCE GROUP my_group WITH (bucket_count = 10) -- normalized!
82-
ALTER RESOURCE GROUP my_group WITH (bucket_count = (10)) -- fully parenthesized
83-
ALTER RESOURCE GROUP my_group WITH (bucket_count = _) -- literals removed
84-
ALTER RESOURCE GROUP _ WITH (bucket_count = 10) -- identifiers removed
63+
at or near "cpu_weight": syntax error
64+
DETAIL: source SQL:
65+
ALTER RESOURCE GROUP my_group WITH cpu_weight = 200
66+
^
67+
HINT: try \h ALTER RESOURCE GROUP
8568

8669
parse
8770
DROP RESOURCE GROUP my_group

0 commit comments

Comments
 (0)