feat(MV): Add MaterializedViewRewrite pass to the optimizer (#1065)#1065
Open
zation99 wants to merge 6 commits intofacebookincubator:mainfrom
Open
feat(MV): Add MaterializedViewRewrite pass to the optimizer (#1065)#1065zation99 wants to merge 6 commits intofacebookincubator:mainfrom
zation99 wants to merge 6 commits intofacebookincubator:mainfrom
Conversation
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
055c7e0 to
7b3c353
Compare
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
7b3c353 to
2481d87
Compare
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
2481d87 to
edb2f0a
Compare
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
edb2f0a to
2faf2fc
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.
Detect MV tables. Traverse the DerivedTable tree looking for BaseTable nodes whose connectorTable carries a MaterializedViewDefinition.
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.
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):
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.
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.
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