Skip to content

Commit

Permalink
Merge pull request #54421 from otan-cockroach/backport20.2-54219
Browse files Browse the repository at this point in the history
release-20.2: sql: allow CONNECTION LIMIT syntax for CREATE DATABASE
  • Loading branch information
otan authored Sep 17, 2020
2 parents a26089b + 8c2e04d commit 28979d5
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 21 deletions.
8 changes: 4 additions & 4 deletions docs/generated/sql/bnf/create_database_stmt.bnf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
create_database_stmt ::=
'CREATE' 'DATABASE' database_name opt_with opt_template_clause 'ENCODING' opt_equal non_reserved_word_or_sconst opt_lc_collate_clause opt_lc_ctype_clause
| 'CREATE' 'DATABASE' database_name opt_with opt_template_clause opt_lc_collate_clause opt_lc_ctype_clause
| 'CREATE' 'DATABASE' 'IF' 'NOT' 'EXISTS' database_name opt_with opt_template_clause 'ENCODING' opt_equal non_reserved_word_or_sconst opt_lc_collate_clause opt_lc_ctype_clause
| 'CREATE' 'DATABASE' 'IF' 'NOT' 'EXISTS' database_name opt_with opt_template_clause opt_lc_collate_clause opt_lc_ctype_clause
'CREATE' 'DATABASE' database_name opt_with opt_template_clause 'ENCODING' opt_equal non_reserved_word_or_sconst opt_lc_collate_clause opt_lc_ctype_clause opt_connection_limit
| 'CREATE' 'DATABASE' database_name opt_with opt_template_clause opt_lc_collate_clause opt_lc_ctype_clause opt_connection_limit
| 'CREATE' 'DATABASE' 'IF' 'NOT' 'EXISTS' database_name opt_with opt_template_clause 'ENCODING' opt_equal non_reserved_word_or_sconst opt_lc_collate_clause opt_lc_ctype_clause opt_connection_limit
| 'CREATE' 'DATABASE' 'IF' 'NOT' 'EXISTS' database_name opt_with opt_template_clause opt_lc_collate_clause opt_lc_ctype_clause opt_connection_limit
17 changes: 11 additions & 6 deletions docs/generated/sql/bnf/stmt_block.bnf
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,7 @@ unreserved_keyword ::=
| 'CONFIGURATION'
| 'CONFIGURATIONS'
| 'CONFIGURE'
| 'CONNECTION'
| 'CONSTRAINTS'
| 'CONTROLCHANGEFEED'
| 'CONTROLJOB'
Expand Down Expand Up @@ -1225,8 +1226,8 @@ create_changefeed_stmt ::=
'CREATE' 'CHANGEFEED' 'FOR' changefeed_targets opt_changefeed_sink opt_with_options

create_database_stmt ::=
'CREATE' 'DATABASE' database_name opt_with opt_template_clause opt_encoding_clause opt_lc_collate_clause opt_lc_ctype_clause
| 'CREATE' 'DATABASE' 'IF' 'NOT' 'EXISTS' database_name opt_with opt_template_clause opt_encoding_clause opt_lc_collate_clause opt_lc_ctype_clause
'CREATE' 'DATABASE' database_name opt_with opt_template_clause opt_encoding_clause opt_lc_collate_clause opt_lc_ctype_clause opt_connection_limit
| 'CREATE' 'DATABASE' 'IF' 'NOT' 'EXISTS' database_name opt_with opt_template_clause opt_encoding_clause opt_lc_collate_clause opt_lc_ctype_clause opt_connection_limit

create_index_stmt ::=
'CREATE' opt_unique 'INDEX' opt_concurrently opt_index_name 'ON' table_name opt_using_gin_btree '(' index_params ')' opt_hash_sharded opt_storing opt_interleave opt_partition_by opt_where_clause
Expand Down Expand Up @@ -1684,6 +1685,10 @@ opt_lc_ctype_clause ::=
'LC_CTYPE' opt_equal non_reserved_word_or_sconst
|

opt_connection_limit ::=
'CONNECTION' 'LIMIT' opt_equal signed_iconst
|

opt_unique ::=
'UNIQUE'
|
Expand Down Expand Up @@ -2108,6 +2113,10 @@ opt_equal ::=
'='
|

signed_iconst ::=
'ICONST'
| only_signed_iconst

