Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

release-20.2: sql: allow CONNECTION LIMIT syntax for CREATE DATABASE #54421

Merged
merged 1 commit into from
Sep 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -2100,6 +2105,10 @@ opt_equal ::=
'='
|

signed_iconst ::=
'ICONST'
| only_signed_iconst

opt_name ::=
name
|
Expand Down Expand Up @@ -2730,10 +2739,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 @@ -7213,17 +7214,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 @@ -7232,8 +7234,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 @@ -7276,6 +7279,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 @@ -11392,6 +11409,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