# LogicTest: multiregion-15node-5region-3azs

statement ok
SET enable_super_regions = 'on'

statement ok
CREATE DATABASE oncreate PRIMARY REGION "ca-central-1" REGIONS "ap-southeast-2", "us-east-1" SUPER REGION "create" VALUES "ap-southeast-2", "us-east-1"

statement ok
CREATE TABLE oncreate.rbt(x INT) LOCALITY REGIONAL BY TABLE IN "us-east-1";

statement ok
CREATE TABLE oncreate.rbr(x INT) LOCALITY REGIONAL BY ROW;

query TT
SHOW ZONE CONFIGURATION FOR TABLE oncreate.rbt;
----
TABLE oncreate.public.rbt  ALTER TABLE oncreate.public.rbt CONFIGURE ZONE USING
                             range_min_bytes = 134217728,
                             range_max_bytes = 536870912,
                             gc.ttlseconds = 14400,
                             num_replicas = 4,
                             num_voters = 3,
                             constraints = '{+region=ap-southeast-2: 1, +region=us-east-1: 1}',
                             voter_constraints = '[+region=us-east-1]',
                             lease_preferences = '[[+region=us-east-1]]'

query TT
SHOW ZONE CONFIGURATION FOR PARTITION "us-east-1" OF TABLE oncreate.rbr
----
PARTITION "us-east-1" OF TABLE oncreate.public.rbr  ALTER PARTITION "us-east-1" OF TABLE oncreate.public.rbr CONFIGURE ZONE USING
                                                      range_min_bytes = 134217728,
                                                      range_max_bytes = 536870912,
                                                      gc.ttlseconds = 14400,
                                                      num_replicas = 4,
                                                      num_voters = 3,
                                                      constraints = '{+region=ap-southeast-2: 1, +region=us-east-1: 1}',
                                                      voter_constraints = '[+region=us-east-1]',
                                                      lease_preferences = '[[+region=us-east-1]]'

statement ok
CREATE DATABASE db PRIMARY REGION "ca-central-1"

# Cannot add super regions with regions that are not regions of the database.
statement error pq: region ap-southeast-2 not part of database
ALTER DATABASE db ADD SUPER REGION "test" VALUES "ap-southeast-2", "us-east-1"

statement ok
ALTER DATABASE db ADD REGION "us-east-1"

statement ok
ALTER DATABASE db ADD REGION "ap-southeast-2"

# Add super region.
statement ok
ALTER DATABASE db ADD SUPER REGION "test" VALUES "ap-southeast-2", "us-east-1"

# Can't add super region with same name.
statement error pq: super region test already exists
ALTER DATABASE db ADD SUPER REGION "test" VALUES "ap-southeast-2", "us-east-1"

# Can't add super region with overlapping region set with previously defined
# super regions.
statement error pq: region us-east-1 is already part of super region test
ALTER DATABASE db ADD SUPER REGION "test2" VALUES "us-east-1"

statement ok
CREATE DATABASE mr1 PRIMARY REGION "us-east-1";

statement ok
CREATE TABLE mr1.rbt(x INT) LOCALITY REGIONAL BY TABLE;

statement ok
CREATE TABLE mr1.rbr(x INT) LOCALITY REGIONAL BY ROW;

statement ok
ALTER DATABASE mr1 ADD SUPER REGION "test" VALUES "us-east-1";

query TT
SHOW ZONE CONFIGURATION FOR TABLE mr1.rbt;
----
TABLE mr1.public.rbt  ALTER TABLE mr1.public.rbt CONFIGURE ZONE USING
                        range_min_bytes = 134217728,
                        range_max_bytes = 536870912,
                        gc.ttlseconds = 14400,
                        num_replicas = 3,
                        num_voters = 3,
                        constraints = '{+region=us-east-1: 1}',
                        voter_constraints = '[+region=us-east-1]',
                        lease_preferences = '[[+region=us-east-1]]'

# mr1.rbr should only have its subzones changed.
query TT
SHOW ZONE CONFIGURATION FOR TABLE mr1.public.rbr
----
DATABASE mr1  ALTER DATABASE mr1 CONFIGURE ZONE USING
                range_min_bytes = 134217728,
                range_max_bytes = 536870912,
                gc.ttlseconds = 14400,
                num_replicas = 3,
                num_voters = 3,
                constraints = '{+region=us-east-1: 1}',
                voter_constraints = '[+region=us-east-1]',
                lease_preferences = '[[+region=us-east-1]]'

query TT
SHOW ZONE CONFIGURATION FOR PARTITION "us-east-1" OF TABLE mr1.rbr
----
PARTITION "us-east-1" OF TABLE mr1.public.rbr  ALTER PARTITION "us-east-1" OF TABLE mr1.public.rbr CONFIGURE ZONE USING
                                                 range_min_bytes = 134217728,
                                                 range_max_bytes = 536870912,
                                                 gc.ttlseconds = 14400,
                                                 num_replicas = 3,
                                                 num_voters = 3,
                                                 constraints = '{+region=us-east-1: 1}',
                                                 voter_constraints = '[+region=us-east-1]',
                                                 lease_preferences = '[[+region=us-east-1]]'

statement ok
use mr1;

