yujun777 opened a new pull request, #71138: URL: https://github.com/apache/doris/pull/71138
### Background An asynchronous materialized view (MV) tracks freshness by partition. The optimizer can use fresh MV partitions and, when UNION ALL compensation is enabled, read missing partitions from the base table. ### What problem does this PR solve? Issue Number: None Related PR: #49514 (introduced the partition-check gate) PR Type: Bugfix Problem Summary: With `enable_materialized_view_union_rewrite=false`, the optimizer could rewrite a query to scan a stale partition of a partially fresh MV. The rewrite now checks partition coverage before using the MV. A query covered by fresh partitions can still use the MV, and a query that needs data from a stale partition falls back to the base table when compensation is disabled. In the regression fixture, the MV contains `1` and `10`. The base table receives an additional `11` in its second partition after the MV refresh. ```sql SELECT SUM(k) FROM partition_freshness_base; ``` | Before the fix | With the fix | |---|---| | `11` from the MV | `22` from the base table | ### Details - **Frequency:** Reproducible when UNION compensation is disabled and the optimizer selects a partially fresh MV for a query spanning a stale partition. - **Root cause:** `AbstractMaterializedViewRule` used the UNION switch to gate invalid-partition calculation. With the switch off, it could admit an MV scan with incomplete coverage. - **Why existing tests missed it:** `PartitionCompensatorTest` and `PartitionCompensatorBatchTest` cover partition calculation. The `grace_period` and `partition_curd_union_rewrite` suites cover freshness and compensation with the switch enabled. - **Fix:** Calculate partition coverage for eligible MV candidates and check the switch when the query needs base-table partitions. The same calculation can remove unused or stale MV partitions when the query is fully covered by the fresh ones. - **Other paths and branches:** The shared rule serves both pre-rewrite and CBO. The same gate is present in `branch-3.1`, `branch-4.0`, `branch-4.1`, and `branch-4.2` by source inspection. - **Behavior change:** EXPLAIN can report that a candidate was rejected because it requires compensation and UNION compensation is disabled. ### Testing - `partition_freshness_without_union` failed the base-table plan assertion on the unmodified master baseline and passed with the fix. With UNION compensation disabled, the fresh-only query scanned one MV partition and returned `1`. The stale-only and full-table queries scanned the base table and returned `21` and `22`. - Seven suites passed locally across the MV `availability` and `union_rewrite` directories in shared-nothing mode. The Hive-dependent suite was excluded. ### Release note With UNION compensation disabled, queries that need a stale asynchronous MV partition now read current data from the base table. Queries covered by fresh MV partitions remain eligible for MV rewrite. ### Check List (For Author) - Behavior changed: Yes (see Details) - Does this need documentation: No - Pre-submission status: Ready -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
