# LogicTest: multiregion-9node-3region-3azs multiregion-9node-3region-3azs-no-los

statement ok
CREATE DATABASE alter_locality_test primary region "ca-central-1" regions "ap-southeast-2", "us-east-1"

statement ok
CREATE TABLE no_table_locality (
  pk INT PRIMARY KEY,
  i INT,
  FAMILY (pk, i)
)

statement error cannot alter a table's LOCALITY if its database is not multi-region enabled\nHINT: database must first be multi-region enabled using ALTER DATABASE ... SET PRIMARY REGION <region>
ALTER TABLE no_table_locality SET LOCALITY REGIONAL BY TABLE

statement ok
ALTER TABLE IF EXISTS table_does_not_exist SET LOCALITY REGIONAL BY TABLE

statement ok
use alter_locality_test

statement ok
CREATE TABLE regional_by_row (
  pk INT PRIMARY KEY,
  i INT,
  INDEX(i),
  FAMILY (pk, i)
) LOCALITY REGIONAL BY ROW

query TT
SHOW CREATE TABLE regional_by_row
----
regional_by_row  CREATE TABLE public.regional_by_row (
                   pk INT8 NOT NULL,
                   i INT8 NULL,
                   crdb_region public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::public.crdb_internal_region,
                   CONSTRAINT regional_by_row_pkey PRIMARY KEY (pk ASC),
                   INDEX regional_by_row_i_idx (i ASC),
                   FAMILY fam_0_pk_i_crdb_region (pk, i, crdb_region)
                 ) LOCALITY REGIONAL BY ROW

query TTT
SELECT zone_config, index_name, partition_name FROM [SHOW PARTITIONS FROM TABLE regional_by_row]
ORDER BY partition_name, index_name
----
num_voters = 3,\nvoter_constraints = '[+region=ap-southeast-2]',\nlease_preferences = '[[+region=ap-southeast-2]]'  regional_by_row@regional_by_row_i_idx  ap-southeast-2
num_voters = 3,\nvoter_constraints = '[+region=ap-southeast-2]',\nlease_preferences = '[[+region=ap-southeast-2]]'  regional_by_row@regional_by_row_pkey   ap-southeast-2
num_voters = 3,\nvoter_constraints = '[+region=ca-central-1]',\nlease_preferences = '[[+region=ca-central-1]]'      regional_by_row@regional_by_row_i_idx  ca-central-1
num_voters = 3,\nvoter_constraints = '[+region=ca-central-1]',\nlease_preferences = '[[+region=ca-central-1]]'      regional_by_row@regional_by_row_pkey   ca-central-1
num_voters = 3,\nvoter_constraints = '[+region=us-east-1]',\nlease_preferences = '[[+region=us-east-1]]'            regional_by_row@regional_by_row_i_idx  us-east-1
num_voters = 3,\nvoter_constraints = '[+region=us-east-1]',\nlease_preferences = '[[+region=us-east-1]]'            regional_by_row@regional_by_row_pkey   us-east-1

query TT
SHOW ZONE CONFIGURATION FROM TABLE regional_by_row
----
DATABASE alter_locality_test  ALTER DATABASE alter_locality_test 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 regional_by_row_as (
  pk INT PRIMARY KEY,
  i INT,
  cr crdb_internal_region NOT NULL DEFAULT 'ap-southeast-2',
  INDEX(i),
  FAMILY (cr, pk, i)
) LOCALITY REGIONAL BY ROW AS "cr";

query TT
SHOW CREATE TABLE regional_by_row_as
----
regional_by_row_as  CREATE TABLE public.regional_by_row_as (
                      pk INT8 NOT NULL,
                      i INT8 NULL,
                      cr public.crdb_internal_region NOT NULL DEFAULT 'ap-southeast-2':::public.crdb_internal_region,
                      CONSTRAINT regional_by_row_as_pkey PRIMARY KEY (pk ASC),
                      INDEX regional_by_row_as_i_idx (i ASC),
                      FAMILY fam_0_cr_pk_i (cr, pk, i)
                    ) LOCALITY REGIONAL BY ROW AS cr

query TTT
SELECT zone_config, index_name, partition_name FROM [SHOW PARTITIONS FROM TABLE regional_by_row]
ORDER BY partition_name, index_name
----
num_voters = 3,\nvoter_constraints = '[+region=ap-southeast-2]',\nlease_preferences = '[[+region=ap-southeast-2]]'  regional_by_row@regional_by_row_i_idx  ap-southeast-2
num_voters = 3,\nvoter_constraints = '[+region=ap-southeast-2]',\nlease_preferences = '[[+region=ap-southeast-2]]'  regional_by_row@regional_by_row_pkey   ap-southeast-2
num_voters = 3,\nvoter_constraints = '[+region=ca-central-1]',\nlease_preferences = '[[+region=ca-central-1]]'      regional_by_row@regional_by_row_i_idx  ca-central-1
num_voters = 3,\nvoter_constraints = '[+region=ca-central-1]',\nlease_preferences = '[[+region=ca-central-1]]'      regional_by_row@regional_by_row_pkey   ca-central-1
num_voters = 3,\nvoter_constraints = '[+region=us-east-1]',\nlease_preferences = '[[+region=us-east-1]]'            regional_by_row@regional_by_row_i_idx  us-east-1
num_voters = 3,\nvoter_constraints = '[+region=us-east-1]',\nlease_preferences = '[[+region=us-east-1]]'            regional_by_row@regional_by_row_pkey   us-east-1

query TT
SHOW ZONE CONFIGURATION FROM TABLE regional_by_row_as
----
DATABASE alter_locality_test  ALTER DATABASE alter_locality_test 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 regional_by_table_in_primary_region (
  pk INT PRIMARY KEY,
  i INT,
  FAMILY (pk, i)
) LOCALITY REGIONAL BY TABLE IN PRIMARY REGION

query TT
SHOW CREATE TABLE regional_by_table_in_primary_region
----
regional_by_table_in_primary_region             CREATE TABLE public.regional_by_table_in_primary_region (
                                                pk INT8 NOT NULL,
                                                i INT8 NULL,
                                                CONSTRAINT regional_by_table_in_primary_region_pkey PRIMARY KEY (pk ASC),
                                                FAMILY fam_0_pk_i (pk, i)
) LOCALITY REGIONAL BY TABLE IN PRIMARY REGION

query TTT
SELECT zone_config, index_name, partition_name FROM [SHOW PARTITIONS FROM TABLE regional_by_row]
ORDER BY partition_name, index_name
----
num_voters = 3,\nvoter_constraints = '[+region=ap-southeast-2]',\nlease_preferences = '[[+region=ap-southeast-2]]'  regional_by_row@regional_by_row_i_idx  ap-southeast-2
num_voters = 3,\nvoter_constraints = '[+region=ap-southeast-2]',\nlease_preferences = '[[+region=ap-southeast-2]]'  regional_by_row@regional_by_row_pkey   ap-southeast-2
num_voters = 3,\nvoter_constraints = '[+region=ca-central-1]',\nlease_preferences = '[[+region=ca-central-1]]'      regional_by_row@regional_by_row_i_idx  ca-central-1
num_voters = 3,\nvoter_constraints = '[+region=ca-central-1]',\nlease_preferences = '[[+region=ca-central-1]]'      regional_by_row@regional_by_row_pkey   ca-central-1
num_voters = 3,\nvoter_constraints = '[+region=us-east-1]',\nlease_preferences = '[[+region=us-east-1]]'            regional_by_row@regional_by_row_i_idx  us-east-1
num_voters = 3,\nvoter_constraints = '[+region=us-east-1]',\nlease_preferences = '[[+region=us-east-1]]'            regional_by_row@regional_by_row_pkey   us-east-1

query TT
SHOW ZONE CONFIGURATION FROM TABLE regional_by_table_in_primary_region
----
DATABASE alter_locality_test  ALTER DATABASE alter_locality_test 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 regional_by_table_no_region (
  pk INT PRIMARY KEY,
  i INT,
  FAMILY (pk, i)
) LOCALITY REGIONAL BY TABLE

query TT
SHOW CREATE TABLE regional_by_table_no_region
----
regional_by_table_no_region                     CREATE TABLE public.regional_by_table_no_region (
                                                pk INT8 NOT NULL,
                                                i INT8 NULL,
                                                CONSTRAINT regional_by_table_no_region_pkey PRIMARY KEY (pk ASC),
                                                FAMILY fam_0_pk_i (pk, i)
) LOCALITY REGIONAL BY TABLE IN PRIMARY REGION

query TTT
SELECT zone_config, index_name, partition_name FROM [SHOW PARTITIONS FROM TABLE regional_by_row]
ORDER BY partition_name, index_name
----
num_voters = 3,\nvoter_constraints = '[+region=ap-southeast-2]',\nlease_preferences = '[[+region=ap-southeast-2]]'  regional_by_row@regional_by_row_i_idx  ap-southeast-2
num_voters = 3,\nvoter_constraints = '[+region=ap-southeast-2]',\nlease_preferences = '[[+region=ap-southeast-2]]'  regional_by_row@regional_by_row_pkey   ap-southeast-2
num_voters = 3,\nvoter_constraints = '[+region=ca-central-1]',\nlease_preferences = '[[+region=ca-central-1]]'      regional_by_row@regional_by_row_i_idx  ca-central-1
num_voters = 3,\nvoter_constraints = '[+region=ca-central-1]',\nlease_preferences = '[[+region=ca-central-1]]'      regional_by_row@regional_by_row_pkey   ca-central-1
num_voters = 3,\nvoter_constraints = '[+region=us-east-1]',\nlease_preferences = '[[+region=us-east-1]]'            regional_by_row@regional_by_row_i_idx  us-east-1
num_voters = 3,\nvoter_constraints = '[+region=us-east-1]',\nlease_preferences = '[[+region=us-east-1]]'            regional_by_row@regional_by_row_pkey   us-east-1

query TT
SHOW ZONE CONFIGURATION FROM TABLE regional_by_table_no_region
----
DATABASE alter_locality_test  ALTER DATABASE alter_locality_test 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 regional_by_table_in_us_east (
  pk INT PRIMARY KEY,
  i INT,
  FAMILY (pk, i)
) LOCALITY REGIONAL BY TABLE IN "us-east-1"

query TT
SHOW CREATE TABLE regional_by_table_in_us_east
----
regional_by_table_in_us_east                 CREATE TABLE public.regional_by_table_in_us_east (
                                             pk INT8 NOT NULL,
                                             i INT8 NULL,
                                             CONSTRAINT regional_by_table_in_us_east_pkey PRIMARY KEY (pk ASC),
                                             FAMILY fam_0_pk_i (pk, i)
) LOCALITY REGIONAL BY TABLE IN "us-east-1"

query TTT
SELECT zone_config, index_name, partition_name FROM [SHOW PARTITIONS FROM TABLE regional_by_row]
ORDER BY partition_name, index_name
----
num_voters = 3,\nvoter_constraints = '[+region=ap-southeast-2]',\nlease_preferences = '[[+region=ap-southeast-2]]'  regional_by_row@regional_by_row_i_idx  ap-southeast-2
num_voters = 3,\nvoter_constraints = '[+region=ap-southeast-2]',\nlease_preferences = '[[+region=ap-southeast-2]]'  regional_by_row@regional_by_row_pkey   ap-southeast-2
num_voters = 3,\nvoter_constraints = '[+region=ca-central-1]',\nlease_preferences = '[[+region=ca-central-1]]'      regional_by_row@regional_by_row_i_idx  ca-central-1
num_voters = 3,\nvoter_constraints = '[+region=ca-central-1]',\nlease_preferences = '[[+region=ca-central-1]]'      regional_by_row@regional_by_row_pkey   ca-central-1
num_voters = 3,\nvoter_constraints = '[+region=us-east-1]',\nlease_preferences = '[[+region=us-east-1]]'            regional_by_row@regional_by_row_i_idx  us-east-1
num_voters = 3,\nvoter_constraints = '[+region=us-east-1]',\nlease_preferences = '[[+region=us-east-1]]'            regional_by_row@regional_by_row_pkey   us-east-1

query TT
SHOW ZONE CONFIGURATION FROM TABLE regional_by_table_in_us_east
----
TABLE regional_by_table_in_us_east  ALTER TABLE regional_by_table_in_us_east 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=us-east-1]',
                                      lease_preferences = '[[+region=us-east-1]]'

statement ok
CREATE TABLE created_as_global (pk INT PRIMARY KEY, i int, FAMILY (pk, i)) LOCALITY GLOBAL

query TT
SHOW CREATE TABLE created_as_global
----
created_as_global  CREATE TABLE public.created_as_global (
                   pk INT8 NOT NULL,
                   i INT8 NULL,
                   CONSTRAINT created_as_global_pkey PRIMARY KEY (pk ASC),
                   FAMILY fam_0_pk_i (pk, i)
) LOCALITY GLOBAL

query TTT
SELECT zone_config, index_name, partition_name FROM [SHOW PARTITIONS FROM TABLE regional_by_row]
ORDER BY partition_name, index_name
----
num_voters = 3,\nvoter_constraints = '[+region=ap-southeast-2]',\nlease_preferences = '[[+region=ap-southeast-2]]'  regional_by_row@regional_by_row_i_idx  ap-southeast-2
num_voters = 3,\nvoter_constraints = '[+region=ap-southeast-2]',\nlease_preferences = '[[+region=ap-southeast-2]]'  regional_by_row@regional_by_row_pkey   ap-southeast-2
num_voters = 3,\nvoter_constraints = '[+region=ca-central-1]',\nlease_preferences = '[[+region=ca-central-1]]'      regional_by_row@regional_by_row_i_idx  ca-central-1
num_voters = 3,\nvoter_constraints = '[+region=ca-central-1]',\nlease_preferences = '[[+region=ca-central-1]]'      regional_by_row@regional_by_row_pkey   ca-central-1
num_voters = 3,\nvoter_constraints = '[+region=us-east-1]',\nlease_preferences = '[[+region=us-east-1]]'            regional_by_row@regional_by_row_i_idx  us-east-1
num_voters = 3,\nvoter_constraints = '[+region=us-east-1]',\nlease_preferences = '[[+region=us-east-1]]'            regional_by_row@regional_by_row_pkey   us-east-1

query TT
SHOW ZONE CONFIGURATION FROM TABLE created_as_global
----
TABLE created_as_global  ALTER TABLE created_as_global CONFIGURE ZONE USING
                           range_min_bytes = 134217728,
                           range_max_bytes = 536870912,
                           gc.ttlseconds = 14400,
                           global_reads = true,
                           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]]'

# Altering from GLOBAL
statement ok
ALTER TABLE created_as_global SET LOCALITY REGIONAL BY TABLE

query TT
SHOW CREATE TABLE created_as_global
----
created_as_global                               CREATE TABLE public.created_as_global (
                                                pk INT8 NOT NULL,
                                                i INT8 NULL,
                                                CONSTRAINT created_as_global_pkey PRIMARY KEY (pk ASC),
                                                FAMILY fam_0_pk_i (pk, i)
) LOCALITY REGIONAL BY TABLE IN PRIMARY REGION

query TTT
SELECT zone_config, index_name, partition_name FROM [SHOW PARTITIONS FROM TABLE regional_by_row]
ORDER BY partition_name, index_name
----
num_voters = 3,\nvoter_constraints = '[+region=ap-southeast-2]',\nlease_preferences = '[[+region=ap-southeast-2]]'  regional_by_row@regional_by_row_i_idx  ap-southeast-2
num_voters = 3,\nvoter_constraints = '[+region=ap-southeast-2]',\nlease_preferences = '[[+region=ap-southeast-2]]'  regional_by_row@regional_by_row_pkey   ap-southeast-2
num_voters = 3,\nvoter_constraints = '[+region=ca-central-1]',\nlease_preferences = '[[+region=ca-central-1]]'      regional_by_row@regional_by_row_i_idx  ca-central-1
num_voters = 3,\nvoter_constraints = '[+region=ca-central-1]',\nlease_preferences = '[[+region=ca-central-1]]'      regional_by_row@regional_by_row_pkey   ca-central-1
num_voters = 3,\nvoter_constraints = '[+region=us-east-1]',\nlease_preferences = '[[+region=us-east-1]]'            regional_by_row@regional_by_row_i_idx  us-east-1
num_voters = 3,\nvoter_constraints = '[+region=us-east-1]',\nlease_preferences = '[[+region=us-east-1]]'            regional_by_row@regional_by_row_pkey   us-east-1

query TT
SHOW ZONE CONFIGURATION FROM TABLE created_as_global
----
DATABASE alter_locality_test  ALTER DATABASE alter_locality_test 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]]'

# Alter back, to get back to original state
statement ok
ALTER TABLE created_as_global SET LOCALITY GLOBAL

statement ok
ALTER TABLE created_as_global SET LOCALITY REGIONAL BY TABLE in "ap-southeast-2"

query TT
SHOW CREATE TABLE created_as_global
----
created_as_global                                 CREATE TABLE public.created_as_global (
                                                  pk INT8 NOT NULL,
                                                  i INT8 NULL,
                                                  CONSTRAINT created_as_global_pkey PRIMARY KEY (pk ASC),
                                                  FAMILY fam_0_pk_i (pk, i)
) LOCALITY REGIONAL BY TABLE IN "ap-southeast-2"

query TTT
SELECT zone_config, index_name, partition_name FROM [SHOW PARTITIONS FROM TABLE regional_by_row]
ORDER BY partition_name, index_name
----
num_voters = 3,\nvoter_constraints = '[+region=ap-southeast-2]',\nlease_preferences = '[[+region=ap-southeast-2]]'  regional_by_row@regional_by_row_i_idx  ap-southeast-2
num_voters = 3,\nvoter_constraints = '[+region=ap-southeast-2]',\nlease_preferences = '[[+region=ap-southeast-2]]'  regional_by_row@regional_by_row_pkey   ap-southeast-2
num_voters = 3,\nvoter_constraints = '[+region=ca-central-1]',\nlease_preferences = '[[+region=ca-central-1]]'      regional_by_row@regional_by_row_i_idx  ca-central-1
num_voters = 3,\nvoter_constraints = '[+region=ca-central-1]',\nlease_preferences = '[[+region=ca-central-1]]'      regional_by_row@regional_by_row_pkey   ca-central-1
num_voters = 3,\nvoter_constraints = '[+region=us-east-1]',\nlease_preferences = '[[+region=us-east-1]]'            regional_by_row@regional_by_row_i_idx  us-east-1
num_voters = 3,\nvoter_constraints = '[+region=us-east-1]',\nlease_preferences = '[[+region=us-east-1]]'            regional_by_row@regional_by_row_pkey   us-east-1

query TT
SHOW ZONE CONFIGURATION FROM TABLE created_as_global
----
TABLE created_as_global  ALTER TABLE created_as_global 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=ap-southeast-2]',
                           lease_preferences = '[[+region=ap-southeast-2]]'

# Alter back, to get back to original state
statement ok
ALTER TABLE created_as_global SET LOCALITY GLOBAL

statement ok
ALTER TABLE created_as_global SET LOCALITY REGIONAL BY TABLE IN PRIMARY REGION

query TT
SHOW CREATE TABLE created_as_global
----
created_as_global                               CREATE TABLE public.created_as_global (
                                                pk INT8 NOT NULL,
                                                i INT8 NULL,
                                                CONSTRAINT created_as_global_pkey PRIMARY KEY (pk ASC),
                                                FAMILY fam_0_pk_i (pk, i)
) LOCALITY REGIONAL BY TABLE IN PRIMARY REGION

query TTT
SELECT zone_config, index_name, partition_name FROM [SHOW PARTITIONS FROM TABLE regional_by_row]
ORDER BY partition_name, index_name
----
num_voters = 3,\nvoter_constraints = '[+region=ap-southeast-2]',\nlease_preferences = '[[+region=ap-southeast-2]]'  regional_by_row@regional_by_row_i_idx  ap-southeast-2
num_voters = 3,\nvoter_constraints = '[+region=ap-southeast-2]',\nlease_preferences = '[[+region=ap-southeast-2]]'  regional_by_row@regional_by_row_pkey   ap-southeast-2
num_voters = 3,\nvoter_constraints = '[+region=ca-central-1]',\nlease_preferences = '[[+region=ca-central-1]]'      regional_by_row@regional_by_row_i_idx  ca-central-1
num_voters = 3,\nvoter_constraints = '[+region=ca-central-1]',\nlease_preferences = '[[+region=ca-central-1]]'      regional_by_row@regional_by_row_pkey   ca-central-1
num_voters = 3,\nvoter_constraints = '[+region=us-east-1]',\nlease_preferences = '[[+region=us-east-1]]'            regional_by_row@regional_by_row_i_idx  us-east-1
num_voters = 3,\nvoter_constraints = '[+region=us-east-1]',\nlease_preferences = '[[+region=us-east-1]]'            regional_by_row@regional_by_row_pkey   us-east-1

query TT
SHOW ZONE CONFIGURATION FROM TABLE created_as_global
----
DATABASE alter_locality_test  ALTER DATABASE alter_locality_test 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]]'

# Alter back, to get back to original state
statement ok
ALTER TABLE created_as_global SET LOCALITY GLOBAL

statement ok
ALTER TABLE created_as_global SET LOCALITY GLOBAL

query TT
SHOW CREATE TABLE created_as_global
----
created_as_global  CREATE TABLE public.created_as_global (
                   pk INT8 NOT NULL,
                   i INT8 NULL,
                   CONSTRAINT created_as_global_pkey PRIMARY KEY (pk ASC),
                   FAMILY fam_0_pk_i (pk, i)
) LOCALITY GLOBAL

# Drop the table and recreate it with columns and indexes appropriate
# for REGIONAL BY ROW transformations.
statement ok
DROP TABLE created_as_global;

statement ok
CREATE TABLE created_as_global (
  pk int primary key,
  i int,
  b int,
  index(b),
  unique(i),
  FAMILY (pk, i, b)
) LOCALITY GLOBAL;

statement ok
INSERT INTO created_as_global VALUES (0, 1, 2)

query TTT
SELECT zone_config, index_name, partition_name FROM [SHOW PARTITIONS FROM TABLE regional_by_row]
ORDER BY partition_name, index_name
----
num_voters = 3,\nvoter_constraints = '[+region=ap-southeast-2]',\nlease_preferences = '[[+region=ap-southeast-2]]'  regional_by_row@regional_by_row_i_idx  ap-southeast-2
num_voters = 3,\nvoter_constraints = '[+region=ap-southeast-2]',\nlease_preferences = '[[+region=ap-southeast-2]]'  regional_by_row@regional_by_row_pkey   ap-southeast-2
num_voters = 3,\nvoter_constraints = '[+region=ca-central-1]',\nlease_preferences = '[[+region=ca-central-1]]'      regional_by_row@regional_by_row_i_idx  ca-central-1
num_voters = 3,\nvoter_constraints = '[+region=ca-central-1]',\nlease_preferences = '[[+region=ca-central-1]]'      regional_by_row@regional_by_row_pkey   ca-central-1
num_voters = 3,\nvoter_constraints = '[+region=us-east-1]',\nlease_preferences = '[[+region=us-east-1]]'            regional_by_row@regional_by_row_i_idx  us-east-1
num_voters = 3,\nvoter_constraints = '[+region=us-east-1]',\nlease_preferences = '[[+region=us-east-1]]'            regional_by_row@regional_by_row_pkey   us-east-1

query TT
SHOW ZONE CONFIGURATION FROM TABLE created_as_global
----
TABLE created_as_global  ALTER TABLE created_as_global CONFIGURE ZONE USING
                           range_min_bytes = 134217728,
                           range_max_bytes = 536870912,
                           gc.ttlseconds = 14400,
                           global_reads = true,
                           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
ALTER TABLE created_as_global ADD COLUMN crdb_region INT;

statement error cannot use column crdb_region for REGIONAL BY ROW table as it does not have the crdb_internal_region type
ALTER TABLE created_as_global SET LOCALITY REGIONAL BY ROW

statement ok
ALTER TABLE created_as_global DROP COLUMN crdb_region

statement error cannot perform a locality change on created_as_global with other schema changes on created_as_global in the same transaction
BEGIN;
ALTER TABLE created_as_global ADD COLUMN c INT;
ALTER TABLE created_as_global SET LOCALITY REGIONAL BY ROW

# Alter to REGIONAL BY ROW with no crdb_region column defined.
statement ok
ROLLBACK;
ALTER TABLE created_as_global SET LOCALITY REGIONAL BY ROW

query TT
SHOW CREATE TABLE created_as_global
----
created_as_global  CREATE TABLE public.created_as_global (
                     pk INT8 NOT NULL,
                     i INT8 NULL,
                     b INT8 NULL,
                     crdb_region public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::public.crdb_internal_region,
                     CONSTRAINT created_as_global_pkey PRIMARY KEY (pk ASC),
                     INDEX created_as_global_b_idx (b ASC),
                     UNIQUE INDEX created_as_global_i_key (i ASC),
                     FAMILY fam_0_pk_i_b (pk, i, b, crdb_region)
                   ) LOCALITY REGIONAL BY ROW

query TTT
SELECT zone_config, index_name, partition_name FROM [SHOW PARTITIONS FROM TABLE regional_by_row]
ORDER BY partition_name, index_name
----
num_voters = 3,\nvoter_constraints = '[+region=ap-southeast-2]',\nlease_preferences = '[[+region=ap-southeast-2]]'  regional_by_row@regional_by_row_i_idx  ap-southeast-2
num_voters = 3,\nvoter_constraints = '[+region=ap-southeast-2]',\nlease_preferences = '[[+region=ap-southeast-2]]'  regional_by_row@regional_by_row_pkey   ap-southeast-2
num_voters = 3,\nvoter_constraints = '[+region=ca-central-1]',\nlease_preferences = '[[+region=ca-central-1]]'      regional_by_row@regional_by_row_i_idx  ca-central-1
num_voters = 3,\nvoter_constraints = '[+region=ca-central-1]',\nlease_preferences = '[[+region=ca-central-1]]'      regional_by_row@regional_by_row_pkey   ca-central-1
num_voters = 3,\nvoter_constraints = '[+region=us-east-1]',\nlease_preferences = '[[+region=us-east-1]]'            regional_by_row@regional_by_row_i_idx  us-east-1
num_voters = 3,\nvoter_constraints = '[+region=us-east-1]',\nlease_preferences = '[[+region=us-east-1]]'            regional_by_row@regional_by_row_pkey   us-east-1

query TT
SHOW ZONE CONFIGURATION FROM TABLE created_as_global
----
DATABASE alter_locality_test  ALTER DATABASE alter_locality_test 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]]'

query T
INSERT INTO created_as_global (pk) VALUES (1) RETURNING crdb_region
----
ap-southeast-2

# Check the row was backfilled to the primary region, but the row
# just inserted is on the current gateway_region.
query TI
SELECT crdb_region, pk FROM created_as_global ORDER BY pk ASC
----
ca-central-1    0
ap-southeast-2  1

# Drop the table and try again with the crdb_region column defined.
statement ok
DROP TABLE created_as_global;

statement ok
CREATE TABLE created_as_global (
  pk int primary key,
  i int,
  b int,
  index(b),
  unique(i),
  crdb_region int,
  FAMILY (pk, i, b, crdb_region)
) LOCALITY GLOBAL

statement error cannot use column crdb_region for REGIONAL BY ROW table as it does not have the crdb_internal_region type
ALTER TABLE created_as_global SET LOCALITY REGIONAL BY ROW

statement ok
DROP TABLE created_as_global;

statement ok
CREATE TABLE created_as_global (
  pk int primary key,
  i int,
  b int,
  index(b),
  unique(i),
  crdb_region crdb_internal_region,
  FAMILY (pk, i, b, crdb_region)
) LOCALITY GLOBAL;

statement ok
INSERT INTO created_as_global VALUES (0, 1, 2, 'us-east-1')

statement error cannot use column crdb_region for REGIONAL BY ROW table as it may contain NULL values
ALTER TABLE created_as_global SET LOCALITY REGIONAL BY ROW

statement ok
ALTER TABLE created_as_global ALTER COLUMN crdb_region SET NOT NULL;

statement ok
ALTER TABLE created_as_global SET LOCALITY REGIONAL BY ROW

