Skip to content

Commit

Permalink
Merge #39750
Browse files Browse the repository at this point in the history
39750: sql: support wildcard in ALTER PARTITION OF INDEX r=solongordon a=solongordon

sql: support wildcard in ALTER PARTITION OF INDEX

`ALTER PARTITION ... OF INDEX` now allows a user to specify all indexes
of a table via `tbl@*` syntax. This means that every partition with the
specified name across all of a table's indexes should be affected.

Refers #39357

Release note (sql change): The `ALTER PARTITION` statement now supports
applying a zone configuration to all the partitions of a table and its
indexes that share the same partition name. The syntax for this is `ALTER
PARTITION <partition name> OF INDEX <table name>@*`.

Co-authored-by: Solon Gordon <solon@cockroachlabs.com>
  • Loading branch information
craig[bot] and solongordon committed Aug 26, 2019
2 parents d0f2003 + 12a33d7 commit affe885
Show file tree
Hide file tree
Showing 10 changed files with 390 additions and 268 deletions.
6 changes: 0 additions & 6 deletions docs/generated/sql/bnf/alter_zone_index_stmt.bnf
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,3 @@ alter_zone_index_stmt ::=
| 'ALTER' 'INDEX' index_name 'CONFIGURE' 'ZONE' 'USING' variable '=' 'COPY' 'FROM' 'PARENT' ( ( ',' variable '=' value | ',' variable '=' 'COPY' 'FROM' 'PARENT' ) )*
| 'ALTER' 'INDEX' index_name 'CONFIGURE' 'ZONE' 'USING' variable '=' value ( ( ',' variable '=' value | ',' variable '=' 'COPY' 'FROM' 'PARENT' ) )*
| 'ALTER' 'INDEX' index_name 'CONFIGURE' 'ZONE' 'DISCARD'
| 'ALTER' 'PARTITION' partition_name 'OF' 'INDEX' table_name '@' index_name 'CONFIGURE' 'ZONE' 'USING' variable '=' 'COPY' 'FROM' 'PARENT' ( ( ',' variable '=' value | ',' variable '=' 'COPY' 'FROM' 'PARENT' ) )*
| 'ALTER' 'PARTITION' partition_name 'OF' 'INDEX' table_name '@' index_name 'CONFIGURE' 'ZONE' 'USING' variable '=' value ( ( ',' variable '=' value | ',' variable '=' 'COPY' 'FROM' 'PARENT' ) )*
| 'ALTER' 'PARTITION' partition_name 'OF' 'INDEX' table_name '@' index_name 'CONFIGURE' 'ZONE' 'DISCARD'
| 'ALTER' 'PARTITION' partition_name 'OF' 'INDEX' index_name 'CONFIGURE' 'ZONE' 'USING' variable '=' 'COPY' 'FROM' 'PARENT' ( ( ',' variable '=' value | ',' variable '=' 'COPY' 'FROM' 'PARENT' ) )*
| 'ALTER' 'PARTITION' partition_name 'OF' 'INDEX' index_name 'CONFIGURE' 'ZONE' 'USING' variable '=' value ( ( ',' variable '=' value | ',' variable '=' 'COPY' 'FROM' 'PARENT' ) )*
| 'ALTER' 'PARTITION' partition_name 'OF' 'INDEX' index_name 'CONFIGURE' 'ZONE' 'DISCARD'
13 changes: 13 additions & 0 deletions docs/generated/sql/bnf/alter_zone_partition_stmt.bnf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
alter_zone_partition_stmt ::=
'ALTER' 'PARTITION' partition_name 'OF' 'TABLE' table_name 'CONFIGURE' 'ZONE' 'USING' variable '=' 'COPY' 'FROM' 'PARENT' ( ( ',' variable '=' value | ',' variable '=' 'COPY' 'FROM' 'PARENT' ) )*
| 'ALTER' 'PARTITION' partition_name 'OF' 'TABLE' table_name 'CONFIGURE' 'ZONE' 'USING' variable '=' value ( ( ',' variable '=' value | ',' variable '=' 'COPY' 'FROM' 'PARENT' ) )*
| 'ALTER' 'PARTITION' partition_name 'OF' 'TABLE' table_name 'CONFIGURE' 'ZONE' 'DISCARD'
| 'ALTER' 'PARTITION' partition_name 'OF' 'INDEX' table_name '@' index_name 'CONFIGURE' 'ZONE' 'USING' variable '=' 'COPY' 'FROM' 'PARENT' ( ( ',' variable '=' value | ',' variable '=' 'COPY' 'FROM' 'PARENT' ) )*
| 'ALTER' 'PARTITION' partition_name 'OF' 'INDEX' table_name '@' index_name 'CONFIGURE' 'ZONE' 'USING' variable '=' value ( ( ',' variable '=' value | ',' variable '=' 'COPY' 'FROM' 'PARENT' ) )*
| 'ALTER' 'PARTITION' partition_name 'OF' 'INDEX' table_name '@' index_name 'CONFIGURE' 'ZONE' 'DISCARD'
| 'ALTER' 'PARTITION' partition_name 'OF' 'INDEX' index_name 'CONFIGURE' 'ZONE' 'USING' variable '=' 'COPY' 'FROM' 'PARENT' ( ( ',' variable '=' value | ',' variable '=' 'COPY' 'FROM' 'PARENT' ) )*
| 'ALTER' 'PARTITION' partition_name 'OF' 'INDEX' index_name 'CONFIGURE' 'ZONE' 'USING' variable '=' value ( ( ',' variable '=' value | ',' variable '=' 'COPY' 'FROM' 'PARENT' ) )*
| 'ALTER' 'PARTITION' partition_name 'OF' 'INDEX' index_name 'CONFIGURE' 'ZONE' 'DISCARD'
| 'ALTER' 'PARTITION' partition_name 'OF' 'INDEX' table_name '@' '*' 'CONFIGURE' 'ZONE' 'USING' variable '=' 'COPY' 'FROM' 'PARENT' ( ( ',' variable '=' value | ',' variable '=' 'COPY' 'FROM' 'PARENT' ) )*
| 'ALTER' 'PARTITION' partition_name 'OF' 'INDEX' table_name '@' '*' 'CONFIGURE' 'ZONE' 'USING' variable '=' value ( ( ',' variable '=' value | ',' variable '=' 'COPY' 'FROM' 'PARENT' ) )*
| 'ALTER' 'PARTITION' partition_name 'OF' 'INDEX' table_name '@' '*' 'CONFIGURE' 'ZONE' 'DISCARD'
3 changes: 0 additions & 3 deletions docs/generated/sql/bnf/alter_zone_table_stmt.bnf
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,3 @@ alter_zone_table_stmt ::=
'ALTER' 'TABLE' table_name 'CONFIGURE' 'ZONE' 'USING' variable '=' 'COPY' 'FROM' 'PARENT' ( ( ',' variable '=' value | ',' variable '=' 'COPY' 'FROM' 'PARENT' ) )*
| 'ALTER' 'TABLE' table_name 'CONFIGURE' 'ZONE' 'USING' variable '=' value ( ( ',' variable '=' value | ',' variable '=' 'COPY' 'FROM' 'PARENT' ) )*
| 'ALTER' 'TABLE' table_name 'CONFIGURE' 'ZONE' 'DISCARD'
| 'ALTER' 'PARTITION' partition_name 'OF' 'TABLE' table_name 'CONFIGURE' 'ZONE' 'USING' variable '=' 'COPY' 'FROM' 'PARENT' ( ( ',' variable '=' value | ',' variable '=' 'COPY' 'FROM' 'PARENT' ) )*
| 'ALTER' 'PARTITION' partition_name 'OF' 'TABLE' table_name 'CONFIGURE' 'ZONE' 'USING' variable '=' value ( ( ',' variable '=' value | ',' variable '=' 'COPY' 'FROM' 'PARENT' ) )*
| 'ALTER' 'PARTITION' partition_name 'OF' 'TABLE' table_name 'CONFIGURE' 'ZONE' 'DISCARD'
11 changes: 9 additions & 2 deletions docs/generated/sql/bnf/stmt_block.bnf
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ alter_ddl_stmt ::=
| alter_sequence_stmt
| alter_database_stmt
| alter_range_stmt
| alter_partition_stmt

