ALTER MATERIALIZED VIEW
Applies to: Databricks SQL
Alters metadata associated with the view.
Allows you to perform any of the following actions:
- Add a schedule for refreshing an existing materialized view.
- Alter an existing refresh schedule for a materialized view.
- Drop the refresh schedule for a materialized view. If the schedule is dropped, the object needs to be refreshed manually to reflect the latest data.
To add or alter a comment on a materialized view, use COMMENT ON.
Altering a pipeline-created dataset in ways that contradict the defining SQL can cause some changes to be reverted. See Using ALTER commands with Lakeflow Spark Declarative Pipelines.
Syntax
ALTER MATERIALIZED VIEW view_name
{
{ ADD | ALTER } schedule |
DROP SCHEDULE |
ALTER COLUMN column_clause |
SET ROW FILTER clause |
DROP ROW FILTER |
SET TAGS clause |
UNSET TAGS clause }
schedule
{ SCHEDULE [ REFRESH ] schedule_clause |
TRIGGER ON UPDATE [ AT MOST EVERY trigger_interval ] }
schedule_clause
{ EVERY number { HOUR | HOURS | DAY | DAYS | WEEK | WEEKS } |
CRON cron_string [ AT TIME ZONE timezone_id ] }
column_clause
column_identifier
{ COMMENT clause |
SET MASK clause |
DROP MASK |
SET TAGS clause |
UNSET TAGS clause }
Parameters
-
The name of the materialized view to alter the definition of. The name must not include a temporal specification.
-
schedule
Add or alter a
SCHEDULEorTRIGGERstatement on the materialized view.注記You can't modify the schedule of a materialized view created with Lakeflow Spark Declarative Pipelines with this command. Use the pipeline editor.
-
SCHEDULE [ REFRESH ] schedule_clause-
EVERY number { HOUR | HOURS | DAY | DAYS | WEEK | WEEKS }To schedule a refresh that occurs periodically, use
EVERYsyntax. IfEVERYsyntax is specified, the streaming table or materialized view is refreshed periodically at the specified interval based on the provided value, such asHOUR,HOURS,DAY,DAYS,WEEK, orWEEKS. The following table lists accepted integer values fornumber.Time unit
Integer value
HOUR or HOURS1 <= H <= 72
DAY or DAYS1 <= D <= 31
WEEK or WEEKS1 <= W <= 8
注記The singular and plural forms of the included time unit are semantically equivalent.
-
CRON cron_string [ AT TIME ZONE timezone_id ]To schedule a refresh using a quartz cron value. Valid time_zone_values are accepted.
AT TIME ZONE LOCALis not supported.If
AT TIME ZONEis absent, the session time zone is used. IfAT TIME ZONEis absent and the session time zone is not set, an error is thrown.SCHEDULEis semantically equivalent toSCHEDULE REFRESH.
-
-
TRIGGER ON UPDATE [ AT MOST EVERY trigger_interval ]BetaThe
TRIGGER ON UPDATEfeature is in Beta.Sets the materialized view to refresh when an upstream data source is updated, at most once every minute. Set a value for
AT MOST EVERYto require at least a minimum time between refreshes.The upstream data sources must be either external or managed Delta tables (including materialized views or streaming tables), or managed views whose dependencies are limited to supported table types.
Enabling file events can make triggers more performant, and increases some of the limits on trigger updates.
The
trigger_intervalis an INTERVAL statement that is at least 1 minute.TRIGGER ON UPDATEhas the following limitations- No more than 10 upstream data sources per materialized view when using TRIGGER ON UPDATE.
- Maximum of 1000 streaming tables or materialized views can be specified with TRIGGER ON UPDATE.
- The
AT MOST EVERYclause defaults to 1 minute, and cannot be less than 1 minute.
-
-
DROP SCHEDULERemoves a schedule from the materialized view.
-
ALTER COLUMN clauseSee ALTER COLUMN clause.
-
Preview
This feature is in Public Preview.
Adds a row filter function to the materialized view. All subsequent queries to the materialized view receive a subset of the rows where the function evaluates to boolean
TRUE. This can be useful for fine-grained access control purposes where the function can inspect the identity or group memberships of the invoking user to determine whether to filter certain rows.Row filters added after creation only propagate to downstream tables after the next update. For continuous pipelines, this requires a pipeline restart.
-
DROP ROW FILTERPreviewThis feature is in Public Preview.
Drops the row filter from the materialized view, if any. Future queries will return all rows from the table without any automatic filtering.
-
SET TAGS ( { tag_name = tag_value } [, ...] )PreviewThis feature is in Public Preview.
Apply tags to the materialized view. You need to have
APPLY TAGpermission to add tags to the materialized view.-
tag_nameA literal
STRING. Thetag_namemust be unique within the materialized view or column. -
tag_valueA literal
STRING.
-
-
UNSET TAGS ( tag_name [, ...] )PreviewThis feature is in Public Preview.
Remove tags from the materialized view. You need to have
APPLY TAGpermission to remove tags from the materialized view.-
tag_nameA literal
STRING. Thetag_namemust be unique within the materialized view or column.
-
ALTER COLUMN clause
This feature is in Public Preview.
Changes a property or the location of a column.
Syntax
ALTER COLUMN
column_identifier
{ COMMENT comment |
SET MASK mask_clause |
DROP MASK |
SET TAGS clause |
UNSET TAGS clause }
Parameters
-
The name of the column to be altered.
-
COMMENT commentChanges the description of the
column_namecolumn.commentmust be aSTRINGliteral. -
SETMASK clauseAdds a column mask function to anonymize sensitive data. All subsequent queries from that column will receive the result of evaluating that function over the column in place of the column's original value. This can be useful for fine-grained access control purposes where the function can inspect the identity or group memberships of the invoking user to determine whether to redact the value.
Column masks added after creation only propagate to downstream tables after the next update. For continuous pipelines, this requires a pipeline restart.
-
DROP MASKRemoves the column mask for this column, if any. Future queries from this column will receive the column's original values.
-
SET TAGS ( { tag_name = tag_value } [, ...] )Apply tags to the column. You need to have
APPLY TAGpermission to add tags to the column.-
tag_name
A literal
STRING. Thetag_namemust be unique within the table or column. -
tag_value
A literal
STRING.
-
-
UNSET TAGS ( tag_name [, ...] )Remove the given tags from the column. You need to have
APPLY TAGpermission to remove tags from the column.-
tag_name
A literal
STRING. Thetag_namemust be unique within the table or column.
-
Examples
-- Adds a schedule to refresh the materialized view whenever its upstream data gets updated.
> ALTER MATERIALIZED VIEW my_mv
ADD TRIGGER ON UPDATE;
-- Alters the schedule to refresh the materialized view when its upstream data
-- gets updated, and make sure the update frequency is no more than 1 update
-- per hour.
> ALTER MATERIALIZED VIEW my_mv
ALTER TRIGGER ON UPDATE AT MOST EVERY INTERVAL 1 hour;
-- Alters the schedule to run every two hours for a materialized view
> ALTER MATERIALIZED VIEW my_mv
ALTER SCHEDULE EVERY 2 HOURS;
-- Alters the schedule to refresh a materialized view once a day
-- at midnight in Los Angeles
> ALTER MATERIALIZED VIEW my_mv
ALTER SCHEDULE CRON '0 0 0 * * ? *' AT TIME ZONE 'America/Los_Angeles';
-- Drops the schedule for a materialized view
> ALTER MATERIALIZED VIEW my_mv
DROP SCHEDULE;
-- Adds a column comment
> ALTER MATERIALIZED VIEW my_mv
ALTER COLUMN my_column_name COMMENT 'test'