query TT
SHOW CREATE TABLE created_as_global
----
created_as_global  CREATE TABLE public.created_as_global (
                     pk INT8 NOT NULL,
                     i INT8 NULL,
                     b INT8 NULL,
                     crdb_region public.crdb_internal_region NOT NULL,
                     CONSTRAINT created_as_global_pkey PRIMARY KEY (pk ASC),
                     INDEX created_as_global_b_idx (b ASC),
                     UNIQUE INDEX created_as_global_i_key (i ASC),
                     FAMILY fam_0_pk_i_b_crdb_region (pk, i, b, crdb_region)
                   ) LOCALITY REGIONAL BY ROW

query TTT
SELECT zone_config, index_name, partition_name FROM [SHOW PARTITIONS FROM TABLE regional_by_row]
ORDER BY partition_name, index_name
----
num_voters = 3,\nvoter_constraints = '[+region=ap-southeast-2]',\nlease_preferences = '[[+region=ap-southeast-2]]'  regional_by_row@regional_by_row_i_idx  ap-southeast-2
num_voters = 3,\nvoter_constraints = '[+region=ap-southeast-2]',\nlease_preferences = '[[+region=ap-southeast-2]]'  regional_by_row@regional_by_row_pkey   ap-southeast-2
num_voters = 3,\nvoter_constraints = '[+region=ca-central-1]',\nlease_preferences = '[[+region=ca-central-1]]'      regional_by_row@regional_by_row_i_idx  ca-central-1
num_voters = 3,\nvoter_constraints = '[+region=ca-central-1]',\nlease_preferences = '[[+region=ca-central-1]]'      regional_by_row@regional_by_row_pkey   ca-central-1
num_voters = 3,\nvoter_constraints = '[+region=us-east-1]',\nlease_preferences = '[[+region=us-east-1]]'            regional_by_row@regional_by_row_i_idx  us-east-1
num_voters = 3,\nvoter_constraints = '[+region=us-east-1]',\nlease_preferences = '[[+region=us-east-1]]'            regional_by_row@regional_by_row_pkey   us-east-1

query TT
SHOW ZONE CONFIGURATION FROM TABLE created_as_global
----
DATABASE alter_locality_test  ALTER DATABASE alter_locality_test 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]]'

query TT
SHOW CREATE TABLE created_as_global
----
created_as_global  CREATE TABLE public.created_as_global (
                     pk INT8 NOT NULL,
                     i INT8 NULL,
                     b INT8 NULL,
                     crdb_region public.crdb_internal_region NOT NULL,
                     CONSTRAINT created_as_global_pkey PRIMARY KEY (pk ASC),
                     INDEX created_as_global_b_idx (b ASC),
                     UNIQUE INDEX created_as_global_i_key (i ASC),
                     FAMILY fam_0_pk_i_b_crdb_region (pk, i, b, crdb_region)
                   ) LOCALITY REGIONAL BY ROW

# Test altering to REGIONAL BY ROW AS <col>.
statement ok
DROP TABLE created_as_global;

statement ok
CREATE TABLE created_as_global (
  pk int primary key,
  i int,
  b int,
  index(b),
  unique(i),
  cr crdb_internal_region,
  FAMILY (pk, i, b, cr)
) LOCALITY GLOBAL;

statement ok
INSERT INTO created_as_global VALUES (0, 1, 2, 'us-east-1')

statement error column "non_exist" does not exist
ALTER TABLE created_as_global SET LOCALITY REGIONAL BY ROW AS "non_exist"

statement error cannot use column i for REGIONAL BY ROW table as it does not have the crdb_internal_region type
ALTER TABLE created_as_global SET LOCALITY REGIONAL BY ROW AS "i"

statement error cannot use column cr for REGIONAL BY ROW table as it may contain NULL values
ALTER TABLE created_as_global SET LOCALITY REGIONAL BY ROW AS "cr"

statement ok
ALTER TABLE created_as_global ALTER COLUMN cr SET NOT NULL

statement error cannot perform a locality change on created_as_global with other schema changes on created_as_global in the same transaction
BEGIN;
ALTER TABLE created_as_global ADD COLUMN c INT;
ALTER TABLE created_as_global SET LOCALITY REGIONAL BY ROW AS "cr"

statement ok
ROLLBACK;
ALTER TABLE created_as_global SET LOCALITY REGIONAL BY ROW AS "cr"

query TT
SHOW CREATE TABLE created_as_global
----
created_as_global  CREATE TABLE public.created_as_global (
                     pk INT8 NOT NULL,
                     i INT8 NULL,
                     b INT8 NULL,
                     cr public.crdb_internal_region NOT NULL,
                     CONSTRAINT created_as_global_pkey PRIMARY KEY (pk ASC),
                     INDEX created_as_global_b_idx (b ASC),
                     UNIQUE INDEX created_as_global_i_key (i ASC),
                     FAMILY fam_0_pk_i_b_cr (pk, i, b, cr)
                   ) LOCALITY REGIONAL BY ROW AS cr

query TTT
SELECT zone_config, index_name, partition_name FROM [SHOW PARTITIONS FROM TABLE regional_by_row]
ORDER BY partition_name, index_name
----
num_voters = 3,\nvoter_constraints = '[+region=ap-southeast-2]',\nlease_preferences = '[[+region=ap-southeast-2]]'  regional_by_row@regional_by_row_i_idx  ap-southeast-2
num_voters = 3,\nvoter_constraints = '[+region=ap-southeast-2]',\nlease_preferences = '[[+region=ap-southeast-2]]'  regional_by_row@regional_by_row_pkey   ap-southeast-2
num_voters = 3,\nvoter_constraints = '[+region=ca-central-1]',\nlease_preferences = '[[+region=ca-central-1]]'      regional_by_row@regional_by_row_i_idx  ca-central-1
num_voters = 3,\nvoter_constraints = '[+region=ca-central-1]',\nlease_preferences = '[[+region=ca-central-1]]'      regional_by_row@regional_by_row_pkey   ca-central-1
num_voters = 3,\nvoter_constraints = '[+region=us-east-1]',\nlease_preferences = '[[+region=us-east-1]]'            regional_by_row@regional_by_row_i_idx  us-east-1
num_voters = 3,\nvoter_constraints = '[+region=us-east-1]',\nlease_preferences = '[[+region=us-east-1]]'            regional_by_row@regional_by_row_pkey   us-east-1

query TT
SHOW ZONE CONFIGURATION FROM TABLE created_as_global
----
DATABASE alter_locality_test  ALTER DATABASE alter_locality_test 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]]'

# Altering from REGIONAL BY ROW IN PRIMARY REGION

statement error region "invalid-region" has not been added to database "alter_locality_test"
ALTER TABLE regional_by_table_in_primary_region SET LOCALITY REGIONAL BY TABLE in "invalid-region"

statement ok
ALTER TABLE regional_by_table_in_primary_region SET LOCALITY REGIONAL BY TABLE in "ap-southeast-2"

query TT
SHOW CREATE TABLE regional_by_table_in_primary_region
----
regional_by_table_in_primary_region               CREATE TABLE public.regional_by_table_in_primary_region (
                                                  pk INT8 NOT NULL,
                                                  i INT8 NULL,
                                                  CONSTRAINT regional_by_table_in_primary_region_pkey PRIMARY KEY (pk ASC),
                                                  FAMILY fam_0_pk_i (pk, i)
) LOCALITY REGIONAL BY TABLE IN "ap-southeast-2"

query TTT
SELECT zone_config, index_name, partition_name FROM [SHOW PARTITIONS FROM TABLE regional_by_row]
ORDER BY partition_name, index_name
----
num_voters = 3,\nvoter_constraints = '[+region=ap-southeast-2]',\nlease_preferences = '[[+region=ap-southeast-2]]'  regional_by_row@regional_by_row_i_idx  ap-southeast-2
num_voters = 3,\nvoter_constraints = '[+region=ap-southeast-2]',\nlease_preferences = '[[+region=ap-southeast-2]]'  regional_by_row@regional_by_row_pkey   ap-southeast-2
num_voters = 3,\nvoter_constraints = '[+region=ca-central-1]',\nlease_preferences = '[[+region=ca-central-1]]'      regional_by_row@regional_by_row_i_idx  ca-central-1
num_voters = 3,\nvoter_constraints = '[+region=ca-central-1]',\nlease_preferences = '[[+region=ca-central-1]]'      regional_by_row@regional_by_row_pkey   ca-central-1
num_voters = 3,\nvoter_constraints = '[+region=us-east-1]',\nlease_preferences = '[[+region=us-east-1]]'            regional_by_row@regional_by_row_i_idx  us-east-1
num_voters = 3,\nvoter_constraints = '[+region=us-east-1]',\nlease_preferences = '[[+region=us-east-1]]'            regional_by_row@regional_by_row_pkey   us-east-1

query TT
SHOW ZONE CONFIGURATION FROM TABLE regional_by_table_in_primary_region
----
TABLE regional_by_table_in_primary_region  ALTER TABLE regional_by_table_in_primary_region 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=ap-southeast-2]',
                                             lease_preferences = '[[+region=ap-southeast-2]]'

# Alter back to original state
statement ok
ALTER TABLE regional_by_table_in_primary_region SET LOCALITY REGIONAL BY TABLE IN PRIMARY REGION

query TT
SHOW CREATE TABLE regional_by_table_in_primary_region
----
regional_by_table_in_primary_region             CREATE TABLE public.regional_by_table_in_primary_region (
                                                pk INT8 NOT NULL,
                                                i INT8 NULL,
                                                CONSTRAINT regional_by_table_in_primary_region_pkey PRIMARY KEY (pk ASC),
                                                FAMILY fam_0_pk_i (pk, i)
) LOCALITY REGIONAL BY TABLE IN PRIMARY REGION

query TTT
SELECT zone_config, index_name, partition_name FROM [SHOW PARTITIONS FROM TABLE regional_by_row]
ORDER BY partition_name, index_name
----
num_voters = 3,\nvoter_constraints = '[+region=ap-southeast-2]',\nlease_preferences = '[[+region=ap-southeast-2]]'  regional_by_row@regional_by_row_i_idx  ap-southeast-2
num_voters = 3,\nvoter_constraints = '[+region=ap-southeast-2]',\nlease_preferences = '[[+region=ap-southeast-2]]'  regional_by_row@regional_by_row_pkey   ap-southeast-2
num_voters = 3,\nvoter_constraints = '[+region=ca-central-1]',\nlease_preferences = '[[+region=ca-central-1]]'      regional_by_row@regional_by_row_i_idx  ca-central-1
num_voters = 3,\nvoter_constraints = '[+region=ca-central-1]',\nlease_preferences = '[[+region=ca-central-1]]'      regional_by_row@regional_by_row_pkey   ca-central-1
num_voters = 3,\nvoter_constraints = '[+region=us-east-1]',\nlease_preferences = '[[+region=us-east-1]]'            regional_by_row@regional_by_row_i_idx  us-east-1
num_voters = 3,\nvoter_constraints = '[+region=us-east-1]',\nlease_preferences = '[[+region=us-east-1]]'            regional_by_row@regional_by_row_pkey   us-east-1

query TT
SHOW ZONE CONFIGURATION FROM TABLE regional_by_table_in_primary_region
----
DATABASE alter_locality_test  ALTER DATABASE alter_locality_test 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]]'

# Alter to same state
statement ok
ALTER TABLE regional_by_table_in_primary_region SET LOCALITY REGIONAL BY TABLE IN PRIMARY REGION

query TT
SHOW CREATE TABLE regional_by_table_in_primary_region
----
regional_by_table_in_primary_region             CREATE TABLE public.regional_by_table_in_primary_region (
                                                pk INT8 NOT NULL,
                                                i INT8 NULL,
                                                CONSTRAINT regional_by_table_in_primary_region_pkey PRIMARY KEY (pk ASC),
                                                FAMILY fam_0_pk_i (pk, i)
) LOCALITY REGIONAL BY TABLE IN PRIMARY REGION

query TTT
SELECT zone_config, index_name, partition_name FROM [SHOW PARTITIONS FROM TABLE regional_by_row]
ORDER BY partition_name, index_name
----
num_voters = 3,\nvoter_constraints = '[+region=ap-southeast-2]',\nlease_preferences = '[[+region=ap-southeast-2]]'  regional_by_row@regional_by_row_i_idx  ap-southeast-2
num_voters = 3,\nvoter_constraints = '[+region=ap-southeast-2]',\nlease_preferences = '[[+region=ap-southeast-2]]'  regional_by_row@regional_by_row_pkey   ap-southeast-2
num_voters = 3,\nvoter_constraints = '[+region=ca-central-1]',\nlease_preferences = '[[+region=ca-central-1]]'      regional_by_row@regional_by_row_i_idx  ca-central-1
num_voters = 3,\nvoter_constraints = '[+region=ca-central-1]',\nlease_preferences = '[[+region=ca-central-1]]'      regional_by_row@regional_by_row_pkey   ca-central-1
num_voters = 3,\nvoter_constraints = '[+region=us-east-1]',\nlease_preferences = '[[+region=us-east-1]]'            regional_by_row@regional_by_row_i_idx  us-east-1
num_voters = 3,\nvoter_constraints = '[+region=us-east-1]',\nlease_preferences = '[[+region=us-east-1]]'            regional_by_row@regional_by_row_pkey   us-east-1

query TT
SHOW ZONE CONFIGURATION FROM TABLE regional_by_table_in_primary_region
----
DATABASE alter_locality_test  ALTER DATABASE alter_locality_test 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
ALTER TABLE regional_by_table_in_primary_region SET LOCALITY REGIONAL BY TABLE

query TT
SHOW CREATE TABLE regional_by_table_in_primary_region
----
regional_by_table_in_primary_region             CREATE TABLE public.regional_by_table_in_primary_region (
                                                pk INT8 NOT NULL,
                                                i INT8 NULL,
                                                CONSTRAINT regional_by_table_in_primary_region_pkey PRIMARY KEY (pk ASC),
                                                FAMILY fam_0_pk_i (pk, i)
) LOCALITY REGIONAL BY TABLE IN PRIMARY REGION