alter_user_stmt ::=
alter_user_password_stmt
Expand Down Expand Up @@ -961,6 +962,9 @@ alter_database_stmt ::=
alter_range_stmt ::=
alter_zone_range_stmt

alter_partition_stmt ::=
alter_zone_partition_stmt

alter_user_password_stmt ::=
'ALTER' 'USER' string_or_placeholder 'WITH' 'PASSWORD' string_or_placeholder
| 'ALTER' 'USER' 'IF' 'EXISTS' string_or_placeholder 'WITH' 'PASSWORD' string_or_placeholder
Expand Down Expand Up @@ -1252,7 +1256,6 @@ alter_scatter_stmt ::=

alter_zone_table_stmt ::=
'ALTER' 'TABLE' table_name set_zone_config
| 'ALTER' 'PARTITION' partition_name 'OF' 'TABLE' table_name set_zone_config

alter_rename_table_stmt ::=
'ALTER' 'TABLE' relation_expr 'RENAME' 'TO' table_name
Expand Down Expand Up @@ -1280,7 +1283,6 @@ alter_rename_index_stmt ::=

alter_zone_index_stmt ::=
'ALTER' 'INDEX' table_index_name set_zone_config
| 'ALTER' 'PARTITION' partition_name 'OF' 'INDEX' table_index_name set_zone_config

