Skip to content

Commit

Permalink
sql: implement COMMENT ON TABLE
Browse files Browse the repository at this point in the history
See cockroachdb#19472

Release note (sql change): support COMMENT ON TABLE syntax
  • Loading branch information
hueypark committed Nov 24, 2018
1 parent 9f54fc1 commit 672b4e8
Show file tree
Hide file tree
Showing 28 changed files with 357 additions and 12 deletions.
8 changes: 8 additions & 0 deletions docs/generated/sql/bnf/stmt_block.bnf
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ stmt ::=
'HELPTOKEN'
| preparable_stmt
| copy_from_stmt
| comment_stmt
| execute_stmt
| deallocate_stmt
| discard_stmt
Expand Down Expand Up @@ -46,6 +47,9 @@ preparable_stmt ::=
copy_from_stmt ::=
'COPY' table_name opt_column_list 'FROM' 'STDIN'

comment_stmt ::=
'COMMENT' 'ON' 'TABLE' table_name 'IS' comment_text

execute_stmt ::=
'EXECUTE' table_alias_name execute_param_clause

Expand Down Expand Up @@ -196,6 +200,10 @@ opt_column_list ::=
'(' name_list ')'
|

comment_text ::=
'SCONST'
| 'NULL'

table_alias_name ::=
name

Expand Down
2 changes: 2 additions & 0 deletions pkg/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2334,10 +2334,12 @@ writing ` + os.DevNull + `
debug/nodes/1/ranges/18
debug/nodes/1/ranges/19
debug/nodes/1/ranges/20
debug/nodes/1/ranges/21
debug/reports/problemranges
debug/schema/defaultdb@details
debug/schema/postgres@details
debug/schema/system@details
debug/schema/system/comments
debug/schema/system/descriptor
debug/schema/system/eventlog
debug/schema/system/jobs
Expand Down
4 changes: 4 additions & 0 deletions pkg/keys/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,4 +328,8 @@ const (
LocationsTableID = 21
LivenessRangesID = 22
RoleMembersTableID = 23
CommentsTableID = 24

// CommentType is type for system.comments
TableCommentType = 1
)
110 changes: 110 additions & 0 deletions pkg/sql/comment_on_table.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// Copyright 2018 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.

package sql

import (
"context"

"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/sql/privilege"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sqlbase"
)

type commentOnTableNode struct {
n *tree.CommentOnTable
tableDesc *MutableTableDescriptor
dbDesc *sqlbase.DatabaseDescriptor
}

// CommentOnTable add comment on a table.
// Privileges: CREATE on table.
// notes: postgres requires CREATE on the table.
// mysql requires ALTER, CREATE, INSERT on the table.
func (p *planner) CommentOnTable(ctx context.Context, n *tree.CommentOnTable) (planNode, error) {
tableDesc, err := p.ResolveMutableTableDescriptor(ctx, &n.Table, true, requireTableDesc)
if err != nil {
return nil, err
}
if tableDesc == nil {
return newZeroNode(nil /* columns */), nil
}

if err := p.CheckPrivilege(ctx, tableDesc, privilege.CREATE); err != nil {
return nil, err
}

dbDesc, err := getDatabaseDescByID(
ctx,
p.Txn(),
tableDesc.ParentID)
if err != nil {
return nil, err
}

return &commentOnTableNode{n: n, tableDesc: tableDesc, dbDesc: dbDesc}, nil
}

func (n *commentOnTableNode) startExec(params runParams) error {
h := makeOidHasher()
oid := h.TableOid(n.dbDesc, tree.PublicSchema, n.tableDesc.TableDesc())

if n.n.Comment != nil {
_, err := params.p.extendedEvalCtx.ExecCfg.InternalExecutor.Exec(
params.ctx,
"upsert-comment",
params.p.Txn(),
"UPSERT INTO system.comments VALUES ($1, $2, 0, $3)",
keys.TableCommentType,
oid.DInt,
*n.n.Comment)
if err != nil {
return err
}
} else {
_, err := params.p.extendedEvalCtx.ExecCfg.InternalExecutor.Exec(
params.ctx,
"delete-comment",
params.p.Txn(),
"DELETE FROM system.comments WHERE type=$1 AND object_id=$2 AND sub_id=0",
keys.TableCommentType,
oid.DInt)
if err != nil {
return err
}
}

return MakeEventLogger(params.extendedEvalCtx.ExecCfg).InsertEventRecord(
params.ctx,
params.p.txn,
EventLogCommentOnTable,
int32(n.tableDesc.ID),
int32(params.extendedEvalCtx.NodeID),
struct {
TableName string
Statement string
User string
Comment *string
}{
n.n.Table.FQString(),
n.n.String(),
params.SessionData().User,
n.n.Comment},
)
}