statement ok
select crdb_internal.validate_multi_region_zone_configs();

statement ok
CREATE DATABASE mr2 PRIMARY REGION "us-east-1" REGIONS "ap-southeast-2", "ca-central-1"

statement ok
use mr2

statement ok
CREATE TABLE mr2.t_rt(x INT) LOCALITY REGIONAL BY TABLE IN "ap-southeast-2"

statement ok
CREATE TABLE mr2.t_rt_with_idx(x INT, y INT, INDEX idx(y)) LOCALITY REGIONAL BY TABLE IN "ap-southeast-2"

statement ok
CREATE TABLE mr2.t_rbr(x INT) LOCALITY REGIONAL BY ROW;

statement ok
CREATE TABLE mr2.t_rbr_with_idx(x INT, y INT, INDEX idx(y)) LOCALITY REGIONAL BY ROW;

statement ok
ALTER DATABASE mr2 ADD SUPER REGION "test" VALUES "us-east-1", "ap-southeast-2"

statement ok
CREATE TABLE mr2.t_rt_us_east_1() LOCALITY REGIONAL BY TABLE IN "us-east-1";

statement ok
CREATE TABLE mr2.t_rt_primary_region() LOCALITY REGIONAL BY TABLE IN PRIMARY REGION;

query TT
SHOW ZONE CONFIGURATION FOR TABLE mr2.t_rt_us_east_1
----
TABLE mr2.public.t_rt_us_east_1  ALTER TABLE mr2.public.t_rt_us_east_1 CONFIGURE ZONE USING
                                   range_min_bytes = 134217728,
                                   range_max_bytes = 536870912,
                                   gc.ttlseconds = 14400,
                                   num_replicas = 4,
                                   num_voters = 3,
                                   constraints = '{+region=ap-southeast-2: 1, +region=us-east-1: 1}',
                                   voter_constraints = '[+region=us-east-1]',
                                   lease_preferences = '[[+region=us-east-1]]'

query TT
SHOW ZONE CONFIGURATION FOR TABLE mr2.t_rt_primary_region
----
TABLE mr2.public.t_rt_primary_region  ALTER TABLE mr2.public.t_rt_primary_region CONFIGURE ZONE USING
                                        range_min_bytes = 134217728,
                                        range_max_bytes = 536870912,
                                        gc.ttlseconds = 14400,
                                        num_replicas = 4,
                                        num_voters = 3,
                                        constraints = '{+region=ap-southeast-2: 1, +region=us-east-1: 1}',
                                        voter_constraints = '[+region=us-east-1]',
                                        lease_preferences = '[[+region=us-east-1]]'

query TT
SHOW ZONE CONFIGURATION FOR TABLE mr2.t_rt
----
TABLE mr2.public.t_rt  ALTER TABLE mr2.public.t_rt CONFIGURE ZONE USING
                         range_min_bytes = 134217728,
                         range_max_bytes = 536870912,
                         gc.ttlseconds = 14400,
                         num_replicas = 4,
                         num_voters = 3,
                         constraints = '{+region=ap-southeast-2: 1, +region=us-east-1: 1}',
                         voter_constraints = '[+region=ap-southeast-2]',
                         lease_preferences = '[[+region=ap-southeast-2]]'

query TT
SHOW ZONE CONFIGURATION FOR TABLE mr2.t_rt_with_idx
----
TABLE mr2.public.t_rt_with_idx  ALTER TABLE mr2.public.t_rt_with_idx CONFIGURE ZONE USING
                                  range_min_bytes = 134217728,
                                  range_max_bytes = 536870912,
                                  gc.ttlseconds = 14400,
                                  num_replicas = 4,
                                  num_voters = 3,
                                  constraints = '{+region=ap-southeast-2: 1, +region=us-east-1: 1}',
                                  voter_constraints = '[+region=ap-southeast-2]',
                                  lease_preferences = '[[+region=ap-southeast-2]]'

query TT
SHOW ZONE CONFIGURATION FOR INDEX mr2.t_rt_with_idx@idx
----
TABLE mr2.public.t_rt_with_idx  ALTER TABLE mr2.public.t_rt_with_idx CONFIGURE ZONE USING
                                  range_min_bytes = 134217728,
                                  range_max_bytes = 536870912,
                                  gc.ttlseconds = 14400,
                                  num_replicas = 4,
                                  num_voters = 3,
                                  constraints = '{+region=ap-southeast-2: 1, +region=us-east-1: 1}',
                                  voter_constraints = '[+region=ap-southeast-2]',
                                  lease_preferences = '[[+region=ap-southeast-2]]'

query TT
SHOW ZONE CONFIGURATION FOR PARTITION "us-east-1" OF TABLE mr2.t_rbr;
----
PARTITION "us-east-1" OF TABLE mr2.public.t_rbr  ALTER PARTITION "us-east-1" OF TABLE mr2.public.t_rbr CONFIGURE ZONE USING
                                                   range_min_bytes = 134217728,
                                                   range_max_bytes = 536870912,
                                                   gc.ttlseconds = 14400,
                                                   num_replicas = 4,
                                                   num_voters = 3,
                                                   constraints = '{+region=ap-southeast-2: 1, +region=us-east-1: 1}',
                                                   voter_constraints = '[+region=us-east-1]',
                                                   lease_preferences = '[[+region=us-east-1]]'