alter_rename_view_stmt ::=
'ALTER' 'VIEW' relation_expr 'RENAME' 'TO' view_name
Expand All @@ -1303,6 +1305,11 @@ alter_zone_database_stmt ::=
alter_zone_range_stmt ::=
'ALTER' 'RANGE' zone_name set_zone_config

alter_zone_partition_stmt ::=
'ALTER' 'PARTITION' partition_name 'OF' 'TABLE' table_name set_zone_config
| 'ALTER' 'PARTITION' partition_name 'OF' 'INDEX' table_index_name set_zone_config
| 'ALTER' 'PARTITION' partition_name 'OF' 'INDEX' table_name '@' '*' set_zone_config

kv_option ::=
name '=' string_or_placeholder
| name
Expand Down
27 changes: 27 additions & 0 deletions pkg/ccl/logictestccl/testdata/logic_test/zone
Original file line number Diff line number Diff line change
Expand Up @@ -566,3 +566,30 @@ ALTER PARTITION p1 OF INDEX "my database".public.show_test@primary CONFIGURE ZON
constraints = '[+dc=dc1]';
ALTER PARTITION p2 OF INDEX "my database".public.show_test@primary CONFIGURE ZONE USING
constraints = '[+dc=dc2]'

subtest alter_partition_across_all_indexes

statement ok
CREATE TABLE t2 (x INT PRIMARY KEY) PARTITION BY LIST (x) (
PARTITION p1 VALUES IN (1),
PARTITION p2 VALUES IN (2)
);
CREATE INDEX x1 ON t2 (x) PARTITION BY LIST (x) (
PARTITION p1 VALUES IN (1),
PARTITION p2 VALUES IN (2)
);
CREATE INDEX x2 ON t2 (x) PARTITION BY LIST (x) (
PARTITION p1 VALUES IN (1),
PARTITION p2 VALUES IN (2)
);
ALTER PARTITION p1 OF INDEX t2@* CONFIGURE ZONE USING num_replicas = 1

