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

sql: add support for materialized views #52530

Merged
merged 1 commit into from
Aug 14, 2020
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
2 changes: 1 addition & 1 deletion docs/generated/settings/settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,6 @@
<tr><td><code>trace.debug.enable</code></td><td>boolean</td><td><code>false</code></td><td>if set, traces for recent requests can be seen in the /debug page</td></tr>
<tr><td><code>trace.lightstep.token</code></td><td>string</td><td><code></code></td><td>if set, traces go to Lightstep using this token</td></tr>
<tr><td><code>trace.zipkin.collector</code></td><td>string</td><td><code></code></td><td>if set, traces go to the given Zipkin instance (example: '127.0.0.1:9411'); ignored if trace.lightstep.token is set</td></tr>
<tr><td><code>version</code></td><td>custom validation</td><td><code>20.1-15</code></td><td>set the active cluster version in the format '<major>.<minor>'</td></tr>
<tr><td><code>version</code></td><td>custom validation</td><td><code>20.1-16</code></td><td>set the active cluster version in the format '<major>.<minor>'</td></tr>
</tbody>
</table>
2 changes: 2 additions & 0 deletions docs/generated/sql/bnf/create_view_stmt.bnf
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ create_view_stmt ::=
| 'CREATE' 'OR' 'REPLACE' opt_temp 'VIEW' view_name 'AS' select_stmt
| 'CREATE' opt_temp 'VIEW' 'IF' 'NOT' 'EXISTS' view_name '(' name_list ')' 'AS' select_stmt
| 'CREATE' opt_temp 'VIEW' 'IF' 'NOT' 'EXISTS' view_name 'AS' select_stmt
| 'CREATE' 'MATERIALIZED' 'VIEW' view_name '(' name_list ')' 'AS' select_stmt
| 'CREATE' 'MATERIALIZED' 'VIEW' view_name 'AS' select_stmt
12 changes: 9 additions & 3 deletions docs/generated/sql/bnf/stmt_block.bnf
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ stmt ::=
| revoke_stmt
| savepoint_stmt
| release_stmt
| refresh_stmt
| nonpreparable_set_stmt
| transaction_stmt
| close_cursor_stmt
Expand Down Expand Up @@ -87,6 +88,9 @@ savepoint_stmt ::=
release_stmt ::=
'RELEASE' savepoint_name

refresh_stmt ::=
'REFRESH' 'MATERIALIZED' 'VIEW' view_name

nonpreparable_set_stmt ::=
set_transaction_stmt

Expand Down Expand Up @@ -286,6 +290,9 @@ savepoint_name ::=
'SAVEPOINT' name
| name

view_name ::=
table_name

set_transaction_stmt ::=
'SET' 'TRANSACTION' transaction_mode_list
| 'SET' 'SESSION' 'TRANSACTION' transaction_mode_list
Expand Down Expand Up @@ -871,6 +878,7 @@ unreserved_keyword ::=
| 'RECURRING'
| 'RECURSIVE'
| 'REF'
| 'REFRESH'
| 'REINDEX'
| 'RELEASE'
| 'RENAME'
Expand Down Expand Up @@ -1160,6 +1168,7 @@ create_view_stmt ::=
'CREATE' opt_temp 'VIEW' view_name opt_column_list 'AS' select_stmt
| 'CREATE' 'OR' 'REPLACE' opt_temp 'VIEW' view_name opt_column_list 'AS' select_stmt
| 'CREATE' opt_temp 'VIEW' 'IF' 'NOT' 'EXISTS' view_name opt_column_list 'AS' select_stmt
| 'CREATE' 'MATERIALIZED' 'VIEW' view_name opt_column_list 'AS' select_stmt

create_sequence_stmt ::=
'CREATE' opt_temp 'SEQUENCE' sequence_name opt_sequence_option_list
Expand Down Expand Up @@ -1635,9 +1644,6 @@ opt_temp ::=
| 'TEMP'
|

view_name ::=
table_name

sequence_name ::=
db_object_name

Expand Down
7 changes: 7 additions & 0 deletions pkg/clusterversion/cockroach_versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ const (
VersionMinPasswordLength
VersionAbortSpanBytes
VersionAlterSystemJobsAddSqllivenessColumnsAddNewSystemSqllivenessTable
VersionMaterializedViews

// Add new versions here (step one of two).
)

Expand Down Expand Up @@ -564,6 +566,11 @@ var versionsSingleton = keyedVersions([]keyedVersion{
Key: VersionAlterSystemJobsAddSqllivenessColumnsAddNewSystemSqllivenessTable,
Version: roachpb.Version{Major: 20, Minor: 1, Unstable: 15},
},
{
// VersionMaterializedViews enables the use of materialized views.
Key: VersionMaterializedViews,
Version: roachpb.Version{Major: 20, Minor: 1, Unstable: 16},
},

// Add new versions here (step two of two).

Expand Down
5 changes: 3 additions & 2 deletions pkg/clusterversion/versionkey_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 14 additions & 1 deletion pkg/sql/backfill.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ func (sc *SchemaChanger) runBackfill(ctx context.Context) error {
var constraintsToAddBeforeValidation []descpb.ConstraintToUpdate
var constraintsToValidate []descpb.ConstraintToUpdate

var viewToRefresh *descpb.MaterializedViewRefresh

tableDesc, err := sc.updateJobRunningStatus(ctx, RunningStatusBackfill)
if err != nil {
return err
Expand Down Expand Up @@ -210,6 +212,8 @@ func (sc *SchemaChanger) runBackfill(ctx context.Context) error {
}
case *descpb.DescriptorMutation_PrimaryKeySwap, *descpb.DescriptorMutation_ComputedColumnSwap:
// The backfiller doesn't need to do anything here.
case *descpb.DescriptorMutation_MaterializedViewRefresh:
viewToRefresh = t.MaterializedViewRefresh
default:
return errors.AssertionFailedf(
"unsupported mutation: %+v", m)
Expand All @@ -225,7 +229,9 @@ func (sc *SchemaChanger) runBackfill(ctx context.Context) error {
}
case *descpb.DescriptorMutation_Constraint:
constraintsToDrop = append(constraintsToDrop, *t.Constraint)
case *descpb.DescriptorMutation_PrimaryKeySwap, *descpb.DescriptorMutation_ComputedColumnSwap:
case *descpb.DescriptorMutation_PrimaryKeySwap,
*descpb.DescriptorMutation_ComputedColumnSwap,
*descpb.DescriptorMutation_MaterializedViewRefresh:
// The backfiller doesn't need to do anything here.
default:
return errors.AssertionFailedf(
Expand All @@ -234,6 +240,13 @@ func (sc *SchemaChanger) runBackfill(ctx context.Context) error {
}
}

// If we were requested to refresh a view, then do so.
if viewToRefresh != nil {
if err := sc.refreshMaterializedView(ctx, tableDesc, viewToRefresh); err != nil {
return err
}
}

// First drop constraints and indexes, then add/drop columns, and only then
// add indexes and constraints.

Expand Down
6 changes: 6 additions & 0 deletions pkg/sql/catalog/descpb/structured.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,12 @@ func (desc *TableDescriptor) IsView() bool {
return desc.ViewQuery != ""
}

// MaterializedView returns whether or not this TableDescriptor is a
// MaterializedView.
func (desc *TableDescriptor) MaterializedView() bool {
return desc.IsMaterializedView
}

// IsAs returns true if the TableDescriptor actually describes
// a Table resource with an As source.
func (desc *TableDescriptor) IsAs() bool {
Expand Down
Loading