diff --git a/docs/generated/sql/bnf/BUILD.bazel b/docs/generated/sql/bnf/BUILD.bazel index 8f80a01399d4..9be5f5e46728 100644 --- a/docs/generated/sql/bnf/BUILD.bazel +++ b/docs/generated/sql/bnf/BUILD.bazel @@ -18,7 +18,6 @@ FILES = [ "alter_index_stmt", "alter_partition_stmt", "alter_primary_key", - "alter_range_relocate_lease_stmt", "alter_range_relocate_stmt", "alter_range_stmt", "alter_rename_view_stmt", diff --git a/docs/generated/sql/bnf/alter_range_relocate_lease_stmt.bnf b/docs/generated/sql/bnf/alter_range_relocate_lease_stmt.bnf deleted file mode 100644 index 95b9c4bea0c7..000000000000 --- a/docs/generated/sql/bnf/alter_range_relocate_lease_stmt.bnf +++ /dev/null @@ -1,3 +0,0 @@ -alter_range_relocate_lease_stmt ::= - 'ALTER' 'RANGE' relocate_kw 'LEASE' 'TO' iconst64 'FOR' select_stmt - | 'ALTER' 'RANGE' iconst64 relocate_kw 'LEASE' 'TO' iconst64 diff --git a/docs/generated/sql/bnf/alter_range_relocate_stmt.bnf b/docs/generated/sql/bnf/alter_range_relocate_stmt.bnf index af265852b67f..0e7f675e191a 100644 --- a/docs/generated/sql/bnf/alter_range_relocate_stmt.bnf +++ b/docs/generated/sql/bnf/alter_range_relocate_stmt.bnf @@ -1,5 +1,5 @@ alter_range_relocate_stmt ::= - 'ALTER' 'RANGE' relocate_kw voters_kw 'FROM' iconst64 'TO' iconst64 'FOR' select_stmt - | 'ALTER' 'RANGE' iconst64 relocate_kw voters_kw 'FROM' iconst64 'TO' iconst64 - | 'ALTER' 'RANGE' relocate_kw 'NON_VOTERS' 'FROM' iconst64 'TO' iconst64 'FOR' select_stmt - | 'ALTER' 'RANGE' iconst64 relocate_kw 'NON_VOTERS' 'FROM' iconst64 'TO' iconst64 + 'ALTER' 'RANGE' relocate_kw 'LEASE' 'TO' iconst64 'FOR' select_stmt + | 'ALTER' 'RANGE' iconst64 relocate_kw 'LEASE' 'TO' iconst64 + | 'ALTER' 'RANGE' relocate_kw relocate_subject_nonlease 'FROM' iconst64 'TO' iconst64 'FOR' select_stmt + | 'ALTER' 'RANGE' iconst64 relocate_kw relocate_subject_nonlease 'FROM' iconst64 'TO' iconst64 diff --git a/docs/generated/sql/bnf/alter_range_stmt.bnf b/docs/generated/sql/bnf/alter_range_stmt.bnf index f77f9af42222..f8a65e7db6c0 100644 --- a/docs/generated/sql/bnf/alter_range_stmt.bnf +++ b/docs/generated/sql/bnf/alter_range_stmt.bnf @@ -1,4 +1,3 @@ alter_range_stmt ::= alter_zone_range_stmt - | alter_range_relocate_lease_stmt | alter_range_relocate_stmt diff --git a/docs/generated/sql/bnf/stmt_block.bnf b/docs/generated/sql/bnf/stmt_block.bnf index 92d70c8a58f5..e1c85205b317 100644 --- a/docs/generated/sql/bnf/stmt_block.bnf +++ b/docs/generated/sql/bnf/stmt_block.bnf @@ -1038,7 +1038,7 @@ unreserved_keyword ::= | 'NOCONTROLJOB' | 'NOLOGIN' | 'NOMODIFYCLUSTERSETTING' - | 'NON_VOTERS' + | 'NONVOTERS' | 'NOVIEWACTIVITY' | 'NOWAIT' | 'NULLS' @@ -1354,7 +1354,6 @@ alter_database_stmt ::= alter_range_stmt ::= alter_zone_range_stmt - | alter_range_relocate_lease_stmt | alter_range_relocate_stmt alter_partition_stmt ::= @@ -1846,15 +1845,11 @@ alter_database_primary_region_stmt ::= alter_zone_range_stmt ::= 'ALTER' 'RANGE' zone_name set_zone_config -alter_range_relocate_lease_stmt ::= +alter_range_relocate_stmt ::= 'ALTER' 'RANGE' relocate_kw 'LEASE' 'TO' iconst64 'FOR' select_stmt | 'ALTER' 'RANGE' iconst64 relocate_kw 'LEASE' 'TO' iconst64 - -alter_range_relocate_stmt ::= - 'ALTER' 'RANGE' relocate_kw voters_kw 'FROM' iconst64 'TO' iconst64 'FOR' select_stmt - | 'ALTER' 'RANGE' iconst64 relocate_kw voters_kw 'FROM' iconst64 'TO' iconst64 - | 'ALTER' 'RANGE' relocate_kw 'NON_VOTERS' 'FROM' iconst64 'TO' iconst64 'FOR' select_stmt - | 'ALTER' 'RANGE' iconst64 relocate_kw 'NON_VOTERS' 'FROM' iconst64 'TO' iconst64 + | 'ALTER' 'RANGE' relocate_kw relocate_subject_nonlease 'FROM' iconst64 'TO' iconst64 'FOR' select_stmt + | 'ALTER' 'RANGE' iconst64 relocate_kw relocate_subject_nonlease 'FROM' iconst64 'TO' iconst64 alter_zone_partition_stmt ::= 'ALTER' 'PARTITION' partition_name 'OF' 'TABLE' table_name set_zone_config @@ -2336,9 +2331,10 @@ relocate_kw ::= | 'EXPERIMENTAL_RELOCATE' | 'RELOCATE' -voters_kw ::= +relocate_subject_nonlease ::= 'VOTERS' | + | 'NONVOTERS' alter_default_privileges_target_object ::= 'TABLES' diff --git a/pkg/ccl/kvccl/kvfollowerreadsccl/followerreads_test.go b/pkg/ccl/kvccl/kvfollowerreadsccl/followerreads_test.go index 414189919e15..610161725256 100644 --- a/pkg/ccl/kvccl/kvfollowerreadsccl/followerreads_test.go +++ b/pkg/ccl/kvccl/kvfollowerreadsccl/followerreads_test.go @@ -642,7 +642,7 @@ func TestFollowerReadsWithStaleDescriptor(t *testing.T) { // Remove the follower and add a new non-voter to n3. n2 will no longer have a // replica. n1.Exec(t, `ALTER TABLE test EXPERIMENTAL_RELOCATE VOTERS VALUES (ARRAY[1], 1)`) - n1.Exec(t, `ALTER TABLE test EXPERIMENTAL_RELOCATE NON_VOTERS VALUES (ARRAY[3], 1)`) + n1.Exec(t, `ALTER TABLE test EXPERIMENTAL_RELOCATE NONVOTERS VALUES (ARRAY[3], 1)`) // Execute the query again and assert the cache is updated. This query will // not be executed as a follower read since it attempts to use n2 which diff --git a/pkg/sql/distsql_spec_exec_factory.go b/pkg/sql/distsql_spec_exec_factory.go index 2057afe5c4dc..cb7fe977e326 100644 --- a/pkg/sql/distsql_spec_exec_factory.go +++ b/pkg/sql/distsql_spec_exec_factory.go @@ -1070,13 +1070,13 @@ func (e *distSQLSpecExecFactory) ConstructAlterTableUnsplitAll(index cat.Index) } func (e *distSQLSpecExecFactory) ConstructAlterTableRelocate( - index cat.Index, input exec.Node, relocateLease bool, relocateNonVoters bool, + index cat.Index, input exec.Node, relocateSubject tree.RelocateSubject, ) (exec.Node, error) { return nil, unimplemented.NewWithIssue(47473, "experimental opt-driven distsql planning: alter table relocate") } func (e *distSQLSpecExecFactory) ConstructAlterRangeRelocate( - input exec.Node, relocateLease bool, relocateNonVoters bool, toStoreID int64, fromStoreID int64, + input exec.Node, relocateSubject tree.RelocateSubject, toStoreID int64, fromStoreID int64, ) (exec.Node, error) { return nil, unimplemented.NewWithIssue(47473, "experimental opt-driven distsql planning: alter range relocate") } diff --git a/pkg/sql/opt/exec/execbuilder/statement.go b/pkg/sql/opt/exec/execbuilder/statement.go index 06bb42d5156a..84f71059a1b4 100644 --- a/pkg/sql/opt/exec/execbuilder/statement.go +++ b/pkg/sql/opt/exec/execbuilder/statement.go @@ -222,8 +222,7 @@ func (b *Builder) buildAlterTableRelocate(relocate *memo.AlterTableRelocateExpr) node, err := b.factory.ConstructAlterTableRelocate( table.Index(relocate.Index), input.root, - relocate.RelocateLease, - relocate.RelocateNonVoters, + relocate.SubjectReplicas, ) if err != nil { return execPlan{}, err @@ -238,8 +237,7 @@ func (b *Builder) buildAlterRangeRelocate(relocate *memo.AlterRangeRelocateExpr) } node, err := b.factory.ConstructAlterRangeRelocate( input.root, - relocate.RelocateLease, - relocate.RelocateNonVoters, + relocate.SubjectReplicas, relocate.ToStoreID, relocate.FromStoreID, ) diff --git a/pkg/sql/opt/exec/factory.opt b/pkg/sql/opt/exec/factory.opt index e28650c3ceae..79e33ef5cb8e 100644 --- a/pkg/sql/opt/exec/factory.opt +++ b/pkg/sql/opt/exec/factory.opt @@ -658,8 +658,7 @@ define AlterTableUnsplitAll { define AlterTableRelocate { Index cat.Index input exec.Node - relocateLease bool - relocateNonVoters bool + subjectReplicas tree.RelocateSubject } # Buffer passes through the input rows but also saves them in a buffer, which @@ -736,8 +735,7 @@ define Export { # AlterTableRelocate implements ALTER RANGE RELOCATE. define AlterRangeRelocate { input exec.Node - relocateLease bool - relocateNonVoters bool + subjectReplicas tree.RelocateSubject toStoreID int64 fromStoreID int64 } diff --git a/pkg/sql/opt/memo/expr_format.go b/pkg/sql/opt/memo/expr_format.go index eda2076eefa3..73ae2f688f87 100644 --- a/pkg/sql/opt/memo/expr_format.go +++ b/pkg/sql/opt/memo/expr_format.go @@ -1502,11 +1502,12 @@ func FormatPrivate(f *ExprFmtCtx, private interface{}, physProps *physical.Requi case *AlterTableRelocatePrivate: FormatPrivate(f, &t.AlterTableSplitPrivate, nil) - if t.RelocateLease { + switch t.SubjectReplicas { + case tree.RelocateLease: f.Buffer.WriteString(" [lease]") - } else if t.RelocateNonVoters { + case tree.RelocateNonVoters: f.Buffer.WriteString(" [non-voters]") - } else { + case tree.RelocateVoters: f.Buffer.WriteString(" [voters]") } diff --git a/pkg/sql/opt/memo/interner.go b/pkg/sql/opt/memo/interner.go index 9869f8d4ddf1..93323371c2a3 100644 --- a/pkg/sql/opt/memo/interner.go +++ b/pkg/sql/opt/memo/interner.go @@ -568,6 +568,10 @@ func (h *hasher) HashIndexOrdinal(val cat.IndexOrdinal) { h.HashInt(val) } +func (h *hasher) HashRelocateSubject(val tree.RelocateSubject) { + h.HashUint64(uint64(val)) +} + func (h *hasher) HashIndexOrdinals(val cat.IndexOrdinals) { hash := h.hash for _, ord := range val { @@ -978,6 +982,10 @@ func (h *hasher) IsIndexOrdinalEqual(l, r cat.IndexOrdinal) bool { return l == r } +func (h *hasher) IsRelocateSubjectEqual(l, r tree.RelocateSubject) bool { + return l == r +} + func (h *hasher) IsIndexOrdinalsEqual(l, r cat.IndexOrdinals) bool { if len(l) != len(r) { return false diff --git a/pkg/sql/opt/ops/statement.opt b/pkg/sql/opt/ops/statement.opt index e6d8e7f8f6b0..f5f71708320b 100644 --- a/pkg/sql/opt/ops/statement.opt +++ b/pkg/sql/opt/ops/statement.opt @@ -191,8 +191,7 @@ define AlterTableRelocate { [Private] define AlterTableRelocatePrivate { - RelocateLease bool - RelocateNonVoters bool + SubjectReplicas RelocateSubject _ AlterTableSplitPrivate } @@ -303,8 +302,7 @@ define AlterRangeRelocate { [Private] define AlterRangeRelocatePrivate { - RelocateLease bool - RelocateNonVoters bool + SubjectReplicas RelocateSubject ToStoreID int64 FromStoreID int64 diff --git a/pkg/sql/opt/optbuilder/alter_range.go b/pkg/sql/opt/optbuilder/alter_range.go index b4a775cac1df..dd41efc87522 100644 --- a/pkg/sql/opt/optbuilder/alter_range.go +++ b/pkg/sql/opt/optbuilder/alter_range.go @@ -33,10 +33,7 @@ func (b *Builder) buildAlterRangeRelocate( outScope = inScope.push() b.synthesizeResultColumns(outScope, colinfo.AlterTableRelocateColumns) - cmdName := "RELOCATE" - if relocate.RelocateLease { - cmdName += " LEASE" - } + cmdName := "RELOCATE " + relocate.SubjectReplicas.String() colNames := []string{"range ids"} colTypes := []*types.T{types.Int} @@ -51,12 +48,11 @@ func (b *Builder) buildAlterRangeRelocate( outScope.expr = b.factory.ConstructAlterRangeRelocate( inputScope.expr, &memo.AlterRangeRelocatePrivate{ - RelocateLease: relocate.RelocateLease, - RelocateNonVoters: relocate.RelocateNonVoters, - ToStoreID: relocate.ToStoreID, - FromStoreID: relocate.FromStoreID, - Columns: colsToColList(outScope.cols), - Props: physical.MinRequired, + SubjectReplicas: relocate.SubjectReplicas, + ToStoreID: relocate.ToStoreID, + FromStoreID: relocate.FromStoreID, + Columns: colsToColList(outScope.cols), + Props: physical.MinRequired, }, ) return outScope diff --git a/pkg/sql/opt/optbuilder/alter_table.go b/pkg/sql/opt/optbuilder/alter_table.go index de8086b5bb32..8a36f6d50239 100644 --- a/pkg/sql/opt/optbuilder/alter_table.go +++ b/pkg/sql/opt/optbuilder/alter_table.go @@ -163,8 +163,8 @@ func (b *Builder) buildAlterTableRelocate( // The first column is the target leaseholder or the relocation array, // depending on variant. - cmdName := "EXPERIMENTAL_RELOCATE" - if relocate.RelocateLease { + cmdName := "RELOCATE" + if relocate.SubjectReplicas == tree.RelocateLease { cmdName += " LEASE" colNames = append([]string{"target leaseholder"}, colNames...) colTypes = append([]*types.T{types.Int}, colTypes...) @@ -181,8 +181,7 @@ func (b *Builder) buildAlterTableRelocate( outScope.expr = b.factory.ConstructAlterTableRelocate( inputScope.expr, &memo.AlterTableRelocatePrivate{ - RelocateLease: relocate.RelocateLease, - RelocateNonVoters: relocate.RelocateNonVoters, + SubjectReplicas: relocate.SubjectReplicas, AlterTableSplitPrivate: memo.AlterTableSplitPrivate{ Table: b.factory.Metadata().AddTable(table, &tn), Index: index.Ordinal(), diff --git a/pkg/sql/opt/optbuilder/testdata/alter_range b/pkg/sql/opt/optbuilder/testdata/alter_range index 0f7b64949019..5cff2736dc39 100644 --- a/pkg/sql/opt/optbuilder/testdata/alter_range +++ b/pkg/sql/opt/optbuilder/testdata/alter_range @@ -6,7 +6,7 @@ CREATE TABLE abc (a INT PRIMARY KEY, b INT, c STRING, INDEX b (b), UNIQUE INDEX build ALTER RANGE 1 RELOCATE FROM 1 TO 2 ---- -alter-range-relocate &{false false 2 1 [3 4 5] []} +alter-range-relocate &{VOTERS 2 1 [3 4 5] []} ├── columns: range_id:3 pretty:4 result:5 └── values ├── columns: column1:6!null @@ -15,7 +15,7 @@ alter-range-relocate &{false false 2 1 [3 4 5] []} build ALTER RANGE RELOCATE FROM 1 TO 2 FOR SELECT a from abc ---- -alter-range-relocate &{false false 2 1 [3 4 5] []} +alter-range-relocate &{VOTERS 2 1 [3 4 5] []} ├── columns: range_id:3 pretty:4 result:5 └── project ├── columns: a:6!null @@ -30,21 +30,21 @@ error (42601): at or near "relocate": syntax error build ALTER RANGE RELOCATE FROM 1 TO 2 FOR SELECT c from abc ---- -error (42601): RELOCATE data column 1 (range ids) must be of type int, not type string +error (42601): RELOCATE VOTERS data column 1 (range ids) must be of type int, not type string build -ALTER RANGE 1 RELOCATE NON_VOTERS FROM 1 TO 2 +ALTER RANGE 1 RELOCATE NONVOTERS FROM 1 TO 2 ---- -alter-range-relocate &{false true 2 1 [3 4 5] []} +alter-range-relocate &{NONVOTERS 2 1 [3 4 5] []} ├── columns: range_id:3 pretty:4 result:5 └── values ├── columns: column1:6!null └── (1,) build -ALTER RANGE RELOCATE NON_VOTERS FROM 1 TO 2 FOR SELECT a from abc +ALTER RANGE RELOCATE NONVOTERS FROM 1 TO 2 FOR SELECT a from abc ---- -alter-range-relocate &{false true 2 1 [3 4 5] []} +alter-range-relocate &{NONVOTERS 2 1 [3 4 5] []} ├── columns: range_id:3 pretty:4 result:5 └── project ├── columns: a:6!null @@ -55,7 +55,7 @@ alter-range-relocate &{false true 2 1 [3 4 5] []} build ALTER RANGE 1 RELOCATE LEASE TO 2 ---- -alter-range-relocate &{true false 2 0 [3 4 5] []} +alter-range-relocate &{LEASE 2 0 [3 4 5] []} ├── columns: range_id:3 pretty:4 result:5 └── values ├── columns: column1:6!null @@ -64,7 +64,7 @@ alter-range-relocate &{true false 2 0 [3 4 5] []} build ALTER RANGE RELOCATE LEASE TO 2 FOR SELECT a from abc ---- -alter-range-relocate &{true false 2 0 [3 4 5] []} +alter-range-relocate &{LEASE 2 0 [3 4 5] []} ├── columns: range_id:3 pretty:4 result:5 └── project ├── columns: a:6!null diff --git a/pkg/sql/opt/optbuilder/testdata/alter_table b/pkg/sql/opt/optbuilder/testdata/alter_table index 7da02b7d34bc..3282396dd4d2 100644 --- a/pkg/sql/opt/optbuilder/testdata/alter_table +++ b/pkg/sql/opt/optbuilder/testdata/alter_table @@ -137,7 +137,7 @@ alter-table-relocate abc [voters] └── (ARRAY[4], 2) build -ALTER TABLE abc EXPERIMENTAL_RELOCATE NON_VOTERS VALUES (ARRAY[1,2,3], 1), (ARRAY[4], 2) +ALTER TABLE abc EXPERIMENTAL_RELOCATE NONVOTERS VALUES (ARRAY[1,2,3], 1), (ARRAY[4], 2) ---- alter-table-relocate abc [non-voters] ├── columns: key:1 pretty:2 @@ -149,12 +149,12 @@ alter-table-relocate abc [non-voters] build ALTER TABLE abc EXPERIMENTAL_RELOCATE LEASE VALUES (10), (11) ---- -error (42601): less than 2 columns in EXPERIMENTAL_RELOCATE LEASE data +error (42601): less than 2 columns in RELOCATE LEASE data build ALTER TABLE abc EXPERIMENTAL_RELOCATE LEASE VALUES (10, 1, 2), (11, 3, 4) ---- -error (42601): too many columns in EXPERIMENTAL_RELOCATE LEASE data +error (42601): too many columns in RELOCATE LEASE data build ALTER INDEX abc@bc EXPERIMENTAL_RELOCATE VALUES (ARRAY[5], 1, 'foo'), (ARRAY[6,7,8], 2, 'bar') @@ -167,7 +167,7 @@ alter-table-relocate abc@bc [voters] └── (ARRAY[6,7,8], 2, 'bar') build -ALTER INDEX abc@bc EXPERIMENTAL_RELOCATE NON_VOTERS VALUES (ARRAY[5], 1, 'foo'), (ARRAY[6,7,8], 2, 'bar') +ALTER INDEX abc@bc EXPERIMENTAL_RELOCATE NONVOTERS VALUES (ARRAY[5], 1, 'foo'), (ARRAY[6,7,8], 2, 'bar') ---- alter-table-relocate abc@bc [non-voters] ├── columns: key:1 pretty:2 @@ -179,22 +179,22 @@ alter-table-relocate abc@bc [non-voters] build ALTER INDEX abc@bc EXPERIMENTAL_RELOCATE VALUES (5, 1, 'foo'), (6, 2, 'bar') ---- -error (42601): EXPERIMENTAL_RELOCATE data column 1 (relocation array) must be of type int[], not type int +error (42601): RELOCATE data column 1 (relocation array) must be of type int[], not type int build ALTER INDEX abc@bc EXPERIMENTAL_RELOCATE LEASE VALUES (ARRAY[5], 1, 'foo'), (ARRAY[6,7,8], 2, 'bar') ---- -error (42601): EXPERIMENTAL_RELOCATE LEASE data column 1 (target leaseholder) must be of type int, not type int[] +error (42601): RELOCATE LEASE data column 1 (target leaseholder) must be of type int, not type int[] build ALTER INDEX abc@bc EXPERIMENTAL_RELOCATE VALUES (1, 2), (3, 4) ---- -error (42601): EXPERIMENTAL_RELOCATE data column 1 (relocation array) must be of type int[], not type int +error (42601): RELOCATE data column 1 (relocation array) must be of type int[], not type int build ALTER INDEX abc@bc EXPERIMENTAL_RELOCATE VALUES (ARRAY[1,2], 1, 2), (ARRAY[3,4], 3, 4) ---- -error (42601): EXPERIMENTAL_RELOCATE data column 3 (c) must be of type string, not type int +error (42601): RELOCATE data column 3 (c) must be of type string, not type int build ALTER INDEX abc@bc EXPERIMENTAL_RELOCATE LEASE VALUES (10, 1, 'foo'), (11, 3, 'bar') diff --git a/pkg/sql/opt/optgen/cmd/optgen/metadata.go b/pkg/sql/opt/optgen/cmd/optgen/metadata.go index e48a96bde743..31a32c1d847a 100644 --- a/pkg/sql/opt/optgen/cmd/optgen/metadata.go +++ b/pkg/sql/opt/optgen/cmd/optgen/metadata.go @@ -238,6 +238,7 @@ func newMetadata(compiled *lang.CompiledExpr, pkg string) *metadata { "ScheduleCommand": {fullName: "tree.ScheduleCommand", passByVal: true}, "IndexOrdinal": {fullName: "cat.IndexOrdinal", passByVal: true}, "IndexOrdinals": {fullName: "cat.IndexOrdinals", passByVal: true}, + "RelocateSubject": {fullName: "tree.RelocateSubject", passByVal: true}, "UniqueOrdinals": {fullName: "cat.UniqueOrdinals", passByVal: true}, "ViewDeps": {fullName: "opt.ViewDeps", passByVal: true}, "ViewTypeDeps": {fullName: "opt.ViewTypeDeps", passByVal: true}, diff --git a/pkg/sql/opt_exec_factory.go b/pkg/sql/opt_exec_factory.go index 23fb930f53fb..8177d6d15538 100644 --- a/pkg/sql/opt_exec_factory.go +++ b/pkg/sql/opt_exec_factory.go @@ -1955,35 +1955,33 @@ func (ef *execFactory) ConstructAlterTableUnsplitAll(index cat.Index) (exec.Node // ConstructAlterTableRelocate is part of the exec.Factory interface. func (ef *execFactory) ConstructAlterTableRelocate( - index cat.Index, input exec.Node, relocateLease bool, relocateNonVoters bool, + index cat.Index, input exec.Node, relocateSubject tree.RelocateSubject, ) (exec.Node, error) { if !ef.planner.ExecCfg().Codec.ForSystemTenant() { return nil, errorutil.UnsupportedWithMultiTenancy(54250) } return &relocateNode{ - relocateLease: relocateLease, - relocateNonVoters: relocateNonVoters, - tableDesc: index.Table().(*optTable).desc, - index: index.(*optIndex).idx, - rows: input.(planNode), + subjectReplicas: relocateSubject, + tableDesc: index.Table().(*optTable).desc, + index: index.(*optIndex).idx, + rows: input.(planNode), }, nil } // ConstructAlterRangeRelocate is part of the exec.Factory interface. func (ef *execFactory) ConstructAlterRangeRelocate( - input exec.Node, relocateLease bool, relocateNonVoters bool, toStoreID int64, fromStoreID int64, + input exec.Node, relocateSubject tree.RelocateSubject, toStoreID int64, fromStoreID int64, ) (exec.Node, error) { if !ef.planner.ExecCfg().Codec.ForSystemTenant() { return nil, errorutil.UnsupportedWithMultiTenancy(54250) } return &relocateRange{ - rows: input.(planNode), - relocateLease: relocateLease, - relocateNonVoters: relocateNonVoters, - toStoreID: roachpb.StoreID(toStoreID), - fromStoreID: roachpb.StoreID(fromStoreID), + rows: input.(planNode), + subjectReplicas: relocateSubject, + toStoreID: roachpb.StoreID(toStoreID), + fromStoreID: roachpb.StoreID(fromStoreID), }, nil } diff --git a/pkg/sql/parser/sql.y b/pkg/sql/parser/sql.y index 679537e0d99d..25580da51e36 100644 --- a/pkg/sql/parser/sql.y +++ b/pkg/sql/parser/sql.y @@ -597,6 +597,9 @@ func (u *sqlSymUnion) rangePartition() tree.RangePartition { func (u *sqlSymUnion) rangePartitions() []tree.RangePartition { return u.val.([]tree.RangePartition) } +func (u *sqlSymUnion) relocateSubject() tree.RelocateSubject { + return u.val.(tree.RelocateSubject) +} func (u *sqlSymUnion) setZoneConfig() *tree.SetZoneConfig { return u.val.(*tree.SetZoneConfig) } @@ -815,7 +818,7 @@ func (u *sqlSymUnion) setVar() *tree.SetVar { %token NAN NAME NAMES NATURAL NEVER NEW_DB_NAME NEXT NO NOCANCELQUERY NOCONTROLCHANGEFEED %token NOCONTROLJOB NOCREATEDB NOCREATELOGIN NOCREATEROLE NOLOGIN NOMODIFYCLUSTERSETTING -%token NO_INDEX_JOIN NO_ZIGZAG_JOIN NO_FULL_SCAN NONE NON_VOTERS NORMAL NOT NOTHING NOTNULL +%token NO_INDEX_JOIN NO_ZIGZAG_JOIN NO_FULL_SCAN NONE NONVOTERS NORMAL NOT NOTHING NOTNULL %token NOVIEWACTIVITY NOWAIT NULL NULLIF NULLS NUMERIC %token OF OFF OFFSET OID OIDS OIDVECTOR ON ONLY OPT OPTION OPTIONS OR @@ -903,7 +906,6 @@ func (u *sqlSymUnion) setVar() *tree.SetVar { // ALTER RANGE %type alter_zone_range_stmt -%type alter_range_relocate_lease_stmt %type alter_range_relocate_stmt // ALTER TABLE @@ -913,7 +915,6 @@ func (u *sqlSymUnion) setVar() *tree.SetVar { %type alter_rename_table_stmt %type alter_scatter_stmt %type alter_relocate_stmt -%type alter_relocate_lease_stmt %type alter_zone_table_stmt %type alter_table_set_schema_stmt %type alter_table_locality_stmt @@ -941,7 +942,6 @@ func (u *sqlSymUnion) setVar() *tree.SetVar { %type alter_unsplit_index_stmt %type alter_rename_index_stmt %type alter_relocate_index_stmt -%type alter_relocate_index_lease_stmt %type alter_zone_index_stmt // ALTER VIEW @@ -1384,6 +1384,7 @@ func (u *sqlSymUnion) setVar() *tree.SetVar { %type <*tree.ReplicationOptions> opt_with_replication_options replication_options replication_options_list %type relocate_kw +%type relocate_subject relocate_subject_nonlease %type <*tree.SetZoneConfig> set_zone_config @@ -1553,6 +1554,7 @@ alter_ddl_stmt: // ALTER TABLE ... UNSPLIT ALL // ALTER TABLE ... SCATTER [ FROM ( ) TO ( ) ] // ALTER TABLE ... INJECT STATISTICS ... (experimental) +// ALTER TABLE ... RELOCATE [ LEASE | VOTERS | NONVOTERS ] FOR (experimental) // ALTER TABLE ... PARTITION BY RANGE ( ) ( ) // ALTER TABLE ... PARTITION BY LIST ( ) ( ) // ALTER TABLE ... PARTITION BY NOTHING @@ -1576,7 +1578,6 @@ alter_ddl_stmt: alter_table_stmt: alter_onetable_stmt | alter_relocate_stmt -| alter_relocate_lease_stmt | alter_split_stmt | alter_unsplit_stmt | alter_scatter_stmt @@ -1781,10 +1782,10 @@ alter_database_primary_region_stmt: // // Commands: // ALTER RANGE ... CONFIGURE ZONE -// ALTER RANGE RELOCATE from to FOR -// ALTER RANGE r RELOCATE from to -// ALTER RANGE RELOCATE LEASE to FOR -// ALTER RANGE r RELOCATE LEASE to +// ALTER RANGE RELOCATE { VOTERS | NONVOTERS } FROM TO FOR +// ALTER RANGE r RELOCATE { VOTERS | NONVOTERS } FROM TO +// ALTER RANGE RELOCATE LEASE TO FOR +// ALTER RANGE r RELOCATE LEASE TO // // Zone configurations: // DISCARD @@ -1795,7 +1796,6 @@ alter_database_primary_region_stmt: // %SeeAlso: ALTER TABLE alter_range_stmt: alter_zone_range_stmt -| alter_range_relocate_lease_stmt | alter_range_relocate_stmt | ALTER RANGE error // SHOW HELP: ALTER RANGE @@ -1810,6 +1810,7 @@ alter_range_stmt: // ALTER INDEX ... UNSPLIT AT // ALTER INDEX ... UNSPLIT ALL // ALTER INDEX ... SCATTER [ FROM ( ) TO ( ) ] +// ALTER INDEX ... RELOCATE [ LEASE | VOTERS | NONVOTERS ] FOR // // Zone configurations: // DISCARD @@ -1821,7 +1822,6 @@ alter_range_stmt: alter_index_stmt: alter_oneindex_stmt | alter_relocate_index_stmt -| alter_relocate_index_lease_stmt | alter_split_index_stmt | alter_unsplit_index_stmt | alter_scatter_index_stmt @@ -1914,60 +1914,49 @@ relocate_kw: | EXPERIMENTAL_RELOCATE | RELOCATE -voters_kw: - VOTERS {} -| /* EMPTY */ {} - -alter_relocate_stmt: - ALTER TABLE table_name relocate_kw voters_kw select_stmt +relocate_subject: + relocate_subject_nonlease +| LEASE { - /* SKIP DOC */ - name := $3.unresolvedObjectName().ToTableName() - $$.val = &tree.Relocate{ - TableOrIndex: tree.TableIndexName{Table: name}, - Rows: $6.slct(), - } + $$.val = tree.RelocateLease } -| ALTER TABLE table_name relocate_kw NON_VOTERS select_stmt + +relocate_subject_nonlease: + VOTERS { - /* SKIP DOC */ - name := $3.unresolvedObjectName().ToTableName() - $$.val = &tree.Relocate{ - TableOrIndex: tree.TableIndexName{Table: name}, - Rows: $6.slct(), - RelocateNonVoters: true, - } + $$.val = tree.RelocateVoters } - -alter_relocate_index_stmt: - ALTER INDEX table_index_name relocate_kw voters_kw select_stmt +| /* EMPTY */ { - /* SKIP DOC */ - $$.val = &tree.Relocate{TableOrIndex: $3.tableIndexName(), Rows: $6.slct()} + // No keyword is an alias for VOTERS. + $$.val = tree.RelocateVoters } -| ALTER INDEX table_index_name relocate_kw NON_VOTERS select_stmt +| NONVOTERS { - /* SKIP DOC */ - $$.val = &tree.Relocate{TableOrIndex: $3.tableIndexName(), Rows: $6.slct(), RelocateNonVoters: true} + $$.val = tree.RelocateNonVoters } -alter_relocate_lease_stmt: - ALTER TABLE table_name relocate_kw LEASE select_stmt +alter_relocate_stmt: + ALTER TABLE table_name relocate_kw relocate_subject select_stmt { /* SKIP DOC */ name := $3.unresolvedObjectName().ToTableName() $$.val = &tree.Relocate{ TableOrIndex: tree.TableIndexName{Table: name}, Rows: $6.slct(), - RelocateLease: true, + SubjectReplicas: $5.relocateSubject(), } } -alter_relocate_index_lease_stmt: - ALTER INDEX table_index_name relocate_kw LEASE select_stmt +alter_relocate_index_stmt: + ALTER INDEX table_index_name relocate_kw relocate_subject select_stmt { /* SKIP DOC */ - $$.val = &tree.Relocate{TableOrIndex: $3.tableIndexName(), Rows: $6.slct(), RelocateLease: true} + $$.val = &tree.Relocate{ + TableOrIndex: $3.tableIndexName(), + Rows: $6.slct(), + SubjectReplicas: $5.relocateSubject(), + } } alter_zone_range_stmt: @@ -1978,14 +1967,13 @@ alter_zone_range_stmt: $$.val = s } -alter_range_relocate_lease_stmt: +alter_range_relocate_stmt: ALTER RANGE relocate_kw LEASE TO iconst64 FOR select_stmt { $$.val = &tree.RelocateRange{ Rows: $8.slct(), ToStoreID: $6.int64(), - RelocateLease: true, - RelocateNonVoters: false, + SubjectReplicas: tree.RelocateLease, } } | ALTER RANGE iconst64 relocate_kw LEASE TO iconst64 @@ -1995,23 +1983,19 @@ alter_range_relocate_lease_stmt: Select: &tree.ValuesClause{Rows: []tree.Exprs{tree.Exprs{tree.NewDInt(tree.DInt($3.int64()))}}}, }, ToStoreID: $7.int64(), - RelocateLease: true, - RelocateNonVoters: false, + SubjectReplicas: tree.RelocateLease, } } - -alter_range_relocate_stmt: -ALTER RANGE relocate_kw voters_kw FROM iconst64 TO iconst64 FOR select_stmt +| ALTER RANGE relocate_kw relocate_subject_nonlease FROM iconst64 TO iconst64 FOR select_stmt { $$.val = &tree.RelocateRange{ Rows: $10.slct(), FromStoreID: $6.int64(), ToStoreID: $8.int64(), - RelocateLease: false, - RelocateNonVoters: false, + SubjectReplicas: $4.relocateSubject(), } } -| ALTER RANGE iconst64 relocate_kw voters_kw FROM iconst64 TO iconst64 +| ALTER RANGE iconst64 relocate_kw relocate_subject_nonlease FROM iconst64 TO iconst64 { $$.val = &tree.RelocateRange{ Rows: &tree.Select{ @@ -2019,32 +2003,9 @@ ALTER RANGE relocate_kw voters_kw FROM iconst64 TO iconst64 FOR select_stmt }, FromStoreID: $7.int64(), ToStoreID: $9.int64(), - RelocateLease: false, - RelocateNonVoters: false, - } - } -| ALTER RANGE relocate_kw NON_VOTERS FROM iconst64 TO iconst64 FOR select_stmt - { - $$.val = &tree.RelocateRange{ - Rows: $10.slct(), - FromStoreID: $6.int64(), - ToStoreID: $8.int64(), - RelocateLease: false, - RelocateNonVoters: true, - } + SubjectReplicas: $5.relocateSubject(), } -| ALTER RANGE iconst64 relocate_kw NON_VOTERS FROM iconst64 TO iconst64 - { - $$.val = &tree.RelocateRange{ - Rows: &tree.Select{ - Select: &tree.ValuesClause{Rows: []tree.Exprs{tree.Exprs{tree.NewDInt(tree.DInt($3.int64()))}}}, - }, - FromStoreID: $7.int64(), - ToStoreID: $9.int64(), - RelocateLease: false, - RelocateNonVoters: true, } -} set_zone_config: CONFIGURE ZONE to_or_eq a_expr @@ -13445,7 +13406,7 @@ unreserved_keyword: | NOCONTROLJOB | NOLOGIN | NOMODIFYCLUSTERSETTING -| NON_VOTERS +| NONVOTERS | NOVIEWACTIVITY | NOWAIT | NULLS diff --git a/pkg/sql/parser/testdata/alter_index b/pkg/sql/parser/testdata/alter_index index 1ad8f64407d2..3b09daeecbbb 100644 --- a/pkg/sql/parser/testdata/alter_index +++ b/pkg/sql/parser/testdata/alter_index @@ -153,18 +153,18 @@ ALTER INDEX _._._ UNSPLIT AT VALUES (2) -- identifiers removed parse ALTER INDEX d.i EXPERIMENTAL_RELOCATE VALUES (ARRAY[1], 2) ---- -ALTER INDEX d.i EXPERIMENTAL_RELOCATE VOTERS VALUES (ARRAY[1], 2) -- normalized! -ALTER INDEX d.i EXPERIMENTAL_RELOCATE VOTERS VALUES ((ARRAY[(1)]), (2)) -- fully parenthesized -ALTER INDEX d.i EXPERIMENTAL_RELOCATE VOTERS VALUES (ARRAY[_], _) -- literals removed -ALTER INDEX _._ EXPERIMENTAL_RELOCATE VOTERS VALUES (ARRAY[1], 2) -- identifiers removed +ALTER INDEX d.i RELOCATE VOTERS VALUES (ARRAY[1], 2) -- normalized! +ALTER INDEX d.i RELOCATE VOTERS VALUES ((ARRAY[(1)]), (2)) -- fully parenthesized +ALTER INDEX d.i RELOCATE VOTERS VALUES (ARRAY[_], _) -- literals removed +ALTER INDEX _._ RELOCATE VOTERS VALUES (ARRAY[1], 2) -- identifiers removed parse ALTER INDEX d.i EXPERIMENTAL_RELOCATE LEASE VALUES (1, 2) ---- -ALTER INDEX d.i EXPERIMENTAL_RELOCATE LEASE VALUES (1, 2) -ALTER INDEX d.i EXPERIMENTAL_RELOCATE LEASE VALUES ((1), (2)) -- fully parenthesized -ALTER INDEX d.i EXPERIMENTAL_RELOCATE LEASE VALUES (_, _) -- literals removed -ALTER INDEX _._ EXPERIMENTAL_RELOCATE LEASE VALUES (1, 2) -- identifiers removed +ALTER INDEX d.i RELOCATE LEASE VALUES (1, 2) -- normalized! +ALTER INDEX d.i RELOCATE LEASE VALUES ((1), (2)) -- fully parenthesized +ALTER INDEX d.i RELOCATE LEASE VALUES (_, _) -- literals removed +ALTER INDEX _._ RELOCATE LEASE VALUES (1, 2) -- identifiers removed parse ALTER INDEX d.i SCATTER FROM (1) TO (2) diff --git a/pkg/sql/parser/testdata/alter_range b/pkg/sql/parser/testdata/alter_range index 0c3f15e53683..da8310a5da54 100644 --- a/pkg/sql/parser/testdata/alter_range +++ b/pkg/sql/parser/testdata/alter_range @@ -119,12 +119,12 @@ ALTER RANGE RELOCATE VOTERS FROM 3 TO 2 FOR VALUES (_) -- literals removed ALTER RANGE RELOCATE VOTERS FROM 3 TO 2 FOR VALUES (1) -- identifiers removed parse -ALTER RANGE 1 RELOCATE NON_VOTERS FROM 3 TO 2 +ALTER RANGE 1 RELOCATE NONVOTERS FROM 3 TO 2 ---- -ALTER RANGE RELOCATE NON_VOTERS FROM 3 TO 2 FOR VALUES (1) -- normalized! -ALTER RANGE RELOCATE NON_VOTERS FROM 3 TO 2 FOR VALUES ((1)) -- fully parenthesized -ALTER RANGE RELOCATE NON_VOTERS FROM 3 TO 2 FOR VALUES (_) -- literals removed -ALTER RANGE RELOCATE NON_VOTERS FROM 3 TO 2 FOR VALUES (1) -- identifiers removed +ALTER RANGE RELOCATE NONVOTERS FROM 3 TO 2 FOR VALUES (1) -- normalized! +ALTER RANGE RELOCATE NONVOTERS FROM 3 TO 2 FOR VALUES ((1)) -- fully parenthesized +ALTER RANGE RELOCATE NONVOTERS FROM 3 TO 2 FOR VALUES (_) -- literals removed +ALTER RANGE RELOCATE NONVOTERS FROM 3 TO 2 FOR VALUES (1) -- identifiers removed parse ALTER RANGE RELOCATE FROM 3 TO 2 FOR SELECT range_id FROM foo @@ -135,9 +135,9 @@ ALTER RANGE RELOCATE VOTERS FROM 3 TO 2 FOR SELECT range_id FROM foo -- literals ALTER RANGE RELOCATE VOTERS FROM 3 TO 2 FOR SELECT _ FROM _ -- identifiers removed parse -ALTER RANGE RELOCATE NON_VOTERS FROM 3 TO 2 FOR SELECT range_id FROM foo +ALTER RANGE RELOCATE NONVOTERS FROM 3 TO 2 FOR SELECT range_id FROM foo ---- -ALTER RANGE RELOCATE NON_VOTERS FROM 3 TO 2 FOR SELECT range_id FROM foo -ALTER RANGE RELOCATE NON_VOTERS FROM 3 TO 2 FOR SELECT (range_id) FROM foo -- fully parenthesized -ALTER RANGE RELOCATE NON_VOTERS FROM 3 TO 2 FOR SELECT range_id FROM foo -- literals removed -ALTER RANGE RELOCATE NON_VOTERS FROM 3 TO 2 FOR SELECT _ FROM _ -- identifiers removed +ALTER RANGE RELOCATE NONVOTERS FROM 3 TO 2 FOR SELECT range_id FROM foo +ALTER RANGE RELOCATE NONVOTERS FROM 3 TO 2 FOR SELECT (range_id) FROM foo -- fully parenthesized +ALTER RANGE RELOCATE NONVOTERS FROM 3 TO 2 FOR SELECT range_id FROM foo -- literals removed +ALTER RANGE RELOCATE NONVOTERS FROM 3 TO 2 FOR SELECT _ FROM _ -- identifiers removed diff --git a/pkg/sql/parser/testdata/alter_table b/pkg/sql/parser/testdata/alter_table index 43e79b799325..908bcb2f3159 100644 --- a/pkg/sql/parser/testdata/alter_table +++ b/pkg/sql/parser/testdata/alter_table @@ -732,122 +732,122 @@ ALTER TABLE _._ UNSPLIT AT VALUES ('b', 2) -- identifiers removed parse ALTER TABLE a EXPERIMENTAL_RELOCATE VALUES (ARRAY[1], 1) ---- -ALTER TABLE a EXPERIMENTAL_RELOCATE VOTERS VALUES (ARRAY[1], 1) -- normalized! -ALTER TABLE a EXPERIMENTAL_RELOCATE VOTERS VALUES ((ARRAY[(1)]), (1)) -- fully parenthesized -ALTER TABLE a EXPERIMENTAL_RELOCATE VOTERS VALUES (ARRAY[_], _) -- literals removed -ALTER TABLE _ EXPERIMENTAL_RELOCATE VOTERS VALUES (ARRAY[1], 1) -- identifiers removed +ALTER TABLE a RELOCATE VOTERS VALUES (ARRAY[1], 1) -- normalized! +ALTER TABLE a RELOCATE VOTERS VALUES ((ARRAY[(1)]), (1)) -- fully parenthesized +ALTER TABLE a RELOCATE VOTERS VALUES (ARRAY[_], _) -- literals removed +ALTER TABLE _ RELOCATE VOTERS VALUES (ARRAY[1], 1) -- identifiers removed parse EXPLAIN ALTER TABLE a EXPERIMENTAL_RELOCATE TABLE b ---- -EXPLAIN ALTER TABLE a EXPERIMENTAL_RELOCATE VOTERS TABLE b -- normalized! -EXPLAIN ALTER TABLE a EXPERIMENTAL_RELOCATE VOTERS TABLE b -- fully parenthesized -EXPLAIN ALTER TABLE a EXPERIMENTAL_RELOCATE VOTERS TABLE b -- literals removed -EXPLAIN ALTER TABLE _ EXPERIMENTAL_RELOCATE VOTERS TABLE _ -- identifiers removed +EXPLAIN ALTER TABLE a RELOCATE VOTERS TABLE b -- normalized! +EXPLAIN ALTER TABLE a RELOCATE VOTERS TABLE b -- fully parenthesized +EXPLAIN ALTER TABLE a RELOCATE VOTERS TABLE b -- literals removed +EXPLAIN ALTER TABLE _ RELOCATE VOTERS TABLE _ -- identifiers removed parse ALTER TABLE a EXPERIMENTAL_RELOCATE SELECT * FROM t ---- -ALTER TABLE a EXPERIMENTAL_RELOCATE VOTERS SELECT * FROM t -- normalized! -ALTER TABLE a EXPERIMENTAL_RELOCATE VOTERS SELECT (*) FROM t -- fully parenthesized -ALTER TABLE a EXPERIMENTAL_RELOCATE VOTERS SELECT * FROM t -- literals removed -ALTER TABLE _ EXPERIMENTAL_RELOCATE VOTERS SELECT * FROM _ -- identifiers removed +ALTER TABLE a RELOCATE VOTERS SELECT * FROM t -- normalized! +ALTER TABLE a RELOCATE VOTERS SELECT (*) FROM t -- fully parenthesized +ALTER TABLE a RELOCATE VOTERS SELECT * FROM t -- literals removed +ALTER TABLE _ RELOCATE VOTERS SELECT * FROM _ -- identifiers removed parse ALTER TABLE d.a EXPERIMENTAL_RELOCATE VALUES (ARRAY[1, 2, 3], 'b', 2) ---- -ALTER TABLE d.a EXPERIMENTAL_RELOCATE VOTERS VALUES (ARRAY[1, 2, 3], 'b', 2) -- normalized! -ALTER TABLE d.a EXPERIMENTAL_RELOCATE VOTERS VALUES ((ARRAY[(1), (2), (3)]), ('b'), (2)) -- fully parenthesized -ALTER TABLE d.a EXPERIMENTAL_RELOCATE VOTERS VALUES (ARRAY[_, _, __more1__], '_', _) -- literals removed -ALTER TABLE _._ EXPERIMENTAL_RELOCATE VOTERS VALUES (ARRAY[1, 2, 3], 'b', 2) -- identifiers removed +ALTER TABLE d.a RELOCATE VOTERS VALUES (ARRAY[1, 2, 3], 'b', 2) -- normalized! +ALTER TABLE d.a RELOCATE VOTERS VALUES ((ARRAY[(1), (2), (3)]), ('b'), (2)) -- fully parenthesized +ALTER TABLE d.a RELOCATE VOTERS VALUES (ARRAY[_, _, __more1__], '_', _) -- literals removed +ALTER TABLE _._ RELOCATE VOTERS VALUES (ARRAY[1, 2, 3], 'b', 2) -- identifiers removed parse ALTER TABLE a EXPERIMENTAL_RELOCATE VOTERS VALUES (ARRAY[1], 1) ---- -ALTER TABLE a EXPERIMENTAL_RELOCATE VOTERS VALUES (ARRAY[1], 1) -ALTER TABLE a EXPERIMENTAL_RELOCATE VOTERS VALUES ((ARRAY[(1)]), (1)) -- fully parenthesized -ALTER TABLE a EXPERIMENTAL_RELOCATE VOTERS VALUES (ARRAY[_], _) -- literals removed -ALTER TABLE _ EXPERIMENTAL_RELOCATE VOTERS VALUES (ARRAY[1], 1) -- identifiers removed +ALTER TABLE a RELOCATE VOTERS VALUES (ARRAY[1], 1) -- normalized! +ALTER TABLE a RELOCATE VOTERS VALUES ((ARRAY[(1)]), (1)) -- fully parenthesized +ALTER TABLE a RELOCATE VOTERS VALUES (ARRAY[_], _) -- literals removed +ALTER TABLE _ RELOCATE VOTERS VALUES (ARRAY[1], 1) -- identifiers removed parse EXPLAIN ALTER TABLE a EXPERIMENTAL_RELOCATE VOTERS TABLE b ---- -EXPLAIN ALTER TABLE a EXPERIMENTAL_RELOCATE VOTERS TABLE b -EXPLAIN ALTER TABLE a EXPERIMENTAL_RELOCATE VOTERS TABLE b -- fully parenthesized -EXPLAIN ALTER TABLE a EXPERIMENTAL_RELOCATE VOTERS TABLE b -- literals removed -EXPLAIN ALTER TABLE _ EXPERIMENTAL_RELOCATE VOTERS TABLE _ -- identifiers removed +EXPLAIN ALTER TABLE a RELOCATE VOTERS TABLE b -- normalized! +EXPLAIN ALTER TABLE a RELOCATE VOTERS TABLE b -- fully parenthesized +EXPLAIN ALTER TABLE a RELOCATE VOTERS TABLE b -- literals removed +EXPLAIN ALTER TABLE _ RELOCATE VOTERS TABLE _ -- identifiers removed parse ALTER TABLE a EXPERIMENTAL_RELOCATE VOTERS SELECT * FROM t ---- -ALTER TABLE a EXPERIMENTAL_RELOCATE VOTERS SELECT * FROM t -ALTER TABLE a EXPERIMENTAL_RELOCATE VOTERS SELECT (*) FROM t -- fully parenthesized -ALTER TABLE a EXPERIMENTAL_RELOCATE VOTERS SELECT * FROM t -- literals removed -ALTER TABLE _ EXPERIMENTAL_RELOCATE VOTERS SELECT * FROM _ -- identifiers removed +ALTER TABLE a RELOCATE VOTERS SELECT * FROM t -- normalized! +ALTER TABLE a RELOCATE VOTERS SELECT (*) FROM t -- fully parenthesized +ALTER TABLE a RELOCATE VOTERS SELECT * FROM t -- literals removed +ALTER TABLE _ RELOCATE VOTERS SELECT * FROM _ -- identifiers removed parse ALTER TABLE d.a EXPERIMENTAL_RELOCATE VOTERS VALUES (ARRAY[1, 2, 3], 'b', 2) ---- -ALTER TABLE d.a EXPERIMENTAL_RELOCATE VOTERS VALUES (ARRAY[1, 2, 3], 'b', 2) -ALTER TABLE d.a EXPERIMENTAL_RELOCATE VOTERS VALUES ((ARRAY[(1), (2), (3)]), ('b'), (2)) -- fully parenthesized -ALTER TABLE d.a EXPERIMENTAL_RELOCATE VOTERS VALUES (ARRAY[_, _, __more1__], '_', _) -- literals removed -ALTER TABLE _._ EXPERIMENTAL_RELOCATE VOTERS VALUES (ARRAY[1, 2, 3], 'b', 2) -- identifiers removed +ALTER TABLE d.a RELOCATE VOTERS VALUES (ARRAY[1, 2, 3], 'b', 2) -- normalized! +ALTER TABLE d.a RELOCATE VOTERS VALUES ((ARRAY[(1), (2), (3)]), ('b'), (2)) -- fully parenthesized +ALTER TABLE d.a RELOCATE VOTERS VALUES (ARRAY[_, _, __more1__], '_', _) -- literals removed +ALTER TABLE _._ RELOCATE VOTERS VALUES (ARRAY[1, 2, 3], 'b', 2) -- identifiers removed parse -ALTER TABLE a EXPERIMENTAL_RELOCATE NON_VOTERS VALUES (ARRAY[1], 1) +ALTER TABLE a EXPERIMENTAL_RELOCATE NONVOTERS VALUES (ARRAY[1], 1) ---- -ALTER TABLE a EXPERIMENTAL_RELOCATE NON_VOTERS VALUES (ARRAY[1], 1) -ALTER TABLE a EXPERIMENTAL_RELOCATE NON_VOTERS VALUES ((ARRAY[(1)]), (1)) -- fully parenthesized -ALTER TABLE a EXPERIMENTAL_RELOCATE NON_VOTERS VALUES (ARRAY[_], _) -- literals removed -ALTER TABLE _ EXPERIMENTAL_RELOCATE NON_VOTERS VALUES (ARRAY[1], 1) -- identifiers removed +ALTER TABLE a RELOCATE NONVOTERS VALUES (ARRAY[1], 1) -- normalized! +ALTER TABLE a RELOCATE NONVOTERS VALUES ((ARRAY[(1)]), (1)) -- fully parenthesized +ALTER TABLE a RELOCATE NONVOTERS VALUES (ARRAY[_], _) -- literals removed +ALTER TABLE _ RELOCATE NONVOTERS VALUES (ARRAY[1], 1) -- identifiers removed parse -EXPLAIN ALTER TABLE a EXPERIMENTAL_RELOCATE NON_VOTERS TABLE b +EXPLAIN ALTER TABLE a EXPERIMENTAL_RELOCATE NONVOTERS TABLE b ---- -EXPLAIN ALTER TABLE a EXPERIMENTAL_RELOCATE NON_VOTERS TABLE b -EXPLAIN ALTER TABLE a EXPERIMENTAL_RELOCATE NON_VOTERS TABLE b -- fully parenthesized -EXPLAIN ALTER TABLE a EXPERIMENTAL_RELOCATE NON_VOTERS TABLE b -- literals removed -EXPLAIN ALTER TABLE _ EXPERIMENTAL_RELOCATE NON_VOTERS TABLE _ -- identifiers removed +EXPLAIN ALTER TABLE a RELOCATE NONVOTERS TABLE b -- normalized! +EXPLAIN ALTER TABLE a RELOCATE NONVOTERS TABLE b -- fully parenthesized +EXPLAIN ALTER TABLE a RELOCATE NONVOTERS TABLE b -- literals removed +EXPLAIN ALTER TABLE _ RELOCATE NONVOTERS TABLE _ -- identifiers removed parse -ALTER TABLE a EXPERIMENTAL_RELOCATE NON_VOTERS SELECT * FROM t +ALTER TABLE a EXPERIMENTAL_RELOCATE NONVOTERS SELECT * FROM t ---- -ALTER TABLE a EXPERIMENTAL_RELOCATE NON_VOTERS SELECT * FROM t -ALTER TABLE a EXPERIMENTAL_RELOCATE NON_VOTERS SELECT (*) FROM t -- fully parenthesized -ALTER TABLE a EXPERIMENTAL_RELOCATE NON_VOTERS SELECT * FROM t -- literals removed -ALTER TABLE _ EXPERIMENTAL_RELOCATE NON_VOTERS SELECT * FROM _ -- identifiers removed +ALTER TABLE a RELOCATE NONVOTERS SELECT * FROM t -- normalized! +ALTER TABLE a RELOCATE NONVOTERS SELECT (*) FROM t -- fully parenthesized +ALTER TABLE a RELOCATE NONVOTERS SELECT * FROM t -- literals removed +ALTER TABLE _ RELOCATE NONVOTERS SELECT * FROM _ -- identifiers removed parse -ALTER TABLE d.a EXPERIMENTAL_RELOCATE NON_VOTERS VALUES (ARRAY[1, 2, 3], 'b', 2) +ALTER TABLE d.a EXPERIMENTAL_RELOCATE NONVOTERS VALUES (ARRAY[1, 2, 3], 'b', 2) ---- -ALTER TABLE d.a EXPERIMENTAL_RELOCATE NON_VOTERS VALUES (ARRAY[1, 2, 3], 'b', 2) -ALTER TABLE d.a EXPERIMENTAL_RELOCATE NON_VOTERS VALUES ((ARRAY[(1), (2), (3)]), ('b'), (2)) -- fully parenthesized -ALTER TABLE d.a EXPERIMENTAL_RELOCATE NON_VOTERS VALUES (ARRAY[_, _, __more1__], '_', _) -- literals removed -ALTER TABLE _._ EXPERIMENTAL_RELOCATE NON_VOTERS VALUES (ARRAY[1, 2, 3], 'b', 2) -- identifiers removed +ALTER TABLE d.a RELOCATE NONVOTERS VALUES (ARRAY[1, 2, 3], 'b', 2) -- normalized! +ALTER TABLE d.a RELOCATE NONVOTERS VALUES ((ARRAY[(1), (2), (3)]), ('b'), (2)) -- fully parenthesized +ALTER TABLE d.a RELOCATE NONVOTERS VALUES (ARRAY[_, _, __more1__], '_', _) -- literals removed +ALTER TABLE _._ RELOCATE NONVOTERS VALUES (ARRAY[1, 2, 3], 'b', 2) -- identifiers removed parse ALTER TABLE a EXPERIMENTAL_RELOCATE LEASE VALUES (1, 1) ---- -ALTER TABLE a EXPERIMENTAL_RELOCATE LEASE VALUES (1, 1) -ALTER TABLE a EXPERIMENTAL_RELOCATE LEASE VALUES ((1), (1)) -- fully parenthesized -ALTER TABLE a EXPERIMENTAL_RELOCATE LEASE VALUES (_, _) -- literals removed -ALTER TABLE _ EXPERIMENTAL_RELOCATE LEASE VALUES (1, 1) -- identifiers removed +ALTER TABLE a RELOCATE LEASE VALUES (1, 1) -- normalized! +ALTER TABLE a RELOCATE LEASE VALUES ((1), (1)) -- fully parenthesized +ALTER TABLE a RELOCATE LEASE VALUES (_, _) -- literals removed +ALTER TABLE _ RELOCATE LEASE VALUES (1, 1) -- identifiers removed parse ALTER TABLE a EXPERIMENTAL_RELOCATE LEASE SELECT * FROM t ---- -ALTER TABLE a EXPERIMENTAL_RELOCATE LEASE SELECT * FROM t -ALTER TABLE a EXPERIMENTAL_RELOCATE LEASE SELECT (*) FROM t -- fully parenthesized -ALTER TABLE a EXPERIMENTAL_RELOCATE LEASE SELECT * FROM t -- literals removed -ALTER TABLE _ EXPERIMENTAL_RELOCATE LEASE SELECT * FROM _ -- identifiers removed +ALTER TABLE a RELOCATE LEASE SELECT * FROM t -- normalized! +ALTER TABLE a RELOCATE LEASE SELECT (*) FROM t -- fully parenthesized +ALTER TABLE a RELOCATE LEASE SELECT * FROM t -- literals removed +ALTER TABLE _ RELOCATE LEASE SELECT * FROM _ -- identifiers removed parse ALTER TABLE d.a EXPERIMENTAL_RELOCATE LEASE VALUES (1, 'b', 2) ---- -ALTER TABLE d.a EXPERIMENTAL_RELOCATE LEASE VALUES (1, 'b', 2) -ALTER TABLE d.a EXPERIMENTAL_RELOCATE LEASE VALUES ((1), ('b'), (2)) -- fully parenthesized -ALTER TABLE d.a EXPERIMENTAL_RELOCATE LEASE VALUES (_, '_', __more1__) -- literals removed -ALTER TABLE _._ EXPERIMENTAL_RELOCATE LEASE VALUES (1, 'b', 2) -- identifiers removed +ALTER TABLE d.a RELOCATE LEASE VALUES (1, 'b', 2) -- normalized! +ALTER TABLE d.a RELOCATE LEASE VALUES ((1), ('b'), (2)) -- fully parenthesized +ALTER TABLE d.a RELOCATE LEASE VALUES (_, '_', __more1__) -- literals removed +ALTER TABLE _._ RELOCATE LEASE VALUES (1, 'b', 2) -- identifiers removed parse ALTER TABLE a SCATTER diff --git a/pkg/sql/relocate.go b/pkg/sql/relocate.go index 989e7dc6ee86..9095a068209d 100644 --- a/pkg/sql/relocate.go +++ b/pkg/sql/relocate.go @@ -28,11 +28,10 @@ import ( type relocateNode struct { optColumnsSlot - relocateLease bool - relocateNonVoters bool - tableDesc catalog.TableDescriptor - index catalog.Index - rows planNode + subjectReplicas tree.RelocateSubject + tableDesc catalog.TableDescriptor + index catalog.Index + rows planNode run relocateRun } @@ -66,7 +65,7 @@ func (n *relocateNode) Next(params runParams) (bool, error) { var relocationTargets []roachpb.ReplicationTarget var leaseStoreID roachpb.StoreID - if n.relocateLease { + if n.subjectReplicas == tree.RelocateLease { leaseStoreID = roachpb.StoreID(tree.MustBeDInt(data[0])) if leaseStoreID <= 0 { return false, errors.Errorf("invalid target leaseholder store ID %d for EXPERIMENTAL_RELOCATE LEASE", leaseStoreID) @@ -79,7 +78,7 @@ func (n *relocateNode) Next(params runParams) (bool, error) { ) } relocation := data[0].(*tree.DArray) - if !n.relocateNonVoters && len(relocation.Array) == 0 { + if n.subjectReplicas != tree.RelocateNonVoters && len(relocation.Array) == 0 { // We cannot remove all voters. return false, errors.Errorf("empty relocation array for EXPERIMENTAL_RELOCATE") } @@ -122,22 +121,25 @@ func (n *relocateNode) Next(params runParams) (bool, error) { existingVoters := rangeDesc.Replicas().Voters().ReplicationTargets() existingNonVoters := rangeDesc.Replicas().NonVoters().ReplicationTargets() - if n.relocateLease { + switch n.subjectReplicas { + case tree.RelocateLease: if err := params.p.ExecCfg().DB.AdminTransferLease(params.ctx, rowKey, leaseStoreID); err != nil { return false, err } - } else if n.relocateNonVoters { + case tree.RelocateNonVoters: if err := params.p.ExecCfg().DB.AdminRelocateRange( params.ctx, rowKey, existingVoters, relocationTargets, ); err != nil { return false, err } - } else { + case tree.RelocateVoters: if err := params.p.ExecCfg().DB.AdminRelocateRange( params.ctx, rowKey, relocationTargets, existingNonVoters, ); err != nil { return false, err } + default: + return false, errors.AssertionFailedf("unknown relocate mode: %v", n.subjectReplicas) } return true, nil diff --git a/pkg/sql/relocate_range.go b/pkg/sql/relocate_range.go index fa2fdd754f1f..3b09a7eec640 100644 --- a/pkg/sql/relocate_range.go +++ b/pkg/sql/relocate_range.go @@ -23,12 +23,11 @@ import ( type relocateRange struct { optColumnsSlot - rows planNode - relocateLease bool - relocateNonVoters bool - toStoreID roachpb.StoreID - fromStoreID roachpb.StoreID - run relocateRunState + rows planNode + subjectReplicas tree.RelocateSubject + toStoreID roachpb.StoreID + fromStoreID roachpb.StoreID + run relocateRunState } // relocateRunState contains the run-time state of @@ -48,18 +47,17 @@ type relocateResults struct { // relocateRequest is an internal data structure that describes a relocation. type relocateRequest struct { - rangeID roachpb.RangeID - relocateLease bool - relocateNonVoters bool - toStoreDesc *roachpb.StoreDescriptor - fromStoreDesc *roachpb.StoreDescriptor + rangeID roachpb.RangeID + subjectReplicas tree.RelocateSubject + toStoreDesc *roachpb.StoreDescriptor + fromStoreDesc *roachpb.StoreDescriptor } func (n *relocateRange) startExec(params runParams) error { if n.toStoreID <= 0 { return errors.Errorf("invalid target to store ID %d for RELOCATE", n.toStoreID) } - if !n.relocateLease && n.fromStoreID <= 0 { + if n.subjectReplicas != tree.RelocateLease && n.fromStoreID <= 0 { return errors.Errorf("invalid target from store ID %d for RELOCATE", n.fromStoreID) } // Lookup all the store descriptors upfront, so we dont have to do it for each @@ -69,7 +67,7 @@ func (n *relocateRange) startExec(params runParams) error { if err != nil { return err } - if !n.relocateLease { + if n.subjectReplicas != tree.RelocateLease { n.run.fromStoreDesc, err = lookupStoreDesc(n.fromStoreID, params) if err != nil { return err @@ -89,11 +87,10 @@ func (n *relocateRange) Next(params runParams) (bool, error) { rangeID := roachpb.RangeID(tree.MustBeDInt(datum)) rangeDesc, err := relocate(params, relocateRequest{ - rangeID: rangeID, - relocateLease: n.relocateLease, - relocateNonVoters: n.relocateNonVoters, - fromStoreDesc: n.run.fromStoreDesc, - toStoreDesc: n.run.toStoreDesc, + rangeID: rangeID, + subjectReplicas: n.subjectReplicas, + fromStoreDesc: n.run.fromStoreDesc, + toStoreDesc: n.run.toStoreDesc, }) // record the results of the relocation run, so we can output it. @@ -131,14 +128,14 @@ func relocate(params runParams, req relocateRequest) (*roachpb.RangeDescriptor, return nil, errors.Wrapf(err, "error looking up range descriptor") } - if req.relocateLease { + if req.subjectReplicas == tree.RelocateLease { err := params.p.ExecCfg().DB.AdminTransferLease(params.ctx, rangeDesc.StartKey, req.toStoreDesc.StoreID) return rangeDesc, err } toTarget := roachpb.ReplicationTarget{NodeID: req.toStoreDesc.Node.NodeID, StoreID: req.toStoreDesc.StoreID} fromTarget := roachpb.ReplicationTarget{NodeID: req.fromStoreDesc.Node.NodeID, StoreID: req.fromStoreDesc.StoreID} - if req.relocateNonVoters { + if req.subjectReplicas == tree.RelocateNonVoters { _, err := params.p.ExecCfg().DB.AdminChangeReplicas( params.ctx, rangeDesc.StartKey, *rangeDesc, []roachpb.ReplicationChange{ {ChangeType: roachpb.ADD_NON_VOTER, Target: toTarget}, diff --git a/pkg/sql/sem/tree/alter_range.go b/pkg/sql/sem/tree/alter_range.go index 2f2482dac39b..5cb5b1b3de01 100644 --- a/pkg/sql/sem/tree/alter_range.go +++ b/pkg/sql/sem/tree/alter_range.go @@ -10,35 +10,62 @@ package tree -import "strconv" +import ( + "strconv" + + "github.com/cockroachdb/errors" +) // RelocateRange represents an `ALTER RANGE .. RELOCATE ..` // statement. type RelocateRange struct { - Rows *Select - ToStoreID int64 - FromStoreID int64 - RelocateLease bool - RelocateNonVoters bool + Rows *Select + ToStoreID int64 + FromStoreID int64 + SubjectReplicas RelocateSubject +} + +// RelocateSubject indicates what replicas of a range should be relocated. +type RelocateSubject int8 + +const ( + // RelocateLease indicates that leases should be relocated. + RelocateLease RelocateSubject = iota + // RelocateVoters indicates what voter replicas should be relocated. + RelocateVoters + // RelocateNonVoters indicates that non-voter replicas should be relocated. + RelocateNonVoters +) + +// Format implementsthe NodeFormatter interface. +func (n *RelocateSubject) Format(ctx *FmtCtx) { + ctx.WriteString(n.String()) +} + +func (n RelocateSubject) String() string { + switch n { + case RelocateLease: + return "LEASE" + case RelocateVoters: + return "VOTERS" + case RelocateNonVoters: + return "NONVOTERS" + default: + panic(errors.AssertionFailedf("programming error: unhandled case %d", int(n))) + } } // Format implements the NodeFormatter interface. -func (node *RelocateRange) Format(ctx *FmtCtx) { +func (n *RelocateRange) Format(ctx *FmtCtx) { ctx.WriteString("ALTER RANGE RELOCATE ") - if node.RelocateLease { - ctx.WriteString("LEASE ") - } else if node.RelocateNonVoters { - ctx.WriteString("NON_VOTERS ") - } else { - ctx.WriteString("VOTERS ") - } - if !node.RelocateLease { - ctx.WriteString("FROM ") - ctx.WriteString(strconv.FormatInt(node.FromStoreID, 10)) - ctx.WriteString(" ") + ctx.FormatNode(&n.SubjectReplicas) + // When relocating leases, the origin store is implicit. + if n.SubjectReplicas != RelocateLease { + ctx.WriteString(" FROM ") + ctx.WriteString(strconv.FormatInt(n.FromStoreID, 10)) } - ctx.WriteString("TO ") - ctx.WriteString(strconv.FormatInt(node.ToStoreID, 10)) + ctx.WriteString(" TO ") + ctx.WriteString(strconv.FormatInt(n.ToStoreID, 10)) ctx.WriteString(" FOR ") - ctx.FormatNode(node.Rows) + ctx.FormatNode(n.Rows) } diff --git a/pkg/sql/sem/tree/split.go b/pkg/sql/sem/tree/split.go index ace7a804a2d8..132f2ac7dfd5 100644 --- a/pkg/sql/sem/tree/split.go +++ b/pkg/sql/sem/tree/split.go @@ -73,9 +73,8 @@ type Relocate struct { // Each row contains an array with store ids and values for the columns in the // PK or index (or a prefix of the columns). // See docs/RFCS/sql_split_syntax.md. - Rows *Select - RelocateLease bool - RelocateNonVoters bool + Rows *Select + SubjectReplicas RelocateSubject } // Format implements the NodeFormatter interface. @@ -87,14 +86,9 @@ func (node *Relocate) Format(ctx *FmtCtx) { ctx.WriteString("TABLE ") } ctx.FormatNode(&node.TableOrIndex) - ctx.WriteString(" EXPERIMENTAL_RELOCATE ") - if node.RelocateLease { - ctx.WriteString("LEASE ") - } else if node.RelocateNonVoters { - ctx.WriteString("NON_VOTERS ") - } else { - ctx.WriteString("VOTERS ") - } + ctx.WriteString(" RELOCATE ") + ctx.FormatNode(&node.SubjectReplicas) + ctx.WriteByte(' ') ctx.FormatNode(node.Rows) } diff --git a/pkg/sql/sem/tree/stmt.go b/pkg/sql/sem/tree/stmt.go index 3897300e90d9..262361a07dd9 100644 --- a/pkg/sql/sem/tree/stmt.go +++ b/pkg/sql/sem/tree/stmt.go @@ -969,12 +969,11 @@ func (*Relocate) StatementType() StatementType { return TypeDML } // StatementTag returns a short string identifying the type of statement. func (n *Relocate) StatementTag() string { - if n.RelocateLease { - return "EXPERIMENTAL_RELOCATE LEASE" - } else if n.RelocateNonVoters { - return "EXPERIMENTAL_RELOCATE NON_VOTERS" + name := "RELOCATE TABLE " + if n.TableOrIndex.Index != "" { + name = "RELOCATE INDEX " } - return "EXPERIMENTAL_RELOCATE VOTERS" + return name + n.SubjectReplicas.String() } // StatementReturnType implements the Statement interface. @@ -985,12 +984,7 @@ func (*RelocateRange) StatementType() StatementType { return TypeDML } // StatementTag returns a short string identifying the type of statement. func (n *RelocateRange) StatementTag() string { - if n.RelocateLease { - return "RELOCATE LEASE" - } else if n.RelocateNonVoters { - return "RELOCATE NON_VOTERS" - } - return "RELOCATE VOTERS" + return "RELOCATE RANGE " + n.SubjectReplicas.String() } // StatementReturnType implements the Statement interface.