Skip to content

Commit

Permalink
workload/schemachange: implement COMMENT ON
Browse files Browse the repository at this point in the history
This commit adds support for attaching comments to various schema
elements in the RSW. Notably, commenting on databases has been omitted
as the workload operates within the scope of a given database.

Unfortunately, `COMMENT ON` will consistently trigger the bug reported
in #72820. Therefore it is disabled by default.

Epic: CRDB-19168
Informs: CRDB-3265
Informs: #72820
Release note: None
  • Loading branch information
chrisseto committed Dec 20, 2023
1 parent 46c05d8 commit fb02cd9
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 32 deletions.
34 changes: 34 additions & 0 deletions pkg/workload/schemachange/operation_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2687,6 +2687,40 @@ func (og *operationGenerator) survive(ctx context.Context, tx pgx.Tx) (*opStmt,
return stmt, nil
}

func (og *operationGenerator) commentOn(ctx context.Context, tx pgx.Tx) (*opStmt, error) {
q := With([]CTE{
{"descriptors", descJSONQuery},
{"tables", tableDescQuery},
{"columns", `SELECT schema_id::REGNAMESPACE::TEXT as schema_name, name AS table_name, jsonb_array_elements(descriptor->'table'->'columns') AS column FROM tables`},
{"indexes", `SELECT schema_id::REGNAMESPACE::TEXT as schema_name, name AS table_name, jsonb_array_elements(descriptor->'table'->'indexes') AS index FROM tables`},
{"constraints", `SELECT schema_id::REGNAMESPACE::TEXT as schema_name, name AS table_name, jsonb_array_elements(descriptor->'table'->'checks') AS constraint FROM tables`},
}, `
SELECT 'SCHEMA ' || quote_ident(schema_name) FROM [SHOW SCHEMAS] WHERE owner != 'node'
UNION ALL
SELECT 'TABLE ' || quote_ident(schema_name) || '.' || quote_ident(table_name) FROM [SHOW TABLES] WHERE type = 'table'
UNION ALL
SELECT 'COLUMN ' || quote_ident(schema_name) || '.' || quote_ident(table_name) || '.' || quote_ident("column"->>'name') FROM columns
UNION ALL
SELECT 'INDEX ' || quote_ident(schema_name) || '.' || quote_ident(table_name) || '@' || quote_ident("index"->>'name') FROM indexes
UNION ALL
SELECT 'CONSTRAINT ' || quote_ident("constraint"->>'name') || ' ON ' || quote_ident(schema_name) || '.' || quote_ident(table_name) FROM constraints
`)

commentables, err := Collect(ctx, og, tx, pgx.RowTo[string], q)
if err != nil {
return nil, err
}

picked, err := PickOne(og.params.rng, commentables)
if err != nil {
return nil, err
}

stmt := makeOpStmt(OpStmtDDL)
stmt.sql = fmt.Sprintf(`COMMENT ON %s IS 'comment from the RSW'`, picked)
return stmt, nil
}

