Skip to content

sql: support ALTER INDEX … NOT VISIBLE#86032

Merged
craig[bot] merged 1 commit intocockroachdb:masterfrom
wenyihu6:5.5-alter-index-planner
Aug 13, 2022
Merged

sql: support ALTER INDEX … NOT VISIBLE#86032
craig[bot] merged 1 commit intocockroachdb:masterfrom
wenyihu6:5.5-alter-index-planner

Conversation

@wenyihu6
Copy link
Copy Markdown
Contributor

@wenyihu6 wenyihu6 commented Aug 12, 2022

This commit adds support to execute ALTER INDEX … [VISIBLE | NOT VISIBLE].

Fixes: #72576

See also: #85473

Release note (sql change): Altering an index to visible or not visible using
ALTER INDEX … VISIBLE | NOT VISIBLE is now supported.

@wenyihu6 wenyihu6 requested a review from a team August 12, 2022 11:44
@wenyihu6 wenyihu6 requested a review from a team as a code owner August 12, 2022 11:44
@wenyihu6 wenyihu6 requested review from a team August 12, 2022 11:44
@wenyihu6 wenyihu6 requested a review from a team as a code owner August 12, 2022 11:44
@cockroach-teamcity
Copy link
Copy Markdown
Member

This change is Reviewable

@wenyihu6 wenyihu6 force-pushed the 5.5-alter-index-planner branch from e39f174 to 42f934f Compare August 12, 2022 11:56
@wenyihu6 wenyihu6 requested a review from michae2 August 12, 2022 11:57
@wenyihu6
Copy link
Copy Markdown
Contributor Author

The first two commits come from #85794, #85473.

Copy link
Copy Markdown
Collaborator

@michae2 michae2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! This :lgtm: Let's see if we can get someone from @cockroachdb/sql-schema to take a look as well.

Reviewed 6 of 6 files at r3, all commit messages.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @michae2)

Copy link
Copy Markdown
Collaborator

@michae2 michae2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @michae2 and @wenyihu6)


pkg/sql/alter_index_visible.go line 1 at r3 (raw file):

package sql

Above the package declaration, we need a copyright header:

// Copyright 2022 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.