query TT
SHOW ZONE CONFIGURATION FOR PARTITION "ap-southeast-2" OF TABLE mr2.t_rbr_with_idx;
----
PARTITION "ap-southeast-2" OF TABLE mr2.public.t_rbr_with_idx  ALTER PARTITION "ap-southeast-2" OF TABLE mr2.public.t_rbr_with_idx CONFIGURE ZONE USING
                                                                 range_min_bytes = 134217728,
                                                                 range_max_bytes = 536870912,
                                                                 gc.ttlseconds = 14400,
                                                                 num_replicas = 4,
                                                                 num_voters = 3,
                                                                 constraints = '{+region=ap-southeast-2: 1, +region=us-east-1: 1}',
                                                                 voter_constraints = '[+region=ap-southeast-2]',
                                                                 lease_preferences = '[[+region=ap-southeast-2]]'

query TT
SHOW ZONE CONFIGURATION FOR PARTITION "ca-central-1" OF TABLE mr2.t_rbr_with_idx;
----
PARTITION "ca-central-1" OF TABLE mr2.public.t_rbr_with_idx  ALTER PARTITION "ca-central-1" OF TABLE mr2.public.t_rbr_with_idx CONFIGURE ZONE USING
                                                               range_min_bytes = 134217728,
                                                               range_max_bytes = 536870912,
                                                               gc.ttlseconds = 14400,
                                                               num_replicas = 5,
                                                               num_voters = 3,
                                                               constraints = '{+region=ap-southeast-2: 1, +region=ca-central-1: 1, +region=us-east-1: 1}',
                                                               voter_constraints = '[+region=ca-central-1]',
                                                               lease_preferences = '[[+region=ca-central-1]]'

# Check that indexes also have the updated zone configs.
query TT
SHOW ZONE CONFIGURATION FOR PARTITION "ap-southeast-2" OF INDEX mr2.t_rbr_with_idx@idx
----
PARTITION "ap-southeast-2" OF INDEX mr2.public.t_rbr_with_idx@idx  ALTER PARTITION "ap-southeast-2" OF INDEX mr2.public.t_rbr_with_idx@idx CONFIGURE ZONE USING
                                                                     range_min_bytes = 134217728,
                                                                     range_max_bytes = 536870912,
                                                                     gc.ttlseconds = 14400,
                                                                     num_replicas = 4,
                                                                     num_voters = 3,
                                                                     constraints = '{+region=ap-southeast-2: 1, +region=us-east-1: 1}',
                                                                     voter_constraints = '[+region=ap-southeast-2]',
                                                                     lease_preferences = '[[+region=ap-southeast-2]]'

# Create new tables and ensure they adhere to the super region zone configs.
statement ok
CREATE TABLE mr2.t_rt2(x INT) LOCALITY REGIONAL BY TABLE IN "ap-southeast-2"

statement ok
CREATE TABLE mr2.t_rt_with_idx2(x INT, y INT, INDEX idx(y)) LOCALITY REGIONAL BY TABLE IN "ap-southeast-2"

statement ok
CREATE TABLE mr2.t_rbr2(x INT) LOCALITY REGIONAL BY ROW;

statement ok
CREATE TABLE mr2.t_rbr_with_idx2(x INT, y INT, INDEX idx(y)) LOCALITY REGIONAL BY ROW;

query TT
SHOW ZONE CONFIGURATION FOR TABLE mr2.t_rt2
----
TABLE mr2.public.t_rt2  ALTER TABLE mr2.public.t_rt2 CONFIGURE ZONE USING
                          range_min_bytes = 134217728,
                          range_max_bytes = 536870912,
                          gc.ttlseconds = 14400,
                          num_replicas = 4,
                          num_voters = 3,
                          constraints = '{+region=ap-southeast-2: 1, +region=us-east-1: 1}',
                          voter_constraints = '[+region=ap-southeast-2]',
                          lease_preferences = '[[+region=ap-southeast-2]]'

query TT
SHOW ZONE CONFIGURATION FOR TABLE mr2.t_rt_with_idx2
----
TABLE mr2.public.t_rt_with_idx2  ALTER TABLE mr2.public.t_rt_with_idx2 CONFIGURE ZONE USING
                                   range_min_bytes = 134217728,
                                   range_max_bytes = 536870912,
                                   gc.ttlseconds = 14400,
                                   num_replicas = 4,
                                   num_voters = 3,
                                   constraints = '{+region=ap-southeast-2: 1, +region=us-east-1: 1}',
                                   voter_constraints = '[+region=ap-southeast-2]',
                                   lease_preferences = '[[+region=ap-southeast-2]]'

query TT
SHOW ZONE CONFIGURATION FOR INDEX mr2.t_rt_with_idx2@idx
----
TABLE mr2.public.t_rt_with_idx2  ALTER TABLE mr2.public.t_rt_with_idx2 CONFIGURE ZONE USING
                                   range_min_bytes = 134217728,
                                   range_max_bytes = 536870912,
                                   gc.ttlseconds = 14400,
                                   num_replicas = 4,
                                   num_voters = 3,
                                   constraints = '{+region=ap-southeast-2: 1, +region=us-east-1: 1}',
                                   voter_constraints = '[+region=ap-southeast-2]',
                                   lease_preferences = '[[+region=ap-southeast-2]]'