opt_name ::=
name
|
Expand Down Expand Up @@ -2738,10 +2747,6 @@ geo_shape_type ::=
| 'GEOMETRYCOLLECTION'
| 'GEOMETRY'

signed_iconst ::=
'ICONST'
| only_signed_iconst

char_aliases ::=
'CHAR'
| 'CHARACTER'
Expand Down
9 changes: 9 additions & 0 deletions pkg/sql/create_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,15 @@ func (p *planner) CreateDatabase(ctx context.Context, n *tree.CreateDatabase) (p
}
}

if n.ConnectionLimit != -1 {
return nil, unimplemented.NewWithIssueDetailf(
54241,
"create.db.connection_limit",
"only connection limit -1 is supported, got: %d",
n.ConnectionLimit,
)
}

hasCreateDB, err := p.HasRoleOption(ctx, roleoption.CREATEDB)
if err != nil {
return nil, err
Expand Down
10 changes: 9 additions & 1 deletion pkg/sql/logictest/testdata/logic_test/database
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ CREATE DATABASE b5 TEMPLATE=template0 ENCODING='UTF8' LC_COLLATE='C.UTF-8' LC_CT
statement ok
CREATE DATABASE b6 TEMPLATE template0 ENCODING 'UTF8' LC_COLLATE 'C.UTF-8' LC_CTYPE 'C.UTF-8'

statement ok
CREATE DATABASE b7 WITH CONNECTION LIMIT -1

statement error only connection limit -1 is supported, got: 1
CREATE DATABASE b8 WITH CONNECTION LIMIT = 1

statement ok
CREATE DATABASE c

Expand All @@ -102,6 +108,7 @@ b3
b4
b5
b6
b7
c
defaultdb
postgres
Expand Down Expand Up @@ -141,7 +148,8 @@ DROP DATABASE b2 CASCADE;
DROP DATABASE b3 CASCADE;
DROP DATABASE b4 CASCADE;
DROP DATABASE b5 CASCADE;
DROP DATABASE b6 CASCADE
DROP DATABASE b6 CASCADE;
DROP DATABASE b7 CASCADE

statement error pgcode 42601 empty database name
DROP DATABASE ""
Expand Down
9 changes: 9 additions & 0 deletions pkg/sql/parser/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func TestParse(t *testing.T) {
{`CREATE DATABASE a LC_CTYPE = 'C.UTF-8'`},
{`CREATE DATABASE a LC_CTYPE = 'INVALID'`},
{`CREATE DATABASE a TEMPLATE = 'template0' ENCODING = 'UTF8' LC_COLLATE = 'C.UTF-8' LC_CTYPE = 'INVALID'`},
{`CREATE DATABASE a CONNECTION LIMIT = 13`},
{`CREATE DATABASE IF NOT EXISTS a`},
{`CREATE DATABASE IF NOT EXISTS a TEMPLATE = 'template0'`},
{`CREATE DATABASE IF NOT EXISTS a TEMPLATE = 'invalid'`},
Expand Down Expand Up @@ -1674,6 +1675,14 @@ func TestParse2(t *testing.T) {
`CREATE DATABASE a TEMPLATE = 'template0'`},
{`CREATE DATABASE a TEMPLATE = invalid`,
`CREATE DATABASE a TEMPLATE = 'invalid'`},
{
`CREATE DATABASE a WITH CONNECTION LIMIT = 13`,
`CREATE DATABASE a CONNECTION LIMIT = 13`,
},
{
`CREATE DATABASE a WITH CONNECTION LIMIT -1`,
`CREATE DATABASE a`,
},
{`CREATE TABLE a (b INT) WITH (fillfactor=100)`,
`CREATE TABLE a (b INT8)`},
{`CREATE TABLE a (b INT, UNIQUE INDEX foo (b))`,
Expand Down
26 changes: 22 additions & 4 deletions pkg/sql/parser/sql.y
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ func (u *sqlSymUnion) refreshDataOption() tree.RefreshDataOption {
%token <str> CHARACTER CHARACTERISTICS CHECK CLOSE
%token <str> CLUSTER COALESCE COLLATE COLLATION COLUMN COLUMNS COMMENT COMMENTS COMMIT
%token <str> COMMITTED COMPACT COMPLETE CONCAT CONCURRENTLY CONFIGURATION CONFIGURATIONS CONFIGURE
%token <str> CONFLICT CONSTRAINT CONSTRAINTS CONTAINS CONTROLCHANGEFEED CONTROLJOB
%token <str> CONFLICT CONNECTION CONSTRAINT CONSTRAINTS CONTAINS CONTROLCHANGEFEED CONTROLJOB
%token <str> CONVERSION CONVERT COPY COVERING CREATE CREATEDB CREATELOGIN CREATEROLE
%token <str> CROSS CUBE CURRENT CURRENT_CATALOG CURRENT_DATE CURRENT_SCHEMA
%token <str> CURRENT_ROLE CURRENT_TIME CURRENT_TIMESTAMP
Expand Down Expand Up @@ -922,6 +922,7 @@ func (u *sqlSymUnion) refreshDataOption() tree.RefreshDataOption {
%type <tree.ValidationBehavior> opt_validate_behavior

%type <str> opt_template_clause opt_encoding_clause opt_lc_collate_clause opt_lc_ctype_clause
%type <int32> opt_connection_limit

%type <tree.IsolationLevel> transaction_iso_level
%type <tree.UserPriority> transaction_user_priority
Expand Down Expand Up @@ -7214,17 +7215,18 @@ transaction_deferrable_mode:
// %Text: CREATE DATABASE [IF NOT EXISTS] <name>
// %SeeAlso: WEBDOCS/create-database.html
create_database_stmt:
CREATE DATABASE database_name opt_with opt_template_clause opt_encoding_clause opt_lc_collate_clause opt_lc_ctype_clause
CREATE DATABASE database_name opt_with opt_template_clause opt_encoding_clause opt_lc_collate_clause opt_lc_ctype_clause opt_connection_limit
{
$$.val = &tree.CreateDatabase{
Name: tree.Name($3),
Template: $5,
Encoding: $6,
Collate: $7,
CType: $8,
ConnectionLimit: $9.int32(),
}
}
| CREATE DATABASE IF NOT EXISTS database_name opt_with opt_template_clause opt_encoding_clause opt_lc_collate_clause opt_lc_ctype_clause
| CREATE DATABASE IF NOT EXISTS database_name opt_with opt_template_clause opt_encoding_clause opt_lc_collate_clause opt_lc_ctype_clause opt_connection_limit
{
$$.val = &tree.CreateDatabase{
IfNotExists: true,
Expand All @@ -7233,8 +7235,9 @@ create_database_stmt:
Encoding: $9,
Collate: $10,
CType: $11,
ConnectionLimit: $12.int32(),
}
}
}
| CREATE DATABASE error // SHOW HELP: CREATE DATABASE

opt_template_clause:
Expand Down Expand Up @@ -7277,6 +7280,20 @@ opt_lc_ctype_clause:
$$ = ""
}

opt_connection_limit:
CONNECTION LIMIT opt_equal signed_iconst
{
ret, err := $4.numVal().AsInt32()
if err != nil {
return setErr(sqllex, err)
}
$$.val = ret
}
| /* EMPTY */
{
$$.val = int32(-1)
}

opt_equal:
'=' {}
| /* EMPTY */ {}
Expand Down Expand Up @@ -11413,6 +11430,7 @@ unreserved_keyword:
| CONFIGURATION
| CONFIGURATIONS
| CONFIGURE
| CONNECTION
| CONSTRAINTS
| CONTROLCHANGEFEED
| CONTROLJOB
Expand Down
17 changes: 11 additions & 6 deletions pkg/sql/sem/tree/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,13 @@ import (

// CreateDatabase represents a CREATE DATABASE statement.
type CreateDatabase struct {
IfNotExists bool
Name Name
Template string
Encoding string
Collate string
CType string
IfNotExists bool
Name Name
Template string
Encoding string
Collate string
CType string
ConnectionLimit int32
}

// Format implements the NodeFormatter interface.
Expand All @@ -66,6 +67,10 @@ func (node *CreateDatabase) Format(ctx *FmtCtx) {
ctx.WriteString(" LC_CTYPE = ")
lex.EncodeSQLStringWithFlags(&ctx.Buffer, node.CType, ctx.flags.EncodeFlags())
}
if node.ConnectionLimit != -1 {
ctx.WriteString(" CONNECTION LIMIT = ")
ctx.WriteString(strconv.Itoa(int(node.ConnectionLimit)))
}
}

// IndexElem represents a column with a direction in a CREATE INDEX statement.
Expand Down

0 comments on commit 28979d5

Please sign in to comment.