-
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.
33355: sql: support `COMMENT ON COLUMN` r=knz a=hueypark Informs #19472. This patch introduces support for table column comments. The syntax to set or delete a comment is the same as postgres: `COMMENT ON COLUMN ... IS ...`. See: https://www.postgresql.org/docs/9.1/sql-comment.html Release note (sql change): CockroachDB now supports associating comments to SQL table column using PostgreSQL's `COMMENT ON COLIMN` syntax. This also provides proper support for pg's `pg_catalog.pg_description` and built-in function `col_description()`. Co-authored-by: Jaewan Park <jaewan.huey.park@gmail.com>
- Loading branch information
Showing
21 changed files
with
525 additions
and
62 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
// 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 commentOnColumnNode struct { | ||
n *tree.CommentOnColumn | ||
tableDesc *MutableTableDescriptor | ||
} | ||
|
||
// CommentOnColumn add comment on a column. | ||
// Privileges: CREATE on table. | ||
func (p *planner) CommentOnColumn(ctx context.Context, n *tree.CommentOnColumn) (planNode, error) { | ||
tableName, err := tree.NormalizeTableName(&n.ColumnItem.TableName) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
tableDesc, err := p.ResolveMutableTableDescriptor(ctx, &tableName, true, requireTableDesc) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if err := p.CheckPrivilege(ctx, tableDesc, privilege.CREATE); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &commentOnColumnNode{n: n, tableDesc: tableDesc}, nil | ||
} | ||
|
||
func (n *commentOnColumnNode) startExec(params runParams) error { | ||
col, _, err := n.tableDesc.FindColumnByName(n.n.ColumnItem.ColumnName) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if n.n.Comment != nil { | ||
_, err := params.p.extendedEvalCtx.ExecCfg.InternalExecutor.Exec( | ||
params.ctx, | ||
"set-column-comment", | ||
params.p.Txn(), | ||
"UPSERT INTO system.comments VALUES ($1, $2, $3, $4)", | ||
keys.ColumnCommentType, | ||
n.tableDesc.ID, | ||
col.ID, | ||
*n.n.Comment) | ||
if err != nil { | ||
return err | ||
} | ||
} else { | ||
_, err := params.p.extendedEvalCtx.ExecCfg.InternalExecutor.Exec( | ||
params.ctx, | ||
"delete-column-comment", | ||
params.p.Txn(), | ||
"DELETE FROM system.comments WHERE type=$1 AND object_id=$2 AND sub_id=$3", | ||
keys.ColumnCommentType, | ||
n.tableDesc.ID, | ||
col.ID) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return MakeEventLogger(params.extendedEvalCtx.ExecCfg).InsertEventRecord( | ||
params.ctx, | ||
params.p.txn, | ||
EventLogCommentOnColumn, | ||
int32(n.tableDesc.ID), | ||
int32(params.extendedEvalCtx.NodeID), | ||
struct { | ||
TableName string | ||
ColumnName string | ||
Statement string | ||
User string | ||
Comment *string | ||
}{ | ||
n.tableDesc.Name, | ||
string(n.n.ColumnItem.ColumnName), | ||
n.n.String(), | ||
params.SessionData().User, | ||
n.n.Comment}, | ||
) | ||
} | ||
|
||
func (n *commentOnColumnNode) Next(runParams) (bool, error) { return false, nil } | ||
func (n *commentOnColumnNode) Values() tree.Datums { return tree.Datums{} } | ||
func (n *commentOnColumnNode) Close(context.Context) {} |
Oops, something went wrong.