Skip to content

feat(MV): Add MaterializedViewRewrite pass to the optimizer (#1065)#1065

Open
zation99 wants to merge 6 commits intofacebookincubator:mainfrom
zation99:export-D94286245
Open

feat(MV): Add MaterializedViewRewrite pass to the optimizer (#1065)#1065
zation99 wants to merge 6 commits intofacebookincubator:mainfrom
zation99:export-D94286245

Conversation

@zation99
Copy link
Contributor

@zation99 zation99 commented Mar 15, 2026

Summary:

In optimization, add a new pass of MaterializedViewRewrite.

How it works
The pass runs before initializePlans() on the query graph built from the logical plan.

  1. Detect MV tables. Traverse the DerivedTable tree looking for BaseTable nodes whose connectorTable carries a MaterializedViewDefinition.

  2. Query freshness from connector. Call ConnectorMetadata::getMaterializedViewStatus(), which fetches MV and base table partition lists from the metastore (in parallel) and computes contiguous boundary ranges of the refresh column (e.g., ds) where the MV has data.

  3. Dispatch on status:
    -- kFullyMaterialized → no-op (MV covers everything).
    -- kNotMaterialized / kTooManyPartitionsMissing → replace MV scan with a full base table scan (replaceWithBaseTable).
    -- kPartiallyMaterialized → construct UNION ALL (step 4).
    -- Build UNION ALL tree (for kPartiallyMaterialized):

  4. MV child: Wrap original BaseTable in a DerivedTable, add range filter refreshCol >= lo AND refreshCol <= hi.
    Base table child: Create a new BaseTable pointing at the base table, map MV column names → base column names via MaterializedViewDefinition::columnMappings(), add complement filter refreshCol < lo OR refreshCol > hi.
    Union parent: DerivedTable with setOp = kUnionAll, shared output columns, two children.
    Multiple boundary ranges (non-contiguous fresh data) are supported via OR'd range filters and AND'd complement filters.

  5. Replace in parent. Swap the original MV BaseTable for the new UNION ALL DerivedTable in the parent's tables/tableSet/joinOrder. Remap all parent expression references (exprs, conjuncts, having, orderKeys) from old MV columns to the new union output columns.

  6. Filter pushdown happens naturally. Because the pass runs before initializePlans(), user WHERE filters are still in parent.conjuncts. distributeConjuncts() (called by initializePlans()) pushes them into both UNION ALL children automatically.

Differential Revision: D94286245

Jiaqi Zhang added 5 commits March 14, 2026 15:49
Summary: Add materialized view definition. Reference is https://fburl.com/code/cxrxyqql and https://fburl.com/code/cnqa4fki

Differential Revision: D92019614
Summary:
Pull Request resolved: facebookincubator#1059

Metadata connector interface to get MV definition.

Differential Revision: D92085304
Summary:
In PrestoParser, if the table is a view (from findView), only expand it if it is logical view.

Added MVDefinition in Table struct. In findTable, MV table is set with MVDefinition, so in the following milestones, the upper stream consumer like the optimizer can decide how to further resolve it (e.g. stitching) based on this info.

**Note**:
In this diff, we are not doing any UNION to stitch MV with base table, or switch base table and MV when MV lacks behind too much. This diff scans MV as it is.

Differential Revision: D92541143
Differential Revision: D94024233
Summary:
It will be used in the optimizer to perform UNIONs to generate the final plan for MV stitching.

Encapsulates materialized view status in connector metadata, so the optimizer doesn't need to explicitely get partitions list.

Differential Revision: D94098268
@meta-cla meta-cla bot added the CLA Signed This label is managed by the Meta Open Source bot. label Mar 15, 2026
@meta-codesync
Copy link

meta-codesync bot commented Mar 15, 2026

@zation99 has exported this pull request. If you are a Meta employee, you can view the originating Diff in D94286245.

@zation99 zation99 changed the title Add MaterializedViewRewrite pass to the optimizer feat(MV): Add MaterializedViewRewrite pass to the optimizer Mar 15, 2026
@meta-codesync meta-codesync bot changed the title feat(MV): Add MaterializedViewRewrite pass to the optimizer Add MaterializedViewRewrite pass to the optimizer (#1065) Mar 16, 2026
zation99 added a commit to zation99/axiom that referenced this pull request Mar 16, 2026
…1065)

Summary:

In optimization, add a new pass of MaterializedViewRewrite.

**How it works**
The pass runs before initializePlans() on the query graph built from the logical plan.

1. Detect MV tables. Traverse the DerivedTable tree looking for BaseTable nodes whose connectorTable carries a MaterializedViewDefinition.

2. Query freshness from connector. Call ConnectorMetadata::getMaterializedViewStatus(), which fetches MV and base table partition lists from the metastore (in parallel) and computes contiguous boundary ranges of the refresh column (e.g., ds) where the MV has data.

3. Dispatch on status:
-- kFullyMaterialized → no-op (MV covers everything).
-- kNotMaterialized / kTooManyPartitionsMissing → replace MV scan with a full base table scan (replaceWithBaseTable).
-- kPartiallyMaterialized → construct UNION ALL (step 4).
-- Build UNION ALL tree (for kPartiallyMaterialized):

4. MV child: Wrap original BaseTable in a DerivedTable, add range filter refreshCol >= lo AND refreshCol <= hi.
Base table child: Create a new BaseTable pointing at the base table, map MV column names → base column names via MaterializedViewDefinition::columnMappings(), add complement filter refreshCol < lo OR refreshCol > hi.
Union parent: DerivedTable with setOp = kUnionAll, shared output columns, two children.
Multiple boundary ranges (non-contiguous fresh data) are supported via OR'd range filters and AND'd complement filters.
5. Replace in parent. Swap the original MV BaseTable for the new UNION ALL DerivedTable in the parent's tables/tableSet/joinOrder. Remap all parent expression references (exprs, conjuncts, having, orderKeys) from old MV columns to the new union output columns.

6. Filter pushdown happens naturally. Because the pass runs before initializePlans(), user WHERE filters are still in parent.conjuncts. distributeConjuncts() (called by initializePlans()) pushes them into both UNION ALL children automatically.

Differential Revision: D94286245
@zation99 zation99 changed the title Add MaterializedViewRewrite pass to the optimizer (#1065) feat(MV): Add MaterializedViewRewrite pass to the optimizer (#1065) Mar 16, 2026
@meta-codesync meta-codesync bot changed the title feat(MV): Add MaterializedViewRewrite pass to the optimizer (#1065) Add MaterializedViewRewrite pass to the optimizer (#1065) Mar 16, 2026
zation99 added a commit to zation99/axiom that referenced this pull request Mar 16, 2026
…1065)

Summary:

In optimization, add a new pass of MaterializedViewRewrite.

**How it works**
The pass runs before initializePlans() on the query graph built from the logical plan.

1. Detect MV tables. Traverse the DerivedTable tree looking for BaseTable nodes whose connectorTable carries a MaterializedViewDefinition.

2. Query freshness from connector. Call ConnectorMetadata::getMaterializedViewStatus(), which fetches MV and base table partition lists from the metastore (in parallel) and computes contiguous boundary ranges of the refresh column (e.g., ds) where the MV has data.

3. Dispatch on status:
-- kFullyMaterialized → no-op (MV covers everything).
-- kNotMaterialized / kTooManyPartitionsMissing → replace MV scan with a full base table scan (replaceWithBaseTable).
-- kPartiallyMaterialized → construct UNION ALL (step 4).
-- Build UNION ALL tree (for kPartiallyMaterialized):

4. MV child: Wrap original BaseTable in a DerivedTable, add range filter refreshCol >= lo AND refreshCol <= hi.
Base table child: Create a new BaseTable pointing at the base table, map MV column names → base column names via MaterializedViewDefinition::columnMappings(), add complement filter refreshCol < lo OR refreshCol > hi.
Union parent: DerivedTable with setOp = kUnionAll, shared output columns, two children.
Multiple boundary ranges (non-contiguous fresh data) are supported via OR'd range filters and AND'd complement filters.
5. Replace in parent. Swap the original MV BaseTable for the new UNION ALL DerivedTable in the parent's tables/tableSet/joinOrder. Remap all parent expression references (exprs, conjuncts, having, orderKeys) from old MV columns to the new union output columns.

6. Filter pushdown happens naturally. Because the pass runs before initializePlans(), user WHERE filters are still in parent.conjuncts. distributeConjuncts() (called by initializePlans()) pushes them into both UNION ALL children automatically.

Differential Revision: D94286245
zation99 added a commit to zation99/axiom that referenced this pull request Mar 16, 2026
…1065)

Summary:

In optimization, add a new pass of MaterializedViewRewrite.

**How it works**
The pass runs before initializePlans() on the query graph built from the logical plan.

1. Detect MV tables. Traverse the DerivedTable tree looking for BaseTable nodes whose connectorTable carries a MaterializedViewDefinition.

2. Query freshness from connector. Call ConnectorMetadata::getMaterializedViewStatus(), which fetches MV and base table partition lists from the metastore (in parallel) and computes contiguous boundary ranges of the refresh column (e.g., ds) where the MV has data.

3. Dispatch on status:
-- kFullyMaterialized → no-op (MV covers everything).
-- kNotMaterialized / kTooManyPartitionsMissing → replace MV scan with a full base table scan (replaceWithBaseTable).
-- kPartiallyMaterialized → construct UNION ALL (step 4).
-- Build UNION ALL tree (for kPartiallyMaterialized):

4. MV child: Wrap original BaseTable in a DerivedTable, add range filter refreshCol >= lo AND refreshCol <= hi.
Base table child: Create a new BaseTable pointing at the base table, map MV column names → base column names via MaterializedViewDefinition::columnMappings(), add complement filter refreshCol < lo OR refreshCol > hi.
Union parent: DerivedTable with setOp = kUnionAll, shared output columns, two children.
Multiple boundary ranges (non-contiguous fresh data) are supported via OR'd range filters and AND'd complement filters.
5. Replace in parent. Swap the original MV BaseTable for the new UNION ALL DerivedTable in the parent's tables/tableSet/joinOrder. Remap all parent expression references (exprs, conjuncts, having, orderKeys) from old MV columns to the new union output columns.

6. Filter pushdown happens naturally. Because the pass runs before initializePlans(), user WHERE filters are still in parent.conjuncts. distributeConjuncts() (called by initializePlans()) pushes them into both UNION ALL children automatically.

Differential Revision: D94286245
…1065)

Summary:
Pull Request resolved: facebookincubator#1065

In optimization, add a new pass of MaterializedViewRewrite.

**How it works**
The pass runs before initializePlans() on the query graph built from the logical plan.

1. Detect MV tables. Traverse the DerivedTable tree looking for BaseTable nodes whose connectorTable carries a MaterializedViewDefinition.

2. Query freshness from connector. Call ConnectorMetadata::getMaterializedViewStatus(), which fetches MV and base table partition lists from the metastore (in parallel) and computes contiguous boundary ranges of the refresh column (e.g., ds) where the MV has data.

3. Dispatch on status:
-- kFullyMaterialized → no-op (MV covers everything).
-- kNotMaterialized / kTooManyPartitionsMissing → replace MV scan with a full base table scan (replaceWithBaseTable).
-- kPartiallyMaterialized → construct UNION ALL (step 4).
-- Build UNION ALL tree (for kPartiallyMaterialized):

4. MV child: Wrap original BaseTable in a DerivedTable, add range filter refreshCol >= lo AND refreshCol <= hi.
Base table child: Create a new BaseTable pointing at the base table, map MV column names → base column names via MaterializedViewDefinition::columnMappings(), add complement filter refreshCol < lo OR refreshCol > hi.
Union parent: DerivedTable with setOp = kUnionAll, shared output columns, two children.
Multiple boundary ranges (non-contiguous fresh data) are supported via OR'd range filters and AND'd complement filters.
5. Replace in parent. Swap the original MV BaseTable for the new UNION ALL DerivedTable in the parent's tables/tableSet/joinOrder. Remap all parent expression references (exprs, conjuncts, having, orderKeys) from old MV columns to the new union output columns.

6. Filter pushdown happens naturally. Because the pass runs before initializePlans(), user WHERE filters are still in parent.conjuncts. distributeConjuncts() (called by initializePlans()) pushes them into both UNION ALL children automatically.

Differential Revision: D94286245
@zation99 zation99 changed the title Add MaterializedViewRewrite pass to the optimizer (#1065) feat(MV): Add MaterializedViewRewrite pass to the optimizer (#1065) Mar 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot. fb-exported meta-exported

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant