nooneuse opened a new pull request, #71170: URL: https://github.com/apache/doris/pull/71170
### Background Nereids can answer an aggregate query by reading the results already stored in a materialized view (MV) or by aggregating its rows again at the query's grouping grain. Predicate compensation runs before the planner chooses between these paths. ### What problem does this PR solve? Issue Number: None Related PR: #68861 (protects scalar aggregates over grouped views), #71067 (where the grouped case was identified during review) PR Type: Bugfix Problem Summary: A fine-grained MV with a `HAVING` condition on its aggregate output can discard subgroups that a coarser query still needs, even when the query uses the same `HAVING` expression. Reject that MV when the rewrite needs to aggregate its rows again, while preserving rewrites that can read the stored results directly. The fix is 13 lines of production code; the rest of the change adds FE and regression coverage. For example, let `t(g, m)` contain `(1,1), (1,1), (1,2)`: ```sql -- MV definition SELECT g, m, COUNT(*) AS n FROM t GROUP BY g, m HAVING COUNT(*) >= 2; -- Query SELECT g, COUNT(*) AS n FROM t GROUP BY g HAVING COUNT(*) >= 2; ``` The base query counts all three rows. The MV has already discarded the `m = 2` subgroup, so re-aggregating its rows can only count two. Reapplying the query's `HAVING` cannot recover the missing row. ### Details - **Frequency:** Not observed in a Doris run; identified by source inspection. The wrong result requires the planner to choose a rollup from a view that filtered out subgroups the query needs. - **Root cause:** `aggregateOutputIsRecomputed()` protects scalar-over-grouped rewrites, but not rollups between two grouped aggregates. Predicate compensation can therefore remove matching `HAVING` expressions without accounting for their different grouping grains. - **Why existing tests missed it:** `PredicatesTest` and `MvExplorationSuiteTest` exercise the existing recomputation check or an explicitly supplied recomputation flag. The `agg_scalar_empty_input` regression covers scalar queries over grouped MVs, not grouped queries over finer-grained MVs. - **Fix:** After attempting direct projection, inspect the view's own lineage-expanded predicates across all buckets. If any predicate depends on aggregate output, reject that candidate before rollup and record the reason in `EXPLAIN MEMO PLAN`. This avoids relying on predicates that compensation may already have removed and leaves global compensation unchanged. - **Other paths and branches:** Aggregate-view rollup rules share the new check. Direct projection and aggregation over detail MVs are unchanged, and the existing scalar protection remains in place. The same predicate-pruning pattern is present on `branch-3.1`, `branch-4.0`, `branch-4.1`, and `branch-4.2` by source inspection; the case has not been run on those branches. The introducing PR has not been established. - **Behavior change:** Some formerly eligible plans, including same-grain rewrites that still need an aggregate handler, can fall back to another MV or the base tables and run more slowly. Existing MVs do not need to be rebuilt; refresh behavior, DDL, and storage and wire formats are unchanged. ### Testing - Independently checked the three-row example in SQLite: the base query returned `(1,3)` and a deliberately unsafe rollup returned `(1,2)`. This validates the example, not Doris planner behavior. - Added FE cases in `MvExplorationSuiteTest` for grouped `HAVING`, an aggregate-dependent predicate in the range bucket, and the unaffected direct and unfiltered-rollup paths. Added the P0 plan regression `mtmv_having_rollup` to check the rejection reason and safe rewrites. The regression script parsed successfully, but Doris baseline and candidate runs, including the FE tests, were not run locally because the required build tools and cluster were unavailable. ### Release note Fix incorrect results when Nereids rolls up a materialized view that has already filtered out groups based on aggregate values. ### Check List (For Author) - Behavior changed: Yes (see Details) - Does this need documentation: No - Pre-submission status: Partially verified: FE compilation and unit tests, Doris regression tests, and baseline/candidate result checks have not run. ### AI Disclosure - Tool: OpenAI assistant - Generated: Implementation, tests, and PR description - Human review: Not stated -- 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]