func (n *commentOnTableNode) Next(runParams) (bool, error) { return false, nil }
func (n *commentOnTableNode) Values() tree.Datums { return tree.Datums{} }
func (n *commentOnTableNode) Close(context.Context) {}
2 changes: 2 additions & 0 deletions pkg/sql/event_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ const (
EventLogTruncateTable EventLogType = "truncate_table"
// EventLogAlterTable is recorded when a table is altered.
EventLogAlterTable EventLogType = "alter_table"
// EventLogCommentOnTable is recorded when a table is commented.
EventLogCommentOnTable EventLogType = "comment_on_table"

// EventLogCreateIndex is recorded when an index is created.
EventLogCreateIndex EventLogType = "create_index"
Expand Down
2 changes: 2 additions & 0 deletions pkg/sql/expand_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ func doExpandPlan(
case *alterTableNode:
case *alterSequenceNode:
case *alterUserSetPasswordNode:
case *commentOnTableNode:
case *renameColumnNode:
case *renameDatabaseNode:
case *renameIndexNode:
Expand Down Expand Up @@ -854,6 +855,7 @@ func (p *planner) simplifyOrderings(plan planNode, usefulOrdering sqlbase.Column
case *alterTableNode:
case *alterSequenceNode:
case *alterUserSetPasswordNode:
case *commentOnTableNode:
case *renameColumnNode:
case *renameDatabaseNode:
case *renameIndexNode:
Expand Down
15 changes: 15 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/grant_table
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,16 @@ system public NULL admin GRANT
system public NULL admin SELECT
system public NULL root GRANT
system public NULL root SELECT
system public comments admin DELETE
system public comments admin GRANT
system public comments admin INSERT
system public comments admin SELECT
system public comments admin UPDATE
system public comments root DELETE
system public comments root GRANT
system public comments root INSERT
system public comments root SELECT
system public comments root UPDATE
system public descriptor admin GRANT
system public descriptor admin SELECT
system public descriptor root GRANT
Expand Down Expand Up @@ -315,6 +325,11 @@ system pg_catalog NULL root GRANT
system pg_catalog NULL root SELECT
system public NULL root GRANT
system public NULL root SELECT
system public comments root DELETE
system public comments root GRANT
system public comments root INSERT
system public comments root SELECT
system public comments root UPDATE
system public descriptor root GRANT
system public descriptor root SELECT
system public eventlog root DELETE
Expand Down
29 changes: 29 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/information_schema
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,7 @@ system public web_sessions BASE TABLE
system public table_statistics BASE TABLE YES 1
system public locations BASE TABLE YES 1
system public role_members BASE TABLE YES 1
system public comments BASE TABLE YES 1

statement ok
ALTER TABLE other_db.xyz ADD COLUMN j INT
Expand Down Expand Up @@ -627,6 +628,7 @@ FROM system.information_schema.table_constraints
ORDER BY TABLE_NAME, CONSTRAINT_TYPE, CONSTRAINT_NAME
----
constraint_catalog constraint_schema constraint_name table_catalog table_schema table_name constraint_type is_deferrable initially_deferred
system public primary system public comments PRIMARY KEY NO NO
system public primary system public descriptor PRIMARY KEY NO NO
system public primary system public eventlog PRIMARY KEY NO NO
system public primary system public jobs PRIMARY KEY NO NO
Expand All @@ -648,6 +650,9 @@ FROM system.information_schema.constraint_column_usage
ORDER BY TABLE_NAME, COLUMN_NAME, CONSTRAINT_NAME
----
table_catalog table_schema table_name column_name constraint_catalog constraint_schema constraint_name
system public comments object_id system public primary
system public comments sub_id system public primary
system public comments type system public primary
system public descriptor id system public primary
system public eventlog timestamp system public primary
system public eventlog uniqueID system public primary
Expand Down Expand Up @@ -729,6 +734,10 @@ WHERE table_schema != 'information_schema' AND table_schema != 'pg_catalog' AND
ORDER BY 3,4
----
table_catalog table_schema table_name column_name ordinal_position
system public comments comment 4
system public comments object_id 2
system public comments sub_id 3
system public comments type 1
system public descriptor descriptor 2
system public descriptor id 1
system public eventlog eventType 2
Expand Down Expand Up @@ -1130,6 +1139,16 @@ NULL public system pg_catalog pg_type
NULL public system pg_catalog pg_user SELECT NULL NULL
NULL public system pg_catalog pg_user_mapping SELECT NULL NULL
NULL public system pg_catalog pg_views SELECT NULL NULL
NULL admin system public comments DELETE NULL NULL
NULL admin system public comments GRANT NULL NULL
NULL admin system public comments INSERT NULL NULL
NULL admin system public comments SELECT NULL NULL
NULL admin system public comments UPDATE NULL NULL
NULL root system public comments DELETE NULL NULL
NULL root system public comments GRANT NULL NULL
NULL root system public comments INSERT NULL NULL
NULL root system public comments SELECT NULL NULL
NULL root system public comments UPDATE NULL NULL
NULL admin system public descriptor GRANT NULL NULL
NULL admin system public descriptor SELECT NULL NULL
NULL root system public descriptor GRANT NULL NULL
Expand Down Expand Up @@ -1482,6 +1501,16 @@ NULL root system public role_members
NULL root system public role_members INSERT NULL NULL
NULL root system public role_members SELECT NULL NULL
NULL root system public role_members UPDATE NULL NULL
NULL admin system public comments DELETE NULL NULL
NULL admin system public comments GRANT NULL NULL
NULL admin system public comments INSERT NULL NULL
NULL admin system public comments SELECT NULL NULL
NULL admin system public comments UPDATE NULL NULL
NULL root system public comments DELETE NULL NULL
NULL root system public comments GRANT NULL NULL
NULL root system public comments INSERT NULL NULL
NULL root system public comments SELECT NULL NULL
NULL root system public comments UPDATE NULL NULL

statement ok
CREATE TABLE other_db.xyz (i INT)
Expand Down
4 changes: 4 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/pg_catalog
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,7 @@ indexrelid indrelid indnatts indisunique indisprimary indisexclusion ind
633004885 1455563232 1 false false false false false true false false true false 5 0 0 0 NULL NULL
633004886 1455563232 1 true true false true false true false false true false 1 0 0 0 NULL NULL
961325075 2492394317 1 true true false true false true false false true false 1 0 0 0 NULL NULL
1112802205 3710069875 3 true true false true false true false false true false 1 2 3 0 0 0 0 0 0 0 0 0 NULL NULL
1168848597 1038690067 2 true true false true false true false false true false 1 7 0 0 0 0 0 0 NULL NULL
1849259112 3649853378 1 true true false true false true false false true false 1 0 0 0 NULL NULL
1849259115 3649853378 2 false false false false false true false false true false 2 3 1661428263 0 0 0 0 0 NULL NULL
Expand Down Expand Up @@ -623,6 +624,9 @@ indexrelid operator_argument_type_oid operator_argument_position
633004885 0 1
633004886 0 1
961325075 0 1
1112802205 0 1
1112802205 0 2
1112802205 0 3
1168848597 0 1
1168848597 0 2
1849259112 0 1
Expand Down
1 change: 1 addition & 0 deletions pkg/sql/logictest/testdata/logic_test/show_source
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ query T colnames
SELECT * FROM [SHOW TABLES FROM system]
----
table_name
comments
descriptor
eventlog
jobs
Expand Down
13 changes: 13 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/system
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ test
query T
SHOW TABLES FROM system
----
comments
descriptor
eventlog
jobs
Expand All @@ -33,6 +34,7 @@ SELECT * FROM system.namespace
0 postgres 51
0 system 1
0 test 52
1 comments 24
1 descriptor 3
1 eventlog 12
1 jobs 15
Expand Down Expand Up @@ -66,6 +68,7 @@ SELECT id FROM system.descriptor
20
21
23
24
50
51
52
Expand Down Expand Up @@ -188,6 +191,16 @@ system public root SELECT
query TTTTT
SHOW GRANTS ON system.*
----
system public comments admin DELETE
system public comments admin GRANT
system public comments admin INSERT
system public comments admin SELECT
system public comments admin UPDATE
system public comments root DELETE
system public comments root GRANT
system public comments root INSERT
system public comments root SELECT
system public comments root UPDATE
system public descriptor admin GRANT
system public descriptor admin SELECT
system public descriptor root GRANT
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/logictest/testdata/planner_test/explain
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ sort · ·
└── filter · ·
│ filter (table_catalog, table_schema, table_name) IN (('test', 'public', 'foo'),)
└── values · ·
· size 8 columns, 585 rows
· size 8 columns, 595 rows


query TTT
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/opt/exec/execbuilder/testdata/explain
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ sort · ·
└── filter · ·
│ filter (table_catalog, table_schema, table_name) IN (('test', 'public', 'foo'),)
└── values · ·
· size 8 columns, 585 rows
· size 8 columns, 595 rows


query TTT
Expand Down
1 change: 1 addition & 0 deletions pkg/sql/opt_filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,7 @@ func (p *planner) propagateFilters(
case *renameTableNode:
case *scrubNode:
case *truncateNode:
case *commentOnTableNode:
case *createDatabaseNode:
case *createIndexNode:
case *CreateUserNode:
Expand Down
1 change: 1 addition & 0 deletions pkg/sql/opt_limits.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ func (p *planner) applyLimit(plan planNode, numRows int64, soft bool) {
case *renameTableNode:
case *scrubNode:
case *truncateNode:
case *commentOnTableNode:
case *createDatabaseNode:
case *createIndexNode:
case *CreateUserNode:
Expand Down
1 change: 1 addition & 0 deletions pkg/sql/opt_needed.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ func setNeededColumns(plan planNode, needed []bool) {
case *renameTableNode:
case *scrubNode:
case *truncateNode:
case *commentOnTableNode:
case *createDatabaseNode:
case *createIndexNode:
case *CreateUserNode:
Expand Down
Loading

0 comments on commit 672b4e8

Please sign in to comment.