Also, it looks like the crlfmt linter wants us to make the following changes:

    lint_test.go:1389:
        diff -u old/sql/alter_index_visible.go new/sql/alter_index_visible.go
        --- old/sql/alter_index_visible.go  2022-08-12 12:26:48.062773361 +0000
        +++ new/sql/alter_index_visible.go  2022-08-12 12:26:48.062773361 +0000
        @@ -2,6 +2,7 @@
         import (
           "context"
        +
           "github.com/cockroachdb/cockroach/pkg/sql/catalog"
           "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
           "github.com/cockroachdb/cockroach/pkg/sql/catalog/tabledesc"
        @@ -19,7 +20,9 @@
           index     catalog.Index
         }
        -func (p *planner) AlterIndexVisible(ctx context.Context, n *tree.AlterIndexVisible) (planNode, error) {
        +func (p *planner) AlterIndexVisible(
        +  ctx context.Context, n *tree.AlterIndexVisible,
        +) (planNode, error) {
           if err := checkSchemaChangeEnabled(
             ctx,
             p.ExecCfg(),

Copy link
Copy Markdown

@postamar postamar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3rd commit LGTM, only nits

if err := checkSchemaChangeEnabled(
ctx,
p.ExecCfg(),
"ALTER INDEX VISIBILITY",
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: define a constant

Relates to #85473 (comment)

// returned is nil. 2. No IfExists, error is returned.
_, tableDesc, err := expandMutableIndexName(ctx, p, &n.Index, !n.IfExists /* requireTable */)
if err != nil {
// Error if no table is found and IfExists is false.
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: these kinds of comments add little value but increase clutter, remove them.

Same comment applies below.

return newZeroNode(nil /* columns */), nil
}
// Error if no index exists and IfExists is not specified.
return nil, pgerror.WithCandidateCode(err, pgcode.UndefinedObject)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest wrapping this in an assertion error here, expandMutableIndexName promises that if tableDesc is not nil, then the index exists, so an error here should never happen. Mentioning that in the comments would be useful.

if n.n.NotVisible {
str = " not"
}
// TODO (wenyihu6): should we log notices if index is already what they want or is that too noisy?
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc @cockroachdb/sql-experience but I don't think so, if you do

CREATE TABLE t (i INT NOT NULL);
ALTER TABLE t ALTER COLUMN i SET NOT NULL;

that ALTER is entirely redundant yet there's no notice.

@wenyihu6
Copy link
Copy Markdown
Contributor Author

pkg/sql/alter_index_visible.go line 1 at r3 (raw file):

Previously, michae2 (Michael Erickson) wrote…

Above the package declaration, we need a copyright header:

// Copyright 2022 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.

Also, it looks like the crlfmt linter wants us to make the following changes:

    lint_test.go:1389:
        diff -u old/sql/alter_index_visible.go new/sql/alter_index_visible.go
        --- old/sql/alter_index_visible.go  2022-08-12 12:26:48.062773361 +0000
        +++ new/sql/alter_index_visible.go  2022-08-12 12:26:48.062773361 +0000
        @@ -2,6 +2,7 @@
         import (
           "context"
        +
           "github.com/cockroachdb/cockroach/pkg/sql/catalog"
           "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
           "github.com/cockroachdb/cockroach/pkg/sql/catalog/tabledesc"
        @@ -19,7 +20,9 @@
           index     catalog.Index
         }
        -func (p *planner) AlterIndexVisible(ctx context.Context, n *tree.AlterIndexVisible) (planNode, error) {
        +func (p *planner) AlterIndexVisible(
        +  ctx context.Context, n *tree.AlterIndexVisible,
        +) (planNode, error) {
           if err := checkSchemaChangeEnabled(
             ctx,
             p.ExecCfg(),

Done.

@wenyihu6
Copy link
Copy Markdown
Contributor Author

pkg/sql/alter_index_visible.go line 71 at r5 (raw file):

Previously, postamar (Marius Posta) wrote…

I suggest wrapping this in an assertion error here, expandMutableIndexName promises that if tableDesc is not nil, then the index exists, so an error here should never happen. Mentioning that in the comments would be useful.

I was also curious about this. When I tested it out, this errors when you specify the index name with a table that exists but the index does not exists. For example, t@idx returns an error if t exists but idx doesn't exist.

@wenyihu6
Copy link
Copy Markdown
Contributor Author

pkg/sql/alter_index_visible.go line 98 at r5 (raw file):

Previously, postamar (Marius Posta) wrote…

cc @cockroachdb/sql-experience but I don't think so, if you do

CREATE TABLE t (i INT NOT NULL);
ALTER TABLE t ALTER COLUMN i SET NOT NULL;

that ALTER is entirely redundant yet there's no notice.

That's true. I will remove this notice.

@wenyihu6 wenyihu6 force-pushed the 5.5-alter-index-planner branch from 45275a1 to 045f1fc Compare August 12, 2022 18:52
@wenyihu6
Copy link
Copy Markdown
Contributor Author

pkg/sql/alter_index_visible.go line 98 at r5 (raw file):

Previously, wenyihu6 (Wenyi Hu) wrote…

That's true. I will remove this notice.

Done.

@wenyihu6
Copy link
Copy Markdown
Contributor Author

pkg/sql/alter_index_visible.go line 39 at r5 (raw file):

Previously, postamar (Marius Posta) wrote…

nit: define a constant

Relates to #85473 (comment)

I'm not sure if I'm understanding correctly. Do you mean something like const feature = "ALTER INDEX VISIBILITY"? Other functions that call checkSchemaChangeEnabled don't seem to have this convention.

@wenyihu6 wenyihu6 force-pushed the 5.5-alter-index-planner branch from 045f1fc to 17ecc2f Compare August 13, 2022 13:54
This commit adds support to execute ALTER INDEX … [VISIBLE | NOT VISIBLE].

Fixes: cockroachdb#72576

See also: cockroachdb#85473

Release note (sql change): Altering an index to visible or not visible using
ALTER INDEX … VISIBLE | NOT VISIBLE is now supported.
@wenyihu6 wenyihu6 force-pushed the 5.5-alter-index-planner branch from 17ecc2f to 15613bb Compare August 13, 2022 13:55
@wenyihu6
Copy link
Copy Markdown
Contributor Author

TFTRs!!

bors r+

@craig
Copy link
Copy Markdown
Contributor

craig bot commented Aug 13, 2022

Build failed (retrying...):

@craig
Copy link
Copy Markdown
Contributor

craig bot commented Aug 13, 2022

Build succeeded:

@wenyihu6 wenyihu6 deleted the 5.5-alter-index-planner branch September 1, 2022 20:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

opt: add support for "invisible" indexes

4 participants