From 10a054b63d9403aa48ef2f7d7b3615a938879240 Mon Sep 17 00:00:00 2001 From: Rafi Shamim Date: Fri, 10 Jun 2022 18:59:15 -0400 Subject: [PATCH] tree: fix a bug in simple statement visitor The error from this visitor was never being returned. This required a change to one usage of it when creating views, since the old implementation would encounter errors when initializing virtual table views. Release note (bug fix): Views are no longer allowed to reference types that are defined in different databases. Even though this was allowed at view-creation time before, it would cause errors, since cross-database type references are not supported. --- .../backup-restore/user-defined-types | 2 +- .../testdata/logic_test/alter_table_locality | 52 +-- .../testdata/logic_test/auto_rehoming | 4 +- .../testdata/logic_test/multi_region | 8 +- .../testdata/logic_test/multi_region_backup | 16 +- .../logic_test/multi_region_zone_configs | 4 +- .../testdata/logic_test/regional_by_row | 48 +-- .../regional_by_row_hash_sharded_index | 12 +- .../logic_test/regional_by_row_query_behavior | 2 +- .../logic_test/regional_by_row_rename_column | 4 +- pkg/ccl/multiregionccl/show_test.go | 6 +- pkg/sql/create_view.go | 374 ++++++++++-------- .../testdata/logic_test/builtin_function | 2 +- .../testdata/logic_test/distsql_stats | 2 +- pkg/sql/logictest/testdata/logic_test/enums | 46 +-- .../testdata/logic_test/new_schema_changer | 32 +- .../testdata/logic_test/partial_index | 4 +- .../logictest/testdata/logic_test/pg_catalog | 18 +- .../logic_test/show_create_all_tables | 2 +- pkg/sql/logictest/testdata/logic_test/views | 59 +-- pkg/sql/schemachanger/testdata/drop | 2 +- pkg/sql/sem/tree/walk.go | 3 + pkg/sql/types/types.go | 13 +- 23 files changed, 396 insertions(+), 319 deletions(-) diff --git a/pkg/ccl/backupccl/testdata/backup-restore/user-defined-types b/pkg/ccl/backupccl/testdata/backup-restore/user-defined-types index a7cc238c26fa..bc13e9c89c82 100644 --- a/pkg/ccl/backupccl/testdata/backup-restore/user-defined-types +++ b/pkg/ccl/backupccl/testdata/backup-restore/user-defined-types @@ -167,7 +167,7 @@ howdy hello false exec-sql INSERT INTO d.expr VALUES ('hi') ---- -pq: failed to satisfy CHECK constraint (x < 'hi':::public.greeting) +pq: failed to satisfy CHECK constraint (x < 'hi':::d.public.greeting) # We should be able to use the restored types to create new tables. exec-sql diff --git a/pkg/ccl/logictestccl/testdata/logic_test/alter_table_locality b/pkg/ccl/logictestccl/testdata/logic_test/alter_table_locality index 2f299fb0ddbf..291b3d5c6f74 100644 --- a/pkg/ccl/logictestccl/testdata/logic_test/alter_table_locality +++ b/pkg/ccl/logictestccl/testdata/logic_test/alter_table_locality @@ -33,7 +33,7 @@ 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, + crdb_region alter_locality_test.public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::alter_locality_test.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) @@ -90,7 +90,7 @@ 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, + cr alter_locality_test.public.crdb_internal_region NOT NULL DEFAULT 'ap-southeast-2':::alter_locality_test.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) @@ -595,7 +595,7 @@ 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, + crdb_region alter_locality_test.public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::alter_locality_test.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), @@ -702,7 +702,7 @@ 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, + crdb_region alter_locality_test.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), @@ -752,7 +752,7 @@ 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, + crdb_region alter_locality_test.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), @@ -805,7 +805,7 @@ 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, + cr alter_locality_test.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), @@ -1356,7 +1356,7 @@ 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, + crdb_region alter_locality_test.public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::alter_locality_test.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), @@ -1445,7 +1445,7 @@ regional_by_table_no_region CREATE TABLE public.regional_by_table_no_regio pk INT8 NOT NULL, i INT8 NULL, b INT8 NULL, - cr public.crdb_internal_region NOT NULL, + cr alter_locality_test.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), @@ -1721,7 +1721,7 @@ 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, + crdb_region alter_locality_test.public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::alter_locality_test.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), @@ -1793,7 +1793,7 @@ regional_by_table_in_us_east CREATE TABLE public.regional_by_table_in_us_ea pk INT8 NOT NULL, i INT8 NULL, b INT8 NULL, - cr public.crdb_internal_region NOT NULL, + cr alter_locality_test.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), @@ -1848,7 +1848,7 @@ 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, + crdb_region alter_locality_test.public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::alter_locality_test.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) @@ -1895,7 +1895,7 @@ 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, + crdb_region alter_locality_test.public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::alter_locality_test.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) @@ -1942,7 +1942,7 @@ 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, + crdb_region alter_locality_test.public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::alter_locality_test.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) @@ -1989,7 +1989,7 @@ 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, + crdb_region alter_locality_test.public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::alter_locality_test.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) @@ -2037,7 +2037,7 @@ 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, + crdb_region alter_locality_test.public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::alter_locality_test.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) @@ -2088,7 +2088,7 @@ 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, + crdb_region alter_locality_test.public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::alter_locality_test.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) @@ -2140,7 +2140,7 @@ 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, + crdb_region alter_locality_test.public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::alter_locality_test.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) @@ -2203,8 +2203,8 @@ 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, + cr alter_locality_test.public.crdb_internal_region NOT NULL DEFAULT 'ap-southeast-2':::alter_locality_test.public.crdb_internal_region, + crdb_region alter_locality_test.public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::alter_locality_test.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) @@ -2258,7 +2258,7 @@ 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, + cr alter_locality_test.public.crdb_internal_region NOT NULL DEFAULT 'ap-southeast-2':::alter_locality_test.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) @@ -2324,7 +2324,7 @@ 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, + cr alter_locality_test.public.crdb_internal_region NOT NULL DEFAULT 'ap-southeast-2':::alter_locality_test.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) @@ -2390,7 +2390,7 @@ 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, + cr alter_locality_test.public.crdb_internal_region NOT NULL DEFAULT 'ap-southeast-2':::alter_locality_test.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) @@ -2456,7 +2456,7 @@ 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, + cr alter_locality_test.public.crdb_internal_region NOT NULL DEFAULT 'ap-southeast-2':::alter_locality_test.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) @@ -2518,8 +2518,8 @@ 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, + cr alter_locality_test.public.crdb_internal_region NOT NULL DEFAULT 'ap-southeast-2':::alter_locality_test.public.crdb_internal_region, + crdb_region alter_locality_test.public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::alter_locality_test.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) @@ -2581,7 +2581,7 @@ 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, + cr alter_locality_test.public.crdb_internal_region NOT NULL DEFAULT 'ap-southeast-2':::alter_locality_test.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) diff --git a/pkg/ccl/logictestccl/testdata/logic_test/auto_rehoming b/pkg/ccl/logictestccl/testdata/logic_test/auto_rehoming index 581d983896dd..587d838b3023 100644 --- a/pkg/ccl/logictestccl/testdata/logic_test/auto_rehoming +++ b/pkg/ccl/logictestccl/testdata/logic_test/auto_rehoming @@ -22,7 +22,7 @@ SHOW CREATE TABLE db.rbr db.public.rbr CREATE TABLE public.rbr ( p INT8 NOT NULL, s STRING NULL, - crdb_region public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::public.crdb_internal_region ON UPDATE rehome_row()::public.crdb_internal_region, + crdb_region db.public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::db.public.crdb_internal_region ON UPDATE rehome_row()::db.public.crdb_internal_region, CONSTRAINT rbr_pkey PRIMARY KEY (p ASC) ) LOCALITY REGIONAL BY ROW @@ -38,6 +38,6 @@ SHOW CREATE TABLE db.rbr_altered ---- db.public.rbr_altered CREATE TABLE public.rbr_altered ( p 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 ON UPDATE rehome_row()::public.crdb_internal_region, + crdb_region db.public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::db.public.crdb_internal_region ON UPDATE rehome_row()::db.public.crdb_internal_region, CONSTRAINT rbr_altered_pkey PRIMARY KEY (p ASC) ) LOCALITY REGIONAL BY ROW diff --git a/pkg/ccl/logictestccl/testdata/logic_test/multi_region b/pkg/ccl/logictestccl/testdata/logic_test/multi_region index 6598a2a1a23d..366c28f7bced 100644 --- a/pkg/ccl/logictestccl/testdata/logic_test/multi_region +++ b/pkg/ccl/logictestccl/testdata/logic_test/multi_region @@ -1485,7 +1485,7 @@ 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, + crdb_region mr-create-table-as.public.crdb_internal_region NULL, rowid INT8 NOT VISIBLE NOT NULL DEFAULT unique_rowid(), CONSTRAINT t_as_pkey PRIMARY KEY (rowid ASC) ) LOCALITY REGIONAL BY TABLE IN PRIMARY REGION @@ -1512,7 +1512,7 @@ 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, + crdb_region mr-create-table-as.public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::mr-create-table-as.public.crdb_internal_region, rowid INT8 NOT VISIBLE NOT NULL DEFAULT unique_rowid(), CONSTRAINT t_as_pkey PRIMARY KEY (rowid ASC) ) LOCALITY REGIONAL BY ROW AS crdb_region @@ -1555,7 +1555,7 @@ 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, + crdb_region mr-create-table-as.public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::mr-create-table-as.public.crdb_internal_region, CONSTRAINT t_as_pkey PRIMARY KEY (i ASC) ) LOCALITY REGIONAL BY ROW AS crdb_region @@ -1565,7 +1565,7 @@ 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, + crdb_region mr-create-table-as.public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::mr-create-table-as.public.crdb_internal_region, CONSTRAINT t_pkey PRIMARY KEY (i ASC) ) LOCALITY REGIONAL BY ROW diff --git a/pkg/ccl/logictestccl/testdata/logic_test/multi_region_backup b/pkg/ccl/logictestccl/testdata/logic_test/multi_region_backup index 54a6f89634b9..762ce1fdb149 100644 --- a/pkg/ccl/logictestccl/testdata/logic_test/multi_region_backup +++ b/pkg/ccl/logictestccl/testdata/logic_test/multi_region_backup @@ -129,7 +129,7 @@ regional_by_row_table CREATE TABLE public.regional_by_row_table ( a INT8 NOT NULL, b INT8 NOT NULL, j JSONB NULL, - crdb_region public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::public.crdb_internal_region, + crdb_region mr-backup-1.public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::mr-backup-1.public.crdb_internal_region, CONSTRAINT regional_by_row_table_pkey PRIMARY KEY (pk ASC), INDEX regional_by_row_table_a_idx (a ASC), UNIQUE INDEX regional_by_row_table_b_key (b ASC), @@ -299,7 +299,7 @@ regional_by_row_table CREATE TABLE public.regional_by_row_table ( a INT8 NOT NULL, b INT8 NOT NULL, j JSONB NULL, - crdb_region public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::public.crdb_internal_region, + crdb_region mr-backup-2.public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::mr-backup-2.public.crdb_internal_region, CONSTRAINT regional_by_row_table_pkey PRIMARY KEY (pk ASC), INDEX regional_by_row_table_a_idx (a ASC), UNIQUE INDEX regional_by_row_table_b_key (b ASC), @@ -517,7 +517,7 @@ regional_by_row_table CREATE TABLE public.regional_by_row_table ( a INT8 NOT NULL, b INT8 NOT NULL, j JSONB NULL, - crdb_region public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::public.crdb_internal_region, + crdb_region mr-backup-1.public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::mr-backup-1.public.crdb_internal_region, CONSTRAINT regional_by_row_table_pkey PRIMARY KEY (pk ASC), INDEX regional_by_row_table_a_idx (a ASC), UNIQUE INDEX regional_by_row_table_b_key (b ASC), @@ -672,7 +672,7 @@ regional_by_row_table CREATE TABLE public.regional_by_row_table ( a INT8 NOT NULL, b INT8 NOT NULL, j JSONB NULL, - crdb_region public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::public.crdb_internal_region, + crdb_region mr-backup-2.public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::mr-backup-2.public.crdb_internal_region, CONSTRAINT regional_by_row_table_pkey PRIMARY KEY (pk ASC), INDEX regional_by_row_table_a_idx (a ASC), UNIQUE INDEX regional_by_row_table_b_key (b ASC), @@ -854,7 +854,7 @@ regional_by_row_table CREATE TABLE public.regional_by_row_table ( a INT8 NOT NULL, b INT8 NOT NULL, j JSONB NULL, - crdb_region public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::public.crdb_internal_region, + crdb_region mr-backup-1.public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::mr-backup-1.public.crdb_internal_region, CONSTRAINT regional_by_row_table_pkey PRIMARY KEY (pk ASC), INDEX regional_by_row_table_a_idx (a ASC), UNIQUE INDEX regional_by_row_table_b_key (b ASC), @@ -983,7 +983,7 @@ regional_by_row_table CREATE TABLE public.regional_by_row_table ( a INT8 NOT NULL, b INT8 NOT NULL, j JSONB NULL, - crdb_region public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::public.crdb_internal_region, + crdb_region mr-backup-2.public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::mr-backup-2.public.crdb_internal_region, CONSTRAINT regional_by_row_table_pkey PRIMARY KEY (pk ASC), INDEX regional_by_row_table_a_idx (a ASC), UNIQUE INDEX regional_by_row_table_b_key (b ASC), @@ -1116,7 +1116,7 @@ regional_by_row_table CREATE TABLE public.regional_by_row_table ( a INT8 NOT NULL, b INT8 NOT NULL, j JSONB NULL, - crdb_region public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::public.crdb_internal_region, + crdb_region mr-backup-2.public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::mr-backup-2.public.crdb_internal_region, CONSTRAINT regional_by_row_table_pkey PRIMARY KEY (pk ASC), INDEX regional_by_row_table_a_idx (a ASC), UNIQUE INDEX regional_by_row_table_b_key (b ASC), @@ -1279,7 +1279,7 @@ regional_by_row_table CREATE TABLE public.regional_by_row_table ( a INT8 NOT NULL, b INT8 NOT NULL, j JSONB NULL, - crdb_region public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::public.crdb_internal_region, + crdb_region mr-backup-2.public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::mr-backup-2.public.crdb_internal_region, CONSTRAINT regional_by_row_table_pkey PRIMARY KEY (pk ASC), INDEX regional_by_row_table_a_idx (a ASC), UNIQUE INDEX regional_by_row_table_b_key (b ASC), diff --git a/pkg/ccl/logictestccl/testdata/logic_test/multi_region_zone_configs b/pkg/ccl/logictestccl/testdata/logic_test/multi_region_zone_configs index 13e9e14b92cf..bd86f403812e 100644 --- a/pkg/ccl/logictestccl/testdata/logic_test/multi_region_zone_configs +++ b/pkg/ccl/logictestccl/testdata/logic_test/multi_region_zone_configs @@ -702,8 +702,8 @@ 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 'ca-central-1':::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, + cr mr-zone-configs.public.crdb_internal_region NOT NULL DEFAULT 'ca-central-1':::mr-zone-configs.public.crdb_internal_region, + crdb_region mr-zone-configs.public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::mr-zone-configs.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, crdb_region) diff --git a/pkg/ccl/logictestccl/testdata/logic_test/regional_by_row b/pkg/ccl/logictestccl/testdata/logic_test/regional_by_row index a048a6eae536..e1ba1411cf76 100644 --- a/pkg/ccl/logictestccl/testdata/logic_test/regional_by_row +++ b/pkg/ccl/logictestccl/testdata/logic_test/regional_by_row @@ -111,7 +111,7 @@ SELECT create_statement FROM [SHOW CREATE TABLE regional_by_row_table_explicit_c CREATE TABLE public.regional_by_row_table_explicit_crdb_region_column ( pk INT8 NOT NULL, a INT8 NULL, - crdb_region public.crdb_internal_region NOT NULL, + crdb_region multi_region_test_db.public.crdb_internal_region NOT NULL, CONSTRAINT regional_by_row_table_explicit_crdb_region_column_pkey PRIMARY KEY (pk ASC), FAMILY fam_0_pk_a_crdb_region (pk, a, crdb_region) ) LOCALITY REGIONAL BY ROW @@ -185,7 +185,7 @@ CREATE TABLE public.regional_by_row_table ( a INT8 NOT NULL, b INT8 NOT NULL, j JSONB NULL, - crdb_region public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::public.crdb_internal_region, + crdb_region multi_region_test_db.public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::multi_region_test_db.public.crdb_internal_region, CONSTRAINT regional_by_row_table_pkey PRIMARY KEY (pk ASC), INDEX regional_by_row_table_a_idx (a ASC), UNIQUE INDEX regional_by_row_table_b_key (b ASC), @@ -284,7 +284,7 @@ CREATE TABLE public.regional_by_row_table ( a INT8 NOT NULL, b INT8 NOT NULL, j JSONB NULL, - crdb_region public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::public.crdb_internal_region, + crdb_region multi_region_test_db.public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::multi_region_test_db.public.crdb_internal_region, CONSTRAINT regional_by_row_table_pkey PRIMARY KEY (pk ASC), INDEX regional_by_row_table_a_idx (a ASC), UNIQUE INDEX regional_by_row_table_b_key (b ASC), @@ -326,7 +326,7 @@ CREATE TABLE public.regional_by_row_table ( a INT8 NOT NULL, b INT8 NOT NULL, j JSONB NULL, - crdb_region public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::public.crdb_internal_region, + crdb_region multi_region_test_db.public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::multi_region_test_db.public.crdb_internal_region, unique_col INT8 NOT NULL, CONSTRAINT regional_by_row_table_pkey PRIMARY KEY (pk ASC), INDEX regional_by_row_table_a_idx (a ASC), @@ -531,7 +531,7 @@ CREATE TABLE public.regional_by_row_table ( a INT8 NOT NULL, b INT8 NOT NULL, j JSONB NULL, - crdb_region public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::public.crdb_internal_region, + crdb_region multi_region_test_db.public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::multi_region_test_db.public.crdb_internal_region, CONSTRAINT regional_by_row_table_pkey PRIMARY KEY (pk2 ASC), UNIQUE INDEX regional_by_row_table_pk_key (pk ASC), INDEX regional_by_row_table_a_idx (a ASC), @@ -593,7 +593,7 @@ SELECT create_statement FROM [SHOW CREATE TABLE regional_by_row_table_pk_defined ---- CREATE TABLE public.regional_by_row_table_pk_defined_separately ( pk 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, + crdb_region multi_region_test_db.public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::multi_region_test_db.public.crdb_internal_region, CONSTRAINT "primary" PRIMARY KEY (pk ASC) ) LOCALITY REGIONAL BY ROW @@ -632,7 +632,7 @@ CREATE TABLE public.regional_by_row_table_as ( pk INT8 NOT NULL, a INT8 NULL, b INT8 NULL, - crdb_region_col public.crdb_internal_region NOT NULL AS (CASE WHEN pk <= 10:::INT8 THEN 'us-east-1':::public.crdb_internal_region ELSE 'ap-southeast-2':::public.crdb_internal_region END) STORED, + crdb_region_col multi_region_test_db.public.crdb_internal_region NOT NULL AS (CASE WHEN pk <= 10:::INT8 THEN 'us-east-1':::multi_region_test_db.public.crdb_internal_region ELSE 'ap-southeast-2':::multi_region_test_db.public.crdb_internal_region END) STORED, CONSTRAINT regional_by_row_table_as_pkey PRIMARY KEY (pk ASC), INDEX regional_by_row_table_as_a_idx (a ASC), UNIQUE INDEX regional_by_row_table_as_b_key (b ASC), @@ -905,7 +905,7 @@ 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, + crdb_region add_regions.public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::add_regions.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) @@ -941,7 +941,7 @@ 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 'ca-central-1':::public.crdb_internal_region, + cr add_regions.public.crdb_internal_region NOT NULL DEFAULT 'ca-central-1':::add_regions.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) @@ -985,7 +985,7 @@ 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, + crdb_region add_regions.public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::add_regions.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) @@ -1027,7 +1027,7 @@ 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 'ca-central-1':::public.crdb_internal_region, + cr add_regions.public.crdb_internal_region NOT NULL DEFAULT 'ca-central-1':::add_regions.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) @@ -1074,7 +1074,7 @@ 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, + crdb_region add_regions.public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::add_regions.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) @@ -1123,7 +1123,7 @@ 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 'ca-central-1':::public.crdb_internal_region, + cr add_regions.public.crdb_internal_region NOT NULL DEFAULT 'ca-central-1':::add_regions.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) @@ -1201,7 +1201,7 @@ 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, + crdb_region add_regions_in_txn.public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::add_regions_in_txn.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) @@ -1252,7 +1252,7 @@ SHOW CREATE TABLE regional_by_row_like regional_by_row_like CREATE TABLE public.regional_by_row_like ( pk INT8 NOT NULL, i INT8 NULL, - crdb_region public.crdb_internal_region NOT VISIBLE NOT NULL, + crdb_region add_regions_in_txn.public.crdb_internal_region NOT VISIBLE NOT NULL, rowid INT8 NOT VISIBLE NOT NULL DEFAULT unique_rowid(), CONSTRAINT regional_by_row_like_pkey PRIMARY KEY (rowid ASC) ) LOCALITY REGIONAL BY TABLE IN PRIMARY REGION @@ -1269,7 +1269,7 @@ SHOW CREATE TABLE regional_by_row_like regional_by_row_like CREATE TABLE public.regional_by_row_like ( 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, + crdb_region add_regions_in_txn.public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::add_regions_in_txn.public.crdb_internal_region, CONSTRAINT regional_by_row_pkey PRIMARY KEY (crdb_region ASC, pk ASC), INDEX regional_by_row_i_idx (crdb_region ASC, i ASC) ) LOCALITY REGIONAL BY TABLE IN PRIMARY REGION @@ -1287,7 +1287,7 @@ SHOW CREATE TABLE regional_by_row_like regional_by_row_like CREATE TABLE public.regional_by_row_like ( 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, + crdb_region add_regions_in_txn.public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::add_regions_in_txn.public.crdb_internal_region, CONSTRAINT regional_by_row_pkey PRIMARY KEY (crdb_region ASC, pk ASC), INDEX regional_by_row_i_idx (crdb_region ASC, i ASC) ) LOCALITY REGIONAL BY TABLE IN PRIMARY REGION @@ -1301,7 +1301,7 @@ 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 'ca-central-1':::public.crdb_internal_region, + cr add_regions_in_txn.public.crdb_internal_region NOT NULL DEFAULT 'ca-central-1':::add_regions_in_txn.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) @@ -1363,7 +1363,7 @@ regional_by_row_unique_in_column CREATE TABLE public.regional_by_row_unique_in_ c INT8 NULL, d INT8 NULL, e INT8 NULL, - crdb_region public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::public.crdb_internal_region, + crdb_region add_regions_in_txn.public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::add_regions_in_txn.public.crdb_internal_region, CONSTRAINT regional_by_row_unique_in_column_pkey PRIMARY KEY (a ASC), UNIQUE INDEX regional_by_row_unique_in_column_b_key (b ASC), UNIQUE INDEX regional_by_row_unique_in_column_d_e_key (d ASC, e ASC), @@ -1390,7 +1390,7 @@ regional_by_row_fk CREATE TABLE public.regional_by_row_fk ( h INT8 NULL, i INT8 NULL, j INT8 NULL, - crdb_region public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::public.crdb_internal_region, + crdb_region add_regions_in_txn.public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::add_regions_in_txn.public.crdb_internal_region, CONSTRAINT regional_by_row_fk_pkey PRIMARY KEY (f ASC), CONSTRAINT ij_fk FOREIGN KEY (i, j) REFERENCES public.regional_by_row_unique_in_column(e, d), CONSTRAINT regional_by_row_fk_g_fkey FOREIGN KEY (g) REFERENCES public.regional_by_row_unique_in_column(a), @@ -1462,7 +1462,7 @@ 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, + crdb_region drop_regions.public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::drop_regions.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) @@ -1504,7 +1504,7 @@ 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 'ca-central-1':::public.crdb_internal_region, + cr drop_regions.public.crdb_internal_region NOT NULL DEFAULT 'ca-central-1':::drop_regions.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) @@ -1553,7 +1553,7 @@ 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, + crdb_region drop_regions.public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::drop_regions.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) @@ -1595,7 +1595,7 @@ 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 'ca-central-1':::public.crdb_internal_region, + cr drop_regions.public.crdb_internal_region NOT NULL DEFAULT 'ca-central-1':::drop_regions.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) diff --git a/pkg/ccl/logictestccl/testdata/logic_test/regional_by_row_hash_sharded_index b/pkg/ccl/logictestccl/testdata/logic_test/regional_by_row_hash_sharded_index index 49c9e1132b5b..60659e007f71 100644 --- a/pkg/ccl/logictestccl/testdata/logic_test/regional_by_row_hash_sharded_index +++ b/pkg/ccl/logictestccl/testdata/logic_test/regional_by_row_hash_sharded_index @@ -48,7 +48,7 @@ SELECT @2 FROM [SHOW CREATE TABLE t_to_be_hashed]; CREATE TABLE public.t_to_be_hashed ( pk INT8 NOT 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, + crdb_region multi_region_test_db.public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::multi_region_test_db.public.crdb_internal_region, CONSTRAINT t_to_be_hashed_pkey PRIMARY KEY (pk ASC), FAMILY fam_0_b_pk_crdb_region (b, pk, crdb_region) ) LOCALITY REGIONAL BY ROW @@ -62,7 +62,7 @@ SELECT @2 FROM [SHOW CREATE TABLE t_to_be_hashed]; CREATE TABLE public.t_to_be_hashed ( pk INT8 NOT 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, + crdb_region multi_region_test_db.public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::multi_region_test_db.public.crdb_internal_region, crdb_internal_b_shard_16 INT8 NOT VISIBLE NOT NULL AS (mod(fnv32(crdb_internal.datums_to_bytes(b)), 16:::INT8)) VIRTUAL, CONSTRAINT t_to_be_hashed_pkey PRIMARY KEY (pk ASC), INDEX t_to_be_hashed_b_idx (b ASC) USING HASH WITH (bucket_count=16), @@ -78,7 +78,7 @@ SELECT @2 FROM [SHOW CREATE TABLE t_to_be_hashed]; CREATE TABLE public.t_to_be_hashed ( pk INT8 NOT 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, + crdb_region multi_region_test_db.public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::multi_region_test_db.public.crdb_internal_region, crdb_internal_b_shard_16 INT8 NOT VISIBLE NOT NULL AS (mod(fnv32(crdb_internal.datums_to_bytes(b)), 16:::INT8)) VIRTUAL, CONSTRAINT t_to_be_hashed_pkey PRIMARY KEY (pk ASC), INDEX t_to_be_hashed_b_idx (b ASC) USING HASH WITH (bucket_count=16), @@ -95,7 +95,7 @@ SELECT @2 FROM [SHOW CREATE TABLE t_to_be_hashed]; CREATE TABLE public.t_to_be_hashed ( pk INT8 NOT 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, + crdb_region multi_region_test_db.public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::multi_region_test_db.public.crdb_internal_region, crdb_internal_b_shard_16 INT8 NOT VISIBLE NOT NULL AS (mod(fnv32(crdb_internal.datums_to_bytes(b)), 16:::INT8)) VIRTUAL, crdb_internal_pk_shard_16 INT8 NOT VISIBLE NOT NULL AS (mod(fnv32(crdb_internal.datums_to_bytes(pk)), 16:::INT8)) VIRTUAL, CONSTRAINT t_to_be_hashed_pkey PRIMARY KEY (pk ASC) USING HASH WITH (bucket_count=16), @@ -119,7 +119,7 @@ CREATE TABLE public.t_regional_pk_hashed_1 ( crdb_internal_pk_shard_16 INT8 NOT VISIBLE NOT NULL AS (mod(fnv32(crdb_internal.datums_to_bytes(pk)), 16:::INT8)) VIRTUAL, pk INT8 NOT 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, + crdb_region multi_region_test_db.public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::multi_region_test_db.public.crdb_internal_region, crdb_internal_b_shard_16 INT8 NOT VISIBLE NOT NULL AS (mod(fnv32(crdb_internal.datums_to_bytes(b)), 16:::INT8)) VIRTUAL, CONSTRAINT t_regional_pk_hashed_1_pkey PRIMARY KEY (pk ASC) USING HASH WITH (bucket_count=16), INDEX t_regional_pk_hashed_1_b_idx (b ASC) USING HASH WITH (bucket_count=16), @@ -141,7 +141,7 @@ SELECT @2 FROM [SHOW CREATE TABLE t_regional_pk_hashed_2]; CREATE TABLE public.t_regional_pk_hashed_2 ( pk INT8 NOT 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, + crdb_region multi_region_test_db.public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::multi_region_test_db.public.crdb_internal_region, crdb_internal_pk_shard_16 INT8 NOT VISIBLE NOT NULL AS (mod(fnv32(crdb_internal.datums_to_bytes(pk)), 16:::INT8)) VIRTUAL, crdb_internal_b_shard_16 INT8 NOT VISIBLE NOT NULL AS (mod(fnv32(crdb_internal.datums_to_bytes(b)), 16:::INT8)) VIRTUAL, CONSTRAINT t_regional_pk_hashed_2_pkey PRIMARY KEY (pk ASC) USING HASH WITH (bucket_count=16), diff --git a/pkg/ccl/logictestccl/testdata/logic_test/regional_by_row_query_behavior b/pkg/ccl/logictestccl/testdata/logic_test/regional_by_row_query_behavior index 146bada1f9ae..286e1a93e31c 100644 --- a/pkg/ccl/logictestccl/testdata/logic_test/regional_by_row_query_behavior +++ b/pkg/ccl/logictestccl/testdata/logic_test/regional_by_row_query_behavior @@ -1822,7 +1822,7 @@ CREATE TABLE public.regional_by_row_table ( a INT8 NOT NULL, b INT8 NOT NULL, j JSONB NULL, - crdb_region public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::public.crdb_internal_region, + crdb_region multi_region_test_db.public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::multi_region_test_db.public.crdb_internal_region, CONSTRAINT regional_by_row_table_pkey PRIMARY KEY (pk ASC), INDEX regional_by_row_table_a_idx (a ASC), UNIQUE INDEX regional_by_row_table_b_key (b ASC), diff --git a/pkg/ccl/logictestccl/testdata/logic_test/regional_by_row_rename_column b/pkg/ccl/logictestccl/testdata/logic_test/regional_by_row_rename_column index de3e2c86de59..0c900f424877 100644 --- a/pkg/ccl/logictestccl/testdata/logic_test/regional_by_row_rename_column +++ b/pkg/ccl/logictestccl/testdata/logic_test/regional_by_row_rename_column @@ -49,7 +49,7 @@ CREATE TABLE public.regional_by_row_table ( a INT8 NOT NULL, b INT8 NOT NULL, j JSONB NULL, - crdb_region2 public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::public.crdb_internal_region, + crdb_region2 multi_region_test_db.public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::multi_region_test_db.public.crdb_internal_region, CONSTRAINT "primary" PRIMARY KEY (pk2 ASC), UNIQUE INDEX regional_by_row_table_pk_key (pk ASC), INDEX regional_by_row_table_a_idx (a ASC), @@ -87,7 +87,7 @@ CREATE TABLE public.regional_by_row_table_as ( pk INT8 NOT NULL, a INT8 NULL, b INT8 NULL, - cr public.crdb_internal_region NOT NULL AS (CASE WHEN pk <= 10:::INT8 THEN 'us-east-1':::public.crdb_internal_region ELSE 'ap-southeast-2':::public.crdb_internal_region END) STORED, + cr multi_region_test_db.public.crdb_internal_region NOT NULL AS (CASE WHEN pk <= 10:::INT8 THEN 'us-east-1':::multi_region_test_db.public.crdb_internal_region ELSE 'ap-southeast-2':::multi_region_test_db.public.crdb_internal_region END) STORED, CONSTRAINT regional_by_row_table_as_pkey PRIMARY KEY (pk ASC), INDEX regional_by_row_table_as_a_idx (a ASC), UNIQUE INDEX regional_by_row_table_as_b_key (b ASC), diff --git a/pkg/ccl/multiregionccl/show_test.go b/pkg/ccl/multiregionccl/show_test.go index 748cce0c04a6..79663a80240e 100644 --- a/pkg/ccl/multiregionccl/show_test.go +++ b/pkg/ccl/multiregionccl/show_test.go @@ -66,7 +66,7 @@ func TestShowCreateTable(t *testing.T) { ) LOCALITY REGIONAL BY ROW`, Expect: `CREATE TABLE public.%[1]s ( a INT8 NULL, - crdb_region public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::public.crdb_internal_region, + crdb_region mrdb.public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::mrdb.public.crdb_internal_region, rowid INT8 NOT VISIBLE NOT NULL DEFAULT unique_rowid(), CONSTRAINT %[1]s_pkey PRIMARY KEY (rowid ASC), INDEX a_idx (a ASC) @@ -81,7 +81,7 @@ func TestShowCreateTable(t *testing.T) { ) LOCALITY REGIONAL BY ROW AS crdb_region_col`, Expect: `CREATE TABLE public.%[1]s ( a INT8 NULL, - crdb_region_col public.crdb_internal_region NOT NULL, + crdb_region_col mrdb.public.crdb_internal_region NOT NULL, rowid INT8 NOT VISIBLE NOT NULL DEFAULT unique_rowid(), CONSTRAINT %[1]s_pkey PRIMARY KEY (rowid ASC), INDEX a_idx (a ASC) @@ -96,7 +96,7 @@ func TestShowCreateTable(t *testing.T) { ) LOCALITY REGIONAL BY ROW AS crdb_region_col`, Expect: `CREATE TABLE public.%[1]s ( a INT8 NULL, - crdb_region_col public.crdb_internal_region NOT NULL, + crdb_region_col mrdb.public.crdb_internal_region NOT NULL, rowid INT8 NOT VISIBLE NOT NULL DEFAULT unique_rowid(), CONSTRAINT %[1]s_pkey PRIMARY KEY (rowid ASC), INDEX a_idx (a ASC) WHERE a > 0:::INT8 diff --git a/pkg/sql/create_view.go b/pkg/sql/create_view.go index cbac20125e79..70a2eff957e8 100644 --- a/pkg/sql/create_view.go +++ b/pkg/sql/create_view.go @@ -182,171 +182,177 @@ func (n *createViewNode) startExec(params runParams) error { var newDesc *tabledesc.Mutable applyGlobalMultiRegionZoneConfig := false - // If replacingDesc != nil, we found an existing view while resolving - // the name for our view. So instead of creating a new view, replace - // the existing one. - if replacingDesc != nil { - newDesc, err = params.p.replaceViewDesc( - params.ctx, - params.p, - n, - replacingDesc, - backRefMutables, - ) - if err != nil { - return err - } - } else { - // If we aren't replacing anything, make a new table descriptor. - id, err := descidgen.GenerateUniqueDescID(params.ctx, params.p.ExecCfg().DB, params.p.ExecCfg().Codec) - if err != nil { - return err - } - // creationTime is initialized to a zero value and populated at read time. - // See the comment in desc.MaybeIncrementVersion. - // - // TODO(ajwerner): remove the timestamp from MakeViewTableDesc, it's - // currently relied on in import and restore code and tests. - var creationTime hlc.Timestamp - desc, err := makeViewTableDesc( - params.ctx, - viewName, - n.viewQuery, - n.dbDesc.GetID(), - schema.GetID(), - id, - n.columns, - creationTime, - privs, - ¶ms.p.semaCtx, - params.p.EvalContext(), - params.p.EvalContext().Settings, - n.persistence, - n.dbDesc.IsMultiRegion(), - params.p) - if err != nil { - return err - } - - if n.materialized { - // If the view is materialized, set up some more state on the view descriptor. - // In particular, - // * mark the descriptor as a materialized view - // * mark the state as adding and remember the AsOf time to perform - // the view query - // * use AllocateIDs to give the view descriptor a primary key - desc.IsMaterializedView = true - desc.State = descpb.DescriptorState_ADD - version := params.ExecCfg().Settings.Version.ActiveVersion(params.ctx) - if err := desc.AllocateIDs(params.ctx, version); err != nil { - return err - } - // For multi-region databases, we want this descriptor to be GLOBAL instead. - if n.dbDesc.IsMultiRegion() { - desc.SetTableLocalityGlobal() - applyGlobalMultiRegionZoneConfig = true + var retErr error + params.p.runWithOptions(resolveFlags{contextDatabaseID: n.dbDesc.GetID()}, func() { + retErr = func() error { + // If replacingDesc != nil, we found an existing view while resolving + // the name for our view. So instead of creating a new view, replace + // the existing one. + if replacingDesc != nil { + newDesc, err = params.p.replaceViewDesc( + params.ctx, + params.p, + n, + replacingDesc, + backRefMutables, + ) + if err != nil { + return err + } + } else { + // If we aren't replacing anything, make a new table descriptor. + id, err := descidgen.GenerateUniqueDescID(params.ctx, params.p.ExecCfg().DB, params.p.ExecCfg().Codec) + if err != nil { + return err + } + // creationTime is initialized to a zero value and populated at read time. + // See the comment in desc.MaybeIncrementVersion. + // + // TODO(ajwerner): remove the timestamp from MakeViewTableDesc, it's + // currently relied on in import and restore code and tests. + var creationTime hlc.Timestamp + desc, err := makeViewTableDesc( + params.ctx, + viewName, + n.viewQuery, + n.dbDesc.GetID(), + schema.GetID(), + id, + n.columns, + creationTime, + privs, + ¶ms.p.semaCtx, + params.p.EvalContext(), + params.p.EvalContext().Settings, + n.persistence, + n.dbDesc.IsMultiRegion(), + params.p) + if err != nil { + return err + } + + if n.materialized { + // If the view is materialized, set up some more state on the view descriptor. + // In particular, + // * mark the descriptor as a materialized view + // * mark the state as adding and remember the AsOf time to perform + // the view query + // * use AllocateIDs to give the view descriptor a primary key + desc.IsMaterializedView = true + desc.State = descpb.DescriptorState_ADD + version := params.ExecCfg().Settings.Version.ActiveVersion(params.ctx) + if err := desc.AllocateIDs(params.ctx, version); err != nil { + return err + } + // For multi-region databases, we want this descriptor to be GLOBAL instead. + if n.dbDesc.IsMultiRegion() { + desc.SetTableLocalityGlobal() + applyGlobalMultiRegionZoneConfig = true + } + } + + // Collect all the tables/views this view depends on. + orderedDependsOn := catalog.DescriptorIDSet{} + for backrefID := range n.planDeps { + orderedDependsOn.Add(backrefID) + } + desc.DependsOn = append(desc.DependsOn, orderedDependsOn.Ordered()...) + + // Collect all types this view depends on. + orderedTypeDeps := catalog.DescriptorIDSet{} + for backrefID := range n.typeDeps { + orderedTypeDeps.Add(backrefID) + } + desc.DependsOnTypes = append(desc.DependsOnTypes, orderedTypeDeps.Ordered()...) + + // TODO (lucy): I think this needs a NodeFormatter implementation. For now, + // do some basic string formatting (not accurate in the general case). + if err = params.p.createDescriptorWithID( + params.ctx, + catalogkeys.MakeObjectNameKey(params.ExecCfg().Codec, n.dbDesc.GetID(), schema.GetID(), n.viewName.Table()), + id, + &desc, + fmt.Sprintf("CREATE VIEW %q AS %q", n.viewName, n.viewQuery), + ); err != nil { + return err + } + newDesc = &desc } - } - // Collect all the tables/views this view depends on. - orderedDependsOn := catalog.DescriptorIDSet{} - for backrefID := range n.planDeps { - orderedDependsOn.Add(backrefID) - } - desc.DependsOn = append(desc.DependsOn, orderedDependsOn.Ordered()...) - - // Collect all types this view depends on. - orderedTypeDeps := catalog.DescriptorIDSet{} - for backrefID := range n.typeDeps { - orderedTypeDeps.Add(backrefID) - } - desc.DependsOnTypes = append(desc.DependsOnTypes, orderedTypeDeps.Ordered()...) - - // TODO (lucy): I think this needs a NodeFormatter implementation. For now, - // do some basic string formatting (not accurate in the general case). - if err = params.p.createDescriptorWithID( - params.ctx, - catalogkeys.MakeObjectNameKey(params.ExecCfg().Codec, n.dbDesc.GetID(), schema.GetID(), n.viewName.Table()), - id, - &desc, - fmt.Sprintf("CREATE VIEW %q AS %q", n.viewName, n.viewQuery), - ); err != nil { - return err - } - newDesc = &desc - } - - // Persist the back-references in all referenced table descriptors. - for id, updated := range n.planDeps { - backRefMutable := backRefMutables[id] - // In case that we are replacing a view that already depends on - // this table, remove all existing references so that we don't leave - // any out of date references. Then, add the new references. - backRefMutable.DependedOnBy = removeMatchingReferences( - backRefMutable.DependedOnBy, - newDesc.ID, - ) - for _, dep := range updated.deps { - // The logical plan constructor merely registered the dependencies. - // It did not populate the "ID" field of TableDescriptor_Reference, - // because the ID of the newly created view descriptor was not - // yet known. - // We need to do it here. - dep.ID = newDesc.ID - dep.ByID = updated.desc.IsSequence() - backRefMutable.DependedOnBy = append(backRefMutable.DependedOnBy, dep) - } - if err := params.p.writeSchemaChange( - params.ctx, - backRefMutable, - descpb.InvalidMutationID, - fmt.Sprintf("updating view reference %q in table %s(%d)", n.viewName, - updated.desc.GetName(), updated.desc.GetID(), - ), - ); err != nil { - return err - } - } + // Persist the back-references in all referenced table descriptors. + for id, updated := range n.planDeps { + backRefMutable := backRefMutables[id] + // In case that we are replacing a view that already depends on + // this table, remove all existing references so that we don't leave + // any out of date references. Then, add the new references. + backRefMutable.DependedOnBy = removeMatchingReferences( + backRefMutable.DependedOnBy, + newDesc.ID, + ) + for _, dep := range updated.deps { + // The logical plan constructor merely registered the dependencies. + // It did not populate the "ID" field of TableDescriptor_Reference, + // because the ID of the newly created view descriptor was not + // yet known. + // We need to do it here. + dep.ID = newDesc.ID + dep.ByID = updated.desc.IsSequence() + backRefMutable.DependedOnBy = append(backRefMutable.DependedOnBy, dep) + } + if err := params.p.writeSchemaChange( + params.ctx, + backRefMutable, + descpb.InvalidMutationID, + fmt.Sprintf("updating view reference %q in table %s(%d)", n.viewName, + updated.desc.GetName(), updated.desc.GetID(), + ), + ); err != nil { + return err + } + } - // Add back references for the type dependencies. - for id := range n.typeDeps { - jobDesc := fmt.Sprintf("updating type back reference %d for table %d", id, newDesc.ID) - if err := params.p.addTypeBackReference(params.ctx, id, newDesc.ID, jobDesc); err != nil { - return err - } - } + // Add back references for the type dependencies. + for id := range n.typeDeps { + jobDesc := fmt.Sprintf("updating type back reference %d for table %d", id, newDesc.ID) + if err := params.p.addTypeBackReference(params.ctx, id, newDesc.ID, jobDesc); err != nil { + return err + } + } - if err := validateDescriptor(params.ctx, params.p, newDesc); err != nil { - return err - } + if err := validateDescriptor(params.ctx, params.p, newDesc); err != nil { + return err + } - if applyGlobalMultiRegionZoneConfig { - regionConfig, err := SynthesizeRegionConfig(params.ctx, params.p.txn, n.dbDesc.GetID(), params.p.Descriptors()) - if err != nil { - return err - } - if err := ApplyZoneConfigForMultiRegionTable( - params.ctx, - params.p.txn, - params.p.ExecCfg(), - regionConfig, - newDesc, - applyZoneConfigForMultiRegionTableOptionTableNewConfig( - tabledesc.LocalityConfigGlobal(), - ), - ); err != nil { - return err - } - } + if applyGlobalMultiRegionZoneConfig { + regionConfig, err := SynthesizeRegionConfig(params.ctx, params.p.txn, n.dbDesc.GetID(), params.p.Descriptors()) + if err != nil { + return err + } + if err := ApplyZoneConfigForMultiRegionTable( + params.ctx, + params.p.txn, + params.p.ExecCfg(), + regionConfig, + newDesc, + applyZoneConfigForMultiRegionTableOptionTableNewConfig( + tabledesc.LocalityConfigGlobal(), + ), + ); err != nil { + return err + } + } - // Log Create View event. This is an auditable log event and is - // recorded in the same transaction as the table descriptor update. - return params.p.logEvent(params.ctx, - newDesc.ID, - &eventpb.CreateView{ - ViewName: n.viewName.FQString(), - ViewQuery: n.viewQuery, - }) + // Log Create View event. This is an auditable log event and is + // recorded in the same transaction as the table descriptor update. + return params.p.logEvent(params.ctx, + newDesc.ID, + &eventpb.CreateView{ + ViewName: n.viewName.FQString(), + ViewQuery: n.viewQuery, + }) + }() + }) + return retErr } func (*createViewNode) Next(runParams) (bool, error) { return false, nil } @@ -457,24 +463,42 @@ func serializeUserDefinedTypes( ctx context.Context, semaCtx *tree.SemaContext, viewQuery string, ) (string, error) { replaceFunc := func(expr tree.Expr) (recurse bool, newExpr tree.Expr, err error) { + var innerExpr tree.Expr + var typRef tree.ResolvableTypeReference switch n := expr.(type) { - case *tree.CastExpr, *tree.AnnotateTypeExpr: - texpr, err := tree.TypeCheck(ctx, n, semaCtx, types.Any) - if err != nil { - return false, expr, err - } - if !texpr.ResolvedType().UserDefined() { - return true, expr, nil - } - - s := tree.Serialize(texpr) - parsedExpr, err := parser.ParseExpr(s) - if err != nil { - return false, expr, err - } - return false, parsedExpr, nil + case *tree.CastExpr: + innerExpr = n.Expr + typRef = n.Type + case *tree.AnnotateTypeExpr: + innerExpr = n.Expr + typRef = n.Type + default: + return true, expr, nil + } + // semaCtx may be nil if this is a virtual view being created at + // init time. + var typeResolver tree.TypeReferenceResolver + if semaCtx != nil { + typeResolver = semaCtx.TypeResolver + } + var typ *types.T + typ, err = tree.ResolveType(ctx, typRef, typeResolver) + if err != nil { + return false, expr, err + } + if !typ.UserDefined() { + return true, expr, nil + } + texpr, err := innerExpr.TypeCheck(ctx, semaCtx, typ) + if err != nil { + return false, expr, err + } + s := tree.Serialize(texpr) + parsedExpr, err := parser.ParseExpr(s) + if err != nil { + return false, expr, err } - return true, expr, nil + return false, parsedExpr, nil } stmt, err := parser.ParseOne(viewQuery) diff --git a/pkg/sql/logictest/testdata/logic_test/builtin_function b/pkg/sql/logictest/testdata/logic_test/builtin_function index 7a9d15d711a7..792ad8fe90ab 100644 --- a/pkg/sql/logictest/testdata/logic_test/builtin_function +++ b/pkg/sql/logictest/testdata/logic_test/builtin_function @@ -2202,7 +2202,7 @@ CREATE INDEX pg_indexdef_partial_idx ON test.public.pg_indexdef_test USING btree query T SELECT pg_catalog.pg_get_indexdef((SELECT oid from pg_class WHERE relname='pg_indexdef_partial_enum_idx')) ---- -CREATE INDEX pg_indexdef_partial_enum_idx ON test.public.pg_indexdef_test USING btree (a ASC) WHERE (e IN ('foo'::public.testenum, 'bar'::public.testenum)) +CREATE INDEX pg_indexdef_partial_enum_idx ON test.public.pg_indexdef_test USING btree (a ASC) WHERE (e IN ('foo'::test.public.testenum, 'bar'::test.public.testenum)) query T SELECT pg_catalog.pg_get_indexdef(0, 0, true) diff --git a/pkg/sql/logictest/testdata/logic_test/distsql_stats b/pkg/sql/logictest/testdata/logic_test/distsql_stats index dad05ac546a6..a3bb955e5880 100644 --- a/pkg/sql/logictest/testdata/logic_test/distsql_stats +++ b/pkg/sql/logictest/testdata/logic_test/distsql_stats @@ -1264,7 +1264,7 @@ FROM [SHOW STATISTICS USING JSON FOR TABLE greeting_stats] "upper_bound": "hi" } ], - "histo_col_type": "public.greeting", + "histo_col_type": "test.public.greeting", "histo_version": 1, "name": "s", "null_count": 0, diff --git a/pkg/sql/logictest/testdata/logic_test/enums b/pkg/sql/logictest/testdata/logic_test/enums index a3dc2502b476..3ea4e092ca99 100644 --- a/pkg/sql/logictest/testdata/logic_test/enums +++ b/pkg/sql/logictest/testdata/logic_test/enums @@ -363,7 +363,7 @@ SELECT b'\x80'::as_bytes, b'\x80':::as_bytes ---- bytes bytes -query error pq: could not find \[255\] in enum "public.as_bytes" representation +query error pq: could not find \[255\] in enum "test.public.as_bytes" representation SELECT b'\xFF'::as_bytes # Regression for #49300. Ensure that virtual tables have access to hydrated @@ -372,7 +372,7 @@ query TT SHOW CREATE t1 ---- t1 CREATE TABLE public.t1 ( - x public.greeting NULL, + x test.public.greeting NULL, rowid INT8 NOT VISIBLE NOT NULL DEFAULT unique_rowid(), CONSTRAINT t1_pkey PRIMARY KEY (rowid ASC), INDEX i (x ASC) @@ -384,7 +384,7 @@ query T SELECT create_statement FROM crdb_internal.create_statements WHERE descriptor_name = 't1' ---- CREATE TABLE public.t1 ( - x public.greeting NULL, + x test.public.greeting NULL, rowid INT8 NOT VISIBLE NOT NULL DEFAULT unique_rowid(), CONSTRAINT t1_pkey PRIMARY KEY (rowid ASC), INDEX i (x ASC) @@ -397,7 +397,7 @@ SELECT ARRAY['hello']::_greeting, ARRAY['hello'::greeting] {hello} {hello} # Test that we can't mix enums in an array. -query error pq: expected 'cockroach'::public.dbs to be of type greeting, found type dbs +query error pq: expected 'cockroach'::test.public.dbs to be of type greeting, found type dbs SELECT ARRAY['hello'::greeting, 'cockroach'::dbs] statement ok @@ -479,7 +479,7 @@ WHERE AND NOT a.attisdropped AND attname = 'y' ---- -'hello'::public.greeting +'hello'::test.public.greeting # Test that enum default values are formatted in human readable ways. query TT @@ -487,8 +487,8 @@ SHOW CREATE enum_default ---- enum_default CREATE TABLE public.enum_default ( x INT8 NULL, - y public.greeting NULL DEFAULT 'hello':::public.greeting, - z BOOL NULL DEFAULT 'hello':::public.greeting IS OF (public.greeting, public.greeting), + y test.public.greeting NULL DEFAULT 'hello':::test.public.greeting, + z BOOL NULL DEFAULT 'hello':::test.public.greeting IS OF (test.public.greeting, test.public.greeting), rowid INT8 NOT VISIBLE NOT NULL DEFAULT unique_rowid(), CONSTRAINT enum_default_pkey PRIMARY KEY (rowid ASC), FAMILY fam_0_x_y_z_rowid (x, y, z, rowid) @@ -505,8 +505,8 @@ WHERE ORDER BY column_name ---- -y 'hello':::public.greeting -z 'hello':::public.greeting IS OF (public.greeting, public.greeting) +y 'hello':::test.public.greeting +z 'hello':::test.public.greeting IS OF (test.public.greeting, test.public.greeting) # Test information_schema.columns. query TT @@ -519,8 +519,8 @@ WHERE ORDER BY column_name ---- -y 'hello':::public.greeting -z 'hello':::public.greeting IS OF (public.greeting, public.greeting) +y 'hello':::test.public.greeting +z 'hello':::test.public.greeting IS OF (test.public.greeting, test.public.greeting) # Test computed columns with enum values. statement ok @@ -544,9 +544,9 @@ SHOW CREATE enum_computed ---- enum_computed CREATE TABLE public.enum_computed ( x INT8 NULL, - y public.greeting NULL AS ('hello':::public.greeting) STORED, - z BOOL NULL AS (w = 'howdy':::public.greeting) STORED, - w public.greeting NULL, + y test.public.greeting NULL AS ('hello':::test.public.greeting) STORED, + z BOOL NULL AS (w = 'howdy':::test.public.greeting) STORED, + w test.public.greeting NULL, rowid INT8 NOT VISIBLE NOT NULL DEFAULT unique_rowid(), CONSTRAINT enum_computed_pkey PRIMARY KEY (rowid ASC), FAMILY fam_0_x_y_z_w_rowid (x, y, z, w, rowid) @@ -580,11 +580,11 @@ query TT SHOW CREATE enum_checks ---- enum_checks CREATE TABLE public.enum_checks ( - x public.greeting NULL, + x test.public.greeting NULL, rowid INT8 NOT VISIBLE NOT NULL DEFAULT unique_rowid(), CONSTRAINT enum_checks_pkey PRIMARY KEY (rowid ASC), - CONSTRAINT check_x CHECK (x = 'hello':::public.greeting), - CONSTRAINT "check" CHECK ('hello':::public.greeting = 'hello':::public.greeting) + CONSTRAINT check_x CHECK (x = 'hello':::test.public.greeting), + CONSTRAINT "check" CHECK ('hello':::test.public.greeting = 'hello':::test.public.greeting) ) # Ensure that we can add check constraints to tables with enums. @@ -603,7 +603,7 @@ INSERT INTO enum_checks VALUES ('hi'), ('howdy'); ALTER TABLE enum_checks ADD CHECK (x > 'hello') # Ensure that checks are validated on insert. -statement error pq: failed to satisfy CHECK constraint \(x > 'hello':::public.greeting\) +statement error pq: failed to satisfy CHECK constraint \(x > 'hello':::test.public.greeting\) INSERT INTO enum_checks VALUES ('hello') # Try adding a check that fails validation. @@ -620,7 +620,7 @@ CREATE TABLE enum_checks (x greeting); INSERT INTO enum_checks VALUES ('hi'), ('howdy'); ALTER TABLE enum_checks ADD CHECK (x > 'hello') -statement error pq: failed to satisfy CHECK constraint \(x > 'hello':::public.greeting\) +statement error pq: failed to satisfy CHECK constraint \(x > 'hello':::test.public.greeting\) INSERT INTO enum_checks VALUES ('hello') statement ok @@ -632,7 +632,7 @@ CREATE TABLE enum_checks (x greeting); INSERT INTO enum_checks VALUES ('hi'), ('howdy'); # Try adding a check that fails validation. -statement error pq: validation of CHECK "x = 'hello':::public.greeting" failed +statement error pq: validation of CHECK "x = 'hello':::test.public.greeting" failed ALTER TABLE enum_checks ADD CHECK (x = 'hello') statement ok @@ -1511,9 +1511,9 @@ query TT SHOW CREATE TABLE arr_t6 ---- arr_t6 CREATE TABLE public.arr_t6 ( - i public.arr_typ2[] NULL DEFAULT ARRAY['a':::public.arr_typ2]:::public.arr_typ2[], - j public.arr_typ2 NULL DEFAULT (ARRAY['b':::public.arr_typ2]:::public.arr_typ2[])[1:::INT8], - k public.arr_typ2[] NULL DEFAULT ARRAY['c':::public.arr_typ2]:::public.arr_typ2[], + i test_57196.public.arr_typ2[] NULL DEFAULT ARRAY['a':::test_57196.public.arr_typ2]:::test_57196.public.arr_typ2[], + j test_57196.public.arr_typ2 NULL DEFAULT (ARRAY['b':::test_57196.public.arr_typ2]:::test_57196.public.arr_typ2[])[1:::INT8], + k test_57196.public.arr_typ2[] NULL DEFAULT ARRAY['c':::test_57196.public.arr_typ2]:::test_57196.public.arr_typ2[], rowid INT8 NOT VISIBLE NOT NULL DEFAULT unique_rowid(), CONSTRAINT arr_t6_pkey PRIMARY KEY (rowid ASC), FAMILY fam_0_i_j_k_rowid (i, j, k, rowid) diff --git a/pkg/sql/logictest/testdata/logic_test/new_schema_changer b/pkg/sql/logictest/testdata/logic_test/new_schema_changer index f36f1289d77e..c363b7f669ca 100644 --- a/pkg/sql/logictest/testdata/logic_test/new_schema_changer +++ b/pkg/sql/logictest/testdata/logic_test/new_schema_changer @@ -1340,9 +1340,22 @@ CREATE TYPE db1.sc1.typ AS ENUM('a') statement ok CREATE VIEW db1.sc1.v5 AS (SELECT 'a'::db1.sc1.typ::string AS k, n2, n1 from db1.sc1.v4) +statement ok +CREATE SCHEMA sc2 + +statement ok +CREATE TYPE sc2.typ AS ENUM('a') + +# Cross-database type references are not allowed. +statement error cross database type references are not supported: test.sc2.typ +CREATE VIEW db1.sc1.v6 AS (SELECT 'a'::sc2.typ::string AS k, n2, n1 from db1.sc1.v4) + statement ok DROP SCHEMA db1.sc1 CASCADE +statement ok +DROP SCHEMA sc2 CASCADE + statement ok DROP DATABASE db1 CASCADE @@ -1407,6 +1420,16 @@ CREATE TYPE db1.sc1.typ AS ENUM('a') statement ok CREATE VIEW db1.sc1.v5 AS (SELECT 'a'::db1.sc1.typ::string AS k, n2, n1 from db1.sc1.v4) +statement ok +CREATE SCHEMA sc2 + +statement ok +CREATE TYPE sc2.typ AS ENUM('a') + +# Cross-database type references are not allowed. +statement error cross database type references are not supported: test.sc2.typ +CREATE VIEW db1.sc1.v6 AS (SELECT 'a'::sc2.typ::string AS k, n2, n1 from db1.sc1.v4) + # Confirm comments exist. query T SELECT comment FROM system.comments ORDER BY comment ASC @@ -1476,6 +1499,9 @@ select count(*) from system.descriptor statement ok DROP DATABASE db1 CASCADE +statement ok +DROP SCHEMA sc2 CASCADE + # No comments should be left after. query T SELECT comment FROM system.comments ORDER BY comment ASC @@ -1492,12 +1518,12 @@ database_id role_name settings let $desc_count_post_drop select count(*) from system.descriptor -# Excluding anything that needs GC, we should drop by 10 (2 types [array and -# normal], 3 schemas, and 5 views) +# Excluding anything that needs GC, we should drop by 13 (4 types [array and +# normal], 4 schemas, and 5 views) query I select $desc_count_pre_drop-$desc_count_post_drop ---- -10 +13 # Tests for computed column rewrites. statement ok diff --git a/pkg/sql/logictest/testdata/logic_test/partial_index b/pkg/sql/logictest/testdata/logic_test/partial_index index 9acfd98d1e71..2eabdbc87313 100644 --- a/pkg/sql/logictest/testdata/logic_test/partial_index +++ b/pkg/sql/logictest/testdata/logic_test/partial_index @@ -1139,10 +1139,10 @@ SHOW CREATE TABLE enum_table_show ---- enum_table_show CREATE TABLE public.enum_table_show ( a INT8 NULL, - b public.enum_type NULL, + b test.public.enum_type NULL, rowid INT8 NOT VISIBLE NOT NULL DEFAULT unique_rowid(), CONSTRAINT enum_table_show_pkey PRIMARY KEY (rowid ASC), - INDEX i (a ASC) WHERE b IN ('foo':::public.enum_type, 'bar':::public.enum_type), + INDEX i (a ASC) WHERE b IN ('foo':::test.public.enum_type, 'bar':::test.public.enum_type), FAMILY fam_0_a_b_rowid (a, b, rowid) ) diff --git a/pkg/sql/logictest/testdata/logic_test/pg_catalog b/pkg/sql/logictest/testdata/logic_test/pg_catalog index f835d97a2296..b07ad5cc7599 100644 --- a/pkg/sql/logictest/testdata/logic_test/pg_catalog +++ b/pkg/sql/logictest/testdata/logic_test/pg_catalog @@ -1019,7 +1019,7 @@ crdb_oid tablename indexname indexdef 2129466855 t6 t6_expr_idx CREATE INDEX t6_expr_idx ON constraint_db.public.t6 USING btree ((a + b) ASC) 2129466854 t6 t6_expr_expr1_idx CREATE INDEX t6_expr_expr1_idx ON constraint_db.public.t6 USING btree (lower(c) ASC, (a + b) ASC) 2129466848 t6 t6_expr_key CREATE UNIQUE INDEX t6_expr_key ON constraint_db.public.t6 USING btree (lower(c) ASC) -2129466850 t6 t6_expr_idx1 CREATE INDEX t6_expr_idx1 ON constraint_db.public.t6 USING btree ((m = 'foo'::public.mytype) ASC) WHERE (m = 'foo'::public.mytype) +2129466850 t6 t6_expr_idx1 CREATE INDEX t6_expr_idx1 ON constraint_db.public.t6 USING btree ((m = 'foo'::constraint_db.public.mytype) ASC) WHERE (m = 'foo'::constraint_db.public.mytype) 784389845 mv1 mv1_pkey CREATE UNIQUE INDEX mv1_pkey ON constraint_db.public.mv1 USING btree (rowid ASC) ## pg_catalog.pg_index @@ -1065,12 +1065,12 @@ JOIN pg_catalog.pg_class c ON i.indexrelid = c.oid WHERE c.relname LIKE 't6_%' ORDER BY 1 ---- -relname indkey indexprs indpred -t6_expr_expr1_idx 0 0 (lower(c)) (a + b) NULL -t6_expr_idx 0 (a + b) NULL -t6_expr_idx1 0 (m = 'foo'::public.mytype) m = 'foo'::public.mytype -t6_expr_key 0 (lower(c)) NULL -t6_pkey 6 NULL NULL +relname indkey indexprs indpred +t6_expr_expr1_idx 0 0 (lower(c)) (a + b) NULL +t6_expr_idx 0 (a + b) NULL +t6_expr_idx1 0 (m = 'foo'::constraint_db.public.mytype) m = 'foo'::constraint_db.public.mytype +t6_expr_key 0 (lower(c)) NULL +t6_pkey 6 NULL NULL statement ok SET DATABASE = system @@ -4984,7 +4984,7 @@ WHERE tablename = 'partial_index_table' indexname indexdef partial_index_table_pkey CREATE UNIQUE INDEX partial_index_table_pkey ON test.public.partial_index_table USING btree (rowid ASC) partial_index_table_a_key CREATE UNIQUE INDEX partial_index_table_a_key ON test.public.partial_index_table USING btree (a ASC) WHERE (a > 0) -partial_index_table_a_key1 CREATE UNIQUE INDEX partial_index_table_a_key1 ON test.public.partial_index_table USING btree (a ASC) WHERE (b IN ('foo'::public.testenum, 'bar'::public.testenum)) +partial_index_table_a_key1 CREATE UNIQUE INDEX partial_index_table_a_key1 ON test.public.partial_index_table USING btree (a ASC) WHERE (b IN ('foo'::test.public.testenum, 'bar'::test.public.testenum)) query TT colnames SELECT conname, condef @@ -4995,7 +4995,7 @@ ORDER BY conname ---- conname condef partial_index_table_a_key UNIQUE (a ASC) WHERE (a > 0) -partial_index_table_a_key1 UNIQUE (a ASC) WHERE (b IN ('foo'::public.testenum, 'bar'::public.testenum)) +partial_index_table_a_key1 UNIQUE (a ASC) WHERE (b IN ('foo'::test.public.testenum, 'bar'::test.public.testenum)) partial_index_table_pkey PRIMARY KEY (rowid ASC) subtest regression_46799 diff --git a/pkg/sql/logictest/testdata/logic_test/show_create_all_tables b/pkg/sql/logictest/testdata/logic_test/show_create_all_tables index 435ab630d1a1..ec49ffecb118 100644 --- a/pkg/sql/logictest/testdata/logic_test/show_create_all_tables +++ b/pkg/sql/logictest/testdata/logic_test/show_create_all_tables @@ -408,7 +408,7 @@ SHOW CREATE ALL TABLES ---- create_statement CREATE TABLE public.t ( - x public.test NULL, + x type_test.public.test NULL, rowid INT8 NOT VISIBLE NOT NULL DEFAULT unique_rowid(), CONSTRAINT t_pkey PRIMARY KEY (rowid ASC) ); diff --git a/pkg/sql/logictest/testdata/logic_test/views b/pkg/sql/logictest/testdata/logic_test/views index 27918d7cce84..511acc40aa8b 100644 --- a/pkg/sql/logictest/testdata/logic_test/views +++ b/pkg/sql/logictest/testdata/logic_test/views @@ -917,8 +917,19 @@ CREATE VIEW rv as select val from tval; statement ok; USE DB2; -statement ok; -CREATE VIEW vm as (select s.last_value, t.val as a, v.val as b, cast('open' as db1.status) from db1.tval as t, db1.sq1 as s, db1.rv as v) +# Cross-database type references are not allowed. +statement error cross database type references are not supported: db1.public.status +CREATE VIEW vm as (select cast('open' as db1.status) from db1.tval as t) + +# If the view is being created in the same database as the type, that is allowed. +statement ok +CREATE VIEW db1.vm as (select cast('open' as db1.status) from db1.tval as t) + +statement ok +CREATE TYPE db2.status AS ENUM ('open', 'closed', 'inactive'); + +statement ok +CREATE VIEW vm as (select s.last_value, t.val as a, v.val as b, cast('open' as db2.status) from db1.tval as t, db1.sq1 as s, db1.rv as v) query TTTTTTT select * from "".crdb_internal.cross_db_references order by object_database, object_schema, object_name, referenced_object_database, referenced_object_schema, referenced_object_name desc; @@ -928,10 +939,8 @@ db1 public sys2 system public descriptor view references table db2 public v1 db1 public ab view references table db2 public v2 db1 public ab view references table db2 public vm db1 public tval view references table -db2 public vm db1 public status view references type db2 public vm db1 public sq1 view references table db2 public vm db1 public rv view references table -db2 public vm db1 public _status view references type # Test that user defined types used in views are tracked. @@ -1109,9 +1118,9 @@ ALTER TYPE view_typ RENAME TO view_typ_new query TT SHOW CREATE VIEW v10 ---- -v10 CREATE VIEW public.v10 ( - view_typ -) AS SELECT 'a':::public.view_typ_new +v10 CREATE VIEW public.v10 ( + view_typ +) AS SELECT 'a':::db2.public.view_typ_new query T SELECT * FROM v10 @@ -1127,9 +1136,9 @@ ALTER TYPE view_typ_new RENAME TO view_typ query TT SHOW CREATE VIEW v11 ---- -v11 CREATE VIEW public.v11 ( - k -) AS (SELECT 'a':::public.view_typ < 'a':::public.view_typ AS k) +v11 CREATE VIEW public.v11 ( + k +) AS (SELECT 'a':::db2.public.view_typ < 'a':::db2.public.view_typ AS k) query B SELECT * FROM v11 @@ -1145,9 +1154,9 @@ ALTER TYPE view_typ RENAME TO view_type_new query TT SHOW CREATE VIEW v12 ---- -v12 CREATE VIEW public.v12 ( - k -) AS (SELECT k FROM (SELECT 'a':::public.view_type_new AS k)) +v12 CREATE VIEW public.v12 ( + k +) AS (SELECT k FROM (SELECT 'a':::db2.public.view_type_new AS k)) query T SELECT * FROM v12 @@ -1163,9 +1172,9 @@ ALTER TYPE view_type_new RENAME TO view_type query TT SHOW CREATE VIEW v13 ---- -v13 CREATE VIEW public.v13 ( - k -) AS (SELECT 'a':::public.view_type AS k UNION SELECT 'b':::public.view_type) +v13 CREATE VIEW public.v13 ( + k +) AS (SELECT 'a':::db2.public.view_type AS k UNION SELECT 'b':::db2.public.view_type) query T SELECT * FROM v13 ORDER BY k @@ -1182,9 +1191,12 @@ ALTER TYPE view_type RENAME TO view_type_new query TT SHOW CREATE VIEW v14 ---- -v14 CREATE VIEW public.v14 ( - "array" -) AS (SELECT ARRAY['a':::public.view_type_new, 'b':::public.view_type_new]:::public.view_type_new[]) +v14 CREATE VIEW public.v14 ( + "array" +) AS ( + SELECT + ARRAY['a':::db2.public.view_type_new, 'b':::db2.public.view_type_new]:::db2.public.view_type_new[] + ) query T SELECT * FROM v14 @@ -1200,9 +1212,12 @@ ALTER TYPE view_type_new RENAME TO view_type query TT SHOW CREATE VIEW v15 ---- -v15 CREATE VIEW public.v15 ( - view_type_new -) AS (SELECT ARRAY['a':::public.view_type, 'b':::public.view_type][2:::INT8] AS view_type_new) +v15 CREATE VIEW public.v15 ( + view_type_new +) AS ( + SELECT + ARRAY['a':::db2.public.view_type, 'b':::db2.public.view_type][2:::INT8] AS view_type_new + ) query T SELECT * FROM v15 diff --git a/pkg/sql/schemachanger/testdata/drop b/pkg/sql/schemachanger/testdata/drop index 23543c341f1b..85a262dfb19a 100644 --- a/pkg/sql/schemachanger/testdata/drop +++ b/pkg/sql/schemachanger/testdata/drop @@ -1898,7 +1898,7 @@ upsert descriptor #125 unexposedParentSchemaId: 113 - version: "1" + version: "2" - viewQuery: (SELECT 'a':::sc1.typ::STRING AS k, n2, n1 FROM db1.sc1.v4) + viewQuery: (SELECT b'\x80':::@100123::STRING AS k, n2, n1 FROM db1.sc1.v4) delete all comments for table descriptors [114 115 116 117 118 119 120 121 122 125] delete role settings for database on #111 delete scheduleId: diff --git a/pkg/sql/sem/tree/walk.go b/pkg/sql/sem/tree/walk.go index aa8c58a5d518..e8854a086c46 100644 --- a/pkg/sql/sem/tree/walk.go +++ b/pkg/sql/sem/tree/walk.go @@ -1740,6 +1740,9 @@ func SimpleVisit(expr Expr, preFn SimpleVisitFn) (Expr, error) { func SimpleStmtVisit(stmt Statement, preFn SimpleVisitFn) (Statement, error) { v := simpleVisitor{fn: preFn} newStmt, changed := walkStmt(&v, stmt) + if v.err != nil { + return nil, v.err + } if changed { return newStmt, nil } diff --git a/pkg/sql/types/types.go b/pkg/sql/types/types.go index 96960588d9e5..5558659ae4f4 100644 --- a/pkg/sql/types/types.go +++ b/pkg/sql/types/types.go @@ -242,8 +242,17 @@ func (u UserDefinedTypeName) Basename() string { // FQName returns the fully qualified name. func (u UserDefinedTypeName) FQName() string { var sb strings.Builder - // Note that cross-database type references are disabled, so we only - // format the qualified name with the schema. + // Even though cross-database type references are disabled, we still format + // the qualified name with the catalog. Consider the case where the current + // database is db1, and a statement like + // `CREATE VIEW db2.sc.v AS SELECT 'a'::db2.sc.typ` + // is executed. When parsing the inner view query, it's important to include + // the explicit catalog name, so the correct (non-cross-database) type is + // resolved. + if u.Catalog != "" { + sb.WriteString(u.Catalog) + sb.WriteString(".") + } if u.ExplicitSchema { sb.WriteString(u.Schema) sb.WriteString(".")