From 92d1b7c1900908cb2aa9c90edac3a819c2ee4cbe Mon Sep 17 00:00:00 2001 From: Adam Storm Date: Wed, 21 Apr 2021 13:33:36 -0400 Subject: [PATCH] sql: Add test for CREATE TABLE AS on REGIONAL BY ROW table 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 #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 #62326. Release note: None --- .../testdata/logic_test/multi_region | 123 ++++++++++++++++++ 1 file changed, 123 insertions(+) diff --git a/pkg/ccl/logictestccl/testdata/logic_test/multi_region b/pkg/ccl/logictestccl/testdata/logic_test/multi_region index dc3488fdf872..f872756eba2a 100644 --- a/pkg/ccl/logictestccl/testdata/logic_test/multi_region +++ b/pkg/ccl/logictestccl/testdata/logic_test/multi_region @@ -1541,3 +1541,126 @@ 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