query TT
SHOW ZONE CONFIGURATION FOR PARTITION "us-east-1" OF TABLE mr2.t_rbr2;
----
PARTITION "us-east-1" OF TABLE mr2.public.t_rbr2  ALTER PARTITION "us-east-1" OF TABLE mr2.public.t_rbr2 CONFIGURE ZONE USING
                                                    range_min_bytes = 134217728,
                                                    range_max_bytes = 536870912,
                                                    gc.ttlseconds = 14400,
                                                    num_replicas = 4,
                                                    num_voters = 3,
                                                    constraints = '{+region=ap-southeast-2: 1, +region=us-east-1: 1}',
                                                    voter_constraints = '[+region=us-east-1]',
                                                    lease_preferences = '[[+region=us-east-1]]'

query TT
SHOW ZONE CONFIGURATION FOR PARTITION "ap-southeast-2" OF TABLE mr2.t_rbr_with_idx2;
----
PARTITION "ap-southeast-2" OF TABLE mr2.public.t_rbr_with_idx2  ALTER PARTITION "ap-southeast-2" OF TABLE mr2.public.t_rbr_with_idx2 CONFIGURE ZONE USING
                                                                  range_min_bytes = 134217728,
                                                                  range_max_bytes = 536870912,
                                                                  gc.ttlseconds = 14400,
                                                                  num_replicas = 4,
                                                                  num_voters = 3,
                                                                  constraints = '{+region=ap-southeast-2: 1, +region=us-east-1: 1}',
                                                                  voter_constraints = '[+region=ap-southeast-2]',
                                                                  lease_preferences = '[[+region=ap-southeast-2]]'

# Check that indexes also have the updated zone configs.
query TT
SHOW ZONE CONFIGURATION FOR PARTITION "ap-southeast-2" OF INDEX mr2.t_rbr_with_idx2@idx
----
PARTITION "ap-southeast-2" OF INDEX mr2.public.t_rbr_with_idx2@idx  ALTER PARTITION "ap-southeast-2" OF INDEX mr2.public.t_rbr_with_idx2@idx CONFIGURE ZONE USING
                                                                      range_min_bytes = 134217728,
                                                                      range_max_bytes = 536870912,
                                                                      gc.ttlseconds = 14400,
                                                                      num_replicas = 4,
                                                                      num_voters = 3,
                                                                      constraints = '{+region=ap-southeast-2: 1, +region=us-east-1: 1}',
                                                                      voter_constraints = '[+region=ap-southeast-2]',
                                                                      lease_preferences = '[[+region=ap-southeast-2]]'

query TT
SHOW ZONE CONFIGURATION FOR PARTITION "us-east-1" OF INDEX mr2.t_rbr_with_idx2@idx
----
PARTITION "us-east-1" OF INDEX mr2.public.t_rbr_with_idx2@idx  ALTER PARTITION "us-east-1" OF INDEX mr2.public.t_rbr_with_idx2@idx CONFIGURE ZONE USING
                                                                 range_min_bytes = 134217728,
                                                                 range_max_bytes = 536870912,
                                                                 gc.ttlseconds = 14400,
                                                                 num_replicas = 4,
                                                                 num_voters = 3,
                                                                 constraints = '{+region=ap-southeast-2: 1, +region=us-east-1: 1}',
                                                                 voter_constraints = '[+region=us-east-1]',
                                                                 lease_preferences = '[[+region=us-east-1]]'

# "ca-central-1" is not part of any super region so replicas should be placed in
# all 3 regions.
query TT
SHOW ZONE CONFIGURATION FOR PARTITION "ca-central-1" OF INDEX mr2.t_rbr_with_idx2@idx
----
PARTITION "ca-central-1" OF INDEX mr2.public.t_rbr_with_idx2@idx  ALTER PARTITION "ca-central-1" OF INDEX mr2.public.t_rbr_with_idx2@idx CONFIGURE ZONE USING
                                                                    range_min_bytes = 134217728,
                                                                    range_max_bytes = 536870912,
                                                                    gc.ttlseconds = 14400,
                                                                    num_replicas = 5,
                                                                    num_voters = 3,
                                                                    constraints = '{+region=ap-southeast-2: 1, +region=ca-central-1: 1, +region=us-east-1: 1}',
                                                                    voter_constraints = '[+region=ca-central-1]',
                                                                    lease_preferences = '[[+region=ca-central-1]]'

statement ok
CREATE TABLE mr2.t_rt_ca_central_1() LOCALITY REGIONAL BY TABLE IN "ca-central-1";

query TT
SHOW ZONE CONFIGURATION FOR TABLE mr2.t_rt_ca_central_1;
----
TABLE mr2.public.t_rt_ca_central_1  ALTER TABLE mr2.public.t_rt_ca_central_1 CONFIGURE ZONE USING
                                      range_min_bytes = 134217728,
                                      range_max_bytes = 536870912,
                                      gc.ttlseconds = 14400,
                                      num_replicas = 5,
                                      num_voters = 3,
                                      constraints = '{+region=ap-southeast-2: 1, +region=ca-central-1: 1, +region=us-east-1: 1}',
                                      voter_constraints = '[+region=ca-central-1]',
                                      lease_preferences = '[[+region=ca-central-1]]'