query TTT
SELECT zone_config, index_name, partition_name FROM [SHOW PARTITIONS FROM TABLE regional_by_row]
ORDER BY partition_name, index_name
----
num_voters = 3,\nvoter_constraints = '[+region=ap-southeast-2]',\nlease_preferences = '[[+region=ap-southeast-2]]'  regional_by_row@regional_by_row_i_idx  ap-southeast-2
num_voters = 3,\nvoter_constraints = '[+region=ap-southeast-2]',\nlease_preferences = '[[+region=ap-southeast-2]]'  regional_by_row@regional_by_row_pkey   ap-southeast-2
num_voters = 3,\nvoter_constraints = '[+region=ca-central-1]',\nlease_preferences = '[[+region=ca-central-1]]'      regional_by_row@regional_by_row_i_idx  ca-central-1
num_voters = 3,\nvoter_constraints = '[+region=ca-central-1]',\nlease_preferences = '[[+region=ca-central-1]]'      regional_by_row@regional_by_row_pkey   ca-central-1
num_voters = 3,\nvoter_constraints = '[+region=us-east-1]',\nlease_preferences = '[[+region=us-east-1]]'            regional_by_row@regional_by_row_i_idx  us-east-1
num_voters = 3,\nvoter_constraints = '[+region=us-east-1]',\nlease_preferences = '[[+region=us-east-1]]'            regional_by_row@regional_by_row_pkey   us-east-1

query TT
SHOW ZONE CONFIGURATION FROM TABLE regional_by_table_in_primary_region
----
DATABASE alter_locality_test  ALTER DATABASE alter_locality_test 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
ALTER TABLE regional_by_table_in_primary_region SET LOCALITY GLOBAL

query TT
SHOW CREATE TABLE regional_by_table_in_primary_region
----
regional_by_table_in_primary_region  CREATE TABLE public.regional_by_table_in_primary_region (
                                     pk INT8 NOT NULL,
                                     i INT8 NULL,
                                     CONSTRAINT regional_by_table_in_primary_region_pkey PRIMARY KEY (pk ASC),
                                     FAMILY fam_0_pk_i (pk, i)
) LOCALITY GLOBAL

query TTT
SELECT zone_config, index_name, partition_name FROM [SHOW PARTITIONS FROM TABLE regional_by_row]
ORDER BY partition_name, index_name
----
num_voters = 3,\nvoter_constraints = '[+region=ap-southeast-2]',\nlease_preferences = '[[+region=ap-southeast-2]]'  regional_by_row@regional_by_row_i_idx  ap-southeast-2
num_voters = 3,\nvoter_constraints = '[+region=ap-southeast-2]',\nlease_preferences = '[[+region=ap-southeast-2]]'  regional_by_row@regional_by_row_pkey   ap-southeast-2
num_voters = 3,\nvoter_constraints = '[+region=ca-central-1]',\nlease_preferences = '[[+region=ca-central-1]]'      regional_by_row@regional_by_row_i_idx  ca-central-1
num_voters = 3,\nvoter_constraints = '[+region=ca-central-1]',\nlease_preferences = '[[+region=ca-central-1]]'      regional_by_row@regional_by_row_pkey   ca-central-1
num_voters = 3,\nvoter_constraints = '[+region=us-east-1]',\nlease_preferences = '[[+region=us-east-1]]'            regional_by_row@regional_by_row_i_idx  us-east-1
num_voters = 3,\nvoter_constraints = '[+region=us-east-1]',\nlease_preferences = '[[+region=us-east-1]]'            regional_by_row@regional_by_row_pkey   us-east-1

query TT
SHOW ZONE CONFIGURATION FROM TABLE regional_by_table_in_primary_region
----
TABLE regional_by_table_in_primary_region  ALTER TABLE regional_by_table_in_primary_region CONFIGURE ZONE USING
                                             range_min_bytes = 134217728,
                                             range_max_bytes = 536870912,
                                             gc.ttlseconds = 14400,
                                             global_reads = true,
                                             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]]'

# Drop the table and recreate it to get back to original state (this is required because alter table
# from global to regional by table is not yet implemented).
statement ok
DROP TABLE regional_by_table_in_primary_region

statement ok
CREATE TABLE regional_by_table_in_primary_region (
  pk INT PRIMARY KEY,
  i int,
  FAMILY (pk, i)
) LOCALITY REGIONAL BY TABLE IN PRIMARY REGION

# Altering from REGIONAL BY ROW

statement ok
ALTER TABLE regional_by_table_no_region SET LOCALITY REGIONAL BY TABLE

query TT
SHOW CREATE TABLE regional_by_table_no_region
----
regional_by_table_no_region                     CREATE TABLE public.regional_by_table_no_region (
                                                pk INT8 NOT NULL,
                                                i INT8 NULL,
                                                CONSTRAINT regional_by_table_no_region_pkey PRIMARY KEY (pk ASC),
                                                FAMILY fam_0_pk_i (pk, i)
) LOCALITY REGIONAL BY TABLE IN PRIMARY REGION

query TTT
SELECT zone_config, index_name, partition_name FROM [SHOW PARTITIONS FROM TABLE regional_by_row]
ORDER BY partition_name, index_name
----
num_voters = 3,\nvoter_constraints = '[+region=ap-southeast-2]',\nlease_preferences = '[[+region=ap-southeast-2]]'  regional_by_row@regional_by_row_i_idx  ap-southeast-2
num_voters = 3,\nvoter_constraints = '[+region=ap-southeast-2]',\nlease_preferences = '[[+region=ap-southeast-2]]'  regional_by_row@regional_by_row_pkey   ap-southeast-2
num_voters = 3,\nvoter_constraints = '[+region=ca-central-1]',\nlease_preferences = '[[+region=ca-central-1]]'      regional_by_row@regional_by_row_i_idx  ca-central-1
num_voters = 3,\nvoter_constraints = '[+region=ca-central-1]',\nlease_preferences = '[[+region=ca-central-1]]'      regional_by_row@regional_by_row_pkey   ca-central-1
num_voters = 3,\nvoter_constraints = '[+region=us-east-1]',\nlease_preferences = '[[+region=us-east-1]]'            regional_by_row@regional_by_row_i_idx  us-east-1
num_voters = 3,\nvoter_constraints = '[+region=us-east-1]',\nlease_preferences = '[[+region=us-east-1]]'            regional_by_row@regional_by_row_pkey   us-east-1

query TT
SHOW ZONE CONFIGURATION FROM TABLE regional_by_table_no_region
----
DATABASE alter_locality_test  ALTER DATABASE alter_locality_test 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]]'

# Drop and recreate the table to get it back to the "no region" state.
statement ok
DROP TABLE regional_by_table_no_region

statement ok
CREATE TABLE regional_by_table_no_region (
  pk INT PRIMARY KEY,
  i INT,
  FAMILY (pk, i)
)

statement ok
ALTER TABLE regional_by_table_no_region SET LOCALITY REGIONAL BY TABLE in "ap-southeast-2"

query TT
SHOW CREATE TABLE regional_by_table_no_region
----
regional_by_table_no_region                       CREATE TABLE public.regional_by_table_no_region (
                                                  pk INT8 NOT NULL,
                                                  i INT8 NULL,
                                                  CONSTRAINT regional_by_table_no_region_pkey PRIMARY KEY (pk ASC),
                                                  FAMILY fam_0_pk_i (pk, i)
) LOCALITY REGIONAL BY TABLE IN "ap-southeast-2"

query TTT
SELECT zone_config, index_name, partition_name FROM [SHOW PARTITIONS FROM TABLE regional_by_row]
ORDER BY partition_name, index_name
----
num_voters = 3,\nvoter_constraints = '[+region=ap-southeast-2]',\nlease_preferences = '[[+region=ap-southeast-2]]'  regional_by_row@regional_by_row_i_idx  ap-southeast-2
num_voters = 3,\nvoter_constraints = '[+region=ap-southeast-2]',\nlease_preferences = '[[+region=ap-southeast-2]]'  regional_by_row@regional_by_row_pkey   ap-southeast-2
num_voters = 3,\nvoter_constraints = '[+region=ca-central-1]',\nlease_preferences = '[[+region=ca-central-1]]'      regional_by_row@regional_by_row_i_idx  ca-central-1
num_voters = 3,\nvoter_constraints = '[+region=ca-central-1]',\nlease_preferences = '[[+region=ca-central-1]]'      regional_by_row@regional_by_row_pkey   ca-central-1
num_voters = 3,\nvoter_constraints = '[+region=us-east-1]',\nlease_preferences = '[[+region=us-east-1]]'            regional_by_row@regional_by_row_i_idx  us-east-1
num_voters = 3,\nvoter_constraints = '[+region=us-east-1]',\nlease_preferences = '[[+region=us-east-1]]'            regional_by_row@regional_by_row_pkey   us-east-1

query TT
SHOW ZONE CONFIGURATION FROM TABLE regional_by_table_no_region
----
TABLE regional_by_table_no_region  ALTER TABLE regional_by_table_no_region 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=ap-southeast-2]',
                                     lease_preferences = '[[+region=ap-southeast-2]]'

# Drop and recreate the table to get it back to the "no region" state.
statement ok
DROP TABLE regional_by_table_no_region

statement ok
CREATE TABLE regional_by_table_no_region (i int)

statement ok
ALTER TABLE regional_by_table_no_region SET LOCALITY REGIONAL BY TABLE IN PRIMARY REGION

query TT
SHOW CREATE TABLE regional_by_table_no_region
----
regional_by_table_no_region                     CREATE TABLE public.regional_by_table_no_region (
                                                i INT8 NULL,
                                                rowid INT8 NOT VISIBLE NOT NULL DEFAULT unique_rowid(),
                                                CONSTRAINT regional_by_table_no_region_pkey PRIMARY KEY (rowid ASC)
) LOCALITY REGIONAL BY TABLE IN PRIMARY REGION

query TTT
SELECT zone_config, index_name, partition_name FROM [SHOW PARTITIONS FROM TABLE regional_by_row]
ORDER BY partition_name, index_name
----
num_voters = 3,\nvoter_constraints = '[+region=ap-southeast-2]',\nlease_preferences = '[[+region=ap-southeast-2]]'  regional_by_row@regional_by_row_i_idx  ap-southeast-2
num_voters = 3,\nvoter_constraints = '[+region=ap-southeast-2]',\nlease_preferences = '[[+region=ap-southeast-2]]'  regional_by_row@regional_by_row_pkey   ap-southeast-2
num_voters = 3,\nvoter_constraints = '[+region=ca-central-1]',\nlease_preferences = '[[+region=ca-central-1]]'      regional_by_row@regional_by_row_i_idx  ca-central-1
num_voters = 3,\nvoter_constraints = '[+region=ca-central-1]',\nlease_preferences = '[[+region=ca-central-1]]'      regional_by_row@regional_by_row_pkey   ca-central-1
num_voters = 3,\nvoter_constraints = '[+region=us-east-1]',\nlease_preferences = '[[+region=us-east-1]]'            regional_by_row@regional_by_row_i_idx  us-east-1
num_voters = 3,\nvoter_constraints = '[+region=us-east-1]',\nlease_preferences = '[[+region=us-east-1]]'            regional_by_row@regional_by_row_pkey   us-east-1

query TT
SHOW ZONE CONFIGURATION FROM TABLE regional_by_table_no_region
----
DATABASE alter_locality_test  ALTER DATABASE alter_locality_test 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
ALTER TABLE regional_by_table_no_region SET LOCALITY GLOBAL

query TT
SHOW CREATE TABLE regional_by_table_no_region
----
regional_by_table_no_region  CREATE TABLE public.regional_by_table_no_region (
                             i INT8 NULL,
                             rowid INT8 NOT VISIBLE NOT NULL DEFAULT unique_rowid(),
                             CONSTRAINT regional_by_table_no_region_pkey PRIMARY KEY (rowid ASC)
) LOCALITY GLOBAL

query TTT
SELECT zone_config, index_name, partition_name FROM [SHOW PARTITIONS FROM TABLE regional_by_row]
ORDER BY partition_name, index_name
----
num_voters = 3,\nvoter_constraints = '[+region=ap-southeast-2]',\nlease_preferences = '[[+region=ap-southeast-2]]'  regional_by_row@regional_by_row_i_idx  ap-southeast-2
num_voters = 3,\nvoter_constraints = '[+region=ap-southeast-2]',\nlease_preferences = '[[+region=ap-southeast-2]]'  regional_by_row@regional_by_row_pkey   ap-southeast-2
num_voters = 3,\nvoter_constraints = '[+region=ca-central-1]',\nlease_preferences = '[[+region=ca-central-1]]'      regional_by_row@regional_by_row_i_idx  ca-central-1
num_voters = 3,\nvoter_constraints = '[+region=ca-central-1]',\nlease_preferences = '[[+region=ca-central-1]]'      regional_by_row@regional_by_row_pkey   ca-central-1
num_voters = 3,\nvoter_constraints = '[+region=us-east-1]',\nlease_preferences = '[[+region=us-east-1]]'            regional_by_row@regional_by_row_i_idx  us-east-1
num_voters = 3,\nvoter_constraints = '[+region=us-east-1]',\nlease_preferences = '[[+region=us-east-1]]'            regional_by_row@regional_by_row_pkey   us-east-1

query TT
SHOW ZONE CONFIGURATION FROM TABLE regional_by_table_no_region
----
TABLE regional_by_table_no_region  ALTER TABLE regional_by_table_no_region CONFIGURE ZONE USING
                                     range_min_bytes = 134217728,
                                     range_max_bytes = 536870912,
                                     gc.ttlseconds = 14400,
                                     global_reads = true,
                                     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]]'

# Drop the table and recreate it with columns appropriate for
# REGIONAL BY ROW transformations.
statement ok
DROP TABLE regional_by_table_no_region;

statement ok
CREATE TABLE regional_by_table_no_region (
  pk int primary key,
  i int,
  b int,
  index(b),
  unique(i),
  FAMILY (pk, i, b)
) LOCALITY REGIONAL BY TABLE;

statement ok
INSERT INTO regional_by_table_no_region VALUES (0, 1, 2)

statement ok
ALTER TABLE regional_by_table_no_region SET LOCALITY REGIONAL BY ROW

query TT
SHOW CREATE TABLE regional_by_table_no_region
----
regional_by_table_no_region  CREATE TABLE public.regional_by_table_no_region (
                               pk INT8 NOT NULL,
                               i INT8 NULL,
                               b INT8 NULL,
                               crdb_region public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::public.crdb_internal_region,
                               CONSTRAINT regional_by_table_no_region_pkey PRIMARY KEY (pk ASC),
                               INDEX regional_by_table_no_region_b_idx (b ASC),
                               UNIQUE INDEX regional_by_table_no_region_i_key (i ASC),
                               FAMILY fam_0_pk_i_b (pk, i, b, crdb_region)
                             ) LOCALITY REGIONAL BY ROW

statement ok
DROP TABLE regional_by_table_no_region;

statement ok
CREATE TABLE regional_by_table_no_region (
  pk int primary key,
  i int,
  b int,
  index(b),
  unique(i),
  cr crdb_internal_region NOT NULL NOT NULL,
  FAMILY (pk, i, b, cr)
) LOCALITY REGIONAL BY TABLE;

statement ok
INSERT INTO regional_by_table_no_region VALUES (0, 1, 2, 'us-east-1')

query TTT
SELECT zone_config, index_name, partition_name FROM [SHOW PARTITIONS FROM TABLE regional_by_row]
ORDER BY partition_name, index_name
----
num_voters = 3,\nvoter_constraints = '[+region=ap-southeast-2]',\nlease_preferences = '[[+region=ap-southeast-2]]'  regional_by_row@regional_by_row_i_idx  ap-southeast-2
num_voters = 3,\nvoter_constraints = '[+region=ap-southeast-2]',\nlease_preferences = '[[+region=ap-southeast-2]]'  regional_by_row@regional_by_row_pkey   ap-southeast-2
num_voters = 3,\nvoter_constraints = '[+region=ca-central-1]',\nlease_preferences = '[[+region=ca-central-1]]'      regional_by_row@regional_by_row_i_idx  ca-central-1
num_voters = 3,\nvoter_constraints = '[+region=ca-central-1]',\nlease_preferences = '[[+region=ca-central-1]]'      regional_by_row@regional_by_row_pkey   ca-central-1
num_voters = 3,\nvoter_constraints = '[+region=us-east-1]',\nlease_preferences = '[[+region=us-east-1]]'            regional_by_row@regional_by_row_i_idx  us-east-1
num_voters = 3,\nvoter_constraints = '[+region=us-east-1]',\nlease_preferences = '[[+region=us-east-1]]'            regional_by_row@regional_by_row_pkey   us-east-1

query TT
SHOW ZONE CONFIGURATION FROM TABLE regional_by_table_no_region
----
DATABASE alter_locality_test  ALTER DATABASE alter_locality_test 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]]'

# Drop the table and recreate it with columns appropriate for
# REGIONAL BY ROW AS transformations.
statement ok
DROP TABLE regional_by_table_no_region;

statement ok
CREATE TABLE regional_by_table_no_region (
  pk int primary key,
  i int,
  b int,
  cr crdb_internal_region NOT NULL NOT NULL,
  index(b),
  unique(i),
  FAMILY (pk, i, b, cr)
) LOCALITY REGIONAL BY TABLE;

statement ok
INSERT INTO regional_by_table_no_region VALUES (0, 1, 2, 'us-east-1')

statement ok
ALTER TABLE regional_by_table_no_region SET LOCALITY REGIONAL BY ROW AS "cr"

query TT
SHOW CREATE TABLE regional_by_table_no_region
----
regional_by_table_no_region  CREATE TABLE public.regional_by_table_no_region (
                               pk INT8 NOT NULL,
                               i INT8 NULL,
                               b INT8 NULL,
                               cr public.crdb_internal_region NOT NULL,
                               CONSTRAINT regional_by_table_no_region_pkey PRIMARY KEY (pk ASC),
                               INDEX regional_by_table_no_region_b_idx (b ASC),
                               UNIQUE INDEX regional_by_table_no_region_i_key (i ASC),
                               FAMILY fam_0_pk_i_b_cr (pk, i, b, cr)
                             ) LOCALITY REGIONAL BY ROW AS cr

query TTT
SELECT zone_config, index_name, partition_name FROM [SHOW PARTITIONS FROM TABLE regional_by_row]
ORDER BY partition_name, index_name
----
num_voters = 3,\nvoter_constraints = '[+region=ap-southeast-2]',\nlease_preferences = '[[+region=ap-southeast-2]]'  regional_by_row@regional_by_row_i_idx  ap-southeast-2
num_voters = 3,\nvoter_constraints = '[+region=ap-southeast-2]',\nlease_preferences = '[[+region=ap-southeast-2]]'  regional_by_row@regional_by_row_pkey   ap-southeast-2
num_voters = 3,\nvoter_constraints = '[+region=ca-central-1]',\nlease_preferences = '[[+region=ca-central-1]]'      regional_by_row@regional_by_row_i_idx  ca-central-1
num_voters = 3,\nvoter_constraints = '[+region=ca-central-1]',\nlease_preferences = '[[+region=ca-central-1]]'      regional_by_row@regional_by_row_pkey   ca-central-1
num_voters = 3,\nvoter_constraints = '[+region=us-east-1]',\nlease_preferences = '[[+region=us-east-1]]'            regional_by_row@regional_by_row_i_idx  us-east-1
num_voters = 3,\nvoter_constraints = '[+region=us-east-1]',\nlease_preferences = '[[+region=us-east-1]]'            regional_by_row@regional_by_row_pkey   us-east-1

query TT
SHOW ZONE CONFIGURATION FROM TABLE regional_by_table_no_region
----
DATABASE alter_locality_test  ALTER DATABASE alter_locality_test 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]]'

# Altering from REGIONAL BY ROW IN "us-east-1"

statement ok
ALTER TABLE regional_by_table_in_us_east SET LOCALITY REGIONAL BY TABLE

query TT
SHOW CREATE TABLE regional_by_table_in_us_east
----
regional_by_table_in_us_east                    CREATE TABLE public.regional_by_table_in_us_east (
                                                pk INT8 NOT NULL,
                                                i INT8 NULL,
                                                CONSTRAINT regional_by_table_in_us_east_pkey PRIMARY KEY (pk ASC),
                                                FAMILY fam_0_pk_i (pk, i)
) LOCALITY REGIONAL BY TABLE IN PRIMARY REGION

query TTT
SELECT zone_config, index_name, partition_name FROM [SHOW PARTITIONS FROM TABLE regional_by_row]
ORDER BY partition_name, index_name
----
num_voters = 3,\nvoter_constraints = '[+region=ap-southeast-2]',\nlease_preferences = '[[+region=ap-southeast-2]]'  regional_by_row@regional_by_row_i_idx  ap-southeast-2
num_voters = 3,\nvoter_constraints = '[+region=ap-southeast-2]',\nlease_preferences = '[[+region=ap-southeast-2]]'  regional_by_row@regional_by_row_pkey   ap-southeast-2
num_voters = 3,\nvoter_constraints = '[+region=ca-central-1]',\nlease_preferences = '[[+region=ca-central-1]]'      regional_by_row@regional_by_row_i_idx  ca-central-1
num_voters = 3,\nvoter_constraints = '[+region=ca-central-1]',\nlease_preferences = '[[+region=ca-central-1]]'      regional_by_row@regional_by_row_pkey   ca-central-1
num_voters = 3,\nvoter_constraints = '[+region=us-east-1]',\nlease_preferences = '[[+region=us-east-1]]'            regional_by_row@regional_by_row_i_idx  us-east-1
num_voters = 3,\nvoter_constraints = '[+region=us-east-1]',\nlease_preferences = '[[+region=us-east-1]]'            regional_by_row@regional_by_row_pkey   us-east-1

query TT
SHOW ZONE CONFIGURATION FROM TABLE regional_by_table_in_us_east
----
DATABASE alter_locality_test  ALTER DATABASE alter_locality_test 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
ALTER TABLE regional_by_table_in_us_east SET LOCALITY REGIONAL BY TABLE in "us-east-1"

statement ok
ALTER TABLE regional_by_table_in_us_east SET LOCALITY REGIONAL BY TABLE in "ap-southeast-2"

query TT
SHOW CREATE TABLE regional_by_table_in_us_east
----
regional_by_table_in_us_east                      CREATE TABLE public.regional_by_table_in_us_east (
                                                  pk INT8 NOT NULL,
                                                  i INT8 NULL,
                                                  CONSTRAINT regional_by_table_in_us_east_pkey PRIMARY KEY (pk ASC),
                                                  FAMILY fam_0_pk_i (pk, i)
) LOCALITY REGIONAL BY TABLE IN "ap-southeast-2"

query TTT
SELECT zone_config, index_name, partition_name FROM [SHOW PARTITIONS FROM TABLE regional_by_row]
ORDER BY partition_name, index_name
----
num_voters = 3,\nvoter_constraints = '[+region=ap-southeast-2]',\nlease_preferences = '[[+region=ap-southeast-2]]'  regional_by_row@regional_by_row_i_idx  ap-southeast-2
num_voters = 3,\nvoter_constraints = '[+region=ap-southeast-2]',\nlease_preferences = '[[+region=ap-southeast-2]]'  regional_by_row@regional_by_row_pkey   ap-southeast-2
num_voters = 3,\nvoter_constraints = '[+region=ca-central-1]',\nlease_preferences = '[[+region=ca-central-1]]'      regional_by_row@regional_by_row_i_idx  ca-central-1
num_voters = 3,\nvoter_constraints = '[+region=ca-central-1]',\nlease_preferences = '[[+region=ca-central-1]]'      regional_by_row@regional_by_row_pkey   ca-central-1
num_voters = 3,\nvoter_constraints = '[+region=us-east-1]',\nlease_preferences = '[[+region=us-east-1]]'            regional_by_row@regional_by_row_i_idx  us-east-1
num_voters = 3,\nvoter_constraints = '[+region=us-east-1]',\nlease_preferences = '[[+region=us-east-1]]'            regional_by_row@regional_by_row_pkey   us-east-1

query TT
SHOW ZONE CONFIGURATION FROM TABLE regional_by_table_in_us_east
----
TABLE regional_by_table_in_us_east  ALTER TABLE regional_by_table_in_us_east 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=ap-southeast-2]',
                                      lease_preferences = '[[+region=ap-southeast-2]]'

statement ok
ALTER TABLE regional_by_table_in_us_east SET LOCALITY REGIONAL BY TABLE in "us-east-1"

statement ok
ALTER TABLE regional_by_table_in_us_east SET LOCALITY REGIONAL BY TABLE IN PRIMARY REGION

query TT
SHOW CREATE TABLE regional_by_table_in_us_east
----
regional_by_table_in_us_east                    CREATE TABLE public.regional_by_table_in_us_east (
                                                pk INT8 NOT NULL,
                                                i INT8 NULL,
                                                CONSTRAINT regional_by_table_in_us_east_pkey PRIMARY KEY (pk ASC),
                                                FAMILY fam_0_pk_i (pk, i)
) LOCALITY REGIONAL BY TABLE IN PRIMARY REGION

query TTT
SELECT zone_config, index_name, partition_name FROM [SHOW PARTITIONS FROM TABLE regional_by_row]
ORDER BY partition_name, index_name
----
num_voters = 3,\nvoter_constraints = '[+region=ap-southeast-2]',\nlease_preferences = '[[+region=ap-southeast-2]]'  regional_by_row@regional_by_row_i_idx  ap-southeast-2
num_voters = 3,\nvoter_constraints = '[+region=ap-southeast-2]',\nlease_preferences = '[[+region=ap-southeast-2]]'  regional_by_row@regional_by_row_pkey   ap-southeast-2
num_voters = 3,\nvoter_constraints = '[+region=ca-central-1]',\nlease_preferences = '[[+region=ca-central-1]]'      regional_by_row@regional_by_row_i_idx  ca-central-1
num_voters = 3,\nvoter_constraints = '[+region=ca-central-1]',\nlease_preferences = '[[+region=ca-central-1]]'      regional_by_row@regional_by_row_pkey   ca-central-1
num_voters = 3,\nvoter_constraints = '[+region=us-east-1]',\nlease_preferences = '[[+region=us-east-1]]'            regional_by_row@regional_by_row_i_idx  us-east-1
num_voters = 3,\nvoter_constraints = '[+region=us-east-1]',\nlease_preferences = '[[+region=us-east-1]]'            regional_by_row@regional_by_row_pkey   us-east-1

query TT
SHOW ZONE CONFIGURATION FROM TABLE regional_by_table_in_us_east
----
DATABASE alter_locality_test  ALTER DATABASE alter_locality_test 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
ALTER TABLE regional_by_table_in_us_east SET LOCALITY GLOBAL

query TT
SHOW CREATE TABLE regional_by_table_in_us_east
----
regional_by_table_in_us_east  CREATE TABLE public.regional_by_table_in_us_east (
                              pk INT8 NOT NULL,
                              i INT8 NULL,
                              CONSTRAINT regional_by_table_in_us_east_pkey PRIMARY KEY (pk ASC),
                              FAMILY fam_0_pk_i (pk, i)
) LOCALITY GLOBAL

query TTT
SELECT zone_config, index_name, partition_name FROM [SHOW PARTITIONS FROM TABLE regional_by_row]
ORDER BY partition_name, index_name
----
num_voters = 3,\nvoter_constraints = '[+region=ap-southeast-2]',\nlease_preferences = '[[+region=ap-southeast-2]]'  regional_by_row@regional_by_row_i_idx  ap-southeast-2
num_voters = 3,\nvoter_constraints = '[+region=ap-southeast-2]',\nlease_preferences = '[[+region=ap-southeast-2]]'  regional_by_row@regional_by_row_pkey   ap-southeast-2
num_voters = 3,\nvoter_constraints = '[+region=ca-central-1]',\nlease_preferences = '[[+region=ca-central-1]]'      regional_by_row@regional_by_row_i_idx  ca-central-1
num_voters = 3,\nvoter_constraints = '[+region=ca-central-1]',\nlease_preferences = '[[+region=ca-central-1]]'      regional_by_row@regional_by_row_pkey   ca-central-1
num_voters = 3,\nvoter_constraints = '[+region=us-east-1]',\nlease_preferences = '[[+region=us-east-1]]'            regional_by_row@regional_by_row_i_idx  us-east-1
num_voters = 3,\nvoter_constraints = '[+region=us-east-1]',\nlease_preferences = '[[+region=us-east-1]]'            regional_by_row@regional_by_row_pkey   us-east-1

