Skip to content

Commit 1303c66

Browse files
craig[bot]spilchen
andcommitted
Merge #152670
152670: sql/catalog: exempt views from crdb_region validation r=spilchen a=spilchen Previously, views using `crdb_region` in expressions were incorrectly rejected by multi-region validation. The logic assumed that region enums must correspond to explicit columns in the descriptor, failing to account for views that reference `crdb_region` from underlying tables. This change updates the validation to allow such expressions in views. Fixes #151216 Fixes #152197 Release note (bug fix): views can now reference the `crdb_region` column from underlying tables in expressions. Co-authored-by: Matt Spilchen <[email protected]>
2 parents 38a982a + fc280fe commit 1303c66

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

pkg/ccl/logictestccl/testdata/logic_test/multi_region

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1692,4 +1692,29 @@ SELECT * FROM chgme ORDER BY C1;
16921692
statement ok
16931693
DROP TABLE chgme;
16941694

1695+
# Regression test for issue #151216
1696+
# Tests that CREATE VIEW with references to crdb_region column in expressions
1697+
# (but not directly returned) should not trigger validation assertion errors.
1698+
subtest issue_151216_view_crdb_region_reference
1699+
1700+
statement ok
1701+
CREATE DATABASE bank_151216 PRIMARY REGION "us-east-1" REGIONS "us-east-1", "ca-central-1", "ap-southeast-2" SURVIVE REGION FAILURE;
1702+
1703+
statement ok
1704+
USE bank_151216;
1705+
1706+
statement ok
1707+
CREATE TABLE t1 (c1 INT) LOCALITY REGIONAL BY ROW;
1708+
1709+
statement ok
1710+
CREATE MATERIALIZED VIEW mv1 AS SELECT c1, CASE WHEN crdb_region = 'us-east-1' THEN 'east' ELSE 'other' END AS region_type FROM t1;
1711+
1712+
statement ok
1713+
CREATE VIEW v2 AS SELECT c1, crdb_region = 'us-east-1' AS is_us_east1 FROM t1;
1714+
1715+
statement ok
1716+
SET sql_safe_updates = false;
1717+
DROP DATABASE bank_151216;
1718+
SET sql_safe_updates = true
1719+
16951720
subtest end

pkg/sql/catalog/multiregion/validate_table.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,8 @@ func ValidateTableLocalityConfig(
121121
switch lc := lc.Locality.(type) {
122122
case *catpb.LocalityConfig_Global_:
123123
if regionEnumIDReferenced {
124-
if !columnTypesTypeIDs.Contains(regionsEnumID) {
124+
// Omit views since they may reference the multi-region enum type of the base table.
125+
if !columnTypesTypeIDs.Contains(regionsEnumID) && !desc.IsView() {
125126
return errors.AssertionFailedf(
126127
"expected no region Enum ID to be referenced by a GLOBAL TABLE: %q"+
127128
" but found: %d",
@@ -233,8 +234,9 @@ func ValidateTableLocalityConfig(
233234
if regionEnumIDReferenced {
234235
// It may be the case that the multi-region type descriptor is used
235236
// as the type of the table column. Validations should only fail if
236-
// that is not the case.
237-
if !columnTypesTypeIDs.Contains(regionsEnumID) {
237+
// that is not the case. We omit views since they may reference the
238+
// multi-region enum type of the base table.
239+
if !columnTypesTypeIDs.Contains(regionsEnumID) && !desc.IsView() {
238240
return errors.AssertionFailedf(
239241
"expected no region Enum ID to be referenced by a REGIONAL BY TABLE: %q homed in the "+
240242
"primary region, but found: %d",

0 commit comments

Comments
 (0)