# Now let's create a super region with only "ca-central-1" and ensure
# mr2.t_rt_ca_central_1's zone configs are updated to only be part of
# the new super region.
statement ok
ALTER DATABASE mr2 ADD SUPER REGION "ca-central-sr" VALUES "ca-central-1";

query TT
SHOW ZONE CONFIGURATION FOR TABLE mr2.t_rt_ca_central_1;
----
TABLE mr2.public.t_rt_ca_central_1  ALTER TABLE mr2.public.t_rt_ca_central_1 CONFIGURE ZONE USING
                                      range_min_bytes = 134217728,
                                      range_max_bytes = 536870912,
                                      gc.ttlseconds = 14400,
                                      num_replicas = 3,
                                      num_voters = 3,
                                      constraints = '{+region=ca-central-1: 1}',
                                      voter_constraints = '[+region=ca-central-1]',
                                      lease_preferences = '[[+region=ca-central-1]]'

# Ensure regional by row table partitions have the correct zone configs
# when there are multiple super regions defined.

statement ok
CREATE DATABASE mr3 PRIMARY REGION "us-east-1" REGIONS "ap-southeast-2", "ca-central-1", "us-west-1", "us-central-1"

statement ok
CREATE TABLE mr3.rt() LOCALITY REGIONAL BY ROW;

statement ok
ALTER DATABASE mr3 ADD SUPER REGION "r1" VALUES "us-east-1", "ap-southeast-2"

statement ok
ALTER DATABASE mr3 ADD SUPER REGION "r2" VALUES "ca-central-1", "us-west-1", "us-central-1"

query TT
SHOW ZONE CONFIGURATION FOR PARTITION "us-east-1" OF TABLE mr3.rt
----
PARTITION "us-east-1" OF TABLE mr3.public.rt  ALTER PARTITION "us-east-1" OF TABLE mr3.public.rt CONFIGURE ZONE USING
                                                range_min_bytes = 134217728,
                                                range_max_bytes = 536870912,
                                                gc.ttlseconds = 14400,
                                                num_replicas = 4,
                                                num_voters = 3,
                                                constraints = '{+region=ap-southeast-2: 1, +region=us-east-1: 1}',
                                                voter_constraints = '[+region=us-east-1]',
                                                lease_preferences = '[[+region=us-east-1]]'

query TT
SHOW ZONE CONFIGURATION FOR PARTITION "ap-southeast-2" OF TABLE mr3.rt
----
PARTITION "ap-southeast-2" OF TABLE mr3.public.rt  ALTER PARTITION "ap-southeast-2" OF TABLE mr3.public.rt CONFIGURE ZONE USING
                                                     range_min_bytes = 134217728,
                                                     range_max_bytes = 536870912,
                                                     gc.ttlseconds = 14400,
                                                     num_replicas = 4,
                                                     num_voters = 3,
                                                     constraints = '{+region=ap-southeast-2: 1, +region=us-east-1: 1}',
                                                     voter_constraints = '[+region=ap-southeast-2]',
                                                     lease_preferences = '[[+region=ap-southeast-2]]'

query TT
SHOW ZONE CONFIGURATION FOR PARTITION "ca-central-1" OF TABLE mr3.rt
----
PARTITION "ca-central-1" OF TABLE mr3.public.rt  ALTER PARTITION "ca-central-1" OF TABLE mr3.public.rt CONFIGURE ZONE USING
                                                   range_min_bytes = 134217728,
                                                   range_max_bytes = 536870912,
                                                   gc.ttlseconds = 14400,
                                                   num_replicas = 5,
                                                   num_voters = 3,
                                                   constraints = '{+region=ca-central-1: 1, +region=us-central-1: 1, +region=us-west-1: 1}',
                                                   voter_constraints = '[+region=ca-central-1]',
                                                   lease_preferences = '[[+region=ca-central-1]]'

query TT
SHOW ZONE CONFIGURATION FOR PARTITION "us-west-1" OF TABLE mr3.rt
----
PARTITION "us-west-1" OF TABLE mr3.public.rt  ALTER PARTITION "us-west-1" OF TABLE mr3.public.rt CONFIGURE ZONE USING
                                                range_min_bytes = 134217728,
                                                range_max_bytes = 536870912,
                                                gc.ttlseconds = 14400,
                                                num_replicas = 5,
                                                num_voters = 3,
                                                constraints = '{+region=ca-central-1: 1, +region=us-central-1: 1, +region=us-west-1: 1}',
                                                voter_constraints = '[+region=us-west-1]',
                                                lease_preferences = '[[+region=us-west-1]]'

query TT
SHOW ZONE CONFIGURATION FOR PARTITION "us-central-1" OF TABLE mr3.rt
----
PARTITION "us-central-1" OF TABLE mr3.public.rt  ALTER PARTITION "us-central-1" OF TABLE mr3.public.rt CONFIGURE ZONE USING
                                                   range_min_bytes = 134217728,
                                                   range_max_bytes = 536870912,
                                                   gc.ttlseconds = 14400,
                                                   num_replicas = 5,
                                                   num_voters = 3,
                                                   constraints = '{+region=ca-central-1: 1, +region=us-central-1: 1, +region=us-west-1: 1}',
                                                   voter_constraints = '[+region=us-central-1]',
                                                   lease_preferences = '[[+region=us-central-1]]'

