Skip to content

Commit

Permalink
sql: use qualifiable schema name for comment on schema
Browse files Browse the repository at this point in the history
Previously we only use current db to resolve a schema when comment
on a schema. This is painful at least for our testing sometimes
because we need to switch db before commenting on a schema.

Release justification: low impact but can be useful for users.
Release note (sql change): `COMMENT ON SCHEMA` now can use qualified
schema name. So can do both `COMMENT ON SCHEMA sc_name ...` and
`COMMENT ON SCHEMA db_name.sc_name ...`.
  • Loading branch information
chengxiong-ruan committed Mar 30, 2022
1 parent 2f4f3cd commit 029239a
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 15 deletions.
2 changes: 1 addition & 1 deletion docs/generated/sql/bnf/comment.bnf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
comment_stmt ::=
'COMMENT' 'ON' 'DATABASE' database_name 'IS' comment_text
| 'COMMENT' 'ON' 'SCHEMA' schema_name 'IS' comment_text
| 'COMMENT' 'ON' 'SCHEMA' qualifiable_schema_name 'IS' comment_text
| 'COMMENT' 'ON' 'TABLE' table_name 'IS' comment_text
| 'COMMENT' 'ON' 'COLUMN' column_name 'IS' comment_text
| 'COMMENT' 'ON' 'INDEX' table_index_name 'IS' comment_text
Expand Down
12 changes: 6 additions & 6 deletions docs/generated/sql/bnf/stmt_block.bnf
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ copy_from_stmt ::=

comment_stmt ::=
'COMMENT' 'ON' 'DATABASE' database_name 'IS' comment_text
| 'COMMENT' 'ON' 'SCHEMA' schema_name 'IS' comment_text
| 'COMMENT' 'ON' 'SCHEMA' qualifiable_schema_name 'IS' comment_text
| 'COMMENT' 'ON' 'TABLE' table_name 'IS' comment_text
| 'COMMENT' 'ON' 'COLUMN' column_path 'IS' comment_text
| 'COMMENT' 'ON' 'INDEX' table_index_name 'IS' comment_text
Expand Down Expand Up @@ -298,8 +298,9 @@ comment_text ::=
'SCONST'
| 'NULL'

schema_name ::=
qualifiable_schema_name ::=
name
| name '.' name

column_path ::=
name
Expand Down Expand Up @@ -1376,10 +1377,6 @@ privilege ::=
type_name_list ::=
( type_name ) ( ( ',' type_name ) )*

qualifiable_schema_name ::=
name
| name '.' name

type_list ::=
( typename ) ( ( ',' typename ) )*

Expand Down Expand Up @@ -2024,6 +2021,9 @@ alter_zone_partition_stmt ::=
| 'ALTER' 'PARTITION' partition_name 'OF' 'INDEX' table_index_name set_zone_config
| 'ALTER' 'PARTITION' partition_name 'OF' 'INDEX' table_name '@' '*' set_zone_config

schema_name ::=
name

opt_add_val_placement ::=
'BEFORE' 'SCONST'
| 'AFTER' 'SCONST'
Expand Down
10 changes: 7 additions & 3 deletions pkg/sql/comment_on_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,12 @@ func (p *planner) CommentOnSchema(ctx context.Context, n *tree.CommentOnSchema)
return nil, err
}

// Users can't create a schema without being connected to a DB.
dbName := p.CurrentDatabase()
var dbName string
if n.Name.ExplicitCatalog {
dbName = n.Name.Catalog()
} else {
dbName = p.CurrentDatabase()
}
if dbName == "" {
return nil, pgerror.New(pgcode.UndefinedDatabase,
"cannot comment schema without being connected to a database")
Expand All @@ -54,7 +58,7 @@ func (p *planner) CommentOnSchema(ctx context.Context, n *tree.CommentOnSchema)
}

schemaDesc, err := p.Descriptors().GetImmutableSchemaByID(ctx, p.txn,
db.GetSchemaID(string(n.Name)), tree.SchemaLookupFlags{Required: true})
db.GetSchemaID(n.Name.Schema()), tree.SchemaLookupFlags{Required: true})
if err != nil {
return nil, err
}
Expand Down
12 changes: 10 additions & 2 deletions pkg/sql/logictest/testdata/logic_test/schema
Original file line number Diff line number Diff line change
Expand Up @@ -854,10 +854,18 @@ statement ok
CREATE DATABASE comment_db

statement ok
USE comment_db
CREATE SCHEMA comment_db.foo

statement ok
COMMENT ON SCHEMA comment_db.foo IS 'foo'

query T
SELECT comment FROM system.comments LIMIT 1
----
foo

statement ok
CREATE SCHEMA foo
USE comment_db

statement ok
COMMENT ON SCHEMA foo IS 'bar'
Expand Down
4 changes: 2 additions & 2 deletions pkg/sql/parser/sql.y
Original file line number Diff line number Diff line change
Expand Up @@ -3682,9 +3682,9 @@ comment_stmt:
{
$$.val = &tree.CommentOnDatabase{Name: tree.Name($4), Comment: $6.strPtr()}
}
| COMMENT ON SCHEMA schema_name IS comment_text
| COMMENT ON SCHEMA qualifiable_schema_name IS comment_text
{
$$.val = &tree.CommentOnSchema{Name: tree.Name($4), Comment: $6.strPtr()}
$$.val = &tree.CommentOnSchema{Name: $4.objectNamePrefix(), Comment: $6.strPtr()}
}
| COMMENT ON TABLE table_name IS comment_text
{
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/sem/tree/comment_on_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import "github.com/cockroachdb/cockroach/pkg/sql/lexbase"

// CommentOnSchema represents an COMMENT ON SCHEMA statement.
type CommentOnSchema struct {
Name Name
Name ObjectNamePrefix
Comment *string
}

Expand Down

0 comments on commit 029239a

Please sign in to comment.