query TT
SHOW ZONE CONFIGURATION FROM TABLE regional_by_table_in_us_east
----
TABLE regional_by_table_in_us_east  ALTER TABLE regional_by_table_in_us_east CONFIGURE ZONE USING
                                      range_min_bytes = 134217728,
                                      range_max_bytes = 536870912,
                                      gc.ttlseconds = 14400,
                                      global_reads = true,
                                      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]]'

# Drop the table and recreate it with columns appropriate for
# REGIONAL BY ROW transformations.
statement ok
DROP TABLE regional_by_table_in_us_east;

statement ok
CREATE TABLE regional_by_table_in_us_east (
  pk int PRIMARY KEY,
  i int,
  b int,
  index(b),
  unique(i),
  FAMILY (pk, i, b)
) LOCALITY REGIONAL BY TABLE IN "us-east-1";

statement ok
INSERT INTO regional_by_table_in_us_east VALUES (0, 1, 2);

statement ok
ALTER TABLE regional_by_table_in_us_east SET LOCALITY REGIONAL BY ROW

query TT
SHOW CREATE TABLE regional_by_table_in_us_east
----
regional_by_table_in_us_east  CREATE TABLE public.regional_by_table_in_us_east (
                                pk INT8 NOT NULL,
                                i INT8 NULL,
                                b INT8 NULL,
                                crdb_region public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::public.crdb_internal_region,
                                CONSTRAINT regional_by_table_in_us_east_pkey PRIMARY KEY (pk ASC),
                                INDEX regional_by_table_in_us_east_b_idx (b ASC),
                                UNIQUE INDEX regional_by_table_in_us_east_i_key (i ASC),
                                FAMILY fam_0_pk_i_b (pk, i, b, crdb_region)
                              ) LOCALITY REGIONAL BY ROW

query TTT
SELECT zone_config, index_name, partition_name FROM [SHOW PARTITIONS FROM TABLE regional_by_row]
ORDER BY partition_name, index_name
----
num_voters = 3,\nvoter_constraints = '[+region=ap-southeast-2]',\nlease_preferences = '[[+region=ap-southeast-2]]'  regional_by_row@regional_by_row_i_idx  ap-southeast-2
num_voters = 3,\nvoter_constraints = '[+region=ap-southeast-2]',\nlease_preferences = '[[+region=ap-southeast-2]]'  regional_by_row@regional_by_row_pkey   ap-southeast-2
num_voters = 3,\nvoter_constraints = '[+region=ca-central-1]',\nlease_preferences = '[[+region=ca-central-1]]'      regional_by_row@regional_by_row_i_idx  ca-central-1
num_voters = 3,\nvoter_constraints = '[+region=ca-central-1]',\nlease_preferences = '[[+region=ca-central-1]]'      regional_by_row@regional_by_row_pkey   ca-central-1
num_voters = 3,\nvoter_constraints = '[+region=us-east-1]',\nlease_preferences = '[[+region=us-east-1]]'            regional_by_row@regional_by_row_i_idx  us-east-1
num_voters = 3,\nvoter_constraints = '[+region=us-east-1]',\nlease_preferences = '[[+region=us-east-1]]'            regional_by_row@regional_by_row_pkey   us-east-1

query TT
SHOW ZONE CONFIGURATION FROM TABLE regional_by_table_in_us_east
----
DATABASE alter_locality_test  ALTER DATABASE alter_locality_test 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]]'

# Drop the table and recreate it with columns appropriate for
# REGIONAL BY ROW AS transformations.
statement ok
DROP TABLE regional_by_table_in_us_east;

statement ok
CREATE TABLE regional_by_table_in_us_east (
  pk int PRIMARY KEY,
  i int,
  b int,
  index(b),
  unique(i),
  cr crdb_internal_region NOT NULL NOT NULL,
  FAMILY (pk, i, b, cr)
) LOCALITY REGIONAL BY TABLE IN "us-east-1";

statement ok
INSERT INTO regional_by_table_in_us_east VALUES (0, 1, 2, 'us-east-1');

statement ok
ALTER TABLE regional_by_table_in_us_east SET LOCALITY REGIONAL BY ROW AS "cr"

query TT
SHOW CREATE TABLE regional_by_table_in_us_east
----
regional_by_table_in_us_east  CREATE TABLE public.regional_by_table_in_us_east (
                                pk INT8 NOT NULL,
                                i INT8 NULL,
                                b INT8 NULL,
                                cr public.crdb_internal_region NOT NULL,
                                CONSTRAINT regional_by_table_in_us_east_pkey PRIMARY KEY (pk ASC),
                                INDEX regional_by_table_in_us_east_b_idx (b ASC),
                                UNIQUE INDEX regional_by_table_in_us_east_i_key (i ASC),
                                FAMILY fam_0_pk_i_b_cr (pk, i, b, cr)
                              ) LOCALITY REGIONAL BY ROW AS cr

query TTT
SELECT zone_config, index_name, partition_name FROM [SHOW PARTITIONS FROM TABLE regional_by_row]
ORDER BY partition_name, index_name
----
num_voters = 3,\nvoter_constraints = '[+region=ap-southeast-2]',\nlease_preferences = '[[+region=ap-southeast-2]]'  regional_by_row@regional_by_row_i_idx  ap-southeast-2
num_voters = 3,\nvoter_constraints = '[+region=ap-southeast-2]',\nlease_preferences = '[[+region=ap-southeast-2]]'  regional_by_row@regional_by_row_pkey   ap-southeast-2
num_voters = 3,\nvoter_constraints = '[+region=ca-central-1]',\nlease_preferences = '[[+region=ca-central-1]]'      regional_by_row@regional_by_row_i_idx  ca-central-1
num_voters = 3,\nvoter_constraints = '[+region=ca-central-1]',\nlease_preferences = '[[+region=ca-central-1]]'      regional_by_row@regional_by_row_pkey   ca-central-1
num_voters = 3,\nvoter_constraints = '[+region=us-east-1]',\nlease_preferences = '[[+region=us-east-1]]'            regional_by_row@regional_by_row_i_idx  us-east-1
num_voters = 3,\nvoter_constraints = '[+region=us-east-1]',\nlease_preferences = '[[+region=us-east-1]]'            regional_by_row@regional_by_row_pkey   us-east-1

query TT
SHOW ZONE CONFIGURATION FROM TABLE regional_by_table_in_us_east
----
DATABASE alter_locality_test  ALTER DATABASE alter_locality_test 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]]'

# Altering from REGIONAL BY ROW

statement ok
INSERT INTO regional_by_row (pk, i) VALUES (1, 1);
ALTER TABLE regional_by_row SET LOCALITY REGIONAL BY TABLE

query TT
SHOW CREATE TABLE regional_by_row
----
regional_by_row  CREATE TABLE public.regional_by_row (
                   pk INT8 NOT NULL,
                   i INT8 NULL,
                   crdb_region public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::public.crdb_internal_region,
                   CONSTRAINT regional_by_row_pkey PRIMARY KEY (pk ASC),
                   INDEX regional_by_row_i_idx (i ASC),
                   FAMILY fam_0_pk_i_crdb_region (pk, i, crdb_region)
                 ) LOCALITY REGIONAL BY TABLE IN PRIMARY REGION

query TTT
SELECT zone_config, index_name, partition_name FROM [SHOW PARTITIONS FROM TABLE regional_by_row]
ORDER BY partition_name, index_name
----

query TT
SHOW ZONE CONFIGURATION FROM TABLE regional_by_row
----
DATABASE alter_locality_test  ALTER DATABASE alter_locality_test 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
DROP TABLE regional_by_row;

statement ok
CREATE TABLE regional_by_row (
  pk INT PRIMARY KEY,
  i INT,
  INDEX(i),
  FAMILY (pk, i)
) LOCALITY REGIONAL BY ROW;

statement ok
INSERT INTO regional_by_row (pk, i) VALUES (1, 1);

statement ok
ALTER TABLE regional_by_row SET LOCALITY REGIONAL BY TABLE in "ap-southeast-2"

query TT
SHOW CREATE TABLE regional_by_row
----
regional_by_row  CREATE TABLE public.regional_by_row (
                   pk INT8 NOT NULL,
                   i INT8 NULL,
                   crdb_region public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::public.crdb_internal_region,
                   CONSTRAINT regional_by_row_pkey PRIMARY KEY (pk ASC),
                   INDEX regional_by_row_i_idx (i ASC),
                   FAMILY fam_0_pk_i_crdb_region (pk, i, crdb_region)
                 ) LOCALITY REGIONAL BY TABLE IN "ap-southeast-2"

query TTT
SELECT zone_config, index_name, partition_name FROM [SHOW PARTITIONS FROM TABLE regional_by_row]
ORDER BY partition_name, index_name
----

query TT
SHOW ZONE CONFIGURATION FROM TABLE regional_by_row
----
TABLE regional_by_row  ALTER TABLE regional_by_row 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=ap-southeast-2]',
                         lease_preferences = '[[+region=ap-southeast-2]]'

statement ok
DROP TABLE regional_by_row;

statement ok
CREATE TABLE regional_by_row (
  pk INT PRIMARY KEY,
  i INT,
  INDEX(i),
  FAMILY (pk, i)
) LOCALITY REGIONAL BY ROW;

statement ok
INSERT INTO regional_by_row (pk, i) VALUES (1, 1);

statement ok
ALTER TABLE regional_by_row SET LOCALITY REGIONAL BY TABLE IN PRIMARY REGION

query TT
SHOW CREATE TABLE regional_by_row
----
regional_by_row  CREATE TABLE public.regional_by_row (
                   pk INT8 NOT NULL,
                   i INT8 NULL,
                   crdb_region public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::public.crdb_internal_region,
                   CONSTRAINT regional_by_row_pkey PRIMARY KEY (pk ASC),
                   INDEX regional_by_row_i_idx (i ASC),
                   FAMILY fam_0_pk_i_crdb_region (pk, i, crdb_region)
                 ) LOCALITY REGIONAL BY TABLE IN PRIMARY REGION

query TTT
SELECT zone_config, index_name, partition_name FROM [SHOW PARTITIONS FROM TABLE regional_by_row]
ORDER BY partition_name, index_name
----

query TT
SHOW ZONE CONFIGURATION FROM TABLE regional_by_row
----
DATABASE alter_locality_test  ALTER DATABASE alter_locality_test 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
DROP TABLE regional_by_row;

statement ok
CREATE TABLE regional_by_row (
  pk INT PRIMARY KEY,
  i INT,
  INDEX(i),
  FAMILY (pk, i)
) LOCALITY REGIONAL BY ROW;

statement ok
INSERT INTO regional_by_row (pk, i) VALUES (1, 1);

statement ok
ALTER TABLE regional_by_row SET LOCALITY GLOBAL

query TT
SHOW CREATE TABLE regional_by_row
----
regional_by_row  CREATE TABLE public.regional_by_row (
                   pk INT8 NOT NULL,
                   i INT8 NULL,
                   crdb_region public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::public.crdb_internal_region,
                   CONSTRAINT regional_by_row_pkey PRIMARY KEY (pk ASC),
                   INDEX regional_by_row_i_idx (i ASC),
                   FAMILY fam_0_pk_i_crdb_region (pk, i, crdb_region)
                 ) LOCALITY GLOBAL

query TTT
SELECT zone_config, index_name, partition_name FROM [SHOW PARTITIONS FROM TABLE regional_by_row]
ORDER BY partition_name, index_name
----

query TT
SHOW ZONE CONFIGURATION FROM TABLE regional_by_row
----
TABLE regional_by_row  ALTER TABLE regional_by_row CONFIGURE ZONE USING
                         range_min_bytes = 134217728,
                         range_max_bytes = 536870912,
                         gc.ttlseconds = 14400,
                         global_reads = true,
                         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
DROP TABLE regional_by_row;

statement ok
CREATE TABLE regional_by_row (
  pk INT PRIMARY KEY,
  i INT,
  INDEX(i),
  FAMILY (pk, i)
) LOCALITY REGIONAL BY ROW;

statement ok
INSERT INTO regional_by_row (pk, i) VALUES (1, 1);

statement ok
ALTER TABLE regional_by_row SET LOCALITY REGIONAL BY ROW

query TT
SHOW CREATE TABLE regional_by_row
----
regional_by_row  CREATE TABLE public.regional_by_row (
                   pk INT8 NOT NULL,
                   i INT8 NULL,
                   crdb_region public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::public.crdb_internal_region,
                   CONSTRAINT regional_by_row_pkey PRIMARY KEY (pk ASC),
                   INDEX regional_by_row_i_idx (i ASC),
                   FAMILY fam_0_pk_i_crdb_region (pk, i, crdb_region)
                 ) LOCALITY REGIONAL BY ROW

query TTT
SELECT zone_config, index_name, partition_name FROM [SHOW PARTITIONS FROM TABLE regional_by_row]
ORDER BY partition_name, index_name
----
num_voters = 3,\nvoter_constraints = '[+region=ap-southeast-2]',\nlease_preferences = '[[+region=ap-southeast-2]]'  regional_by_row@regional_by_row_i_idx  ap-southeast-2
num_voters = 3,\nvoter_constraints = '[+region=ap-southeast-2]',\nlease_preferences = '[[+region=ap-southeast-2]]'  regional_by_row@regional_by_row_pkey   ap-southeast-2
num_voters = 3,\nvoter_constraints = '[+region=ca-central-1]',\nlease_preferences = '[[+region=ca-central-1]]'      regional_by_row@regional_by_row_i_idx  ca-central-1
num_voters = 3,\nvoter_constraints = '[+region=ca-central-1]',\nlease_preferences = '[[+region=ca-central-1]]'      regional_by_row@regional_by_row_pkey   ca-central-1
num_voters = 3,\nvoter_constraints = '[+region=us-east-1]',\nlease_preferences = '[[+region=us-east-1]]'            regional_by_row@regional_by_row_i_idx  us-east-1
num_voters = 3,\nvoter_constraints = '[+region=us-east-1]',\nlease_preferences = '[[+region=us-east-1]]'            regional_by_row@regional_by_row_pkey   us-east-1