func (og *operationGenerator) insertRow(ctx context.Context, tx pgx.Tx) (stmt *opStmt, err error) {
tableName, err := og.randTable(ctx, tx, og.pctExisting(true), "")
if err != nil {
Expand Down
54 changes: 28 additions & 26 deletions pkg/workload/schemachange/optype.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ const (
createView // CREATE VIEW <view> AS <def>
createFunction // CREATE FUNCTION <function> ...

// COMMENT ON ...

commentOn // COMMENT ON [SCHEMA | TABLE | INDEX | COLUMN | CONSTRAINT] IS <comment>

// DROP ...

dropFunction // DROP FUNCTION <function>
Expand Down Expand Up @@ -169,12 +173,7 @@ const (
// alterTypeRename
// alterTypeRenameValue
// alterTypeSetSchema
// commentOnColumn
// commentOnConstraint
// commentOnDatabase
// commentOnIndex
// commentOnSchema
// commentOnTable
// createDatabase
// createRole
// createStats
Expand Down Expand Up @@ -230,6 +229,7 @@ var opFuncs = []func(*operationGenerator, context.Context, pgx.Tx) (*opStmt, err
alterTableSetColumnDefault: (*operationGenerator).setColumnDefault,
alterTableSetColumnNotNull: (*operationGenerator).setColumnNotNull,
alterTypeDropValue: (*operationGenerator).alterTypeDropValue,
commentOn: (*operationGenerator).commentOn,
createFunction: (*operationGenerator).createFunction,
createIndex: (*operationGenerator).createIndex,
createSchema: (*operationGenerator).createSchema,
Expand Down Expand Up @@ -257,46 +257,47 @@ var opWeights = []int{
validate: 2, // validate twice more often

// DDL Operations
alterTableAddColumn: 1,
alterTableDropConstraint: 0, // TODO(spaskob): unimplemented
alterTableAddConstraintForeignKey: 1, // Tentatively re-enabled, see #91195.
alterDatabaseAddRegion: 1,
alterDatabaseAddSuperRegion: 0, // Disabled and tracked with #111299
alterDatabaseDropSuperRegion: 0, // Disabled and tracked with #111299
alterDatabasePrimaryRegion: 0, // Disabled and tracked with #83831
alterDatabaseSurvivalGoal: 0, // Disabled and tracked with #83831
alterFunctionRename: 1,
alterFunctionSetSchema: 1,
alterTableAddColumn: 1,
alterTableAddConstraintForeignKey: 1, // Tentatively re-enabled, see #91195.
alterTableAddConstraintUnique: 0,
alterTableAlterColumnType: 0, // Disabled and tracked with #66662.
alterTableAlterPrimaryKey: 1,
alterTableDropColumn: 0,
alterTableDropColumnDefault: 1,
alterTableDropConstraint: 0, // TODO(spaskob): unimplemented
alterTableDropNotNull: 1,
alterTableDropStored: 1,
alterTableLocality: 1,
alterTableRenameColumn: 1,
alterTableSetColumnDefault: 1,
alterTableSetColumnNotNull: 1,
alterTypeDropValue: 0, // Disabled and tracked with #114844, #113859, and #115612.
commentOn: 0,
createFunction: 1,
createIndex: 1,
createSchema: 1,
createSequence: 1,
createTable: 1,
createTableAs: 1,
createView: 1,
createTypeEnum: 1,
createSchema: 1,
createFunction: 1,
alterFunctionSetSchema: 1,
alterFunctionRename: 1,
alterTableDropColumn: 0,
alterTableDropColumnDefault: 1,
alterTableDropNotNull: 1,
alterTableDropStored: 1,
createView: 1,
dropFunction: 1,
dropIndex: 1,
dropSchema: 0, // TODO Make a tracking issue
dropSequence: 1,
dropTable: 1,
dropView: 1,
alterTypeDropValue: 0, // Disabled and tracked with #114844, #113859, and #115612.
dropFunction: 1,
dropSchema: 0, // TODO Make a tracking issue
alterTableRenameColumn: 1,
renameIndex: 1,
renameSequence: 1,
renameTable: 1,
renameView: 1,
alterTableSetColumnDefault: 1,
alterTableSetColumnNotNull: 1,
alterTableAlterPrimaryKey: 1,
alterTableAlterColumnType: 0, // Disabled and tracked with #66662.
}

// This workload will maintain its own list of minimal supported versions for
Expand All @@ -311,9 +312,10 @@ var opDeclarativeVersion = map[opType]clusterversion.Key{
alterTableDropConstraint: clusterversion.MinSupported,
alterTableDropNotNull: clusterversion.MinSupported,
alterTypeDropValue: clusterversion.MinSupported,
commentOn: clusterversion.MinSupported,
createIndex: clusterversion.MinSupported,
createSequence: clusterversion.MinSupported,
createSchema: clusterversion.V23_2,
createSequence: clusterversion.MinSupported,
dropIndex: clusterversion.MinSupported,
dropSchema: clusterversion.MinSupported,
dropSequence: clusterversion.MinSupported,
Expand Down
15 changes: 9 additions & 6 deletions pkg/workload/schemachange/optype_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit fb02cd9

Please sign in to comment.