Skip to content

Commit

Permalink
sql: set GLOBAL on MATERIALIZED VIEW tables on first ADD REGION
Browse files Browse the repository at this point in the history
Release note (sql change): Materialized views which are in a database
before the first ADD REGION will become GLOBAL on ADD REGION, in line
with the behavior of CREATE MATERIALIZED VIEW on a multi-region
database.
  • Loading branch information
otan committed Mar 17, 2021
1 parent fbe2b8e commit dd1f652
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
6 changes: 5 additions & 1 deletion pkg/ccl/logictestccl/testdata/logic_test/multi_region
Original file line number Diff line number Diff line change
Expand Up @@ -1259,7 +1259,8 @@ statement ok
CREATE TABLE t (id INT PRIMARY KEY, a INT, b INT);
INSERT INTO t VALUES (1, 2, 3), (4, 5, 6);
CREATE SEQUENCE s;
CREATE VIEW v AS SELECT id, a, b FROM t
CREATE VIEW v AS SELECT id, a, b FROM t;
CREATE MATERIALIZED VIEW mat_view AS SELECT id, a, b FROM t

statement ok
ALTER DATABASE db_with_views_and_sequences SET PRIMARY REGION "ap-southeast-2"
Expand All @@ -1271,6 +1272,7 @@ table_name locality
t REGIONAL BY TABLE IN PRIMARY REGION
s REGIONAL BY TABLE IN PRIMARY REGION
v REGIONAL BY TABLE IN PRIMARY REGION
mat_view GLOBAL

statement ok
CREATE SEQUENCE s2;
Expand All @@ -1284,6 +1286,7 @@ table_name locality
t REGIONAL BY TABLE IN PRIMARY REGION
s REGIONAL BY TABLE IN PRIMARY REGION
v REGIONAL BY TABLE IN PRIMARY REGION
mat_view GLOBAL
s2 REGIONAL BY TABLE IN PRIMARY REGION
v2 REGIONAL BY TABLE IN PRIMARY REGION
mat_view2 GLOBAL
Expand Down Expand Up @@ -1312,6 +1315,7 @@ table_name locality
t NULL
s NULL
v NULL
mat_view NULL
s2 NULL
v2 NULL
mat_view2 NULL
16 changes: 12 additions & 4 deletions pkg/sql/alter_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -592,10 +592,18 @@ func addDefaultLocalityConfigToAllTables(
return err
}

if err := p.alterTableDescLocalityToRegionalByTable(
ctx, tree.PrimaryRegionNotSpecifiedName, mutDesc, regionEnumID,
); err != nil {
return err
if mutDesc.MaterializedView() {
if err := p.alterTableDescLocalityToGlobal(
ctx, mutDesc, regionEnumID,
); err != nil {
return err
}
} else {
if err := p.alterTableDescLocalityToRegionalByTable(
ctx, tree.PrimaryRegionNotSpecifiedName, mutDesc, regionEnumID,
); err != nil {
return err
}
}

if err := p.writeSchemaChangeToBatch(ctx, mutDesc, b); err != nil {
Expand Down

0 comments on commit dd1f652

Please sign in to comment.