Skip to content

Commit

Permalink
schemachanger: support partition zone config
Browse files Browse the repository at this point in the history
This patch adds the functionality to configure
a zone configuration on a partition.

Fixes: cockroachdb#129889

Release note: None
  • Loading branch information
annrpom committed Sep 17, 2024
1 parent bc47d26 commit c4bd018
Show file tree
Hide file tree
Showing 17 changed files with 612 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ statement ok
ALTER partition "us-east-1" of index regional_by_row@regional_by_row_pkey CONFIGURE ZONE USING num_replicas = 10;

statement ok
SET override_multi_region_zone_config = false
SET override_multi_region_zone_config = false;

query TTT
SELECT ZONE_config, index_name, partition_name FROM [SHOW PARTITIONS FROM TABLE regional_by_row]
Expand Down
140 changes: 140 additions & 0 deletions pkg/ccl/schemachangerccl/ccl_generated_test.go

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
setup
CREATE TABLE t (
id INT PRIMARY KEY,
data STRING
) PARTITION BY LIST (id) (
PARTITION p1 VALUES IN (1, 2, 3),
PARTITION p2 VALUES IN (4, 5, 6)
);
----

test
ALTER PARTITION p1 OF INDEX t@t_pkey CONFIGURE ZONE USING num_replicas = 7, gc.ttlseconds = 10000;
----
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* setup */
CREATE TABLE t (
id INT PRIMARY KEY,
data STRING
) PARTITION BY LIST (id) (
PARTITION p1 VALUES IN (1, 2, 3),
PARTITION p2 VALUES IN (4, 5, 6)
);

/* test */
EXPLAIN (DDL) ALTER PARTITION p1 OF INDEX t@t_pkey CONFIGURE ZONE USING num_replicas = 7, gc.ttlseconds = 10000;
----
Schema change plan for ALTER PARTITION ‹p1› OF INDEX ‹defaultdb›.‹public›.‹t›@‹t_pkey› CONFIGURE ZONE USING ‹num_replicas› = ‹7›, ‹"gc.ttlseconds"› = ‹10000›;
├── StatementPhase
│ └── Stage 1 of 1 in StatementPhase
│ ├── 1 element transitioning toward PUBLIC
│ │ └── ABSENT → PUBLIC PartitionZoneConfig:{DescID: 104 (t), IndexID: 1 (t_pkey), SeqNum: 1, PartitionName: "p1"}
│ └── 1 Mutation operation
│ └── AddPartitionZoneConfig {"IndexID":1,"PartitionName":"p1","TableID":104}
└── PreCommitPhase
├── Stage 1 of 2 in PreCommitPhase
│ ├── 1 element transitioning toward PUBLIC
│ │ └── PUBLIC → ABSENT PartitionZoneConfig:{DescID: 104 (t), IndexID: 1 (t_pkey), SeqNum: 1, PartitionName: "p1"}
│ └── 1 Mutation operation
│ └── UndoAllInTxnImmediateMutationOpSideEffects
└── Stage 2 of 2 in PreCommitPhase
├── 1 element transitioning toward PUBLIC
│ └── ABSENT → PUBLIC PartitionZoneConfig:{DescID: 104 (t), IndexID: 1 (t_pkey), SeqNum: 1, PartitionName: "p1"}
└── 1 Mutation operation
└── AddPartitionZoneConfig {"IndexID":1,"PartitionName":"p1","TableID":104}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/* setup */
CREATE TABLE t (
id INT PRIMARY KEY,
data STRING
) PARTITION BY LIST (id) (
PARTITION p1 VALUES IN (1, 2, 3),
PARTITION p2 VALUES IN (4, 5, 6)
);

/* test */
EXPLAIN (DDL, SHAPE) ALTER PARTITION p1 OF INDEX t@t_pkey CONFIGURE ZONE USING num_replicas = 7, gc.ttlseconds = 10000;
----
Schema change plan for ALTER PARTITION ‹p1› OF INDEX ‹defaultdb›.‹public›.‹t›@‹t_pkey› CONFIGURE ZONE USING ‹num_replicas› = ‹7›, ‹"gc.ttlseconds"› = ‹10000›;
└── execute 1 system table mutations transaction
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/* setup */
CREATE TABLE t (
id INT PRIMARY KEY,
data STRING
) PARTITION BY LIST (id) (
PARTITION p1 VALUES IN (1, 2, 3),
PARTITION p2 VALUES IN (4, 5, 6)
);
----
...
+object {100 101 t} -> 104

