You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
An implementation of incremental view maintenance & query rewriting for materialized views in DataFusion.
4
+
5
+
A **materialized view** is a view whose query has been pre-computed and saved for later use. This can drastically speed up workloads by pre-computing at least a large fragment of a user-provided query. Furthermore, by implementing a _view matching_ algorithm, we can implement an optimizer that rewrites queries to automatically make use of materialized views where possible and beneficial, a concept known as *query rewriting*.
6
+
7
+
Efficiently maintaining the up-to-dateness of a materialized view is a problem known as *incremental view maintenance*. It is a hard problem in general, but we make some simplifying assumptions:
8
+
9
+
* Data is stored as Hive-partitioned files in object storage.
10
+
* The smallest unit of data that can be updated is a single file.
11
+
12
+
This is a typical pattern with DataFusion, as files in object storage usually are immutable (especially if they are Parquet) and can only be replaced, not appended to or modified. However, it does mean that our implementation of incremental view maintenance only works for Hive-partitioned materialized views in object storage. (Future work may generalize this to alternate storage sources, but the requirement of logically partitioned tables remains.) In contrast, the view matching problem does not depend on the underlying physical representation of the tables.
13
+
14
+
## Example
15
+
16
+
Here we walk through a hypothetical example of setting up a materialized view, to illustrate
17
+
what this library offers. The core of the incremental view maintenance implementation is a UDTF (User-Defined Table Function),
18
+
called `file_dependencies`, that outputs a build graph for a materialized view. This gives users the information they need to determine
19
+
when partitions of the materialized view need to be recomputed.
20
+
21
+
```sql
22
+
-- Create a base table
23
+
CREATE EXTERNAL TABLE t1 (column0 TEXT, dateDATE)
24
+
STORED AS PARQUET
25
+
PARTITIONED BY (date)
26
+
LOCATION 's3://t1/';
27
+
28
+
INSERT INTO t1 VALUES
29
+
('a', '2021-01-01'),
30
+
('b', '2022-02-02'),
31
+
('c', '2022-02-03'), -- Two values in the year 2022
32
+
('d', '2023-03-03');
33
+
34
+
-- Pretend we can create materialized views in SQL
35
+
-- The TableProvider implementation will need to implement the Materialized trait.
36
+
CREATE MATERIALIZED VIEW m1 ASSELECT
37
+
COUNT(*) AS count,
38
+
date_part('YEAR', date) AS year
39
+
PARTITIONED BY (year)
40
+
LOCATION 's3://m1/';
41
+
42
+
-- Show the dependency graph for m1 using the file_dependencies UDTF
0 commit comments