statement ok
ALTER DATABASE mr3 DROP SUPER REGION "r2"

query TT
SHOW ZONE CONFIGURATION FOR PARTITION "ca-central-1" OF TABLE mr3.rt
----
PARTITION "ca-central-1" OF TABLE mr3.public.rt  ALTER PARTITION "ca-central-1" OF TABLE mr3.public.rt CONFIGURE ZONE USING
                                                   range_min_bytes = 134217728,
                                                   range_max_bytes = 536870912,
                                                   gc.ttlseconds = 14400,
                                                   num_replicas = 7,
                                                   num_voters = 3,
                                                   constraints = '{+region=ap-southeast-2: 1, +region=ca-central-1: 1, +region=us-central-1: 1, +region=us-east-1: 1, +region=us-west-1: 1}',
                                                   voter_constraints = '[+region=ca-central-1]',
                                                   lease_preferences = '[[+region=ca-central-1]]'

query TT
SHOW ZONE CONFIGURATION FOR PARTITION "us-west-1" OF TABLE mr3.rt
----
PARTITION "us-west-1" OF TABLE mr3.public.rt  ALTER PARTITION "us-west-1" OF TABLE mr3.public.rt CONFIGURE ZONE USING
                                                range_min_bytes = 134217728,
                                                range_max_bytes = 536870912,
                                                gc.ttlseconds = 14400,
                                                num_replicas = 7,
                                                num_voters = 3,
                                                constraints = '{+region=ap-southeast-2: 1, +region=ca-central-1: 1, +region=us-central-1: 1, +region=us-east-1: 1, +region=us-west-1: 1}',
                                                voter_constraints = '[+region=us-west-1]',
                                                lease_preferences = '[[+region=us-west-1]]'

query TT
SHOW ZONE CONFIGURATION FOR PARTITION "us-central-1" OF TABLE mr3.rt
----
PARTITION "us-central-1" OF TABLE mr3.public.rt  ALTER PARTITION "us-central-1" OF TABLE mr3.public.rt CONFIGURE ZONE USING
                                                   range_min_bytes = 134217728,
                                                   range_max_bytes = 536870912,
                                                   gc.ttlseconds = 14400,
                                                   num_replicas = 7,
                                                   num_voters = 3,
                                                   constraints = '{+region=ap-southeast-2: 1, +region=ca-central-1: 1, +region=us-central-1: 1, +region=us-east-1: 1, +region=us-west-1: 1}',
                                                   voter_constraints = '[+region=us-central-1]',
                                                   lease_preferences = '[[+region=us-central-1]]'

# Test cases for survive region failure.

statement error pq: super region r1 only has 2 region\(s\): at least 3 regions are required for surviving a region failure
ALTER DATABASE mr3 SURVIVE REGION FAILURE;

statement ok
ALTER DATABASE mr3 DROP SUPER REGION "r1"

statement ok
ALTER DATABASE mr3 ADD SUPER REGION "r2" VALUES "ap-southeast-2", "ca-central-1", "us-central-1";
ALTER DATABASE mr3 SURVIVE REGION FAILURE;

statement error pq: super region r1 only has 2 region\(s\): at least 3 regions are required for surviving a region failure
ALTER DATABASE mr3 ADD SUPER REGION "r1" VALUES "us-west-1", "us-central-1";

# In the case where we have 3 regions within the super region under
# survive region failure mode, the first non-primary region constraint should
# have 2 replicas, this is so all replicas are accounted for.
# ap-southeast-2 in the example below should have 2 replicas because of this.
query TT
SHOW ZONE CONFIGURATION FOR PARTITION "us-central-1" OF TABLE mr3.rt
----
PARTITION "us-central-1" OF TABLE mr3.public.rt  ALTER PARTITION "us-central-1" OF TABLE mr3.public.rt CONFIGURE ZONE USING
                                                   range_min_bytes = 134217728,
                                                   range_max_bytes = 536870912,
                                                   gc.ttlseconds = 14400,
                                                   num_replicas = 5,
                                                   num_voters = 5,
                                                   constraints = '{+region=ap-southeast-2: 2, +region=ca-central-1: 1, +region=us-central-1: 1}',
                                                   voter_constraints = '{+region=us-central-1: 2}',
                                                   lease_preferences = '[[+region=us-central-1]]'

query TT
SHOW ZONE CONFIGURATION FOR PARTITION "ap-southeast-2" OF TABLE mr3.rt
----
PARTITION "ap-southeast-2" OF TABLE mr3.public.rt  ALTER PARTITION "ap-southeast-2" OF TABLE mr3.public.rt CONFIGURE ZONE USING
                                                     range_min_bytes = 134217728,
                                                     range_max_bytes = 536870912,
                                                     gc.ttlseconds = 14400,
                                                     num_replicas = 5,
                                                     num_voters = 5,
                                                     constraints = '{+region=ap-southeast-2: 1, +region=ca-central-1: 2, +region=us-central-1: 1}',
                                                     voter_constraints = '{+region=ap-southeast-2: 2}',
                                                     lease_preferences = '[[+region=ap-southeast-2]]'