query TT
SHOW ZONE CONFIGURATION FROM TABLE regional_by_row
----
DATABASE alter_locality_test  ALTER DATABASE alter_locality_test 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
ALTER TABLE regional_by_row SET LOCALITY REGIONAL BY ROW AS crdb_region

query TT
SHOW CREATE TABLE regional_by_row
----
regional_by_row  CREATE TABLE public.regional_by_row (
                   pk INT8 NOT NULL,
                   i INT8 NULL,
                   crdb_region public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::public.crdb_internal_region,
                   CONSTRAINT regional_by_row_pkey PRIMARY KEY (pk ASC),
                   INDEX regional_by_row_i_idx (i ASC),
                   FAMILY fam_0_pk_i_crdb_region (pk, i, crdb_region)
                 ) LOCALITY REGIONAL BY ROW AS crdb_region

query TTT
SELECT zone_config, index_name, partition_name FROM [SHOW PARTITIONS FROM TABLE regional_by_row]
ORDER BY partition_name, index_name
----
num_voters = 3,\nvoter_constraints = '[+region=ap-southeast-2]',\nlease_preferences = '[[+region=ap-southeast-2]]'  regional_by_row@regional_by_row_i_idx  ap-southeast-2
num_voters = 3,\nvoter_constraints = '[+region=ap-southeast-2]',\nlease_preferences = '[[+region=ap-southeast-2]]'  regional_by_row@regional_by_row_pkey   ap-southeast-2
num_voters = 3,\nvoter_constraints = '[+region=ca-central-1]',\nlease_preferences = '[[+region=ca-central-1]]'      regional_by_row@regional_by_row_i_idx  ca-central-1
num_voters = 3,\nvoter_constraints = '[+region=ca-central-1]',\nlease_preferences = '[[+region=ca-central-1]]'      regional_by_row@regional_by_row_pkey   ca-central-1
num_voters = 3,\nvoter_constraints = '[+region=us-east-1]',\nlease_preferences = '[[+region=us-east-1]]'            regional_by_row@regional_by_row_i_idx  us-east-1
num_voters = 3,\nvoter_constraints = '[+region=us-east-1]',\nlease_preferences = '[[+region=us-east-1]]'            regional_by_row@regional_by_row_pkey   us-east-1

query TT
SHOW ZONE CONFIGURATION FROM TABLE regional_by_row
----
DATABASE alter_locality_test  ALTER DATABASE alter_locality_test 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]]'

# Go back from REGIONAL BY ROW AS crdb_region to just REGIONAL BY ROW.
statement ok
ALTER TABLE regional_by_row SET LOCALITY REGIONAL BY ROW

query TT
SHOW CREATE TABLE regional_by_row
----
regional_by_row  CREATE TABLE public.regional_by_row (
                   pk INT8 NOT NULL,
                   i INT8 NULL,
                   crdb_region public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::public.crdb_internal_region,
                   CONSTRAINT regional_by_row_pkey PRIMARY KEY (pk ASC),
                   INDEX regional_by_row_i_idx (i ASC),
                   FAMILY fam_0_pk_i_crdb_region (pk, i, crdb_region)
                 ) LOCALITY REGIONAL BY ROW

query TTT
SELECT zone_config, index_name, partition_name FROM [SHOW PARTITIONS FROM TABLE regional_by_row]
ORDER BY partition_name, index_name
----
num_voters = 3,\nvoter_constraints = '[+region=ap-southeast-2]',\nlease_preferences = '[[+region=ap-southeast-2]]'  regional_by_row@regional_by_row_i_idx  ap-southeast-2
num_voters = 3,\nvoter_constraints = '[+region=ap-southeast-2]',\nlease_preferences = '[[+region=ap-southeast-2]]'  regional_by_row@regional_by_row_pkey   ap-southeast-2
num_voters = 3,\nvoter_constraints = '[+region=ca-central-1]',\nlease_preferences = '[[+region=ca-central-1]]'      regional_by_row@regional_by_row_i_idx  ca-central-1
num_voters = 3,\nvoter_constraints = '[+region=ca-central-1]',\nlease_preferences = '[[+region=ca-central-1]]'      regional_by_row@regional_by_row_pkey   ca-central-1
num_voters = 3,\nvoter_constraints = '[+region=us-east-1]',\nlease_preferences = '[[+region=us-east-1]]'            regional_by_row@regional_by_row_i_idx  us-east-1
num_voters = 3,\nvoter_constraints = '[+region=us-east-1]',\nlease_preferences = '[[+region=us-east-1]]'            regional_by_row@regional_by_row_pkey   us-east-1

query TT
SHOW ZONE CONFIGURATION FROM TABLE regional_by_row
----
DATABASE alter_locality_test  ALTER DATABASE alter_locality_test 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 regional_by_row_to_regional_by_row_as (
  pk INT PRIMARY KEY,
  i INT,
  cr crdb_internal_region NOT NULL DEFAULT 'ap-southeast-2',
  INDEX(i),
  FAMILY (pk, i)
) LOCALITY REGIONAL BY ROW;

statement ok
INSERT INTO regional_by_row_to_regional_by_row_as (pk, i) VALUES (1, 1);

statement ok
ALTER TABLE regional_by_row_to_regional_by_row_as SET LOCALITY REGIONAL BY ROW AS "cr"

query TT
SHOW CREATE TABLE regional_by_row_to_regional_by_row_as
----
regional_by_row_to_regional_by_row_as  CREATE TABLE public.regional_by_row_to_regional_by_row_as (
                                         pk INT8 NOT NULL,
                                         i INT8 NULL,
                                         cr public.crdb_internal_region NOT NULL DEFAULT 'ap-southeast-2':::public.crdb_internal_region,
                                         crdb_region public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::public.crdb_internal_region,
                                         CONSTRAINT regional_by_row_to_regional_by_row_as_pkey PRIMARY KEY (pk ASC),
                                         INDEX regional_by_row_to_regional_by_row_as_i_idx (i ASC),
                                         FAMILY fam_0_pk_i_cr_crdb_region (pk, i, cr, crdb_region)
                                       ) LOCALITY REGIONAL BY ROW AS cr

query TTT
SELECT zone_config, index_name, partition_name FROM [SHOW PARTITIONS FROM TABLE regional_by_row]
ORDER BY partition_name, index_name
----
num_voters = 3,\nvoter_constraints = '[+region=ap-southeast-2]',\nlease_preferences = '[[+region=ap-southeast-2]]'  regional_by_row@regional_by_row_i_idx  ap-southeast-2
num_voters = 3,\nvoter_constraints = '[+region=ap-southeast-2]',\nlease_preferences = '[[+region=ap-southeast-2]]'  regional_by_row@regional_by_row_pkey   ap-southeast-2
num_voters = 3,\nvoter_constraints = '[+region=ca-central-1]',\nlease_preferences = '[[+region=ca-central-1]]'      regional_by_row@regional_by_row_i_idx  ca-central-1
num_voters = 3,\nvoter_constraints = '[+region=ca-central-1]',\nlease_preferences = '[[+region=ca-central-1]]'      regional_by_row@regional_by_row_pkey   ca-central-1
num_voters = 3,\nvoter_constraints = '[+region=us-east-1]',\nlease_preferences = '[[+region=us-east-1]]'            regional_by_row@regional_by_row_i_idx  us-east-1
num_voters = 3,\nvoter_constraints = '[+region=us-east-1]',\nlease_preferences = '[[+region=us-east-1]]'            regional_by_row@regional_by_row_pkey   us-east-1

query TT
SHOW ZONE CONFIGURATION FROM TABLE regional_by_row_to_regional_by_row_as
----
DATABASE alter_locality_test  ALTER DATABASE alter_locality_test 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]]'

# Altering from REGIONAL BY ROW AS

statement ok
INSERT INTO regional_by_row_as (pk, i) VALUES (1, 1);
ALTER TABLE regional_by_row_as SET LOCALITY REGIONAL BY TABLE

query TT
SHOW CREATE TABLE regional_by_row_as
----
regional_by_row_as  CREATE TABLE public.regional_by_row_as (
                      pk INT8 NOT NULL,
                      i INT8 NULL,
                      cr public.crdb_internal_region NOT NULL DEFAULT 'ap-southeast-2':::public.crdb_internal_region,
                      CONSTRAINT regional_by_row_as_pkey PRIMARY KEY (pk ASC),
                      INDEX regional_by_row_as_i_idx (i ASC),
                      FAMILY fam_0_cr_pk_i (cr, pk, i)
                    ) LOCALITY REGIONAL BY TABLE IN PRIMARY REGION

query TTT
SELECT zone_config, index_name, partition_name FROM [SHOW PARTITIONS FROM TABLE regional_by_row]
ORDER BY partition_name, index_name
----
num_voters = 3,\nvoter_constraints = '[+region=ap-southeast-2]',\nlease_preferences = '[[+region=ap-southeast-2]]'  regional_by_row@regional_by_row_i_idx  ap-southeast-2
num_voters = 3,\nvoter_constraints = '[+region=ap-southeast-2]',\nlease_preferences = '[[+region=ap-southeast-2]]'  regional_by_row@regional_by_row_pkey   ap-southeast-2
num_voters = 3,\nvoter_constraints = '[+region=ca-central-1]',\nlease_preferences = '[[+region=ca-central-1]]'      regional_by_row@regional_by_row_i_idx  ca-central-1
num_voters = 3,\nvoter_constraints = '[+region=ca-central-1]',\nlease_preferences = '[[+region=ca-central-1]]'      regional_by_row@regional_by_row_pkey   ca-central-1
num_voters = 3,\nvoter_constraints = '[+region=us-east-1]',\nlease_preferences = '[[+region=us-east-1]]'            regional_by_row@regional_by_row_i_idx  us-east-1
num_voters = 3,\nvoter_constraints = '[+region=us-east-1]',\nlease_preferences = '[[+region=us-east-1]]'            regional_by_row@regional_by_row_pkey   us-east-1

query TT
SHOW ZONE CONFIGURATION FROM TABLE regional_by_row_as
----
DATABASE alter_locality_test  ALTER DATABASE alter_locality_test 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
DROP TABLE regional_by_row_as;

statement ok
CREATE TABLE regional_by_row_as (
  pk INT PRIMARY KEY,
  i INT,
  cr crdb_internal_region NOT NULL DEFAULT 'ap-southeast-2',
  INDEX(i),
  FAMILY (cr, pk, i)
) LOCALITY REGIONAL BY ROW AS "cr";

statement ok
INSERT INTO regional_by_row_as (pk, i) VALUES (1, 1);

statement ok
ALTER TABLE regional_by_row_as SET LOCALITY REGIONAL BY TABLE in "ap-southeast-2"

query TT
SHOW CREATE TABLE regional_by_row_as
----
regional_by_row_as  CREATE TABLE public.regional_by_row_as (
                      pk INT8 NOT NULL,
                      i INT8 NULL,
                      cr public.crdb_internal_region NOT NULL DEFAULT 'ap-southeast-2':::public.crdb_internal_region,
                      CONSTRAINT regional_by_row_as_pkey PRIMARY KEY (pk ASC),
                      INDEX regional_by_row_as_i_idx (i ASC),
                      FAMILY fam_0_cr_pk_i (cr, pk, i)
                    ) LOCALITY REGIONAL BY TABLE IN "ap-southeast-2"

query TTT
SELECT zone_config, index_name, partition_name FROM [SHOW PARTITIONS FROM TABLE regional_by_row]
ORDER BY partition_name, index_name
----
num_voters = 3,\nvoter_constraints = '[+region=ap-southeast-2]',\nlease_preferences = '[[+region=ap-southeast-2]]'  regional_by_row@regional_by_row_i_idx  ap-southeast-2
num_voters = 3,\nvoter_constraints = '[+region=ap-southeast-2]',\nlease_preferences = '[[+region=ap-southeast-2]]'  regional_by_row@regional_by_row_pkey   ap-southeast-2
num_voters = 3,\nvoter_constraints = '[+region=ca-central-1]',\nlease_preferences = '[[+region=ca-central-1]]'      regional_by_row@regional_by_row_i_idx  ca-central-1
num_voters = 3,\nvoter_constraints = '[+region=ca-central-1]',\nlease_preferences = '[[+region=ca-central-1]]'      regional_by_row@regional_by_row_pkey   ca-central-1
num_voters = 3,\nvoter_constraints = '[+region=us-east-1]',\nlease_preferences = '[[+region=us-east-1]]'            regional_by_row@regional_by_row_i_idx  us-east-1
num_voters = 3,\nvoter_constraints = '[+region=us-east-1]',\nlease_preferences = '[[+region=us-east-1]]'            regional_by_row@regional_by_row_pkey   us-east-1

query TT
SHOW ZONE CONFIGURATION FROM TABLE regional_by_row_as
----
TABLE regional_by_row_as  ALTER TABLE regional_by_row_as 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=ap-southeast-2]',
                            lease_preferences = '[[+region=ap-southeast-2]]'

statement ok
DROP TABLE regional_by_row_as;

statement ok
CREATE TABLE regional_by_row_as (
  pk INT PRIMARY KEY,
  i INT,
  cr crdb_internal_region NOT NULL DEFAULT 'ap-southeast-2',
  INDEX(i),
  FAMILY (cr, pk, i)
) LOCALITY REGIONAL BY ROW AS "cr";

statement ok
INSERT INTO regional_by_row_as (pk, i) VALUES (1, 1);

statement ok
ALTER TABLE regional_by_row_as SET LOCALITY REGIONAL BY TABLE IN PRIMARY REGION

query TT
SHOW CREATE TABLE regional_by_row_as
----
regional_by_row_as  CREATE TABLE public.regional_by_row_as (
                      pk INT8 NOT NULL,
                      i INT8 NULL,
                      cr public.crdb_internal_region NOT NULL DEFAULT 'ap-southeast-2':::public.crdb_internal_region,
                      CONSTRAINT regional_by_row_as_pkey PRIMARY KEY (pk ASC),
                      INDEX regional_by_row_as_i_idx (i ASC),
                      FAMILY fam_0_cr_pk_i (cr, pk, i)
                    ) LOCALITY REGIONAL BY TABLE IN PRIMARY REGION

