Skip to content

Commit 61c0d5e

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 e19742e commit 61c0d5e

4 files changed

Lines changed: 48 additions & 83 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: 29 additions & 32 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 ITIBF colnames
6165
SHOW RESOURCE GROUP high
6266
----
6367
id name cpu_weight max_cpu cpu_share_percent
6468
17 high 100 true 100
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 ITIBF colnames
7875
SHOW RESOURCE GROUP medium
7976
----
8077
id name cpu_weight max_cpu cpu_share_percent
81-
19 medium 50 false 33.33333333333333
78+
18 medium 50 false 33.33333333333333
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 cpu_share_percent
105102
17 high 100 true 66.66666666666666
106-
19 medium 50 false 33.33333333333333
103+
18 medium 50 false 33.33333333333333
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 ITIBF colnames
121118
SHOW RESOURCE GROUP high
@@ -124,7 +121,7 @@ id name cpu_weight max_cpu cpu_share_percent
124121
17 high 200 true 80
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 ITIBF colnames
130127
SHOW RESOURCE GROUP high
@@ -134,14 +131,14 @@ id name cpu_weight max_cpu cpu_share_percent
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

@@ -179,13 +176,13 @@ SELECT cpu_share_percent FROM [SHOW RESOURCE GROUP high]
179176
100
180177

181178
statement ok
182-
ALTER RESOURCE GROUP high WITH cpu_weight = 20
179+
ALTER RESOURCE GROUP high WITH (cpu_weight = 20)
183180

184181
statement ok
185-
CREATE RESOURCE GROUP a WITH cpu_weight = 30
182+
CREATE RESOURCE GROUP a WITH (cpu_weight = 30)
186183

187184
statement ok
188-
CREATE RESOURCE GROUP b WITH cpu_weight = 50
185+
CREATE RESOURCE GROUP b WITH (cpu_weight = 50)
189186

190187
query TF rowsort
191188
SELECT name, cpu_share_percent FROM [SHOW RESOURCE GROUPS]
@@ -211,7 +208,7 @@ statement ok
211208
DROP RESOURCE GROUP a
212209

213210
statement ok
214-
ALTER RESOURCE GROUP high WITH cpu_weight = 200
211+
ALTER RESOURCE GROUP high WITH (cpu_weight = 200)
215212

216213
subtest end
217214

@@ -220,10 +217,10 @@ subtest non_admin
220217
user testuser
221218

222219
statement error pq: only users with the admin role may use resource group statements
223-
CREATE RESOURCE GROUP unauth WITH cpu_weight = 1
220+
CREATE RESOURCE GROUP unauth WITH (cpu_weight = 1)
224221

225222
statement error pq: only users with the admin role may use resource group statements
226-
ALTER RESOURCE GROUP high WITH cpu_weight = 1
223+
ALTER RESOURCE GROUP high WITH (cpu_weight = 1)
227224

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

pkg/sql/parser/sql.y

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

42874287
with_resource_group_options:
4288-
WITH resource_group_option_list
4289-
{
4290-
$$.val = $2.kvOptions()
4291-
}
4292-
| WITH_LA resource_group_option_list
4293-
{
4294-
$$.val = $2.kvOptions()
4295-
}
4296-
| WITH '(' resource_group_option_list ')'
4297-
{
4298-
$$.val = $3.kvOptions()
4299-
}
4300-
| WITH_LA '(' resource_group_option_list ')'
4288+
WITH '(' resource_group_option_list ')'
43014289
{
43024290
$$.val = $3.kvOptions()
43034291
}

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)