# Test region failure with 5 regions in a super region.
# 5 regions is the minimum number of regions we need to have more than
# 5 replicas in the zone config.
statement ok
ALTER DATABASE mr3 DROP SUPER REGION "r2";
ALTER DATABASE mr3 ADD SUPER REGION "r1" VALUES "ca-central-1", "us-west-1", "us-central-1", "ap-southeast-2", "us-east-1";

query TT
SHOW ZONE CONFIGURATION FOR PARTITION "us-central-1" OF TABLE mr3.rt
----
PARTITION "us-central-1" OF TABLE mr3.public.rt  ALTER PARTITION "us-central-1" OF TABLE mr3.public.rt CONFIGURE ZONE USING
                                                   range_min_bytes = 134217728,
                                                   range_max_bytes = 536870912,
                                                   gc.ttlseconds = 14400,
                                                   num_replicas = 6,
                                                   num_voters = 5,
                                                   constraints = '{+region=ap-southeast-2: 1, +region=ca-central-1: 1, +region=us-central-1: 1, +region=us-east-1: 1, +region=us-west-1: 1}',
                                                   voter_constraints = '{+region=us-central-1: 2}',
                                                   lease_preferences = '[[+region=us-central-1]]'

query TT
SHOW ZONE CONFIGURATION FOR PARTITION "ap-southeast-2" OF TABLE mr3.rt
----
PARTITION "ap-southeast-2" OF TABLE mr3.public.rt  ALTER PARTITION "ap-southeast-2" OF TABLE mr3.public.rt CONFIGURE ZONE USING
                                                     range_min_bytes = 134217728,
                                                     range_max_bytes = 536870912,
                                                     gc.ttlseconds = 14400,
                                                     num_replicas = 6,
                                                     num_voters = 5,
                                                     constraints = '{+region=ap-southeast-2: 1, +region=ca-central-1: 1, +region=us-central-1: 1, +region=us-east-1: 1, +region=us-west-1: 1}',
                                                     voter_constraints = '{+region=ap-southeast-2: 2}',
                                                     lease_preferences = '[[+region=ap-southeast-2]]'

# a user should not be able to drop a region that is a member of a super region.
statement error pq: region us-east-1 is part of super region test
ALTER DATABASE db DROP REGION "us-east-1";

# Dropping primary region.
statement ok
CREATE DATABASE db2 PRIMARY REGION "ca-central-1";

statement ok
ALTER DATABASE db2 ADD SUPER REGION "test" VALUES "ca-central-1";

statement error pq: region ca-central-1 is part of super region test
ALTER DATABASE db2 DROP REGION "ca-central-1";

# Test the case where we have regional tables in the primary region. Initially
# the primary region is part of a super region but we alter the primary region
# to be part of another super region.
statement ok
CREATE DATABASE db3 PRIMARY REGION "ca-central-1" REGIONS "us-east-1", "us-west-1";

statement ok
ALTER DATABASE db3 ADD SUPER REGION "test1" VALUES "ca-central-1", "us-west-1";

statement ok
ALTER DATABASE db3 ADD SUPER REGION "test2" VALUES "us-east-1";

statement ok
CREATE TABLE db3.t() LOCALITY REGIONAL BY TABLE;

query TT
SHOW ZONE CONFIGURATION FOR TABLE db3.t
----
TABLE db3.public.t  ALTER TABLE db3.public.t CONFIGURE ZONE USING
                      range_min_bytes = 134217728,
                      range_max_bytes = 536870912,
                      gc.ttlseconds = 14400,
                      num_replicas = 4,
                      num_voters = 3,
                      constraints = '{+region=ca-central-1: 1, +region=us-west-1: 1}',
                      voter_constraints = '[+region=ca-central-1]',
                      lease_preferences = '[[+region=ca-central-1]]'

# ALTER PRIMARY REGION to "us-west-1" does not block because the primary
# region does not change super regions.
statement ok
ALTER DATABASE db3 SET PRIMARY REGION "us-west-1"

statement error pq: moving the primary region into or out of a super region could have undesirable data placement implications as all regional tables in the primary region will now moved\.\s*the new primary region us-east-1 is a member of super region test2\s*the old primary region us-west-1 is a member of super region test1\s*HINT: You can enable this operation by running `SET alter_primary_region_super_region_override = 'on'`\.
ALTER DATABASE db3 SET PRIMARY REGION "us-east-1";

statement ok
SET alter_primary_region_super_region_override = 'on'

# Alter primary region to "us-east-1" such that table db3.t moves from super
# region test1 to test2.
query T noticetrace
ALTER DATABASE db3 SET PRIMARY REGION "us-east-1";
----
NOTICE: all REGIONAL BY TABLE tables without a region explicitly set are now part of the super region test2
NOTICE: all REGIONAL BY TABLE tables without a region explicitly set are no longer part of the super region test1

