Skip to content

Commit

Permalink
sql: Add test for CREATE TABLE AS on REGIONAL BY ROW table
Browse files Browse the repository at this point in the history
Previously we were unable to make CREATE TABLE AS run on a REGIONAL BY
ROW TABLE end up with a table that was (nearly) identical to the
original REGIONAL BY ROW table. This was due to the fact that we didn't
support setting a column to be NOT VISIBLE. With cockroachdb#63052 we added the
support for NOT VISIBLE on ALTER COLUMN which makes getting pretty close
to a REGIONAL BY ROW TABLE now possible when starting from CREATE TABLE
AS. This commit adds a test case to illustrate this fact.

Resolves cockroachdb#62326.

Release note: None
  • Loading branch information
ajstorm committed May 28, 2021
1 parent 1f89609 commit cd09155
Showing 1 changed file with 135 additions and 0 deletions.
135 changes: 135 additions & 0 deletions pkg/ccl/logictestccl/testdata/logic_test/multi_region
Original file line number Diff line number Diff line change
Expand Up @@ -1557,3 +1557,138 @@ DROP TYPE db_enum_name_clash.crdb_internal_region;
ALTER DATABASE db_enum_name_clash SET PRIMARY REGION "us-east-1"


# Verify we can create a view off of a regional by row table.
# Regression for #63191.
subtest regional_by_row_view

statement ok
CREATE DATABASE db;
USE db;
ALTER DATABASE db PRIMARY REGION "us-east-1";
CREATE TABLE kv (k INT PRIMARY KEY, v INT) LOCALITY REGIONAL BY ROW

statement ok
CREATE VIEW v AS SELECT v FROM kv

# Verify that we can use CREATE TABLE AS from a REGIONAL BY ROW table, and
# end up with something that resembles a REGIONAL BY ROW table. Note that
# this is not simple, as we need to explicitly select the crdb_region
# column, and then convert that column to be generated and hidden.
subtest create_table_as_regional_by_row

statement ok
CREATE DATABASE "mr-create-table-as" PRIMARY REGION "ap-southeast-2" REGIONS "ap-southeast-2", "ca-central-1", "us-east-1";
USE "mr-create-table-as"

statement ok
CREATE TABLE t (i int PRIMARY KEY) LOCALITY REGIONAL BY ROW

statement ok
INSERT INTO t VALUES (1),(2),(3)

statement ok
CREATE TABLE t_as AS SELECT i, crdb_region FROM t

query T colnames
SELECT create_statement from [SHOW CREATE TABLE t_as]
----
create_statement
CREATE TABLE public.t_as (
i INT8 NULL,
crdb_region public.crdb_internal_region NULL,
rowid INT8 NOT VISIBLE NOT NULL DEFAULT unique_rowid(),
CONSTRAINT "primary" PRIMARY KEY (rowid ASC),
FAMILY "primary" (i, crdb_region, rowid)
) LOCALITY REGIONAL BY TABLE IN PRIMARY REGION


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

statement ok
ALTER TABLE t_as ALTER COLUMN crdb_region SET NOT NULL

statement ok
ALTER TABLE t_as ALTER COLUMN crdb_region SET NOT VISIBLE

statement ok
ALTER TABLE t_as ALTER COLUMN crdb_region SET DEFAULT default_to_database_primary_region(gateway_region())::public.crdb_internal_region

statement ok
ALTER TABLE t_as SET LOCALITY REGIONAL BY ROW AS crdb_region

query T colnames
SELECT create_statement from [SHOW CREATE TABLE t_as]
----
create_statement
CREATE TABLE public.t_as (
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,
rowid INT8 NOT VISIBLE NOT NULL DEFAULT unique_rowid(),
CONSTRAINT "primary" PRIMARY KEY (rowid ASC),
FAMILY "primary" (i, crdb_region, rowid)
) LOCALITY REGIONAL BY ROW AS crdb_region

query TI colnames
SELECT crdb_region, i FROM t_as
----
crdb_region i
ap-southeast-2 1
ap-southeast-2 2
ap-southeast-2 3

statement ok
INSERT INTO t_as VALUES (4)

# Validate that hidden column works
query TI colnames
SELECT crdb_region, i FROM t_as
----
crdb_region i
ap-southeast-2 1
ap-southeast-2 2
ap-southeast-2 3
ap-southeast-2 4

statement ok
ALTER TABLE t_as ALTER COLUMN i SET NOT NULL

statement ok
ALTER TABLE t_as ALTER PRIMARY KEY USING COLUMNS (i)

statement ok
SET sql_safe_updates = false;
ALTER TABLE t_as DROP COLUMN rowid;
SET sql_safe_updates = true

query T colnames
SELECT create_statement from [SHOW CREATE TABLE t_as]
----
create_statement
CREATE TABLE public.t_as (
i INT8 NOT NULL,
crdb_region public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::public.crdb_internal_region,
CONSTRAINT "primary" PRIMARY KEY (i ASC),
FAMILY "primary" (i, crdb_region)
) LOCALITY REGIONAL BY ROW AS crdb_region

query T colnames
SELECT create_statement from [SHOW CREATE TABLE t]
----
create_statement
CREATE TABLE public.t (
i INT8 NOT NULL,
crdb_region public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::public.crdb_internal_region,
CONSTRAINT "primary" PRIMARY KEY (i ASC),
FAMILY "primary" (i, crdb_region)
) LOCALITY REGIONAL BY ROW

# Declare victory, as this is the closest we can get.
statement ok
DROP TABLE t;
DROP TABLE t_as

statement ok
SET sql_safe_updates = false;
DROP DATABASE "mr-create-table-as";
SET sql_safe_updates = true

0 comments on commit cd09155

Please sign in to comment.