query TT
SELECT * FROM [SHOW ALL ZONE CONFIGURATIONS] WHERE target LIKE '%t2@%'
----
PARTITION p1 OF INDEX "my database".public.t2@primary ALTER PARTITION p1 OF INDEX "my database".public.t2@primary CONFIGURE ZONE USING
num_replicas = 1
PARTITION p1 OF INDEX "my database".public.t2@x1 ALTER PARTITION p1 OF INDEX "my database".public.t2@x1 CONFIGURE ZONE USING
num_replicas = 1
PARTITION p1 OF INDEX "my database".public.t2@x2 ALTER PARTITION p1 OF INDEX "my database".public.t2@x2 CONFIGURE ZONE USING
num_replicas = 1
6 changes: 6 additions & 0 deletions pkg/cmd/docgen/diagrams.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,12 @@ var specs = []stmtSpec{
replace: map[string]string{"var_name": "variable", "var_value": "value"},
unlink: []string{"variable", "value"},
},
{
name: "alter_zone_partition_stmt",
inline: []string{"table_index_name", "set_zone_config", "var_set_list"},
replace: map[string]string{"var_name": "variable", "var_value": "value", "standalone_index_name": "index_name"},
unlink: []string{"variable", "value"},
},
{
name: "backup",
stmt: "backup_stmt",
Expand Down
3 changes: 3 additions & 0 deletions pkg/sql/parser/help_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ func TestContextualHelp(t *testing.T) {
{`ALTER RANGE foo CONFIGURE ??`, `ALTER RANGE`},
{`ALTER RANGE ??`, `ALTER RANGE`},

{`ALTER PARTITION ??`, `ALTER PARTITION`},
{`ALTER PARTITION p OF INDEX tbl@idx ??`, `ALTER PARTITION`},

{`CANCEL ??`, `CANCEL`},
{`CANCEL JOB ??`, `CANCEL JOBS`},
{`CANCEL JOBS ??`, `CANCEL JOBS`},
Expand Down
71 changes: 53 additions & 18 deletions pkg/sql/parser/sql.y
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,7 @@ func newNameFromStr(s string) *tree.Name {
%type <tree.Statement> alter_database_stmt
%type <tree.Statement> alter_user_stmt
%type <tree.Statement> alter_range_stmt
%type <tree.Statement> alter_partition_stmt

// ALTER RANGE
%type <tree.Statement> alter_zone_range_stmt
Expand All @@ -618,6 +619,9 @@ func newNameFromStr(s string) *tree.Name {
%type <tree.Statement> alter_relocate_lease_stmt
%type <tree.Statement> alter_zone_table_stmt

// ALTER PARTITION
%type <tree.Statement> alter_zone_partition_stmt

// ALTER DATABASE
%type <tree.Statement> alter_rename_database_stmt
%type <tree.Statement> alter_zone_database_stmt
Expand Down Expand Up @@ -1096,12 +1100,13 @@ alter_stmt:
| ALTER error // SHOW HELP: ALTER

alter_ddl_stmt:
alter_table_stmt // EXTEND WITH HELP: ALTER TABLE
| alter_index_stmt // EXTEND WITH HELP: ALTER INDEX
| alter_view_stmt // EXTEND WITH HELP: ALTER VIEW
| alter_sequence_stmt // EXTEND WITH HELP: ALTER SEQUENCE
| alter_database_stmt // EXTEND WITH HELP: ALTER DATABASE
| alter_range_stmt // EXTEND WITH HELP: ALTER RANGE
alter_table_stmt // EXTEND WITH HELP: ALTER TABLE
| alter_index_stmt // EXTEND WITH HELP: ALTER INDEX
| alter_view_stmt // EXTEND WITH HELP: ALTER VIEW
| alter_sequence_stmt // EXTEND WITH HELP: ALTER SEQUENCE
| alter_database_stmt // EXTEND WITH HELP: ALTER DATABASE
| alter_range_stmt // EXTEND WITH HELP: ALTER RANGE
| alter_partition_stmt // EXTEND WITH HELP: ALTER PARTITION

// %Help: ALTER TABLE - change the definition of a table
// %Category: DDL
Expand Down Expand Up @@ -1129,7 +1134,6 @@ alter_ddl_stmt:
// ALTER TABLE ... PARTITION BY LIST ( <name...> ) ( <listspec> )
// ALTER TABLE ... PARTITION BY NOTHING
// ALTER TABLE ... CONFIGURE ZONE <zoneconfig>
// ALTER PARTITION ... OF TABLE ... CONFIGURE ZONE <zoneconfig>
//
// Column qualifiers:
// [CONSTRAINT <constraintname>] {NULL | NOT NULL | UNIQUE | PRIMARY KEY | CHECK (<expr>) | DEFAULT <expr>}
Expand All @@ -1156,7 +1160,26 @@ alter_table_stmt:
// ALTER TABLE has its error help token here because the ALTER TABLE
// prefix is spread over multiple non-terminals.
| ALTER TABLE error // SHOW HELP: ALTER TABLE
| ALTER PARTITION error // SHOW HELP: ALTER TABLE

// %Help: ALTER PARTITION - apply zone configurations to a partition
// %Category: DDL
// %Text:
// ALTER PARTITION <name> <command>
//
// Commands:
// ALTER PARTITION ... OF TABLE ... CONFIGURE ZONE <zoneconfig>
// ALTER PARTITION ... OF INDEX ... CONFIGURE ZONE <zoneconfig>
//
// Zone configurations:
// DISCARD
// USING <var> = <expr> [, ...]
// USING <var> = COPY FROM PARENT [, ...]
// { TO | = } <expr>
//
// %SeeAlso: WEBDOCS/configure-zone.html
alter_partition_stmt:
alter_zone_partition_stmt
| ALTER PARTITION error // SHOW HELP: ALTER PARTITION

// %Help: ALTER VIEW - change the definition of a view
// %Category: DDL
Expand Down Expand Up @@ -1245,7 +1268,6 @@ alter_range_stmt:
// ALTER INDEX ... UNSPLIT AT <selectclause>
// ALTER INDEX ... UNSPLIT ALL
// ALTER INDEX ... SCATTER [ FROM ( <exprs...> ) TO ( <exprs...> ) ]
// ALTER PARTITION ... OF INDEX ... CONFIGURE ZONE <zoneconfig>
//
// Zone configurations:
// DISCARD
Expand Down Expand Up @@ -1432,23 +1454,25 @@ alter_zone_table_stmt:
}
$$.val = s
}
| ALTER PARTITION partition_name OF TABLE table_name set_zone_config

alter_zone_index_stmt:
ALTER INDEX table_index_name set_zone_config
{
name := $6.unresolvedObjectName().ToTableName()
s := $7.setZoneConfig()
s := $4.setZoneConfig()
s.ZoneSpecifier = tree.ZoneSpecifier{
TableOrIndex: tree.TableIndexName{Table: name},
Partition: tree.Name($3),
TableOrIndex: $3.tableIndexName(),
}
$$.val = s
}

alter_zone_index_stmt:
ALTER INDEX table_index_name set_zone_config
alter_zone_partition_stmt:
ALTER PARTITION partition_name OF TABLE table_name set_zone_config
{
s := $4.setZoneConfig()
name := $6.unresolvedObjectName().ToTableName()
s := $7.setZoneConfig()
s.ZoneSpecifier = tree.ZoneSpecifier{
TableOrIndex: $3.tableIndexName(),
TableOrIndex: tree.TableIndexName{Table: name},
Partition: tree.Name($3),
}
$$.val = s
}
Expand All @@ -1461,6 +1485,17 @@ alter_zone_index_stmt:
}
$$.val = s
}
| ALTER PARTITION partition_name OF INDEX table_name '@' '*' set_zone_config
{
name := $6.unresolvedObjectName().ToTableName()
s := $9.setZoneConfig()
s.ZoneSpecifier = tree.ZoneSpecifier{
TableOrIndex: tree.TableIndexName{Table: name},
Partition: tree.Name($3),
}
s.AllIndexes = true
$$.val = s
}

var_set_list:
var_name '=' COPY FROM PARENT
Expand Down
8 changes: 8 additions & 0 deletions pkg/sql/sem/tree/zone.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ func (node ZoneSpecifier) TargetsIndex() bool {
return node.TargetsTable() && node.TableOrIndex.Index != ""
}

// TargetsPartition returns whether the zone specifier targets a partition.
func (node ZoneSpecifier) TargetsPartition() bool {
return node.TargetsTable() && node.Partition != ""
}

// Format implements the NodeFormatter interface.
func (node *ZoneSpecifier) Format(ctx *FmtCtx) {
if node.NamedZone != "" {
Expand Down Expand Up @@ -79,6 +84,9 @@ func (node *ShowZoneConfig) Format(ctx *FmtCtx) {
// statement.
type SetZoneConfig struct {
ZoneSpecifier
// AllIndexes indicates that the zone configuration should be applied across
// all of a tables indexes. (ALTER PARTITION ... OF INDEX <tablename>@*)
AllIndexes bool
SetDefault bool
YAMLConfig Expr
Options KVOptions
Expand Down
Loading

0 comments on commit affe885

Please sign in to comment.