query TT
SHOW ZONE CONFIGURATION FOR TABLE db3.t
----
TABLE db3.public.t  ALTER TABLE db3.public.t CONFIGURE ZONE USING
                      range_min_bytes = 134217728,
                      range_max_bytes = 536870912,
                      gc.ttlseconds = 14400,
                      num_replicas = 3,
                      num_voters = 3,
                      constraints = '{+region=us-east-1: 1}',
                      voter_constraints = '[+region=us-east-1]',
                      lease_preferences = '[[+region=us-east-1]]'

# Alter primary region to "us-west-1" such that db3.t is no longer in a
# super region.

query T noticetrace
ALTER DATABASE db3 SET PRIMARY REGION "us-west-1";
----
NOTICE: all REGIONAL BY TABLE tables without a region explicitly set are now part of the super region test1
NOTICE: all REGIONAL BY TABLE tables without a region explicitly set are no longer part of the super region test2

query TT
SHOW ZONE CONFIGURATION FOR TABLE db3.t
----
TABLE db3.public.t  ALTER TABLE db3.public.t CONFIGURE ZONE USING
                      range_min_bytes = 134217728,
                      range_max_bytes = 536870912,
                      gc.ttlseconds = 14400,
                      num_replicas = 4,
                      num_voters = 3,
                      constraints = '{+region=ca-central-1: 1, +region=us-west-1: 1}',
                      voter_constraints = '[+region=us-west-1]',
                      lease_preferences = '[[+region=us-west-1]]'

# ALTER SUPER REGION test cases.

# ALTER SUPER REGION that doesn't exist.
statement error pq: super region "fake" of database "db3" does not exist
ALTER DATABASE db3 ALTER SUPER REGION "fake" VALUES "ca-central-1", "us-west-1", "us-east-1";

# super region has region that is in another region.
statement error pq: region us-east-1 is already part of super region test2
ALTER DATABASE db3 ALTER SUPER REGION "test1" VALUES "ca-central-1", "us-west-1", "us-east-1";

# alter super region with region that does not exist.
statement error pq: region fake-region not part of database
ALTER DATABASE db3 ALTER SUPER REGION "test1" VALUES "fake-region"

# alter super region such that it cannot match the database's survival goal.
statement error pq: super region r1 only has 1 region\(s\): at least 3 regions are required for surviving a region failure
ALTER DATABASE mr3 ALTER SUPER REGION "r1" VALUES "us-west-1";

statement ok
ALTER DATABASE db3 DROP SUPER REGION "test2";
ALTER DATABASE db3 ALTER SUPER REGION "test1" VALUES "ca-central-1", "us-west-1", "us-east-1";

query TT
SHOW ZONE CONFIGURATION FOR TABLE db3.t
----
TABLE db3.public.t  ALTER TABLE db3.public.t CONFIGURE ZONE USING
                      range_min_bytes = 134217728,
                      range_max_bytes = 536870912,
                      gc.ttlseconds = 14400,
                      num_replicas = 5,
                      num_voters = 3,
                      constraints = '{+region=ca-central-1: 1, +region=us-east-1: 1, +region=us-west-1: 1}',
                      voter_constraints = '[+region=us-west-1]',
                      lease_preferences = '[[+region=us-west-1]]'

statement ok
ALTER DATABASE db3 ALTER SUPER REGION "test1" VALUES "us-west-1";

query TT
SHOW ZONE CONFIGURATION FOR TABLE db3.t
----
TABLE db3.public.t  ALTER TABLE db3.public.t CONFIGURE ZONE USING
                      range_min_bytes = 134217728,
                      range_max_bytes = 536870912,
                      gc.ttlseconds = 14400,
                      num_replicas = 3,
                      num_voters = 3,
                      constraints = '{+region=us-west-1: 1}',
                      voter_constraints = '[+region=us-west-1]',
                      lease_preferences = '[[+region=us-west-1]]'

query ITTT rowsort
SELECT id, database_name, super_region_name, regions FROM crdb_internal.super_regions
----
112  db        test           {ap-southeast-2,us-east-1}
142  db2       test           {ca-central-1}
146  db3       test1          {us-west-1}
116  mr1       test           {us-east-1}
122  mr2       ca-central-sr  {ca-central-1}
122  mr2       test           {ap-southeast-2,us-east-1}
137  mr3       r1             {ap-southeast-2,ca-central-1,us-central-1,us-east-1,us-west-1}
106  oncreate  create         {ap-southeast-2,us-east-1}

query TTT
SHOW SUPER REGIONS FROM DATABASE test
----

query TTT
SHOW SUPER REGIONS FROM DATABASE db
----
db  test  {ap-southeast-2,us-east-1}

query TTT
SHOW SUPER REGIONS FROM DATABASE db2
----
db2  test  {ca-central-1}

query TTT
SHOW SUPER REGIONS FROM DATABASE db3
----
db3  test1  {us-west-1}

query TTT
SHOW SUPER REGIONS FROM DATABASE mr1
----
mr1  test  {us-east-1}

query TTT rowsort
SHOW SUPER REGIONS FROM DATABASE mr2
----
mr2  ca-central-sr  {ca-central-1}
mr2  test           {ap-southeast-2,us-east-1}

query TTT
SHOW SUPER REGIONS FROM DATABASE mr3
----
mr3  r1  {ap-southeast-2,ca-central-1,us-central-1,us-east-1,us-west-1}
