-
Notifications
You must be signed in to change notification settings - Fork 3k
[Spec] Add Iceberg Materialized View Spec #10280
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
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | |||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,131 @@ | |||||||||||||||||||||||||||
| <!-- | |||||||||||||||||||||||||||
| - Licensed to the Apache Software Foundation (ASF) under one or more | |||||||||||||||||||||||||||
| - contributor license agreements. See the NOTICE file distributed with | |||||||||||||||||||||||||||
| - this work for additional information regarding copyright ownership. | |||||||||||||||||||||||||||
| - The ASF licenses this file to You under the Apache License, Version 2.0 | |||||||||||||||||||||||||||
| - (the "License"); you may not use this file except in compliance with | |||||||||||||||||||||||||||
| - the License. You may obtain a copy of the License at | |||||||||||||||||||||||||||
| - | |||||||||||||||||||||||||||
| - http://www.apache.org/licenses/LICENSE-2.0 | |||||||||||||||||||||||||||
| - | |||||||||||||||||||||||||||
| - Unless required by applicable law or agreed to in writing, software | |||||||||||||||||||||||||||
| - distributed under the License is distributed on an "AS IS" BASIS, | |||||||||||||||||||||||||||
| - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |||||||||||||||||||||||||||
| - See the License for the specific language governing permissions and | |||||||||||||||||||||||||||
| - limitations under the License. | |||||||||||||||||||||||||||
| --> | |||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||
| # Iceberg Materialized View Spec | |||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||
| ## Background and Motivation | |||||||||||||||||||||||||||
| Iceberg views are a powerful tool to abstract complex queries and share them among different engines. | |||||||||||||||||||||||||||
| However, such views are not materialized by default, which means that they are re-computed every time they are queried. | |||||||||||||||||||||||||||
| This can be inefficient for complex queries that are computed frequently. | |||||||||||||||||||||||||||
| Iceberg Materialized views are a way to store the results of an Iceberg view to reuse the computation in subsequent queries. | |||||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. minor suggestion
|
|||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||
| ## Goals | |||||||||||||||||||||||||||
| The goal of this spec is to define the metadata associated with materialized views in Iceberg. | |||||||||||||||||||||||||||
| Such metadata allows creating and querying Iceberg materialized views across different engines. | |||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||
| ## Specification | |||||||||||||||||||||||||||
| A materialized view is an Iceberg view with a respective Iceberg table that stores the results of the view query. | |||||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you mean, an 'associated Iceberg table'? |
|||||||||||||||||||||||||||
| An Iceberg view is considered a materialized view if it has the `materialized.view` property set to `true`. | |||||||||||||||||||||||||||
| A materialized view must also reference the storage table identifier in its `materialized.view.storage.table` property. | |||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||
| The specification for the materialized view properties on the view is as follows: | |||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. regarding property name iceberg.materialized.view, it is redundant to have iceberg there. metadata field name uses hyphen
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have removed the
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 for Steven's suggestion, which is what I also wrote in the comment below. Take
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ack. I have removed |
|||||||||||||||||||||||||||
| | Property name | Description | | |||||||||||||||||||||||||||
| |--------------------------------------------|-------------------------------------------------------------------------| | |||||||||||||||||||||||||||
| | `materialized.view` | This property is used to mark whether a view is a materialized view. If set to `true`, the view is treated as a materialized view.| | |||||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not just 'materialized'? (view seems redundant) Also, in @JanKaul's proposal https://docs.google.com/document/d/1UnhldHhe3Grz8JBngwXPA6ZZord1xMedY5ukEhZYF-A/edit#heading=h.a0i0qwejiore it looks like it was a single field (materialization), and it was a MV if the field is not null?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the consensus around Iceberg property name is that |
|||||||||||||||||||||||||||
| | `materialized.view.storage.table` | This property specifies the identifier of the storage table associated with the materialized view.| | |||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||
| In addition to the properties on the view, the storage table associated with the materialized view has the following properties: | |||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||
| | Property name | Description | | |||||||||||||||||||||||||||
| |--------------------------------------|-------------------------------------------------------------------------------| | |||||||||||||||||||||||||||
| | `base.snapshot.[UUID]` | These properties store the snapshot IDs of the base tables at the time the materialized view's data was last updated. Each property is prefixed with `base.snapshot.` followed by the UUID of the base table.| | |||||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. other places also we can replace
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment, in https://docs.google.com/document/d/1UnhldHhe3Grz8JBngwXPA6ZZord1xMedY5ukEhZYF-A/edit#heading=h.r4ge4rk4hbp5 by @JanKaul, it was a struct called 'lineage' that has these fields. I think that was the proposal instead of ad-hoc table properties?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For reference it was:
Struct 'source-table'
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think a flat list of views or tables is going to accurately represent the lineage of a MV. You could have a MV whose SQL references the same table at different branches or timestamps. In the Nessie catalog, the same view on different branches could refer to child tables at different snapshots.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Do we want to support that in materialized views yet? This will start requiring understanding the SQL logic and reasoning about aliases, etc, which is a high expectation from the engine code and requires some standardization too. Further, what SQL dialects support this today? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Most engines out there support the time travel clause so its pretty ubiquitous now. We may not want to support the same table at different snapshots in the MV query tree right now. But I think using a struct for each record allows us to add properties in the future to disambiguate the tables (maybe sequence number, parent view, path to root or query tree depth). I regularly see MVs on views that are 100 of non-distinct tables/views. It happens a lot when users build semantic layers out of views.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 100 of non-disitinct tables/views is fine as long as none of them has time travel. They will all share the same snapshot ID and one record can represent them all. I think the only way to truly distinguish is the alias in the query. I do not see why adding query tree depth helps (i.e., it can still lead to collisions). Sequence number does not work either because it has to be interpreted by all engines in the same way (i.e., same sequence number uniquely identifies the same occurrence of the table across all engines). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure I understand your alias idea. Can you elaborate with an example? Thanks
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1. I think supporting time travel is required given it is now a standard ANSI syntax that can be referenced in any MV SQL. Blocking it actually requires more effort to check for time travel query and then fail MV creation. And when you travel by time, I think you need to fix the version it travels to by recording its snapshot ID. Time travel looks at the history entries to identify the snapshot to read. The entry might be removed due to setting of
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I actually think that putting info like this on snapshot is a lot easier for time-travel, as moving between snapshots is what the time travel mechanism is. I understand having the right the lineage properties on snapshot is not critical for the integrity of data, but I feel it would be a great usability improvement if it was updated. I think that would be hard to get in sync if we use table properties (or even table metadata). Look at the many problems we have just getting right schema to be read in some of these scenarios (rollback and time travel) #5591 (still not solved), #9131, #1508, etc, because it is stored as table metadata and not on snapshot.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also as @JanKaul pointed out, Trino already is modeling it as snapshot properties: https://github.com/trinodb/trino/blob/212455d3e1d393f58cbc395d2b9da47ed8f23dd8/plugin/trino-iceberg/src/main/java/io/trino/plugin/iceberg/IcebergMetadata.java#L2915 |
|||||||||||||||||||||||||||
| | `view.version` | This property stores the version of the view that this storage table is associated with at the time of materialization| | |||||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
|||||||||||||||||||||||||||
| | `child.view.version.[UUID]` | If the top level materialized view is nested on top of other views, these properties store the version of the child views that the top level view is associated with at the time of materialization. Each property is prefixed with `child.view.version.` followed by the UUID of the child view.| | |||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||
| A storage table is considered fresh if all the following conditions are true: | |||||||||||||||||||||||||||
| * For each table `T` with `UUID` in `base.snapshot.[UUID]`, the current snapshot ID of `T` is equal to the value stored in `base.snapshot.[UUID]`. | |||||||||||||||||||||||||||
| * The version of the view using the table as the storage table is equal to the value stored in `view.version`. | |||||||||||||||||||||||||||
| * For each child view `V` with `UUID` in `child.view.version.[UUID]`, the version of `V` is equal to the value stored in `child.view.version.[UUID]`. | |||||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. there is a chance that storage table of child views are outdated (not fresh). So, just comparing the view versions is not enough to conclude child views are fresh?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In case child views are materialized, their storage table is not relevant to determining the freshness of the current (top level view) storage table.
The objective is not to tell whether child views are fresh, but rather if the current storage table is a valid materialization of the current view tree. If the view tree changes in any way, the current table is no longer a valid materialization for the current view tree.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have added the above explanation to the spec text. |
|||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||
| Implementations may elect to leverage the storage table with more relaxed freshness conditions, such as allowing base table stored snapshots to be different from current snapshots, as long as the respective snapshot timestamp difference is within a given time range. | |||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||
| ## Examples | |||||||||||||||||||||||||||
| The examples below illustrate the role of different metadata associated with the storage table in determining the freshness of the materialized view. | |||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||
| ### Example 1: | |||||||||||||||||||||||||||
| This example highlights the role of the snapshot ID in determining the freshness of a materialized view. | |||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||
| **Scenario:** | |||||||||||||||||||||||||||
| - **View Name:** `event_summary` | |||||||||||||||||||||||||||
| - **Base Table:** `event` | |||||||||||||||||||||||||||
| - **Base Table UUID:** `123e4567` | |||||||||||||||||||||||||||
| - **View Definition:** `SELECT event_type, COUNT(*) AS total_events FROM event GROUP BY event_type` | |||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||
| **Example States:** | |||||||||||||||||||||||||||
| 1. **Initial State:** | |||||||||||||||||||||||||||
| - The materialized view `event_summary` is created using the current snapshot of the `event` table. | |||||||||||||||||||||||||||
| - Property `base.snapshot.123e4567` is set with the snapshot ID at the time of creation, say `123`. | |||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||
| 2. **Fresh State:** | |||||||||||||||||||||||||||
| - No updates have occurred in the `event` table, so the current snapshot ID, `123` matches the value stored in `base.snapshot.123e4567`. | |||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||
| 3. **Stale State:** | |||||||||||||||||||||||||||
| - The `event` table receives an update, changing the current snapshot ID to `456`. No other changes have taken place. | |||||||||||||||||||||||||||
| - When `event_summary` is queried again, the snapshot ID in `base.snapshot.123e4567` (i.e., `123`) no longer matches the current snapshot ID of the `event` table (i.e., `456`). | |||||||||||||||||||||||||||
| - The view is now considered stale due to the snapshot ID mismatch. | |||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||
| ### Example 2: | |||||||||||||||||||||||||||
| This example highlights the role of the view version in determining the freshness of a materialized view. | |||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||
| **Scenario:** | |||||||||||||||||||||||||||
| - **View Name:** `monthly_event_report` | |||||||||||||||||||||||||||
| - **Base Table:** `event` | |||||||||||||||||||||||||||
| - **View Definition:** `SELECT MONTH(event_date) AS month, COUNT(*) AS count FROM event GROUP BY MONTH(event_date)` | |||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||
| **Example states:** | |||||||||||||||||||||||||||
| 1. **Initial State:** | |||||||||||||||||||||||||||
| - `monthly_event_report` is created using the current data in the `event` table. | |||||||||||||||||||||||||||
| - The property `view.version` is set with the view version at the time of creation, say `1`. | |||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||
| 2. **Fresh State:** | |||||||||||||||||||||||||||
| - The view `monthly_event_report` version has not changed, so the current view version, `1`, matches the value stored in `view.version`. | |||||||||||||||||||||||||||
| - The storage table is considered fresh. | |||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||
| 3. **Stale State:** | |||||||||||||||||||||||||||
| - The definition of `monthly_event_report` is updated (e.g., adding more grouping conditions or changing the aggregation). No other changes have taken place. | |||||||||||||||||||||||||||
| - This change updates the view version, say to `2`. | |||||||||||||||||||||||||||
| - When `monthly_event_report` is queried again, the version recorded in `view.version`, i.e., `1`, does not match the current version of the view, i.e., `2`. | |||||||||||||||||||||||||||
| - The view is now considered stale due to the top level view version mismatch, i.e., the storage table does not contain a valid result for the current view version. | |||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||
| ### Example 3: | |||||||||||||||||||||||||||
| This example highlights the role of the view version in determining the freshness of a materialized view. | |||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||
| **Scenario:** | |||||||||||||||||||||||||||
| - **View Name:** `event_analysis` | |||||||||||||||||||||||||||
| - **Base Tables:** `event1`, `event2` | |||||||||||||||||||||||||||
| - **Child Views:** `event_type_count` (based on `event1`), `event_region_count` (based on `event2`) | |||||||||||||||||||||||||||
| - **Child View UUIDs:** `event_type_count` - `456e7890`, `event_region_count` - `789e0123` | |||||||||||||||||||||||||||
| - **View Definitions:** | |||||||||||||||||||||||||||
| - `event_analysis`: `SELECT E1.event_type, E1.count, E2.region, E2.count FROM EventTypeCount E1 JOIN EventRegionCount E2 ON E1.event_type = E2.event_type` | |||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||
| - `event_analysis`: `SELECT E1.event_type, E1.count, E2.region, E2.count FROM EventTypeCount E1 JOIN EventRegionCount E2 ON E1.event_type = E2.event_type` | |
| - `event_analysis`: `SELECT E1.event_type, E1.count, E2.region, E2.count FROM event_type_count E1 JOIN event_region_count E2 ON E1.event_type = E2.event_type` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: remove however.
However, such views->Such logical views