/* test */
ALTER PARTITION p1 OF INDEX t@t_pkey CONFIGURE ZONE USING num_replicas = 7, gc.ttlseconds = 10000;
----
begin transaction #1
# begin StatementPhase
checking for feature: CONFIGURE ZONE
write *eventpb.AlterTable to event log:
mutationId: 1
sql:
descriptorId: 104
statement: ALTER PARTITION ‹p1› OF INDEX ‹defaultdb›.‹public›.‹t›@‹t_pkey› CONFIGURE ZONE USING ‹num_replicas› = ‹7›, ‹"gc.ttlseconds"› = ‹10000›
tag: CONFIGURE ZONE
user: root
tableName: defaultdb.public.t
## StatementPhase stage 1 of 1 with 1 MutationType op
upsert zone config for #104
# end StatementPhase
# begin PreCommitPhase
## PreCommitPhase stage 1 of 2 with 1 MutationType op
undo all catalog changes within txn #1
persist all catalog changes to storage
## PreCommitPhase stage 2 of 2 with 1 MutationType op
upsert zone config for #104
persist all catalog changes to storage
# end PreCommitPhase
commit transaction #1
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
setup
CREATE TABLE t (
id INT PRIMARY KEY,
data STRING
) PARTITION BY LIST (id) (
PARTITION p1 VALUES IN (1, 2, 3),
PARTITION p2 VALUES IN (4, 5, 6)
);
----

test
ALTER PARTITION p1 OF INDEX t@t_pkey CONFIGURE ZONE USING num_replicas = 7;
ALTER PARTITION p1 OF INDEX t@t_pkey CONFIGURE ZONE USING gc.ttlseconds = 10000;
ALTER PARTITION p1 OF INDEX t@t_pkey CONFIGURE ZONE USING num_replicas = 10;
----
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ go_library(
"drop_view.go",
"helpers.go",
"index_zone_config.go",
"partition_zone_config.go",
"process.go",
"statement_control.go",
"table_zone_config.go",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ func SetZoneConfig(b BuildCtx, n *tree.SetZoneConfig) {
// - Database
// - Table
// - Index
// Left to support:
// - Partition/row
// Left to support:
// - System Ranges
zco, err := astToZoneConfigObject(b, n)
if err != nil {
Expand Down Expand Up @@ -113,17 +113,23 @@ func astToZoneConfigObject(b BuildCtx, n *tree.SetZoneConfig) (zoneConfigObject,
//
// TODO(annie): remove this when we have something equivalent to
// expandMutableIndexName in the DSC.
targetsIndex := n.TargetsIndex() && !n.TargetsPartition()
targetsIndex := n.TargetsIndex()
if targetsIndex && n.TableOrIndex.Table.Table() == "" {
return nil, scerrors.NotImplementedErrorf(n, "referencing an index without a table "+
"prefix is not supported in the DSC")
}

if !n.TargetsTable() || n.TargetsPartition() {
return nil, scerrors.NotImplementedErrorf(n, "zone configurations on partitions "+
"and system ranges are not supported in the DSC")
if !n.TargetsTable() {
return nil, scerrors.NotImplementedErrorf(n, "zone configurations on system ranges "+
"are not supported in the DSC")
}

// If this is an ALTER ALL PARTITIONS statement, fallback to the legacy schema
// changer.
if n.TargetsPartition() && n.ZoneSpecifier.StarIndex {
return nil, scerrors.NotImplementedErrorf(n, "zone configurations on ALL partitions "+
"are not supported in the DSC")
}
tblName := zs.TableOrIndex.Table.ToUnresolvedObjectName()
elems := b.ResolvePhysicalTable(tblName, ResolveParams{})
panicIfSchemaChangeIsDisallowed(elems, n)
Expand Down Expand Up @@ -158,5 +164,12 @@ func astToZoneConfigObject(b BuildCtx, n *tree.SetZoneConfig) (zoneConfigObject,
return &izo, nil
}

// We are a partition object.
if n.TargetsPartition() {
partObj := partitionZoneConfigObj{partitionName: string(n.ZoneSpecifier.Partition),
indexZoneConfigObj: izo}
return &partObj, nil
}

return nil, errors.AssertionFailedf("unexpected zone config object")
}
Loading

0 comments on commit c4bd018

Please sign in to comment.