Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

release-21.1: sql: set locality to GLOBAL for materialized views #62194

Merged
merged 2 commits into from
Mar 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions pkg/ccl/logictestccl/testdata/logic_test/multi_region
Original file line number Diff line number Diff line change
Expand Up @@ -1320,7 +1320,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 @@ -1332,10 +1333,12 @@ 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;
CREATE VIEW v2 AS SELECT id, a, b FROM t
CREATE VIEW v2 AS SELECT id, a, b FROM t;
CREATE MATERIALIZED VIEW mat_view2 AS SELECT id, a, b FROM t

query TT colnames
SELECT table_name, locality FROM [SHOW TABLES]
Expand All @@ -1344,8 +1347,24 @@ 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

query TT
SHOW ZONE CONFIGURATION FROM TABLE mat_view2
----
TABLE mat_view2 ALTER TABLE mat_view2 CONFIGURE ZONE USING
range_min_bytes = 134217728,
range_max_bytes = 536870912,
gc.ttlseconds = 90000,
global_reads = true,
num_replicas = 3,
num_voters = 3,
constraints = '{+region=ap-southeast-2: 1}',
voter_constraints = '[+region=ap-southeast-2]',
lease_preferences = '[[+region=ap-southeast-2]]'

statement ok
ALTER DATABASE db_with_views_and_sequences DROP REGION "ap-southeast-2"
Expand All @@ -1357,5 +1376,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 @@ -588,10 +588,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
27 changes: 27 additions & 0 deletions pkg/sql/catalog/tabledesc/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -1248,6 +1248,33 @@ func (desc *wrapper) validateTableLocalityConfig(
return errors.Wrapf(err, "multi-region enum with ID %d does not exist", regionsEnumID)
}

// Check non-table items have a correctly set locality.
if desc.IsSequence() {
if !desc.IsLocalityRegionalByTable() {
return errors.AssertionFailedf(
"expected sequence %s to have locality REGIONAL BY TABLE",
desc.Name,
)
}
}
if desc.IsView() {
if desc.MaterializedView() {
if !desc.IsLocalityGlobal() {
return errors.AssertionFailedf(
"expected materialized view %s to have locality GLOBAL",
desc.Name,
)
}
} else {
if !desc.IsLocalityRegionalByTable() {
return errors.AssertionFailedf(
"expected view %s to have locality REGIONAL BY TABLE",
desc.Name,
)
}
}
}

// REGIONAL BY TABLE tables homed in the primary region should include a
// reference to the multi-region type descriptor and a corresponding
// backreference. All other patterns should only contain a reference if there
Expand Down
25 changes: 25 additions & 0 deletions pkg/sql/create_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ func (n *createViewNode) startExec(params runParams) error {
privs := CreateInheritedPrivilegesFromDBDesc(n.dbDesc, params.SessionData().User())

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
Expand Down Expand Up @@ -208,6 +209,11 @@ func (n *createViewNode) startExec(params runParams) error {
if err := desc.AllocateIDs(params.ctx); 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.
Expand Down Expand Up @@ -266,6 +272,25 @@ func (n *createViewNode) startExec(params runParams) error {
return err
}

if applyGlobalMultiRegionZoneConfig {
regionConfig, err := SynthesizeRegionConfig(params.ctx, params.p.txn, n.dbDesc.ID, 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,
Expand Down