-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
32442: sql: implement `COMMENT ON TABLE` r=knz a=hueypark Informs #19472. This patch introduces support for table comments. The syntax to set or delete a comment is the same as postgres: `COMMENT ON TABLE ... IS ...`. See: postgresql.org/docs/9.1/sql-comment.html This also makes `pg_catalog.pg_description` and `obj_description()` do the right thing for compatibility with 3rd party clients. This is supported by a new system table `system.comments`, which is extensible to support comments on other database objects than tables: - its `type` column indicates the type of object, to distinguish between db, table, column and others. For now just one type is defined. - `object_id`: table or database ID, relative to the `type`. - `sub_id`: when a comment is placed on an object "inside" another, eg a column inside a table. - `comment`: the comment proper. This design of `system.comments` mimics pg's own `pg_description` which uses the same schema. Release note (sql change): CockroachDB now supports associating comments to SQL tables using PostgreSQL's `COMMENT ON TABLE` syntax. This also provides proper support for pg's `pg_catalog.pg_description` and built-in function `obj_description()`. Release note (sql change): The `SHOW TABLES` statement now supports printing out table comments using the optional phrase `WITH COMMENT`, e.g `SHOW TABLES FROM mydb WITH COMMENT`. Co-authored-by: Jaewan Park <jaewan.huey.park@gmail.com>
- Loading branch information
Showing
36 changed files
with
848 additions
and
184 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// 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" | ||
) | ||
|
||
type commentOnTableNode struct { | ||
n *tree.CommentOnTable | ||
tableDesc *MutableTableDescriptor | ||
} | ||
|
||
// 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 err := p.CheckPrivilege(ctx, tableDesc, privilege.CREATE); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &commentOnTableNode{n: n, tableDesc: tableDesc}, nil | ||
} | ||
|
||
func (n *commentOnTableNode) startExec(params runParams) error { | ||
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, | ||
n.tableDesc.ID, | ||
*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, | ||
n.tableDesc.ID) | ||
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) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
// 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_test | ||
|
||
import ( | ||
"context" | ||
gosql "database/sql" | ||
"testing" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/sql/tests" | ||
"github.com/cockroachdb/cockroach/pkg/testutils/serverutils" | ||
"github.com/cockroachdb/cockroach/pkg/util/leaktest" | ||
) | ||
|
||
func TestCommentOnTable(t *testing.T) { | ||
defer leaktest.AfterTest(t)() | ||
|
||
params, _ := tests.CreateTestServerParams() | ||
s, db, _ := serverutils.StartServer(t, params) | ||
defer s.Stopper().Stop(context.TODO()) | ||
|
||
if _, err := db.Exec(` | ||
CREATE DATABASE d; | ||
SET DATABASE = d; | ||
CREATE TABLE t (i INT ); | ||
`); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
testCases := []struct { | ||
exec string | ||
query string | ||
expect gosql.NullString | ||
}{ | ||
{ | ||
`COMMENT ON TABLE t IS 'foo'`, | ||
`SELECT obj_description('t'::regclass)`, | ||
gosql.NullString{String: `foo`, Valid: true}, | ||
}, | ||
{ | ||
`TRUNCATE t`, | ||
`SELECT obj_description('t'::regclass)`, | ||
gosql.NullString{String: `foo`, Valid: true}, | ||
}, | ||
{ | ||
`COMMENT ON TABLE t IS NULL`, | ||
`SELECT obj_description('t'::regclass)`, | ||
gosql.NullString{Valid: false}, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
if _, err := db.Exec(tc.exec); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
row := db.QueryRow(tc.query) | ||
var comment gosql.NullString | ||
if err := row.Scan(&comment); err != nil { | ||
t.Fatal(err) | ||
} | ||
if tc.expect != comment { | ||
t.Fatalf("expected comment %v, got %v", tc.expect, comment) | ||
} | ||
} | ||
} | ||
|
||
func TestCommentOnTableWhenDrop(t *testing.T) { | ||
defer leaktest.AfterTest(t)() | ||
|
||
params, _ := tests.CreateTestServerParams() | ||
s, db, _ := serverutils.StartServer(t, params) | ||
defer s.Stopper().Stop(context.TODO()) | ||
|
||
if _, err := db.Exec(` | ||
CREATE DATABASE d; | ||
SET DATABASE = d; | ||
CREATE TABLE t (i INT ); | ||
`); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if _, err := db.Exec(`COMMENT ON TABLE t IS 'foo'`); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if _, err := db.Exec(`DROP TABLE t`); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
row := db.QueryRow(`SELECT comment FROM system.comments LIMIT 1`) | ||
var comment string | ||
err := row.Scan(&comment) | ||
if err != gosql.ErrNoRows { | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
t.Fatal("dropped comment remain comment") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.