query TTT
SELECT zone_config, index_name, partition_name FROM [SHOW PARTITIONS FROM TABLE regional_by_row]
ORDER BY partition_name, index_name
----
num_voters = 3,\nvoter_constraints = '[+region=ap-southeast-2]',\nlease_preferences = '[[+region=ap-southeast-2]]'  regional_by_row@regional_by_row_i_idx  ap-southeast-2
num_voters = 3,\nvoter_constraints = '[+region=ap-southeast-2]',\nlease_preferences = '[[+region=ap-southeast-2]]'  regional_by_row@regional_by_row_pkey   ap-southeast-2
num_voters = 3,\nvoter_constraints = '[+region=ca-central-1]',\nlease_preferences = '[[+region=ca-central-1]]'      regional_by_row@regional_by_row_i_idx  ca-central-1
num_voters = 3,\nvoter_constraints = '[+region=ca-central-1]',\nlease_preferences = '[[+region=ca-central-1]]'      regional_by_row@regional_by_row_pkey   ca-central-1
num_voters = 3,\nvoter_constraints = '[+region=us-east-1]',\nlease_preferences = '[[+region=us-east-1]]'            regional_by_row@regional_by_row_i_idx  us-east-1
num_voters = 3,\nvoter_constraints = '[+region=us-east-1]',\nlease_preferences = '[[+region=us-east-1]]'            regional_by_row@regional_by_row_pkey   us-east-1

query TT
SHOW ZONE CONFIGURATION FROM TABLE regional_by_row_as
----
DATABASE alter_locality_test  ALTER DATABASE alter_locality_test 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
DROP TABLE regional_by_row_as;

statement ok
CREATE TABLE regional_by_row_as (
  pk INT PRIMARY KEY,
  i INT,
  cr crdb_internal_region NOT NULL DEFAULT 'ap-southeast-2',
  INDEX(i),
  FAMILY (cr, pk, i)
) LOCALITY REGIONAL BY ROW AS "cr";

statement ok
INSERT INTO regional_by_row_as (pk, i) VALUES (1, 1);

statement ok
ALTER TABLE regional_by_row_as SET LOCALITY GLOBAL

query TT
SHOW CREATE TABLE regional_by_row_as
----
regional_by_row_as  CREATE TABLE public.regional_by_row_as (
                      pk INT8 NOT NULL,
                      i INT8 NULL,
                      cr public.crdb_internal_region NOT NULL DEFAULT 'ap-southeast-2':::public.crdb_internal_region,
                      CONSTRAINT regional_by_row_as_pkey PRIMARY KEY (pk ASC),
                      INDEX regional_by_row_as_i_idx (i ASC),
                      FAMILY fam_0_cr_pk_i (cr, pk, i)
                    ) LOCALITY GLOBAL

query TTT
SELECT zone_config, index_name, partition_name FROM [SHOW PARTITIONS FROM TABLE regional_by_row]
ORDER BY partition_name, index_name
----
num_voters = 3,\nvoter_constraints = '[+region=ap-southeast-2]',\nlease_preferences = '[[+region=ap-southeast-2]]'  regional_by_row@regional_by_row_i_idx  ap-southeast-2
num_voters = 3,\nvoter_constraints = '[+region=ap-southeast-2]',\nlease_preferences = '[[+region=ap-southeast-2]]'  regional_by_row@regional_by_row_pkey   ap-southeast-2
num_voters = 3,\nvoter_constraints = '[+region=ca-central-1]',\nlease_preferences = '[[+region=ca-central-1]]'      regional_by_row@regional_by_row_i_idx  ca-central-1
num_voters = 3,\nvoter_constraints = '[+region=ca-central-1]',\nlease_preferences = '[[+region=ca-central-1]]'      regional_by_row@regional_by_row_pkey   ca-central-1
num_voters = 3,\nvoter_constraints = '[+region=us-east-1]',\nlease_preferences = '[[+region=us-east-1]]'            regional_by_row@regional_by_row_i_idx  us-east-1
num_voters = 3,\nvoter_constraints = '[+region=us-east-1]',\nlease_preferences = '[[+region=us-east-1]]'            regional_by_row@regional_by_row_pkey   us-east-1

query TT
SHOW ZONE CONFIGURATION FROM TABLE regional_by_row_as
----
TABLE regional_by_row_as  ALTER TABLE regional_by_row_as CONFIGURE ZONE USING
                            range_min_bytes = 134217728,
                            range_max_bytes = 536870912,
                            gc.ttlseconds = 14400,
                            global_reads = true,
                            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 regional_by_row_as_to_regional_by_row (
  pk INT PRIMARY KEY,
  i INT,
  cr crdb_internal_region NOT NULL DEFAULT 'ap-southeast-2',
  INDEX(i),
  FAMILY (cr, pk, i)
) LOCALITY REGIONAL BY ROW AS "cr";

statement ok
INSERT INTO regional_by_row_as_to_regional_by_row (pk, i) VALUES (1, 1);
ALTER TABLE regional_by_row_as_to_regional_by_row SET LOCALITY REGIONAL BY ROW

query TT
SHOW CREATE TABLE regional_by_row_as_to_regional_by_row
----
regional_by_row_as_to_regional_by_row  CREATE TABLE public.regional_by_row_as_to_regional_by_row (
                                         pk INT8 NOT NULL,
                                         i INT8 NULL,
                                         cr public.crdb_internal_region NOT NULL DEFAULT 'ap-southeast-2':::public.crdb_internal_region,
                                         crdb_region public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::public.crdb_internal_region,
                                         CONSTRAINT regional_by_row_as_to_regional_by_row_pkey PRIMARY KEY (pk ASC),
                                         INDEX regional_by_row_as_to_regional_by_row_i_idx (i ASC),
                                         FAMILY fam_0_cr_pk_i (cr, pk, i, crdb_region)
                                       ) LOCALITY REGIONAL BY ROW

query TTT
SELECT zone_config, index_name, partition_name FROM [SHOW PARTITIONS FROM TABLE regional_by_row]
ORDER BY partition_name, index_name
----
num_voters = 3,\nvoter_constraints = '[+region=ap-southeast-2]',\nlease_preferences = '[[+region=ap-southeast-2]]'  regional_by_row@regional_by_row_i_idx  ap-southeast-2
num_voters = 3,\nvoter_constraints = '[+region=ap-southeast-2]',\nlease_preferences = '[[+region=ap-southeast-2]]'  regional_by_row@regional_by_row_pkey   ap-southeast-2
num_voters = 3,\nvoter_constraints = '[+region=ca-central-1]',\nlease_preferences = '[[+region=ca-central-1]]'      regional_by_row@regional_by_row_i_idx  ca-central-1
num_voters = 3,\nvoter_constraints = '[+region=ca-central-1]',\nlease_preferences = '[[+region=ca-central-1]]'      regional_by_row@regional_by_row_pkey   ca-central-1
num_voters = 3,\nvoter_constraints = '[+region=us-east-1]',\nlease_preferences = '[[+region=us-east-1]]'            regional_by_row@regional_by_row_i_idx  us-east-1
num_voters = 3,\nvoter_constraints = '[+region=us-east-1]',\nlease_preferences = '[[+region=us-east-1]]'            regional_by_row@regional_by_row_pkey   us-east-1

query TT
SHOW ZONE CONFIGURATION FROM TABLE regional_by_row_as_to_regional_by_row
----
DATABASE alter_locality_test  ALTER DATABASE alter_locality_test 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
DROP TABLE regional_by_row_as;

statement ok
CREATE TABLE regional_by_row_as (
  pk INT PRIMARY KEY,
  i INT,
  cr crdb_internal_region NOT NULL DEFAULT 'ap-southeast-2',
  INDEX(i),
  FAMILY (cr, pk, i)
) LOCALITY REGIONAL BY ROW AS "cr";
INSERT INTO regional_by_row_as (pk, i) VALUES (1, 1);
ALTER TABLE regional_by_row_as SET LOCALITY REGIONAL BY ROW AS "cr"

query TT
SHOW CREATE TABLE regional_by_row_as
----
regional_by_row_as  CREATE TABLE public.regional_by_row_as (
                      pk INT8 NOT NULL,
                      i INT8 NULL,
                      cr public.crdb_internal_region NOT NULL DEFAULT 'ap-southeast-2':::public.crdb_internal_region,
                      CONSTRAINT regional_by_row_as_pkey PRIMARY KEY (pk ASC),
                      INDEX regional_by_row_as_i_idx (i ASC),
                      FAMILY fam_0_cr_pk_i (cr, pk, i)
                    ) LOCALITY REGIONAL BY ROW AS cr

query TTT
SELECT zone_config, index_name, partition_name FROM [SHOW PARTITIONS FROM TABLE regional_by_row]
ORDER BY partition_name, index_name
----
num_voters = 3,\nvoter_constraints = '[+region=ap-southeast-2]',\nlease_preferences = '[[+region=ap-southeast-2]]'  regional_by_row@regional_by_row_i_idx  ap-southeast-2
num_voters = 3,\nvoter_constraints = '[+region=ap-southeast-2]',\nlease_preferences = '[[+region=ap-southeast-2]]'  regional_by_row@regional_by_row_pkey   ap-southeast-2
num_voters = 3,\nvoter_constraints = '[+region=ca-central-1]',\nlease_preferences = '[[+region=ca-central-1]]'      regional_by_row@regional_by_row_i_idx  ca-central-1
num_voters = 3,\nvoter_constraints = '[+region=ca-central-1]',\nlease_preferences = '[[+region=ca-central-1]]'      regional_by_row@regional_by_row_pkey   ca-central-1
num_voters = 3,\nvoter_constraints = '[+region=us-east-1]',\nlease_preferences = '[[+region=us-east-1]]'            regional_by_row@regional_by_row_i_idx  us-east-1
num_voters = 3,\nvoter_constraints = '[+region=us-east-1]',\nlease_preferences = '[[+region=us-east-1]]'            regional_by_row@regional_by_row_pkey   us-east-1

query TT
SHOW ZONE CONFIGURATION FROM TABLE regional_by_row_as
----
DATABASE alter_locality_test  ALTER DATABASE alter_locality_test 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]]'

# Set a table with a gc.ttlseconds to be a non-default value, and check this is
# the same after a change to REGIONAL BY TABLE IN PRIMARY REGION, which overrides
# some other fields.
statement ok
CREATE TABLE rbt_table_gc_ttl () LOCALITY REGIONAL BY TABLE IN "us-east-1"

query TTT
SELECT zone_config, index_name, partition_name FROM [SHOW PARTITIONS FROM TABLE regional_by_row]
ORDER BY partition_name, index_name
----
num_voters = 3,\nvoter_constraints = '[+region=ap-southeast-2]',\nlease_preferences = '[[+region=ap-southeast-2]]'  regional_by_row@regional_by_row_i_idx  ap-southeast-2
num_voters = 3,\nvoter_constraints = '[+region=ap-southeast-2]',\nlease_preferences = '[[+region=ap-southeast-2]]'  regional_by_row@regional_by_row_pkey   ap-southeast-2
num_voters = 3,\nvoter_constraints = '[+region=ca-central-1]',\nlease_preferences = '[[+region=ca-central-1]]'      regional_by_row@regional_by_row_i_idx  ca-central-1
num_voters = 3,\nvoter_constraints = '[+region=ca-central-1]',\nlease_preferences = '[[+region=ca-central-1]]'      regional_by_row@regional_by_row_pkey   ca-central-1
num_voters = 3,\nvoter_constraints = '[+region=us-east-1]',\nlease_preferences = '[[+region=us-east-1]]'            regional_by_row@regional_by_row_i_idx  us-east-1
num_voters = 3,\nvoter_constraints = '[+region=us-east-1]',\nlease_preferences = '[[+region=us-east-1]]'            regional_by_row@regional_by_row_pkey   us-east-1

query TT
SHOW ZONE CONFIGURATION FROM TABLE rbt_table_gc_ttl
----
TABLE rbt_table_gc_ttl  ALTER TABLE rbt_table_gc_ttl 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=us-east-1]',
                          lease_preferences = '[[+region=us-east-1]]'

statement ok
ALTER TABLE rbt_table_gc_ttl CONFIGURE ZONE USING gc.ttlseconds = 999;
ALTER TABLE rbt_table_gc_ttl SET LOCALITY REGIONAL BY TABLE IN PRIMARY REGION

query TTT
SELECT zone_config, index_name, partition_name FROM [SHOW PARTITIONS FROM TABLE regional_by_row]
ORDER BY partition_name, index_name
----
num_voters = 3,\nvoter_constraints = '[+region=ap-southeast-2]',\nlease_preferences = '[[+region=ap-southeast-2]]'  regional_by_row@regional_by_row_i_idx  ap-southeast-2
num_voters = 3,\nvoter_constraints = '[+region=ap-southeast-2]',\nlease_preferences = '[[+region=ap-southeast-2]]'  regional_by_row@regional_by_row_pkey   ap-southeast-2
num_voters = 3,\nvoter_constraints = '[+region=ca-central-1]',\nlease_preferences = '[[+region=ca-central-1]]'      regional_by_row@regional_by_row_i_idx  ca-central-1
num_voters = 3,\nvoter_constraints = '[+region=ca-central-1]',\nlease_preferences = '[[+region=ca-central-1]]'      regional_by_row@regional_by_row_pkey   ca-central-1
num_voters = 3,\nvoter_constraints = '[+region=us-east-1]',\nlease_preferences = '[[+region=us-east-1]]'            regional_by_row@regional_by_row_i_idx  us-east-1
num_voters = 3,\nvoter_constraints = '[+region=us-east-1]',\nlease_preferences = '[[+region=us-east-1]]'            regional_by_row@regional_by_row_pkey   us-east-1

query TT
SHOW ZONE CONFIGURATION FROM TABLE rbt_table_gc_ttl
----
TABLE rbt_table_gc_ttl  ALTER TABLE rbt_table_gc_ttl CONFIGURE ZONE USING
                        range_min_bytes = 134217728,
                        range_max_bytes = 536870912,
                        gc.ttlseconds = 999,
                        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 hash_sharded_idx_table (
  pk INT PRIMARY KEY USING HASH WITH (bucket_count=8)
)
